diff --git a/pkg/controller/history/controller_history.go b/pkg/controller/history/controller_history.go index 1de109ed283..5e4e8f95c61 100644 --- a/pkg/controller/history/controller_history.go +++ b/pkg/controller/history/controller_history.go @@ -56,17 +56,20 @@ func ControllerRevisionName(prefix string, hash uint32) string { } // NewControllerRevision returns a ControllerRevision with a ControllerRef pointing to parent and indicating that -// parent is of parentKind. The ControllerRevision has labels matching pod, contains Data equal to data, and +// parent is of parentKind. The ControllerRevision has labels matching template labels, contains Data equal to data, and // has a Revision equal to revision. The collisionCount is used when creating the name of the ControllerRevision // so the name is likely unique. If the returned error is nil, the returned ControllerRevision is valid. If the // returned error is not nil, the returned ControllerRevision is invalid for use. func NewControllerRevision(parent metav1.Object, parentKind schema.GroupVersionKind, - podLabels map[string]string, + templateLabels map[string]string, data runtime.RawExtension, revision int64, collisionCount *int32) (*apps.ControllerRevision, error) { - labelMap := podLabels + labelMap := make(map[string]string) + for k, v := range templateLabels { + labelMap[k] = v + } blockOwnerDeletion := true isController := true cr := &apps.ControllerRevision{ diff --git a/pkg/controller/history/controller_history_test.go b/pkg/controller/history/controller_history_test.go index 7fcc3aa4578..455d6b67340 100644 --- a/pkg/controller/history/controller_history_test.go +++ b/pkg/controller/history/controller_history_test.go @@ -77,8 +77,10 @@ func TestRealHistory_ListControllerRevisions(t *testing.T) { } ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"}) - sel1 := labels.Set(ss1.Spec.Template.Labels).AsSelector() - + sel1, err := metav1.LabelSelectorAsSelector(ss1.Spec.Selector) + if err != nil { + t.Fatal(err) + } ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil) if err != nil { t.Fatal(err) @@ -172,7 +174,10 @@ func TestFakeHistory_ListControllerRevisions(t *testing.T) { } ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"}) - sel1 := labels.Set(ss1.Spec.Template.Labels).AsSelector() + sel1, err := metav1.LabelSelectorAsSelector(ss1.Spec.Selector) + if err != nil { + t.Fatal(err) + } ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil) if err != nil { t.Fatal(err) @@ -1539,6 +1544,7 @@ func TestSortControllerRevisions(t *testing.T) { } ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss1.Status.CollisionCount = new(int32) + ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, ss1.Status.CollisionCount) if err != nil { t.Fatal(err) @@ -1583,13 +1589,15 @@ func TestSortControllerRevisions(t *testing.T) { } func newStatefulSet(replicas int, name string, uid types.UID, labels map[string]string) *apps.StatefulSet { - var testLabelKey string - var testLabelValue string - // Get the first label's key, value to be used for set based selector. + // Converting all the map-only selectors to set-based selectors. + var testMatchExpressions []metav1.LabelSelectorRequirement for key, value := range labels { - testLabelKey = key - testLabelValue = value - break + sel := metav1.LabelSelectorRequirement{ + Key: key, + Operator: metav1.LabelSelectorOpIn, + Values: []string{value}, + } + testMatchExpressions = append(testMatchExpressions, sel) } return &apps.StatefulSet{ TypeMeta: metav1.TypeMeta{ @@ -1603,14 +1611,10 @@ func newStatefulSet(replicas int, name string, uid types.UID, labels map[string] }, Spec: apps.StatefulSetSpec{ Selector: &metav1.LabelSelector{ - MatchLabels: labels, - MatchExpressions: []metav1.LabelSelectorRequirement{ - { - Key: testLabelKey, - Operator: metav1.LabelSelectorOpIn, - Values: []string{testLabelValue}, - }, - }, + // Purposely leaving MatchLabels nil, so to ensure it will break if any link + // in the chain ignores the set-based MatchExpressions. + MatchLabels: nil, + MatchExpressions: testMatchExpressions, }, Replicas: func() *int32 { i := int32(replicas); return &i }(), Template: v1.PodTemplateSpec{ diff --git a/pkg/controller/statefulset/stateful_set_utils.go b/pkg/controller/statefulset/stateful_set_utils.go index 1cc97647752..77f5809b82d 100644 --- a/pkg/controller/statefulset/stateful_set_utils.go +++ b/pkg/controller/statefulset/stateful_set_utils.go @@ -312,10 +312,9 @@ func newRevision(set *apps.StatefulSet, revision int64, collisionCount *int32) ( if err != nil { return nil, err } - podLabels := set.Spec.Template.Labels cr, err := history.NewControllerRevision(set, controllerKind, - podLabels, + set.Spec.Template.Labels, runtime.RawExtension{Raw: patch}, revision, collisionCount)