-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
- Loading branch information
Showing
5 changed files
with
244 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Hubble | ||
|
||
package list | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
observerpb "github.com/cilium/cilium/api/v1/observer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNamespaceOutputs(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
namespaces []*observerpb.Namespace | ||
expectedJSONOutput string | ||
expectedTableOutput string | ||
expectedWideTableOutput string | ||
}{ | ||
{ | ||
name: "multiple namespaces no cluster", | ||
namespaces: []*observerpb.Namespace{ | ||
{Namespace: "foo"}, | ||
{Namespace: "bar"}, | ||
{Namespace: "baz"}, | ||
{Namespace: "faz"}, | ||
}, | ||
expectedJSONOutput: `[ | ||
{ | ||
"namespace": "foo" | ||
}, | ||
{ | ||
"namespace": "bar" | ||
}, | ||
{ | ||
"namespace": "baz" | ||
}, | ||
{ | ||
"namespace": "faz" | ||
} | ||
] | ||
`, | ||
expectedTableOutput: `NAMESPACE | ||
foo | ||
bar | ||
baz | ||
faz | ||
`, | ||
expectedWideTableOutput: `NAMESPACE CLUSTER | ||
foo | ||
bar | ||
baz | ||
faz | ||
`, | ||
}, | ||
{ | ||
name: "multiple namespaces with cluster", | ||
namespaces: []*observerpb.Namespace{ | ||
{Namespace: "foo", Cluster: "cluster-1"}, | ||
{Namespace: "bar", Cluster: "cluster-1"}, | ||
{Namespace: "baz", Cluster: "cluster-2"}, | ||
{Namespace: "faz", Cluster: "cluster-2"}, | ||
}, | ||
expectedJSONOutput: `[ | ||
{ | ||
"cluster": "cluster-1", | ||
"namespace": "foo" | ||
}, | ||
{ | ||
"cluster": "cluster-1", | ||
"namespace": "bar" | ||
}, | ||
{ | ||
"cluster": "cluster-2", | ||
"namespace": "baz" | ||
}, | ||
{ | ||
"cluster": "cluster-2", | ||
"namespace": "faz" | ||
} | ||
] | ||
`, | ||
expectedTableOutput: `NAMESPACE | ||
foo | ||
bar | ||
baz | ||
faz | ||
`, | ||
expectedWideTableOutput: `NAMESPACE CLUSTER | ||
foo cluster-1 | ||
bar cluster-1 | ||
baz cluster-2 | ||
faz cluster-2 | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer func() { | ||
listOpts.output = "" | ||
}() | ||
buf := bytes.Buffer{} | ||
|
||
// json | ||
require.NoError(t, jsonOutput(&buf, tc.namespaces), tc.name) | ||
assert.Equal(t, tc.expectedJSONOutput, buf.String(), "json %s", tc.name) | ||
|
||
// regular table | ||
buf.Reset() | ||
require.NoError(t, namespaceTableOutput(&buf, tc.namespaces), tc.name) | ||
assert.Equal(t, tc.expectedTableOutput, buf.String(), "regular table %s", tc.name) | ||
|
||
// wide table | ||
listOpts.output = "wide" | ||
buf.Reset() | ||
require.NoError(t, namespaceTableOutput(&buf, tc.namespaces), tc.name) | ||
assert.Equal(t, tc.expectedWideTableOutput, buf.String(), "wide table %s", tc.name) | ||
}) | ||
} | ||
} |
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,97 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Hubble | ||
|
||
package list | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"text/tabwriter" | ||
|
||
observerpb "github.com/cilium/cilium/api/v1/observer" | ||
"github.com/cilium/hubble/cmd/common/config" | ||
"github.com/cilium/hubble/cmd/common/conn" | ||
"github.com/cilium/hubble/cmd/common/template" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func newNamespacesCommand(vp *viper.Viper) *cobra.Command { | ||
namespacesCmd := &cobra.Command{ | ||
Use: "namespaces", | ||
Short: "List namespaces with recent flows", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
hubbleConn, err := conn.New(ctx, vp.GetString(config.KeyServer), vp.GetDuration(config.KeyTimeout)) | ||
if err != nil { | ||
return err | ||
} | ||
defer hubbleConn.Close() | ||
return runListNamespaces(ctx, cmd, hubbleConn) | ||
}, | ||
} | ||
|
||
// formatting flags | ||
formattingFlags := pflag.NewFlagSet("Formatting", pflag.ContinueOnError) | ||
formattingFlags.StringVarP( | ||
&listOpts.output, "output", "o", "table", | ||
`Specify the output format, one of: | ||
json: JSON encoding | ||
table: Tab-aligned columns | ||
wide: Tab-aligned columns with additional information`) | ||
namespacesCmd.Flags().AddFlagSet(formattingFlags) | ||
|
||
// advanced completion for flags | ||
namespacesCmd.RegisterFlagCompletionFunc("output", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return []string{ | ||
"json", | ||
"table", | ||
"wide", | ||
}, cobra.ShellCompDirectiveDefault | ||
}) | ||
|
||
template.RegisterFlagSets(namespacesCmd, formattingFlags, config.ServerFlags) | ||
return namespacesCmd | ||
} | ||
|
||
func runListNamespaces(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error { | ||
req := &observerpb.GetNamespacesRequest{} | ||
res, err := observerpb.NewObserverClient(conn).GetNamespaces(ctx, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
namespaces := res.GetNamespaces() | ||
switch listOpts.output { | ||
case "json": | ||
return jsonOutput(cmd.OutOrStdout(), namespaces) | ||
case "table", "wide": | ||
return namespaceTableOutput(cmd.OutOrStdout(), namespaces) | ||
default: | ||
return fmt.Errorf("unknown output format: %s", listOpts.output) | ||
} | ||
} | ||
|
||
func namespaceTableOutput(buf io.Writer, namespaces []*observerpb.Namespace) error { | ||
tw := tabwriter.NewWriter(buf, 2, 0, 3, ' ', 0) | ||
// header | ||
fmt.Fprint(tw, "NAMESPACE") | ||
if listOpts.output == "wide" { | ||
fmt.Fprint(tw, "\tCLUSTER") | ||
} | ||
fmt.Fprintln(tw) | ||
|
||
// contents | ||
for _, ns := range namespaces { | ||
fmt.Fprint(tw, ns.Namespace) | ||
if listOpts.output == "wide" && ns.Cluster != "" { | ||
fmt.Fprint(tw, "\t", ns.Cluster) | ||
} | ||
fmt.Fprintln(tw) | ||
} | ||
return tw.Flush() | ||
} |
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