-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from pradeepmurugesan/kuma-delete
add kumactl delete command
- Loading branch information
Showing
10 changed files
with
964 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package delete | ||
|
||
import ( | ||
"context" | ||
kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd" | ||
"github.com/Kong/kuma/pkg/core/resources/apis/mesh" | ||
"github.com/Kong/kuma/pkg/core/resources/model" | ||
"github.com/Kong/kuma/pkg/core/resources/registry" | ||
"github.com/Kong/kuma/pkg/core/resources/store" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete TYPE NAME", | ||
Short: "Delete Kuma resources", | ||
Long: `Delete Kuma resources.`, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
rs, err := pctx.CurrentResourceStore() | ||
if err != nil { | ||
return err | ||
} | ||
resourceTypeArg := args[0] | ||
name := args[1] | ||
|
||
var resource model.Resource | ||
var resourceType model.ResourceType | ||
switch resourceTypeArg { | ||
case "mesh": | ||
resourceType = mesh.MeshType | ||
case "dataplane": | ||
resourceType = mesh.DataplaneType | ||
case "proxytemplate": | ||
resourceType = mesh.ProxyTemplateType | ||
case "traffic-log": | ||
resourceType = mesh.TrafficLogType | ||
case "traffic-permission": | ||
resourceType = mesh.TrafficPermissionType | ||
|
||
default: | ||
return errors.Errorf("unknown TYPE: %s. Allowed values: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg) | ||
} | ||
|
||
currentMesh := pctx.CurrentMesh() | ||
if resourceType == mesh.MeshType { | ||
currentMesh = name | ||
} | ||
|
||
if resource, err = registry.Global().NewObject(resourceType); err != nil { | ||
return err | ||
} | ||
|
||
if err := deleteResource(name, currentMesh, resource, resourceType, rs); err != nil { | ||
return err | ||
} | ||
|
||
cmd.Printf("deleted %s %q\n", resourceType, name) | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func deleteResource(name string, currentMesh string, resource model.Resource, resourceType model.ResourceType, rs store.ResourceStore) error { | ||
getOptions := store.GetByKey(model.DefaultNamespace, name, currentMesh) | ||
if err := rs.Get(context.Background(), resource, getOptions); err != nil { | ||
if store.IsResourceNotFound(err) { | ||
return errors.Errorf("there is no %s with name %q", resourceType, name) | ||
} | ||
return errors.Wrapf(err, "failed to get %s with the name %q", resourceType, name) | ||
} | ||
|
||
deleteOptions := store.DeleteByKey(model.DefaultNamespace, name, currentMesh) | ||
if err := rs.Delete(context.Background(), resource, deleteOptions); err != nil { | ||
return errors.Wrapf(err, "failed to delete %s with the name %q", resourceType, name) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package delete_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1" | ||
"github.com/Kong/kuma/app/kumactl/cmd" | ||
kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd" | ||
config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1" | ||
mesh_core "github.com/Kong/kuma/pkg/core/resources/apis/mesh" | ||
core_model "github.com/Kong/kuma/pkg/core/resources/model" | ||
core_store "github.com/Kong/kuma/pkg/core/resources/store" | ||
memory_resources "github.com/Kong/kuma/pkg/plugins/resources/memory" | ||
test_model "github.com/Kong/kuma/pkg/test/resources/model" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
"path/filepath" | ||
) | ||
|
||
var _ = Describe("kumactl delete dataplane", func() { | ||
var dataplanes []*mesh_core.DataplaneResource | ||
|
||
BeforeEach(func() { | ||
dataplanes = []*mesh_core.DataplaneResource{ | ||
{ | ||
Meta: &test_model.ResourceMeta{ | ||
Namespace: "default", | ||
Mesh: "demo", | ||
Name: "web-01", | ||
}, | ||
Spec: mesh_proto.Dataplane{ | ||
Networking: &mesh_proto.Dataplane_Networking{ | ||
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{ | ||
{ | ||
Interface: "127.0.0.1:8080:80", | ||
Tags: map[string]string{ | ||
"service": "mobile", | ||
"version": "v1", | ||
}, | ||
}, | ||
{ | ||
Interface: "127.0.0.1:8090:90", | ||
Tags: map[string]string{ | ||
"service": "metrics", | ||
"version": "v1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Meta: &test_model.ResourceMeta{ | ||
Namespace: "default", | ||
Mesh: "demo1", | ||
Name: "web-02", | ||
}, | ||
Spec: mesh_proto.Dataplane{ | ||
Networking: &mesh_proto.Dataplane_Networking{ | ||
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{ | ||
{ | ||
Interface: "127.0.0.2:8080:80", | ||
Tags: map[string]string{ | ||
"service": "web", | ||
"version": "v2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
Describe("Delete dataplane", func() { | ||
var rootCtx *kumactl_cmd.RootContext | ||
var rootCmd *cobra.Command | ||
var outbuf, errbuf *bytes.Buffer | ||
var store core_store.ResourceStore | ||
|
||
BeforeEach(func() { | ||
// setup | ||
|
||
rootCtx = &kumactl_cmd.RootContext{ | ||
Runtime: kumactl_cmd.RootRuntime{ | ||
NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) { | ||
return store, nil | ||
}, | ||
}, | ||
} | ||
|
||
store = memory_resources.NewStore() | ||
|
||
for _, pt := range dataplanes { | ||
key := core_model.MetaToResourceKey(pt.Meta) | ||
err := store.Create(context.Background(), pt, core_store.CreateBy(key)) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
|
||
rootCmd = cmd.NewRootCmd(rootCtx) | ||
outbuf = &bytes.Buffer{} | ||
errbuf = &bytes.Buffer{} | ||
rootCmd.SetOut(outbuf) | ||
rootCmd.SetErr(errbuf) | ||
}) | ||
|
||
It("should throw an error in case of a non existing dataplane", func() { | ||
// given | ||
rootCmd.SetArgs([]string{ | ||
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"), | ||
"delete", "dataplane", "some-non-existing-dataplane"}) | ||
|
||
// when | ||
err := rootCmd.Execute() | ||
|
||
// then | ||
Expect(err).To(HaveOccurred()) | ||
// and | ||
Expect(err.Error()).To(Equal("there is no Dataplane with name \"some-non-existing-dataplane\"")) | ||
// and | ||
Expect(outbuf.String()).To(Equal("Error: there is no Dataplane with name \"some-non-existing-dataplane\"\n")) | ||
// and | ||
Expect(errbuf.Bytes()).To(BeEmpty()) | ||
}) | ||
|
||
It("should delete the dataplane if exists", func() { | ||
|
||
// given | ||
rootCmd.SetArgs([]string{ | ||
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"), | ||
"delete", "dataplane", "web-01", "--mesh", "demo"}) | ||
|
||
// when | ||
err := rootCmd.Execute() | ||
|
||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// and | ||
list := &mesh_core.DataplaneResourceList{} | ||
err = store.List(context.Background(), list, core_store.ListByNamespace("default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(list.Items).To(HaveLen(1)) | ||
// and | ||
Expect(errbuf.String()).To(BeEmpty()) | ||
// and | ||
Expect(outbuf.String()).To(Equal("deleted Dataplane \"web-01\"\n")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.