Skip to content

Commit

Permalink
Add label filtering (-l) for get namespaces (#2915)
Browse files Browse the repository at this point in the history
* Add label filtering (-l) for get namespaces

* Add ns as a shorthand
  • Loading branch information
deszhou authored Apr 3, 2024
1 parent ec846b4 commit 437eff1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gwctl/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func runDescribe(cmd *cobra.Command, args []string, params *utils.CmdParams) {
}
backendsPrinter.PrintDescribeView(resourceModel)

case "namespace", "namespaces":
case "namespace", "namespaces", "ns":
filter := resourcediscovery.Filter{}
if len(args) > 1 {
filter.Name = args[1]
Expand Down
9 changes: 7 additions & 2 deletions gwctl/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ func runGet(cmd *cobra.Command, args []string, params *utils.CmdParams) {
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Out: params.Out, Clock: realClock}

switch kind {
case "namespace", "namespaces":
resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{})
case "namespace", "namespaces", "ns":
selector, err := labels.Parse(labelSelector)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to find resources that match the label selector \"%s\": %v\n", labelSelector, err)
os.Exit(1)
}
resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{Labels: selector})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to discover Namespace resources: %v\n", err)
os.Exit(1)
Expand Down
56 changes: 56 additions & 0 deletions gwctl/pkg/printer/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
testingclock "k8s.io/utils/clock/testing"

Expand Down Expand Up @@ -257,3 +258,58 @@ DirectlyAttachedPolicies:
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
}
}

// TestNamespacesPrinter_LabelSelector tests label selector filtering for Namespaces in 'get' command.
func TestNamespacesPrinter_LabelSelector(t *testing.T) {
fakeClock := testingclock.NewFakeClock(time.Now())
namespace := func(name string, labels map[string]string) *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
CreationTimestamp: metav1.Time{
Time: fakeClock.Now().Add(-46 * 24 * time.Hour),
},
Labels: labels,
},
Status: corev1.NamespaceStatus{
Phase: corev1.NamespaceActive,
},
}
}

objects := []runtime.Object{
namespace("namespace-1", map[string]string{"app": "foo"}),
namespace("namespace-2", map[string]string{"app": "foo", "env": "internal"}),
}

params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
discoverer := resourcediscovery.Discoverer{
K8sClients: params.K8sClients,
PolicyManager: params.PolicyManager,
}
labelSelector := "env=internal"
selector, err := labels.Parse(labelSelector)
if err != nil {
t.Errorf("Unable to find resources that match the label selector \"%s\": %v\n", labelSelector, err)
}
resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{Labels: selector})
if err != nil {
t.Fatalf("Failed to construct resourceModel: %v", resourceModel)
}

nsp := &NamespacesPrinter{
Out: params.Out,
Clock: fakeClock,
}
nsp.Print(resourceModel)

got := params.Out.(*bytes.Buffer).String()
want := `
NAME STATUS AGE
namespace-2 Active 46d
`

if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
}
}

0 comments on commit 437eff1

Please sign in to comment.