Merge pull request #74941 from cwdsuzhou/fix_pvc_label

Do not replace labels of pvc with those of StastefulSet
This commit is contained in:
Kubernetes Prow Robot 2019-04-03 02:05:44 -07:00 committed by GitHub
commit be374388f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -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

View File

@ -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{