Skip to content

Commit

Permalink
Merge pull request #4 from cybozu-go/list-simple
Browse files Browse the repository at this point in the history
Implement list -o simple
  • Loading branch information
yokaze committed May 16, 2024
2 parents 321b6a5 + e737f03 commit 90b7e0f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
33 changes: 26 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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])
},
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/spf13/cobra"
)

const (
OutputJson = "json"
OutputSimple = "simple"
)

var rootOptions struct {
namespace string
proxySelector string
Expand All @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/; \
Expand Down
2 changes: 1 addition & 1 deletion e2e/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit 90b7e0f

Please sign in to comment.