Skip to content

Commit

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

* returns an error in case of no traffic-log with the given name.

Fix kumahq#279
  • Loading branch information
pradeepmurugesan committed Oct 17, 2019
1 parent ef37497 commit ae25b0d
Show file tree
Hide file tree
Showing 2 changed files with 176 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 @@ -34,6 +34,8 @@ func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
resourceType = model.ResourceType(mesh.DataplaneType)
case "proxytemplate":
resourceType = model.ResourceType(mesh.ProxyTemplateType)
case "traffic-log":
resourceType = model.ResourceType(mesh.TrafficLogType)

default:
return errors.Errorf("unknown resource type: %s. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg)
Expand Down
174 changes: 174 additions & 0 deletions app/kumactl/cmd/delete/delete_trafficlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
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"
"time"

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 trafficlog", func() {

trafficLoggingResources := []*mesh_core.TrafficLogResource{
{
Spec: mesh_proto.TrafficLog{
Rules: []*mesh_proto.TrafficLog_Rule{
{
Sources: []*mesh_proto.Selector{
{
Match: map[string]string{
"service": "web1",
"version": "1.0",
},
},
},
Destinations: []*mesh_proto.Selector{
{
Match: map[string]string{
"service": "backend1",
"env": "dev",
},
},
},
Conf: &mesh_proto.TrafficLog_Rule_Conf{
Backend: "file",
},
},
},
},
Meta: &test_model.ResourceMeta{
Mesh: "web1-to-backend1",
Name: "web1-to-backend1",
Namespace: "default",
},
},
{
Spec: mesh_proto.TrafficLog{
Rules: []*mesh_proto.TrafficLog_Rule{
{
Sources: []*mesh_proto.Selector{
{
Match: map[string]string{
"service": "web2",
"version": "1.0",
},
},
},
Destinations: []*mesh_proto.Selector{
{
Match: map[string]string{
"service": "backend2",
"env": "dev",
},
},
},
Conf: &mesh_proto.TrafficLog_Rule_Conf{
Backend: "logstash",
},
},
},
},
Meta: &test_model.ResourceMeta{
Mesh: "web2-to-backend2",
Name: "web2-to-backend2",
Namespace: "default",
},
},
}

Describe("delete trafficlog", 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 trafficLoggingResources {
err := store.Create(context.Background(), ds, core_store.CreateBy(core_model.MetaToResourceKey(ds.GetMeta())))
Expect(err).ToNot(HaveOccurred())
}

rootCmd = cmd.NewRootCmd(rootCtx)
outbuf = &bytes.Buffer{}
errbuf = &bytes.Buffer{}
rootCmd.SetOut(outbuf)
rootCmd.SetErr(errbuf)
})

Describe("Delete TrafficLog", func() {

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

// when
err := rootCmd.Execute()

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

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

// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "traffic-log", "web1-to-backend1"})

// when
err := rootCmd.Execute()

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

// and
list := &mesh_core.TrafficLogResourceList{}
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())
Expect(errbuf.String()).To(BeEmpty())
// and
Expect(outbuf.String()).To(Equal("deleted TrafficLog \"web1-to-backend1\"\n"))
})

})
})

})

0 comments on commit ae25b0d

Please sign in to comment.