Merge pull request #74941 from cwdsuzhou/fix_pvc_label
Do not replace labels of pvc with those of StastefulSet
This commit is contained in:
commit
be374388f6
@ -149,7 +149,13 @@ func getPersistentVolumeClaims(set *apps.StatefulSet, pod *v1.Pod) map[string]v1
|
||||
claim := templates[i]
|
||||
claim.Name = getPersistentVolumeClaimName(set, &claim, ordinal)
|
||||
claim.Namespace = set.Namespace
|
||||
claim.Labels = set.Spec.Selector.MatchLabels
|
||||
if claim.Labels != nil {
|
||||
for key, value := range set.Spec.Selector.MatchLabels {
|
||||
claim.Labels[key] = value
|
||||
}
|
||||
} else {
|
||||
claim.Labels = set.Spec.Selector.MatchLabels
|
||||
}
|
||||
claims[templates[i].Name] = claim
|
||||
}
|
||||
return claims
|
||||
|
@ -19,6 +19,7 @@ package statefulset
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"testing"
|
||||
@ -288,6 +289,68 @@ func TestCreateApplyRevision(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPersistentVolumeClaims(t *testing.T) {
|
||||
|
||||
// nil inherits statefulset labels
|
||||
pod := newPod()
|
||||
statefulSet := newStatefulSet(1)
|
||||
statefulSet.Spec.Selector.MatchLabels = nil
|
||||
claims := getPersistentVolumeClaims(statefulSet, pod)
|
||||
pvc := newPVC("datadir-foo-0")
|
||||
pvc.SetNamespace(v1.NamespaceDefault)
|
||||
resultClaims := map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||
|
||||
if !reflect.DeepEqual(claims, resultClaims) {
|
||||
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||
}
|
||||
|
||||
// nil inherits statefulset labels
|
||||
statefulSet.Spec.Selector.MatchLabels = map[string]string{"test": "test"}
|
||||
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||
pvc.SetLabels(map[string]string{"test": "test"})
|
||||
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||
if !reflect.DeepEqual(claims, resultClaims) {
|
||||
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||
}
|
||||
|
||||
// non-nil with non-overlapping labels merge pvc and statefulset labels
|
||||
statefulSet.Spec.Selector.MatchLabels = map[string]string{"name": "foo"}
|
||||
statefulSet.Spec.VolumeClaimTemplates[0].ObjectMeta.Labels = map[string]string{"test": "test"}
|
||||
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||
pvc.SetLabels(map[string]string{"test": "test", "name": "foo"})
|
||||
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||
if !reflect.DeepEqual(claims, resultClaims) {
|
||||
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||
}
|
||||
|
||||
// non-nil with overlapping labels merge pvc and statefulset labels and prefer statefulset labels
|
||||
statefulSet.Spec.Selector.MatchLabels = map[string]string{"test": "foo"}
|
||||
statefulSet.Spec.VolumeClaimTemplates[0].ObjectMeta.Labels = map[string]string{"test": "test"}
|
||||
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||
pvc.SetLabels(map[string]string{"test": "foo"})
|
||||
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||
if !reflect.DeepEqual(claims, resultClaims) {
|
||||
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||
}
|
||||
}
|
||||
|
||||
func newPod() *v1.Pod {
|
||||
return &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo-0",
|
||||
Namespace: v1.NamespaceDefault,
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{
|
||||
{
|
||||
Name: "nginx",
|
||||
Image: "nginx",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newPVC(name string) v1.PersistentVolumeClaim {
|
||||
return v1.PersistentVolumeClaim{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
|
Loading…
Reference in New Issue
Block a user