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 fefcbf1
Show file tree
Hide file tree
Showing 5 changed files with 234 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
}
41 changes: 41 additions & 0 deletions app/kumactl/cmd/delete/delete_meshes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 {
return errors.Wrapf(err, "failed to get Mesh with the name %s", meshName)
}

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

cmd.Printf("Successfully deleted the mesh: %s", meshName)
return nil
},
}
return cmd
}
155 changes: 155 additions & 0 deletions app/kumactl/cmd/delete/delete_meshes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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 buf *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.ResourceKey{
Mesh: ds.Meta.GetMesh(),
Namespace: ds.Meta.GetNamespace(),
Name: ds.Meta.GetName(),
}
err := store.Create(context.Background(), ds, core_store.CreateBy(key))
Expect(err).ToNot(HaveOccurred())
}

rootCmd = cmd.NewRootCmd(rootCtx)
buf = &bytes.Buffer{}
rootCmd.SetOut(buf)
})

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"))
})

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(MatchRegexp("failed to get Mesh with the name some-non-existing-mesh: .*"))
})

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{}
e := store.List(context.Background(), list, core_store.ListByNamespace("default"))
Expect(e).To(BeNil())
Expect(len(list.Items)).To(Equal(1))
})
})

})
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

0 comments on commit fefcbf1

Please sign in to comment.