Merge pull request #10027 from kaovilai/add-restore-notin-selector-tests

Add set-based label selector test and docs coverage for restore
This commit is contained in:
Chlins Zhang
2026-09-15 16:01:54 +08:00
committed by GitHub
3 changed files with 139 additions and 0 deletions
+22
View File
@@ -24,6 +24,28 @@ func TestSetOfLabelSelector(t *testing.T) {
assert.True(t, str == "k1=v1,k2=v2" || str == "k2=v2,k2=v2")
}
func TestSetOfSetBasedLabelSelector(t *testing.T) {
selector := &LabelSelector{}
require.NoError(t, selector.Set("pr-label notin (1)"))
require.NotNil(t, selector.LabelSelector)
require.Len(t, selector.LabelSelector.MatchExpressions, 1)
req := selector.LabelSelector.MatchExpressions[0]
assert.Equal(t, "pr-label", req.Key)
assert.Equal(t, metav1.LabelSelectorOpNotIn, req.Operator)
assert.Equal(t, []string{"1"}, req.Values)
}
func TestSetOfDoesNotExistLabelSelector(t *testing.T) {
selector := &LabelSelector{}
require.NoError(t, selector.Set("!pr-label"))
require.NotNil(t, selector.LabelSelector)
require.Len(t, selector.LabelSelector.MatchExpressions, 1)
req := selector.LabelSelector.MatchExpressions[0]
assert.Equal(t, "pr-label", req.Key)
assert.Equal(t, metav1.LabelSelectorOpDoesNotExist, req.Operator)
assert.Empty(t, req.Values)
}
func TestTypeOfLabelSelector(t *testing.T) {
selector := &LabelSelector{}
assert.Equal(t, "labelSelector", selector.Type())
+93
View File
@@ -453,6 +453,99 @@ func TestRestoreResourceFiltering(t *testing.T) {
test.PVs(): {"/pv-1"},
},
},
{
name: "notin label selector excludes matching resources",
restore: defaultRestore().LabelSelector(&metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "pr-label", Operator: metav1.LabelSelectorOpNotIn, Values: []string{"1"}},
}}).Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems("pods",
builder.ForPod("ns-1", "pod-1").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
builder.ForPod("ns-2", "pod-2").Result(),
).
AddItems("deployments.apps",
builder.ForDeployment("ns-1", "deploy-1").Result(),
builder.ForDeployment("ns-2", "deploy-2").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
).
AddItems("persistentvolumes",
builder.ForPersistentVolume("pv-1").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
builder.ForPersistentVolume("pv-2").ObjectMeta(builder.WithLabels("pr-label", "2")).Result(),
).
Done(),
apiResources: []*test.APIResource{
test.Pods(),
test.Deployments(),
test.PVs(),
},
want: map[*test.APIResource][]string{
test.Pods(): {"ns-2/pod-2"},
test.Deployments(): {"ns-1/deploy-1"},
test.PVs(): {"/pv-2"},
},
},
{
name: "in label selector only restores matching resources",
restore: defaultRestore().LabelSelector(&metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "pr-label", Operator: metav1.LabelSelectorOpIn, Values: []string{"1", "2"}},
}}).Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems("pods",
builder.ForPod("ns-1", "pod-1").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
builder.ForPod("ns-2", "pod-2").ObjectMeta(builder.WithLabels("pr-label", "3")).Result(),
).
AddItems("deployments.apps",
builder.ForDeployment("ns-1", "deploy-1").Result(),
builder.ForDeployment("ns-2", "deploy-2").ObjectMeta(builder.WithLabels("pr-label", "2")).Result(),
).
AddItems("persistentvolumes",
builder.ForPersistentVolume("pv-1").ObjectMeta(builder.WithLabels("pr-label", "2")).Result(),
builder.ForPersistentVolume("pv-2").Result(),
).
Done(),
apiResources: []*test.APIResource{
test.Pods(),
test.Deployments(),
test.PVs(),
},
want: map[*test.APIResource][]string{
test.Pods(): {"ns-1/pod-1"},
test.Deployments(): {"ns-2/deploy-2"},
test.PVs(): {"/pv-1"},
},
},
{
name: "doesnotexist label selector only restores resources without the label key",
restore: defaultRestore().LabelSelector(&metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "pr-label", Operator: metav1.LabelSelectorOpDoesNotExist},
}}).Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems("pods",
builder.ForPod("ns-1", "pod-1").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
builder.ForPod("ns-2", "pod-2").Result(),
).
AddItems("deployments.apps",
builder.ForDeployment("ns-1", "deploy-1").Result(),
builder.ForDeployment("ns-2", "deploy-2").ObjectMeta(builder.WithLabels("pr-label", "2")).Result(),
).
AddItems("persistentvolumes",
builder.ForPersistentVolume("pv-1").ObjectMeta(builder.WithLabels("other-label", "x")).Result(),
builder.ForPersistentVolume("pv-2").ObjectMeta(builder.WithLabels("pr-label", "1")).Result(),
).
Done(),
apiResources: []*test.APIResource{
test.Pods(),
test.Deployments(),
test.PVs(),
},
want: map[*test.APIResource][]string{
test.Pods(): {"ns-2/pod-2"},
test.Deployments(): {"ns-1/deploy-1"},
test.PVs(): {"/pv-1"},
},
},
{
name: "OrLabelSelectors only restores matching resources",
restore: defaultRestore().OrLabelSelector([]*metav1.LabelSelector{{MatchLabels: map[string]string{"a1": "b1"}}, {MatchLabels: map[string]string{"a2": "b2"}},
@@ -103,6 +103,30 @@ Includes cluster-scoped resources. Cannot work with `--include-cluster-scoped-re
velero backup create <backup-name> --selector "<key> notin (<value>)"
```
The same selector syntax works on restore. Set-based selectors are useful for phased restores: restore labeled resources first, then everything else.
* Restore only resources matching the label selector.
```bash
velero restore create --from-backup <backup-name> --selector <key>=<value>
```
* Restore everything in the backup except resources matching the selector.
```bash
velero restore create --from-backup <backup-name> --selector "<key> notin (<value>)"
```
`notin` also matches resources that don't have the `<key>` label at all: this restores resources whose `<key>` label has any other value, as well as resources without the `<key>` label.
* Restore only resources that do not have a particular label key.
```bash
velero restore create --from-backup <backup-name> --selector '!<key>'
```
Note: resources pulled in as dependencies of selected items by restore item actions (for example, a restored pod's service account or persistent volume claims) are restored even if the label selector would exclude them.
For more information read the [Kubernetes label selector documentation](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors)
### --or-selector