Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kumactl) TrafficTrace #584

Merged
merged 3 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changes:

* feature: generate HTTP-specific outbound listeners for services tagged with `protocol: http`
[#585](https://github.com/Kong/kuma/pull/585)
* feature: TracingTrace in kumactl
[#584](https://github.com/Kong/kuma/pull/584)
* feature: TracingTrace in Kuma REST API
[#583](https://github.com/Kong/kuma/pull/583)
* feature: TracingTrace entity
Expand Down
4 changes: 3 additions & 1 deletion app/kumactl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
resourceType = mesh.TrafficPermissionType
case "traffic-route":
resourceType = mesh.TrafficRouteType
case "traffic-trace":
resourceType = mesh.TrafficTraceType

default:
return errors.Errorf("unknown TYPE: %s. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route", resourceTypeArg)
return errors.Errorf("unknown TYPE: %s. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route, traffic-trace", resourceTypeArg)
}

currentMesh := pctx.CurrentMesh()
Expand Down
16 changes: 14 additions & 2 deletions app/kumactl/cmd/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ var _ = Describe("kumactl delete ", func() {
// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("unknown TYPE: some-type. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route"))
Expect(err.Error()).To(Equal("unknown TYPE: some-type. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route, traffic-trace"))
// and
Expect(outbuf.String()).To(MatchRegexp(`unknown TYPE: some-type. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route`))
Expect(outbuf.String()).To(MatchRegexp(`unknown TYPE: some-type. Allowed values: mesh, dataplane, healthcheck, proxytemplate, traffic-log, traffic-permission, traffic-route, traffic-trace`))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})
Expand Down Expand Up @@ -171,6 +171,12 @@ var _ = Describe("kumactl delete ", func() {
resource: func() core_model.Resource { return &mesh_core.TrafficRouteResource{} },
expectedMessage: "deleted TrafficRoute \"web-to-backend\"\n",
}),
Entry("traffic-traces", testCase{
typ: "traffic-trace",
name: "web",
resource: func() core_model.Resource { return &mesh_core.TrafficTraceResource{} },
expectedMessage: "deleted TrafficTrace \"web\"\n",
}),
)

DescribeTable("should fail if resource doesn't exist",
Expand Down Expand Up @@ -220,6 +226,12 @@ var _ = Describe("kumactl delete ", func() {
resource: func() core_model.Resource { return &mesh_core.TrafficRouteResource{} },
expectedMessage: "Error: there is no TrafficRoute with name \"web-to-backend\"\n",
}),
Entry("traffic-traces", testCase{
typ: "traffic-trace",
name: "web",
resource: func() core_model.Resource { return &mesh_core.TrafficRouteResource{} },
expectedMessage: "Error: there is no TrafficTrace with name \"web\"\n",
}),
)
})
})
Expand Down
1 change: 1 addition & 0 deletions app/kumactl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ func NewGetCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd.AddCommand(newGetTrafficPermissionsCmd(ctx))
cmd.AddCommand(newGetTrafficRoutesCmd(ctx))
cmd.AddCommand(newGetTrafficLogsCmd(ctx))
cmd.AddCommand(newGetTrafficTracesCmd(ctx))
return cmd
}
67 changes: 67 additions & 0 deletions app/kumactl/cmd/get/get_traffic_traces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package get

