-
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.
feat(kumactl) add kumactl delete command
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
1 parent
2fa1f95
commit ae78bee
Showing
6 changed files
with
248 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,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 | ||
} |
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,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 | ||
} |
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,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")) | ||
}) | ||
}) | ||
|
||
}) |
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,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") | ||
} |
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
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