-
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.
refactor(kumactl) generalized the delete command in order to use for …
…all resource types Right now the design is to handle the resource types via subcommands. Refactored so that the delete command accepts a resourceType arg. Based on the passed resource type arg the corresponding resource will be deleted. * returns an error in case of no mesh with the given name. Fix #279
- Loading branch information
1 parent
ae78bee
commit f505a47
Showing
4 changed files
with
134 additions
and
54 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 |
---|---|---|
@@ -1,23 +1,69 @@ | ||
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" | ||
) | ||
|
||
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.`, | ||
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 = model.ResourceType(mesh.MeshType) | ||
|
||
default: | ||
return errors.Errorf("unknown resource type: %s. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg) | ||
} | ||
|
||
if resource, err = registry.Global().NewObject(resourceType); err != nil { | ||
return err | ||
} | ||
|
||
if err := deleteResource(name, resource, resourceType, rs); err != nil { | ||
return err | ||
} | ||
|
||
cmd.Printf("deleted %s %q\n", resourceType, name) | ||
return nil | ||
}, | ||
} | ||
|
||
// sub-commands | ||
cmd.AddCommand(newDeleteMeshCmd(ctx)) | ||
return cmd | ||
} | ||
|
||
func deleteResource(name string, resource model.Resource, resourceType model.ResourceType, rs store.ResourceStore) error { | ||
getOptions := store.GetByKey(model.DefaultNamespace, name, name) | ||
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, name) | ||
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package delete_test | ||
|
||
import ( | ||
"bytes" | ||
"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" | ||
core_store "github.com/Kong/kuma/pkg/core/resources/store" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
var _ = Describe("kumactl delete ", func() { | ||
Describe("Delete Command", 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 | ||
}, | ||
}, | ||
} | ||
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"}) | ||
|
||
// when | ||
err := rootCmd.Execute() | ||
|
||
// then | ||
Expect(err).To(HaveOccurred()) | ||
// and | ||
Expect(err.Error()).To(Equal("accepts 2 arg(s), received 0")) | ||
// and | ||
Expect(outbuf.String()).To(MatchRegexp(`Error: accepts 2 arg\(s\), received 0`)) | ||
// and | ||
Expect(errbuf.Bytes()).To(BeEmpty()) | ||
}) | ||
|
||
It("should throw an error in case of unsupported resource type", func() { | ||
// given | ||
rootCmd.SetArgs([]string{ | ||
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"), | ||
"delete", "some-type", "some-name"}) | ||
|
||
// when | ||
err := rootCmd.Execute() | ||
|
||
// then | ||
Expect(err).To(HaveOccurred()) | ||
// and | ||
Expect(err.Error()).To(Equal("unknown resource type: some-type. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission")) | ||
// and | ||
Expect(outbuf.String()).To(MatchRegexp(`unknown resource type: some-type. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission`)) | ||
// and | ||
Expect(errbuf.Bytes()).To(BeEmpty()) | ||
}) | ||
}) | ||
}) |