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

[WIP] kuma-ctl: adds get traffic-logs name.namespace #344

Closed
Closed
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
27 changes: 27 additions & 0 deletions app/kumactl/cmd/get/arg_regex_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package get

import (
"fmt"
"regexp"

"github.com/spf13/cobra"
)

// RegexArgs return error if arguments don't pass regex test
func RegexArgs(validationMap map[int][]string) func(cmd *cobra.Command, args []string) error {
michal-franc marked this conversation as resolved.
Show resolved Hide resolved
return func(cmd *cobra.Command, args []string) error {

// check if there are more args than expected
if err := cobra.MaximumNArgs(len(validationMap))(cmd, args); err != nil {
return err
}

for i, arg := range args {
re := regexp.MustCompile(validationMap[i][0])
if !re.MatchString(arg) {
return fmt.Errorf("expected '%s', got '%s'", validationMap[i][1], arg)
}
}
return nil
}
}
40 changes: 31 additions & 9 deletions app/kumactl/cmd/get/get_traffic_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package get
import (
"context"
"io"
"strings"

"github.com/Kong/kuma/app/kumactl/pkg/output"
"github.com/Kong/kuma/app/kumactl/pkg/output/printers"
Expand All @@ -18,43 +19,64 @@ func newGetTrafficLogsCmd(pctx *getContext) *cobra.Command {
Use: "traffic-logs",
Short: "Show TrafficLogs",
Long: `Show TrafficLog entities.`,
RunE: func(cmd *cobra.Command, _ []string) error {
Args: RegexArgs(map[int][]string{
michal-franc marked this conversation as resolved.
Show resolved Hide resolved
0: []string{".*\\..*", "<name>.<namespace>"},
}),
RunE: func(cmd *cobra.Command, args []string) error {
rs, err := pctx.CurrentResourceStore()
if err != nil {
return err
}

trafficLogging := mesh.TrafficLogResourceList{}
if err := rs.List(context.Background(), &trafficLogging, core_store.ListByMesh(pctx.CurrentMesh())); err != nil {
return errors.Wrapf(err, "failed to list TrafficLog")
var trafficLogs mesh.TrafficLogResourceList

if len(args) == 1 {
s := strings.Split(args[0], ".")
michal-franc marked this conversation as resolved.
Show resolved Hide resolved
name := s[0]
namespace := s[1]

trafficLog := mesh.TrafficLogResource{}
if err := rs.Get(context.Background(), &trafficLog, core_store.GetByKey(namespace, name, pctx.CurrentMesh())); err != nil {
michal-franc marked this conversation as resolved.
Show resolved Hide resolved
return errors.Wrapf(err, "failed to get TrafficLog")
}

trafficLogs = mesh.TrafficLogResourceList{
Items: []*mesh.TrafficLogResource{&trafficLog},
}

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

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

func printTrafficLog(trafficLogging *mesh.TrafficLogResourceList, out io.Writer) error {
func printTrafficLogs(trafficLogs *mesh.TrafficLogResourceList, 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(trafficLogging.Items) <= i {
if len(trafficLogs.Items) <= i {
return nil
}
trafficLogging := trafficLogging.Items[i]
trafficLogging := trafficLogs.Items[i]

return []string{
trafficLogging.GetMeta().GetMesh(), // MESH
Expand Down
65 changes: 61 additions & 4 deletions app/kumactl/cmd/get/get_traffic_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var _ = Describe("kumactl get traffic-logs", func() {
Meta: &test_model.ResourceMeta{
Mesh: "default",
Name: "web1-to-backend1",
Namespace: "",
Namespace: "namespace1",
},
},
{
Expand Down Expand Up @@ -88,7 +88,7 @@ var _ = Describe("kumactl get traffic-logs", func() {
Meta: &test_model.ResourceMeta{
Mesh: "default",
Name: "web2-to-backend2",
Namespace: "",
Namespace: "namespace2",
},
},
}
Expand Down Expand Up @@ -132,9 +132,15 @@ var _ = Describe("kumactl get traffic-logs", func() {
DescribeTable("kumactl get traffic-logs -o table|json|yaml",
func(given testCase) {
// given
rootCmd.SetArgs(append([]string{
args := append([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"get", "traffic-logs"}, given.outputFormat))
"get", "traffic-logs"})

if given.outputFormat != "" {
args = append(args, given.outputFormat)
}

rootCmd.SetArgs(args)

// when
err := rootCmd.Execute()
Expand Down Expand Up @@ -173,6 +179,57 @@ var _ = Describe("kumactl get traffic-logs", func() {
matcher: MatchYAML,
}),
)

DescribeTable("kumactl get traffic-logs web2-to-backend2.namespace2 -o table|json|yaml",
func(given testCase) {
// given

args := append([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"get", "traffic-logs", "web2-to-backend2.namespace2"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test case for kubernetes environment (because of web2-to-backend2.namespace2).

We also need a test case for universal environment (i.e. web2-to-backend2)


if given.outputFormat != "" {
args = append(args, given.outputFormat)
}
rootCmd.SetArgs(args)

// 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-logs_one_item.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-logs_one_item.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-logs_one_item.golden.json",
matcher: MatchJSON,
}),
Entry("should support YAML output", testCase{
outputFormat: "-oyaml",
goldenFile: "get-traffic-logs_one_item.golden.yaml",
matcher: MatchYAML,
}),
)
})

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"items": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of kumactl get <type> <name> should be a single object rather than a list

{
"mesh": "default",
"name": "web2-to-backend2",
"rules": [
{
"sources": [
{
"match": {
"service": "web2",
"version": "1.0"
}
}
],
"destinations": [
{
"match": {
"service": "backend2",
"env": "dev"
}
}
],
"conf": {
"backend": "logstash"
}
}
],
"type": "TrafficLog"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MESH NAME
default web2-to-backend2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
items:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of kumactl get <type> <name> should be a single object rather than a list

- mesh: default
name: web2-to-backend2
rules:
- destinations:
- match:
env: dev
service: backend2
sources:
- match:
service: web2
version: "1.0"
conf:
backend: logstash
type: TrafficLog