mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Fix a bug in Deployment controller when comparing templates
This commit is contained in:
parent
76920d88b2
commit
19b4fee95c
@ -632,25 +632,16 @@ func ListPods(deployment *extensions.Deployment, rsList []*extensions.ReplicaSet
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EqualIgnoreHash returns true if two given podTemplateSpec are equal, ignoring the diff in value of Labels[pod-template-hash]
|
// EqualIgnoreHash returns true if two given podTemplateSpec are equal, ignoring the diff in value of Labels[pod-template-hash]
|
||||||
// We ignore pod-template-hash because the hash result would be different upon podTemplateSpec API changes
|
// We ignore pod-template-hash because:
|
||||||
|
// 1. The hash result would be different upon podTemplateSpec API changes
|
||||||
// (e.g. the addition of a new field will cause the hash code to change)
|
// (e.g. the addition of a new field will cause the hash code to change)
|
||||||
// Note that we assume input podTemplateSpecs contain non-empty labels
|
// 2. The deployment template won't have hash labels
|
||||||
func EqualIgnoreHash(template1, template2 *v1.PodTemplateSpec) bool {
|
func EqualIgnoreHash(template1, template2 *v1.PodTemplateSpec) bool {
|
||||||
t1Copy := template1.DeepCopy()
|
t1Copy := template1.DeepCopy()
|
||||||
t2Copy := template2.DeepCopy()
|
t2Copy := template2.DeepCopy()
|
||||||
// First, compare template.Labels (ignoring hash)
|
// Remove hash labels from template.Labels before comparing
|
||||||
labels1, labels2 := t1Copy.Labels, t2Copy.Labels
|
delete(t1Copy.Labels, extensions.DefaultDeploymentUniqueLabelKey)
|
||||||
if len(labels1) > len(labels2) {
|
delete(t2Copy.Labels, extensions.DefaultDeploymentUniqueLabelKey)
|
||||||
labels1, labels2 = labels2, labels1
|
|
||||||
}
|
|
||||||
// We make sure len(labels2) >= len(labels1)
|
|
||||||
for k, v := range labels2 {
|
|
||||||
if labels1[k] != v && k != extensions.DefaultDeploymentUniqueLabelKey {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Then, compare the templates without comparing their labels
|
|
||||||
t1Copy.Labels, t2Copy.Labels = nil, nil
|
|
||||||
return apiequality.Semantic.DeepEqual(t1Copy, t2Copy)
|
return apiequality.Semantic.DeepEqual(t1Copy, t2Copy)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,6 +356,18 @@ func TestEqualIgnoreHash(t *testing.T) {
|
|||||||
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2", "something": "else"}),
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"Same spec, the label is different, the former doesn't have pod-template-hash label, same number of labels",
|
||||||
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{"something": "else"}),
|
||||||
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-2"}),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Same spec, the label is different, the latter doesn't have pod-template-hash label, same number of labels",
|
||||||
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1"}),
|
||||||
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{"something": "else"}),
|
||||||
|
false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"Same spec, the label is different, and the pod-template-hash label value is the same",
|
"Same spec, the label is different, and the pod-template-hash label value is the same",
|
||||||
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1"}),
|
generatePodTemplateSpec("foo", "foo-node", map[string]string{}, map[string]string{extensions.DefaultDeploymentUniqueLabelKey: "value-1"}),
|
||||||
|
Loading…
Reference in New Issue
Block a user