From cec25b5d1ee087d0bc2e6f31cb4612dd2526e5ec Mon Sep 17 00:00:00 2001 From: Matheus Pimenta Date: Thu, 25 Jun 2026 12:14:24 +0100 Subject: [PATCH] Fix 'flux get all --status-selector' for empty results Signed-off-by: Matheus Pimenta --- cmd/flux/get.go | 3 + cmd/flux/get_test.go | 62 +++++++++++++++++++ cmd/flux/testdata/get/kustomization_only.yaml | 26 ++++++++ 3 files changed, 91 insertions(+) create mode 100644 cmd/flux/testdata/get/kustomization_only.yaml diff --git a/cmd/flux/get.go b/cmd/flux/get.go index 09fb7da9..a736e025 100644 --- a/cmd/flux/get.go +++ b/cmd/flux/get.go @@ -207,6 +207,9 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error { if err != nil { return err } + if getAll && len(rows) == 0 { + return nil + } err = printers.TablePrinter(header).Print(cmd.OutOrStdout(), rows) if err != nil { diff --git a/cmd/flux/get_test.go b/cmd/flux/get_test.go index 7376ff04..90e77ce2 100644 --- a/cmd/flux/get_test.go +++ b/cmd/flux/get_test.go @@ -103,6 +103,68 @@ func Test_GetCmdStatusSelector(t *testing.T) { } } +func Test_GetAllCmdStatusSelectorNoMatches(t *testing.T) { + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile("./testdata/get/status_objects.yaml", tmpl, t) + + cmd := cmdTestCase{ + args: "get all --status-selector foo=bar -n " + tmpl["fluxns"], + assert: assertGoldenValue(""), + } + + cmd.runTestCmd(t) +} + +func Test_GetAllCmdStatusSelectorKustomizationOnlyMatches(t *testing.T) { + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile("./testdata/get/kustomization_only.yaml", tmpl, t) + + unfilteredOutput, err := executeCommand("get all -n " + tmpl["fluxns"]) + if err != nil { + t.Fatalf("get all failed: %v", err) + } + if unfilteredOutput == "" { + t.Fatal("expected get all output for namespace with one Kustomization") + } + + filteredOutput, err := executeCommand("get all --status-selector Ready=True -n " + tmpl["fluxns"]) + if err != nil { + t.Fatalf("get all with matching status selector failed: %v", err) + } + + if filteredOutput != unfilteredOutput { + t.Fatalf("expected filtered output to match unfiltered output:\nfiltered:\n%s\nunfiltered:\n%s", filteredOutput, unfilteredOutput) + } +} + +func Test_GetAllCmdStatusSelectorKustomizationOnlyNoMatch(t *testing.T) { + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile("./testdata/get/kustomization_only.yaml", tmpl, t) + + emptyNamespace := allocateNamespace("empty") + setupTestNamespace(emptyNamespace, t) + + emptyOutput, err := executeCommand("get all -n " + emptyNamespace) + if err != nil { + t.Fatalf("get all in empty namespace failed: %v", err) + } + + filteredOutput, err := executeCommand("get all --status-selector Ready=False -n " + tmpl["fluxns"]) + if err != nil { + t.Fatalf("get all with non-matching status selector failed: %v", err) + } + + if filteredOutput != emptyOutput { + t.Fatalf("expected filtered output to match empty namespace output:\nfiltered:\n%s\nempty namespace:\n%s", filteredOutput, emptyOutput) + } +} + func Test_GetCmdErrors(t *testing.T) { tmpl := map[string]string{ "fluxns": allocateNamespace("flux-system"), diff --git a/cmd/flux/testdata/get/kustomization_only.yaml b/cmd/flux/testdata/get/kustomization_only.yaml new file mode 100644 index 00000000..50d62ccc --- /dev/null +++ b/cmd/flux/testdata/get/kustomization_only.yaml @@ -0,0 +1,26 @@ +--- +apiVersion: v1 +kind: Namespace +metadata: + name: {{ .fluxns }} +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1 +kind: Kustomization +metadata: + name: podinfo + namespace: {{ .fluxns }} +spec: + interval: 5m + path: ./clusters/production + prune: true + sourceRef: + kind: GitRepository + name: podinfo +status: + lastAppliedRevision: main@sha1:696f056df216eea4f9401adbee0ff744d4df390f + conditions: + - lastTransitionTime: "2021-08-01T04:52:56Z" + message: 'Applied revision: main@sha1:696f056df216eea4f9401adbee0ff744d4df390f' + reason: ReconciliationSucceeded + status: "True" + type: Ready