diff --git a/pkg/k8s/persistentvolumeclaim/client_test.go b/pkg/k8s/persistentvolumeclaim/client_test.go index b94be6f0..144cb6e1 100644 --- a/pkg/k8s/persistentvolumeclaim/client_test.go +++ b/pkg/k8s/persistentvolumeclaim/client_test.go @@ -34,7 +34,6 @@ func TestSet(t *testing.T) { Labels: map[string]string{"label_1": "label_value_1"}, }, expectedSpec: v1.PersistentVolumeClaimSpec{ - Selector: &metav1.LabelSelector{}, VolumeMode: func() *v1.PersistentVolumeMode { m := v1.PersistentVolumeFilesystem return &m @@ -58,7 +57,6 @@ func TestSet(t *testing.T) { Annotations: map[string]string{"annotation_1": "annotation_value_X", "annotation_2": "annotation_value_2"}, }, expectedSpec: v1.PersistentVolumeClaimSpec{ - Selector: &metav1.LabelSelector{}, VolumeMode: func() *v1.PersistentVolumeMode { m := v1.PersistentVolumeFilesystem return &m @@ -106,7 +104,7 @@ func TestSet(t *testing.T) { } if !reflect.DeepEqual(response, expected) { - t.Errorf("response expected: %q, got: %q", response, expected) + t.Errorf("response expected: %q, got: %q", expected, response) } } else { diff --git a/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim.go b/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim.go index 5ac32349..d11db1b6 100644 --- a/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim.go +++ b/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim.go @@ -50,7 +50,7 @@ type Spec struct { AccessModes AccessModes DataSource DataSource RequestStorage string - Selector Selector + Selector *Selector StorageClass string VolumeMode string VolumeName string diff --git a/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim_test.go b/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim_test.go index 820fd7ef..314b8523 100644 --- a/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim_test.go +++ b/pkg/k8s/persistentvolumeclaim/persistentvolumeclaim_test.go @@ -27,7 +27,7 @@ func TestToK8s(t *testing.T) { Spec: pvc.Spec{ AccessModes: []pvc.AccessMode{"1", "2"}, RequestStorage: "1Gi", - Selector: pvc.Selector{ + Selector: &pvc.Selector{ MatchLabels: map[string]string{"label_1": "label_value_1"}, MatchExpressions: []pvc.LabelSelectorRequirement{ { @@ -102,7 +102,6 @@ func TestToK8s(t *testing.T) { expectedPvcs: []v1.PersistentVolumeClaim{ { Spec: v1.PersistentVolumeClaimSpec{ - Selector: &metav1.LabelSelector{}, VolumeMode: func() *v1.PersistentVolumeMode { m := v1.PersistentVolumeBlock return &m @@ -113,6 +112,44 @@ func TestToK8s(t *testing.T) { }, }, }, + { + name: "selector_match_expressions_only", + pvcs: pvc.PersistentVolumeClaims{ + { + Spec: pvc.Spec{ + Selector: &pvc.Selector{ + MatchExpressions: []pvc.LabelSelectorRequirement{ + { + Key: "label_1", + Operator: "In", + Values: []string{"label_value_1"}, + }, + }, + }, + }, + }, + }, + expectedPvcs: []v1.PersistentVolumeClaim{ + { + Spec: v1.PersistentVolumeClaimSpec{ + Selector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "label_1", + Operator: "In", + Values: []string{"label_value_1"}, + }, + }, + }, + VolumeMode: func() *v1.PersistentVolumeMode { + m := v1.PersistentVolumeFilesystem + return &m + }(), + StorageClassName: new(""), + }, + }, + }, + }, } for _, test := range testTable { diff --git a/pkg/k8s/persistentvolumeclaim/selector.go b/pkg/k8s/persistentvolumeclaim/selector.go index 468bc76d..8e2bbbab 100644 --- a/pkg/k8s/persistentvolumeclaim/selector.go +++ b/pkg/k8s/persistentvolumeclaim/selector.go @@ -10,8 +10,13 @@ type Selector struct { MatchExpressions LabelSelectorRequirements } -// toK8S converts Selector to Kubernetes client object +// toK8S converts Selector to Kubernetes client object. +// A nil selector is left unset. Dynamic provisioners such as local-path +// reject a claim when spec.selector is present, even if it matches nothing. func (s *Selector) toK8S() *metav1.LabelSelector { + if s == nil { + return nil + } return &metav1.LabelSelector{ MatchLabels: s.MatchLabels, MatchExpressions: s.MatchExpressions.toK8S(),