Skip to content

Commit

Permalink
feat(kumactl) add kumactl delete command
Browse files Browse the repository at this point in the history
Implemented the support for delete command. The delete command takes the mesh name as a parameter.
Check if the the given mesh name exists and then delete.

* returns an error in case of no mesh with the given name.

Fix #279
  • Loading branch information
pradeepmurugesan committed Oct 16, 2019
1 parent 2fa1f95 commit ae78bee
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/kumactl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package delete

import (
kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd"
"github.com/spf13/cobra"
)

type deleteContext struct {
*kumactl_cmd.RootContext
}

func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
ctx := &deleteContext{RootContext: pctx}
cmd := &cobra.Command{
Use: "delete",
Short: "Delete Kuma resources",
Long: `Delete Kuma resources.`,
}

// sub-commands
cmd.AddCommand(newDeleteMeshCmd(ctx))
return cmd
}
44 changes: 44 additions & 0 deletions app/kumactl/cmd/delete/delete_meshes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package delete

import (
"context"
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
"github.com/Kong/kuma/pkg/core/resources/model"
"github.com/Kong/kuma/pkg/core/resources/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func newDeleteMeshCmd(pctx *deleteContext) *cobra.Command {
cmd := &cobra.Command{
Use: "mesh",
Short: "Delete Mesh",
Long: `Delete Mesh.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rs, err := pctx.CurrentResourceStore()
if err != nil {
return err
}
meshName := args[0]

getOptions := store.GetByKey(model.DefaultNamespace, meshName, meshName)
meshDetails := mesh.MeshResource{}
if err := rs.Get(context.Background(), &meshDetails, getOptions); err != nil {
if store.IsResourceNotFound(err) {
return errors.Errorf("there is no Mesh with name %q", meshName)
}
return errors.Wrapf(err, "failed to get Mesh with the name %q", meshName)
}

deleteOptions := store.DeleteByKey(model.DefaultNamespace, meshName, meshName)
if err := rs.Delete(context.Background(), &mesh.MeshResource{}, deleteOptions); err != nil {
return errors.Wrapf(err, "failed to delete Mesh with the name %q", meshName)
}

cmd.Printf("deleted Mesh %q\n", meshName)
return nil
},
}
return cmd
}
165 changes: 165 additions & 0 deletions app/kumactl/cmd/delete/delete_meshes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package delete_test

import (
"bytes"
"context"
"path/filepath"
"time"

"github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/app/kumactl/cmd"
config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1"
"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"

kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)

var _ = Describe("kumactl delete mesh", func() {

sampleMeshes := []*mesh.MeshResource{
{
Spec: v1alpha1.Mesh{
Mtls: &v1alpha1.Mesh_Mtls{
Enabled: true,
Ca: &v1alpha1.CertificateAuthority{
Type: &v1alpha1.CertificateAuthority_Builtin_{
Builtin: &v1alpha1.CertificateAuthority_Builtin{},
},
},
},
Logging: &v1alpha1.Logging{
AccessLogs: &v1alpha1.Logging_AccessLogs{
Enabled: true,
FilePath: "/tmp/access.log",
},
},
},
Meta: &test_model.ResourceMeta{
Mesh: "mesh1",
Name: "mesh1",
Namespace: "default",
},
},
{
Spec: v1alpha1.Mesh{
Mtls: &v1alpha1.Mesh_Mtls{
Enabled: false,
Ca: &v1alpha1.CertificateAuthority{
Type: &v1alpha1.CertificateAuthority_Builtin_{
Builtin: &v1alpha1.CertificateAuthority_Builtin{},
},
},
},
},
Meta: &test_model.ResourceMeta{
Mesh: "mesh2",
Name: "mesh2",
Namespace: "default",
},
},
}

Describe("DeleteMeshCommand", 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{
Now: func() time.Time { return time.Now() },
NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) {
return store, nil
},
},
}

store = memory_resources.NewStore()

for _, ds := range sampleMeshes {
key := core_model.MetaToResourceKey(ds.Meta)
err := store.Create(context.Background(), ds, 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 no args", func() {
// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "mesh"})

// when
err := rootCmd.Execute()

// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("accepts 1 arg(s), received 0"))
// and
Expect(outbuf.String()).To(MatchRegexp(`Error: accepts 1 arg\(s\), received 0`))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})

It("should throw an error in case of a non existing mesh", func() {
// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "mesh", "some-non-existing-mesh"})

// when
err := rootCmd.Execute()

// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("there is no Mesh with name \"some-non-existing-mesh\""))
// and
Expect(outbuf.String()).To(Equal("Error: there is no Mesh with name \"some-non-existing-mesh\"\n"))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})

It("should delete the mesh if exists", func() {

// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "mesh", "mesh2"})

// when
err := rootCmd.Execute()

// then
Expect(err).ToNot(HaveOccurred())

// and
list := &mesh.MeshResourceList{}
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 Mesh \"mesh2\"\n"))
})
})

})
13 changes: 13 additions & 0 deletions app/kumactl/cmd/delete/delete_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package delete_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestDeleteCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Delete Cmd Suite")
}
2 changes: 2 additions & 0 deletions app/kumactl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Kong/kuma/app/kumactl/cmd/apply"
"github.com/Kong/kuma/app/kumactl/cmd/config"
"github.com/Kong/kuma/app/kumactl/cmd/delete"
"github.com/Kong/kuma/app/kumactl/cmd/get"
"github.com/Kong/kuma/app/kumactl/cmd/inspect"
"github.com/Kong/kuma/app/kumactl/cmd/install"
Expand Down Expand Up @@ -59,6 +60,7 @@ func NewRootCmd(root *kumactl_cmd.RootContext) *cobra.Command {
cmd.AddCommand(install.NewInstallCmd(root))
cmd.AddCommand(config.NewConfigCmd(root))
cmd.AddCommand(get.NewGetCmd(root))
cmd.AddCommand(delete.NewDeleteCmd(root))
cmd.AddCommand(inspect.NewInspectCmd(root))
cmd.AddCommand(apply.NewApplyCmd(root))
cmd.AddCommand(version.NewVersionCmd())
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kumactl/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage:
Available Commands:
apply Create or modify Kuma resources
config Manage kumactl config
delete Delete Kuma resources
get Show Kuma resources
help Help about any command
inspect Inspect Kuma resources
Expand Down

0 comments on commit ae78bee

Please sign in to comment.