import (
"context"
"io"

"github.com/Kong/kuma/app/kumactl/pkg/output"
"github.com/Kong/kuma/app/kumactl/pkg/output/printers"
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
rest_types "github.com/Kong/kuma/pkg/core/resources/model/rest"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func newGetTrafficTracesCmd(pctx *getContext) *cobra.Command {
cmd := &cobra.Command{
Use: "traffic-traces",
Short: "Show TrafficTraces",
Long: `Show TrafficTrace entities.`,
RunE: func(cmd *cobra.Command, _ []string) error {
rs, err := pctx.CurrentResourceStore()
if err != nil {
return err
}

trafficTraces := mesh.TrafficTraceResourceList{}
if err := rs.List(context.Background(), &trafficTraces, core_store.ListByMesh(pctx.CurrentMesh())); err != nil {
return errors.Wrapf(err, "failed to list TrafficTrace")
}

switch format := output.Format(pctx.args.outputFormat); format {
case output.TableFormat:
return printTrafficTrace(&trafficTraces, cmd.OutOrStdout())
default:
printer, err := printers.NewGenericPrinter(format)
if err != nil {
return err
}
return printer.Print(rest_types.From.ResourceList(&trafficTraces), cmd.OutOrStdout())
}
},
}
return cmd
}

func printTrafficTrace(trafficTraces *mesh.TrafficTraceResourceList, out io.Writer) error {
data := printers.Table{
Headers: []string{"MESH", "NAME"},
NextRow: func() func() []string {
i := 0
return func() []string {
defer func() { i++ }()
if len(trafficTraces.Items) <= i {
return nil
}
trafficTraces := trafficTraces.Items[i]

return []string{
trafficTraces.GetMeta().GetMesh(), // MESH
trafficTraces.GetMeta().GetName(), // NAME
}
}
}(),
}
return printers.NewTablePrinter().Print(data, out)
}
152 changes: 152 additions & 0 deletions app/kumactl/cmd/get/get_traffic_traces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package get_test

import (
"bytes"
"context"
"io/ioutil"
"path/filepath"
"strings"
"time"

"github.com/Kong/kuma/api/mesh/v1alpha1"
"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"
"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"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
gomega_types "github.com/onsi/gomega/types"
"github.com/spf13/cobra"
)

var _ = Describe("kumactl get traffic-traces", func() {

trafficTraceResources := []*mesh.TrafficTraceResource{
{
Spec: v1alpha1.TrafficTrace{
Selectors: []*v1alpha1.Selector{
{
Match: map[string]string{
"service": "web1",
"version": "1.0",
},
},
},
Conf: &v1alpha1.TrafficTrace_Conf{
Backend: "zipkin",
},
},
Meta: &test_model.ResourceMeta{
Mesh: "default",
Name: "web1",
},
},
{
Spec: v1alpha1.TrafficTrace{
Selectors: []*v1alpha1.Selector{
{
Match: map[string]string{
"service": "web2",
"version": "1.0",
},
},
},
Conf: &v1alpha1.TrafficTrace_Conf{
Backend: "zipkin",
},
},
Meta: &test_model.ResourceMeta{
Mesh: "default",
Name: "web2",
},
},
}

Describe("GetTrafficTraceCmd", 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: time.Now,
NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) {
return store, nil
},
},
}

store = memory_resources.NewStore()

for _, ds := range trafficTraceResources {
err := store.Create(context.Background(), ds, core_store.CreateBy(core_model.MetaToResourceKey(ds.GetMeta())))
Expect(err).ToNot(HaveOccurred())
}

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

type testCase struct {
outputFormat string
goldenFile string
matcher func(interface{}) gomega_types.GomegaMatcher
}

DescribeTable("kumactl get traffic-traces -o table|json|yaml",
func(given testCase) {
// given
rootCmd.SetArgs(append([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"get", "traffic-traces"}, given.outputFormat))

// when
err := rootCmd.Execute()
// then
Expect(err).ToNot(HaveOccurred())

// when
expected, err := ioutil.ReadFile(filepath.Join("testdata", given.goldenFile))
// then
Expect(err).ToNot(HaveOccurred())
// and
Expect(buf.String()).To(given.matcher(expected))
},
Entry("should support Table output by default", testCase{
outputFormat: "",
goldenFile: "get-traffic-traces.golden.txt",
matcher: func(expected interface{}) gomega_types.GomegaMatcher {
return WithTransform(strings.TrimSpace, Equal(strings.TrimSpace(string(expected.([]byte)))))
},
}),
Entry("should support Table output explicitly", testCase{
outputFormat: "-otable",
goldenFile: "get-traffic-traces.golden.txt",
matcher: func(expected interface{}) gomega_types.GomegaMatcher {
return WithTransform(strings.TrimSpace, Equal(strings.TrimSpace(string(expected.([]byte)))))
},
}),
Entry("should support JSON output", testCase{
outputFormat: "-ojson",
goldenFile: "get-traffic-traces.golden.json",
matcher: MatchJSON,
}),
Entry("should support YAML output", testCase{
outputFormat: "-oyaml",
goldenFile: "get-traffic-traces.golden.yaml",
matcher: MatchYAML,
}),
)
})

})
36 changes: 36 additions & 0 deletions app/kumactl/cmd/get/testdata/get-traffic-traces.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"items": [
{
"mesh": "default",
"name": "web1",
"selectors": [
{
"match": {
"service": "web1",
"version": "1.0"
}
}
],
"conf": {
"backend": "zipkin"
},
"type": "TrafficTrace"
},
{
"mesh": "default",
"name": "web2",
"selectors": [
{
"match": {
"service": "web2",
"version": "1.0"
}
}
],
"conf": {
"backend": "zipkin"
},
"type": "TrafficTrace"
}
]
}
3 changes: 3 additions & 0 deletions app/kumactl/cmd/get/testdata/get-traffic-traces.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MESH NAME
default web1
default web2
19 changes: 19 additions & 0 deletions app/kumactl/cmd/get/testdata/get-traffic-traces.golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
items:
- mesh: default
name: web1
selectors:
- match:
service: web1
version: "1.0"
conf:
backend: zipkin
type: TrafficTrace
- mesh: default
name: web2
selectors:
- match:
service: web2
version: "1.0"
conf:
backend: zipkin
type: TrafficTrace
19 changes: 19 additions & 0 deletions docs/cmd/kumactl/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ Available Commands:
traffic-logs Show TrafficLogs
traffic-permissions Show TrafficPermissions
traffic-routes Show TrafficRoutes
traffic-traces Show TrafficTraces

Flags:
-h, --help help for get
Expand Down Expand Up @@ -474,6 +475,24 @@ Global Flags:
-o, --output string output format: one of table|yaml|json (default "table")
```

### kumactl get traffic-traces

```
Show TrafficTrace entities.

Usage:
kumactl get traffic-traces [flags]

Flags:
-h, --help help for traffic-traces

Global Flags:
--config-file string path to the configuration file to use
--log-level string log level: one of off|info|debug (default "off")
--mesh string mesh to use (default "default")
-o, --output string output format: one of table|yaml|json (default "table")
```

## kumactl delete

```
Expand Down
1 change: 1 addition & 0 deletions tools/docs/kumactl/gen_help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gen_help kumactl get proxytemplates
gen_help kumactl get traffic-logs
gen_help kumactl get traffic-permissions
gen_help kumactl get traffic-routes
gen_help kumactl get traffic-traces
gen_help kumactl delete
gen_help kumactl inspect
gen_help kumactl inspect dataplanes
Expand Down
Loading