mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-15 19:56:06 +00:00
fix: trim spaces in ordered-resources names (#10259)
* fix: trim spaces in ordered-resources names Signed-off-by: Jay2006sawant <jay242902@gmail.com> * chore: rename changelog for PR 10259 Signed-off-by: Jay2006sawant <jay242902@gmail.com> --------- Signed-off-by: Jay2006sawant <jay242902@gmail.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Trim spaces around resource names in --ordered-resources so comma-separated lists with spaces still match
|
||||
@@ -346,7 +346,15 @@ func getOrderedResourcesForType(
|
||||
if !ok || len(orderStr) == 0 {
|
||||
return nil
|
||||
}
|
||||
orders := strings.Split(orderStr, ",")
|
||||
parts := strings.Split(orderStr, ",")
|
||||
orders := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
name := strings.TrimSpace(part)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
orders = append(orders, name)
|
||||
}
|
||||
return orders
|
||||
}
|
||||
|
||||
|
||||
@@ -445,3 +445,24 @@ func TestGetResourceItems(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrderedResourcesForTypeTrimsSpaces(t *testing.T) {
|
||||
// Spaces after commas are common in CLI input and should not break ordering.
|
||||
orders := getOrderedResourcesForType(map[string]string{
|
||||
"pods": "ns1/pod2, ns1/pod1",
|
||||
}, "pods")
|
||||
require.Equal(t, []string{"ns1/pod2", "ns1/pod1"}, orders)
|
||||
|
||||
log := logrus.StandardLogger()
|
||||
podResources := []*kubernetesResource{
|
||||
{namespace: "ns1", name: "pod3"},
|
||||
{namespace: "ns1", name: "pod1"},
|
||||
{namespace: "ns1", name: "pod2"},
|
||||
}
|
||||
sorted := sortResourcesByOrder(log, podResources, orders)
|
||||
require.Equal(t, []*kubernetesResource{
|
||||
{namespace: "ns1", name: "pod2", orderedResource: true},
|
||||
{namespace: "ns1", name: "pod1", orderedResource: true},
|
||||
{namespace: "ns1", name: "pod3"},
|
||||
}, sorted)
|
||||
}
|
||||
|
||||
@@ -385,8 +385,19 @@ func ParseOrderedResources(orderMapStr string) (map[string]string, error) {
|
||||
return nil, fmt.Errorf("invalid OrderedResources '%s'", entry)
|
||||
}
|
||||
kind := strings.TrimSpace(kv[0])
|
||||
order := strings.TrimSpace(kv[1])
|
||||
orderedResources[kind] = order
|
||||
orderParts := strings.Split(kv[1], ",")
|
||||
cleaned := make([]string, 0, len(orderParts))
|
||||
for _, part := range orderParts {
|
||||
name := strings.TrimSpace(part)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
cleaned = append(cleaned, name)
|
||||
}
|
||||
if kind == "" || len(cleaned) == 0 {
|
||||
return nil, fmt.Errorf("invalid OrderedResources '%s'", entry)
|
||||
}
|
||||
orderedResources[kind] = strings.Join(cleaned, ",")
|
||||
}
|
||||
return orderedResources, nil
|
||||
}
|
||||
|
||||
@@ -234,6 +234,14 @@ func TestCreateOptions_OrderedResources(t *testing.T) {
|
||||
"persistentvolumes": "pv1,pv2",
|
||||
}
|
||||
assert.Equal(t, expectedMixedResources, orderedResources)
|
||||
|
||||
// Spaces after commas in the resource list must be trimmed.
|
||||
orderedResources, err = ParseOrderedResources("pods=ns1/p1, ns1/p2 ; persistentvolumeclaims= ns2/pvc1, ns2/pvc2")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, map[string]string{
|
||||
"pods": "ns1/p1,ns1/p2",
|
||||
"persistentvolumeclaims": "ns2/pvc1,ns2/pvc2",
|
||||
}, orderedResources)
|
||||
}
|
||||
|
||||
func TestCreateCommand(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user