From 226fefbc29a7c823f50a9f0417f3b1a9405f0ba2 Mon Sep 17 00:00:00 2001 From: PratikMane0112 Date: Sun, 30 Aug 2026 11:00:07 +0530 Subject: [PATCH 1/4] Fix snapshot-location get --selector to filter VSLs by label Signed-off-by: PratikMane0112 --- changelogs/unreleased/XXXX-PratikMane0112 | 1 + pkg/cmd/cli/snapshotlocation/get.go | 7 ++- pkg/cmd/cli/snapshotlocation/get_test.go | 62 +++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/XXXX-PratikMane0112 create mode 100644 pkg/cmd/cli/snapshotlocation/get_test.go diff --git a/changelogs/unreleased/XXXX-PratikMane0112 b/changelogs/unreleased/XXXX-PratikMane0112 new file mode 100644 index 000000000..8174c2b65 --- /dev/null +++ b/changelogs/unreleased/XXXX-PratikMane0112 @@ -0,0 +1 @@ +Fix snapshot-location get --selector flag to actually filter VolumeSnapshotLocations by label diff --git a/pkg/cmd/cli/snapshotlocation/get.go b/pkg/cmd/cli/snapshotlocation/get.go index 79da478bf..ff5b2af9e 100644 --- a/pkg/cmd/cli/snapshotlocation/get.go +++ b/pkg/cmd/cli/snapshotlocation/get.go @@ -1,5 +1,5 @@ /* -Copyright 2018 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -50,7 +50,10 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command { locations.Items = append(locations.Items, *location) } } else { - err = client.List(context.TODO(), locations, &kbclient.ListOptions{Namespace: f.Namespace()}) + err = client.List(context.TODO(), locations, &kbclient.ListOptions{ + Namespace: f.Namespace(), + Raw: &listOptions, + }) cmd.CheckError(err) } _, err = output.PrintWithFormat(c, locations) diff --git a/pkg/cmd/cli/snapshotlocation/get_test.go b/pkg/cmd/cli/snapshotlocation/get_test.go new file mode 100644 index 000000000..9a5ce6ef7 --- /dev/null +++ b/pkg/cmd/cli/snapshotlocation/get_test.go @@ -0,0 +1,62 @@ +/* +Copyright The Velero Contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package snapshotlocation + +import ( + "fmt" + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + + factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks" + cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec" +) + +func TestNewGetCommand(t *testing.T) { + vslList := []string{"vsl1", "vsl2"} + + f := &factorymocks.Factory{} + kbclient := velerotest.NewFakeControllerRuntimeClient(t) + f.On("Namespace").Return(mock.Anything) + f.On("KubebuilderClient").Return(kbclient, nil) + + // get command + c := NewGetCommand(f, "velero snapshot-location get") + assert.Equal(t, "Get snapshot locations", c.Short) + + c.Execute() + + if os.Getenv(cmdtest.CaptureFlag) == "1" { + c.SetArgs([]string{"vsl1", "vsl2"}) + c.Execute() + return + } + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand"}...) + cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) + _, stderr, err := veleroexec.RunCommand(cmd) + + if err != nil { + assert.Contains(t, stderr, fmt.Sprintf("volumesnapshotlocations.velero.io \"%s\" not found", vslList[0])) + return + } + t.Fatalf("process ran with err %v, want snapshot location get to fail for non-existent VSL", err) +} From 1155635e20295e7c2351b8e9c0981d78e432f58c Mon Sep 17 00:00:00 2001 From: PratikMane0112 Date: Sun, 30 Aug 2026 11:14:19 +0530 Subject: [PATCH 2/4] Update changelog with PR no Signed-off-by: PratikMane0112 --- changelogs/unreleased/10449-PratikMane0112 | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelogs/unreleased/10449-PratikMane0112 diff --git a/changelogs/unreleased/10449-PratikMane0112 b/changelogs/unreleased/10449-PratikMane0112 new file mode 100644 index 000000000..8174c2b65 --- /dev/null +++ b/changelogs/unreleased/10449-PratikMane0112 @@ -0,0 +1 @@ +Fix snapshot-location get --selector flag to actually filter VolumeSnapshotLocations by label From 375ae2a1c1294ddc7ae822c47ce82140fc1d13d9 Mon Sep 17 00:00:00 2001 From: PratikMane0112 Date: Sun, 30 Aug 2026 11:15:56 +0530 Subject: [PATCH 3/4] Remove old changelog Signed-off-by: PratikMane0112 --- changelogs/unreleased/XXXX-PratikMane0112 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 changelogs/unreleased/XXXX-PratikMane0112 diff --git a/changelogs/unreleased/XXXX-PratikMane0112 b/changelogs/unreleased/XXXX-PratikMane0112 deleted file mode 100644 index 8174c2b65..000000000 --- a/changelogs/unreleased/XXXX-PratikMane0112 +++ /dev/null @@ -1 +0,0 @@ -Fix snapshot-location get --selector flag to actually filter VolumeSnapshotLocations by label From 3c47a2c3269f896d60cdff2f112f2e81949a2190 Mon Sep 17 00:00:00 2001 From: PratikMane0112 Date: Mon, 31 Aug 2026 14:59:41 +0530 Subject: [PATCH 4/4] Update label selector pattern and add a test case Signed-off-by: PratikMane0112 --- pkg/cmd/cli/snapshotlocation/get.go | 9 +++-- pkg/cmd/cli/snapshotlocation/get_test.go | 42 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/cli/snapshotlocation/get.go b/pkg/cmd/cli/snapshotlocation/get.go index ff5b2af9e..46c4897ca 100644 --- a/pkg/cmd/cli/snapshotlocation/get.go +++ b/pkg/cmd/cli/snapshotlocation/get.go @@ -1,5 +1,5 @@ /* -Copyright the Velero contributors. +Copyright 2018 the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" kbclient "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" @@ -50,9 +51,11 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command { locations.Items = append(locations.Items, *location) } } else { + parsedSelector, err := labels.Parse(listOptions.LabelSelector) + cmd.CheckError(err) err = client.List(context.TODO(), locations, &kbclient.ListOptions{ - Namespace: f.Namespace(), - Raw: &listOptions, + LabelSelector: parsedSelector, + Namespace: f.Namespace(), }) cmd.CheckError(err) } diff --git a/pkg/cmd/cli/snapshotlocation/get_test.go b/pkg/cmd/cli/snapshotlocation/get_test.go index 9a5ce6ef7..e6ed31001 100644 --- a/pkg/cmd/cli/snapshotlocation/get_test.go +++ b/pkg/cmd/cli/snapshotlocation/get_test.go @@ -24,7 +24,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + kbclient "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/vmware-tanzu/velero/pkg/builder" factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks" cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test" velerotest "github.com/vmware-tanzu/velero/pkg/test" @@ -60,3 +63,42 @@ func TestNewGetCommand(t *testing.T) { } t.Fatalf("process ran with err %v, want snapshot location get to fail for non-existent VSL", err) } + +func TestNewGetCommand_SelectorFiltersVSLs(t *testing.T) { + f := &factorymocks.Factory{} + client := velerotest.NewFakeControllerRuntimeClient(t) + + vslLabeled := builder.ForVolumeSnapshotLocation(cmdtest.VeleroNameSpace, "vsl-labeled"). + ObjectMeta(builder.WithLabels("env", "test")). + Result() + err := client.Create(t.Context(), vslLabeled, &kbclient.CreateOptions{}) + require.NoError(t, err) + + vslUnlabeled := builder.ForVolumeSnapshotLocation(cmdtest.VeleroNameSpace, "vsl-unlabeled"). + Result() + err = client.Create(t.Context(), vslUnlabeled, &kbclient.CreateOptions{}) + require.NoError(t, err) + + f.On("KubebuilderClient").Return(client, nil) + f.On("Namespace").Return(cmdtest.VeleroNameSpace) + + // get command with selector + c := NewGetCommand(f, "velero snapshot-location get") + c.SetArgs([]string{"--selector", "env=test"}) + err = c.Execute() + require.NoError(t, err) + + if os.Getenv(cmdtest.CaptureFlag) == "1" { + return + } + + cmd := exec.CommandContext(t.Context(), os.Args[0], []string{"-test.run=TestNewGetCommand_SelectorFiltersVSLs"}...) + cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag)) + stdout, _, err := veleroexec.RunCommand(cmd) + require.NoError(t, err) + + // assert that the labeled VSL is returned + assert.Contains(t, stdout, "vsl-labeled") + // assert that the unlabeled VSL is not returned + assert.NotContains(t, stdout, "vsl-unlabeled") +}