From e737f0370a3d521441c31c153fb141e2d6bec468 Mon Sep 17 00:00:00 2001 From: Daichi Sakaue Date: Wed, 3 Apr 2024 14:48:53 +0900 Subject: [PATCH] Implement list -o simple Signed-off-by: Daichi Sakaue --- .github/workflows/ci.yaml | 2 +- cmd/dump.go | 6 +++--- cmd/list.go | 33 ++++++++++++++++++++++++++------- cmd/root.go | 7 ++++++- e2e/Makefile | 4 ++-- e2e/list_test.go | 2 +- 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 31f7938..16423b4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,7 +34,7 @@ jobs: run: | make start make install-test-pod - make install-policy-viewer + make install-cilium-policy - name: Test working-directory: e2e run: make test diff --git a/cmd/dump.go b/cmd/dump.go index 68c206b..8c0b274 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -22,11 +22,11 @@ var dumpCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return runDump(context.Background(), args[0]) + return runDump(context.Background(), cmd.OutOrStdout(), args[0]) }, } -func runDump(ctx context.Context, name string) error { +func runDump(ctx context.Context, w io.Writer, name string) error { clientset, dynamicClient, _, err := createClients(ctx, name) if err != nil { return err @@ -54,6 +54,6 @@ func runDump(ctx context.Context, name string) error { var buf bytes.Buffer json.Indent(&buf, data, "", " ") - fmt.Println(buf.String()) + buf.WriteTo(w) return nil } diff --git a/cmd/list.go b/cmd/list.go index c0dd88c..03a5861 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -4,8 +4,10 @@ import ( "context" "encoding/json" "fmt" + "io" "strconv" "strings" + "text/tabwriter" "github.com/cilium/cilium/api/v1/client/endpoint" "github.com/spf13/cobra" @@ -22,7 +24,7 @@ var listCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return runList(context.Background(), args[0]) + return runList(context.Background(), cmd.OutOrStdout(), args[0]) }, } @@ -55,7 +57,7 @@ func parseDerivedFromEntry(input []string, direction string) derivedFromEntry { return val } -func runList(ctx context.Context, name string) error { +func runList(ctx context.Context, w io.Writer, name string) error { _, dynamicClient, client, err := createClients(ctx, name) if err != nil { return err @@ -91,11 +93,28 @@ func runList(ctx context.Context, name string) error { } } - text, err := json.MarshalIndent(policyList, "", " ") - if err != nil { + switch rootOptions.output { + case OutputJson: + text, err := json.MarshalIndent(policyList, "", " ") + if err != nil { + return err + } + _, err = w.Write(text) return err + case OutputSimple: + tw := tabwriter.NewWriter(w, 0, 1, 1, ' ', 0) + _, err := tw.Write([]byte("DIRECTION\tKIND\tNAMESPACE\tNAME\n")) + if err != nil { + return err + } + for _, p := range policyList { + _, err := tw.Write([]byte(fmt.Sprintf("%v\t%v\t%v\t%v\n", p.Direction, p.Kind, p.Namespace, p.Name))) + if err != nil { + return err + } + } + return tw.Flush() + default: + return fmt.Errorf("unknown format: %s", rootOptions.output) } - - fmt.Println(string(text)) - return nil } diff --git a/cmd/root.go b/cmd/root.go index 0f10b04..3451d32 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,11 @@ import ( "github.com/spf13/cobra" ) +const ( + OutputJson = "json" + OutputSimple = "simple" +) + var rootOptions struct { namespace string proxySelector string @@ -18,7 +23,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&rootOptions.namespace, "namespace", "n", "default", "namespace of a pod") rootCmd.PersistentFlags().StringVar(&rootOptions.proxySelector, "proxy-selector", "app.kubernetes.io/name=cilium-agent-proxy", "label selector to find the proxy pods") rootCmd.PersistentFlags().Uint16Var(&rootOptions.proxyPort, "proxy-port", 8080, "port number of the proxy endpoints") - rootCmd.PersistentFlags().StringVarP(&rootOptions.output, "output", "o", "json", "output format") + rootCmd.PersistentFlags().StringVarP(&rootOptions.output, "output", "o", OutputSimple, "output format") } var rootCmd = &cobra.Command{} diff --git a/e2e/Makefile b/e2e/Makefile index 10d0ddc..a6264b9 100644 --- a/e2e/Makefile +++ b/e2e/Makefile @@ -54,8 +54,8 @@ install-test-pod: $(MAKE) --no-print-directory wait-for-workloads $(KUBECTL) apply -f testdata/policy/l3.yaml -.PHONY: install-policy-viewer -install-policy-viewer: +.PHONY: install-cilium-policy +install-cilium-policy: $(MAKE) -C ../ build PODNAME=$$($(KUBECTL) get po -l app=ubuntu -o name | cut -d'/' -f2); \ $(KUBECTL) cp $(CILIUM_POLICY) $${PODNAME}:/tmp/; \ diff --git a/e2e/list_test.go b/e2e/list_test.go index b2e89b2..2f64dee 100644 --- a/e2e/list_test.go +++ b/e2e/list_test.go @@ -64,7 +64,7 @@ func testList() { It("should list applied policies", func() { for _, c := range cases { podName := onePodByLabelSelector(Default, "default", c.Selector) - result := runViewerSafe(Default, nil, "list", podName) + result := runViewerSafe(Default, nil, "list", "-o=json", podName) testJson(Default, result, c.Expected) } })