Skip to content

Commit

Permalink
feat(kumactl) add support for deleting proxytemplate
Browse files Browse the repository at this point in the history
Implemented the support for deleting proxytemplate. Check if the the given proxytemplate exists and then delete.

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

Fix kumahq#279
  • Loading branch information
pradeepmurugesan committed Oct 22, 2019
1 parent 64bac30 commit 8c2a042
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/kumactl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
resourceType = model.ResourceType(mesh.MeshType)
case "dataplane":
resourceType = model.ResourceType(mesh.DataplaneType)
case "proxytemplate":
resourceType = model.ResourceType(mesh.ProxyTemplateType)

default:
return errors.Errorf("unknown resource type: %s. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg)
Expand Down
135 changes: 135 additions & 0 deletions app/kumactl/cmd/delete/delete_proxytemplate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package delete_test

import (
"bytes"
"context"
"github.com/Kong/kuma/app/kumactl/cmd"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"path/filepath"

mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
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"
)

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

var sampleProxyTemplates []*mesh_core.ProxyTemplateResource

BeforeEach(func() {
sampleProxyTemplates = []*mesh_core.ProxyTemplateResource{
{
Meta: &test_model.ResourceMeta{
Namespace: "default",
Mesh: "custom-template",
Name: "custom-template",
},
Spec: mesh_proto.ProxyTemplate{},
},
{
Meta: &test_model.ResourceMeta{
Namespace: "default",
Mesh: "another-template",
Name: "another-template",
},
Spec: mesh_proto.ProxyTemplate{},
},
{
Meta: &test_model.ResourceMeta{
Namespace: "default",
Mesh: "simple-template",
Name: "simple-template",
},
Spec: mesh_proto.ProxyTemplate{},
},
}
})

Describe("Delete ProxyTemplate", 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 sampleProxyTemplates {
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 proxytemplate", func() {
// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "proxytemplate", "some-non-existing-proxytemplate"})

// when
err := rootCmd.Execute()

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

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

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

// when
err := rootCmd.Execute()

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

// and
list := &mesh_core.ProxyTemplateResourceList{}
err = store.List(context.Background(), list, core_store.ListByNamespace("default"))
Expect(err).ToNot(HaveOccurred())
Expect(list.Items).To(HaveLen(2))
// and
Expect(errbuf.String()).To(BeEmpty())
Expect(errbuf.String()).To(BeEmpty())
// and
Expect(outbuf.String()).To(Equal("deleted ProxyTemplate \"custom-template\"\n"))
})


})
})

0 comments on commit 8c2a042

Please sign in to comment.