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

Add --cluster flag for filtering by cluster #1309

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/observe/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ func newFlowsCmdHelper(usage cmdUsage, vp *viper.Viper, ofilter *flowFilter) *co
filterFlags.Var(filterVar(
"node-name", ofilter,
`Show all flows which match the given node names (e.g. "k8s*", "test-cluster/*.company.com")`))
filterFlags.Var(filterVar(
"cluster", ofilter,
`Show all flows which match the cluster names (e.g. "test-cluster", "prod-*")`))
filterFlags.Var(filterVar(
"protocol", ofilter,
`Show only flows which match the given L4/L7 flow protocol (e.g. "udp", "http")`))
Expand Down
8 changes: 7 additions & 1 deletion cmd/observe/flows_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func newFlowFilter() *flowFilter {
{"identity", "from-identity"},
{"workload", "to-workload"},
{"workload", "from-workload"},
{"node-name"},
{"node-name", "cluster"},
{"tcp-flags"},
{"uuid"},
{"traffic-direction"},
Expand Down Expand Up @@ -657,6 +657,12 @@ func (of *flowFilter) set(f *filterTracker, name, val string, track bool) error
f.NodeName = append(f.GetNodeName(), val)
})

// cluster Name filters
case "cluster":
f.apply(func(f *flowpb.FlowFilter) {
f.NodeName = append(f.GetNodeName(), val+"/")
})

// TCP Flags filter
case "tcp-flags":
flags, err := parseTCPFlags(val)
Expand Down
48 changes: 48 additions & 0 deletions cmd/observe/flows_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,51 @@ func TestNamespace(t *testing.T) {
})
}
}

func TestCluster(t *testing.T) {
tt := []struct {
name string
flags []string
filters []*flowpb.FlowFilter
err string
}{
{
name: "Single cluster filter",
flags: []string{"--cluster", "foo"},
filters: []*flowpb.FlowFilter{
{NodeName: []string{"foo/"}},
},
},
{
name: "Multiple cluster filter",
flags: []string{"--cluster", "foo", "--cluster", "bar"},
filters: []*flowpb.FlowFilter{
{NodeName: []string{"foo/", "bar/"}},
},
},
{
name: "Cluster and node-name conflict",
flags: []string{"--cluster", "foo", "--node-name", "baz"},
filters: []*flowpb.FlowFilter{},
err: `invalid argument "baz" for "--node-name" flag: filters --node-name and --cluster cannot be combined`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
f := newFlowFilter()
cmd := newFlowsCmdWithFilter(viper.New(), f)
err := cmd.Flags().Parse(tc.flags)
if tc.err != "" {
require.Errorf(t, err, tc.err)
return
} else {
require.NoError(t, err)
}
assert.Nil(t, f.blacklist)
diff := cmp.Diff(tc.filters, f.whitelist.flowFilters(), cmpopts.IgnoreUnexported(flowpb.FlowFilter{}))
if diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
1 change: 1 addition & 0 deletions cmd/observe_help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Selectors Flags:


Filters Flags:
--cluster filter Show all flows which match the cluster names (e.g. "test-cluster", "prod-*")
--fqdn filter Show all flows related to the given fully qualified domain name (e.g. "*.cilium.io").
--from-fqdn filter Show all flows originating at the given fully qualified domain name (e.g. "*.cilium.io").
--from-identity filter Show all flows originating at an endpoint with the given security identity
Expand Down