-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kumactl) support for TrafficTrace (#584)
- Loading branch information
1 parent
805116c
commit 741c998
Showing
12 changed files
with
322 additions
and
3 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
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
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,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) | ||
} |
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,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
36
app/kumactl/cmd/get/testdata/get-traffic-traces.golden.json
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,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" | ||
} | ||
] | ||
} |
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,3 @@ | ||
MESH NAME | ||
default web1 | ||
default web2 |
19 changes: 19 additions & 0 deletions
19
app/kumactl/cmd/get/testdata/get-traffic-traces.golden.yaml
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,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 |
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
Oops, something went wrong.