Merge pull request #39525 from kargakis/update-equality-helper

Automatic merge from submit-queue (batch tested with PRs 39807, 37505, 39844, 39525, 39109)

Update deployment equality helper

@mfojtik @janetkuo this is split out of https://github.com/kubernetes/kubernetes/pull/38714 to reduce the size of that PR, ptal
This commit is contained in:
Kubernetes Submit Queue 2017-01-13 13:40:45 -08:00 committed by GitHub
commit 5723979b60
6 changed files with 26 additions and 51 deletions

View File

@ -18,7 +18,6 @@ package deployment
import ( import (
"fmt" "fmt"
"reflect"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
@ -66,7 +65,7 @@ func (dc *DeploymentController) rollback(deployment *extensions.Deployment, toRe
} }
func (dc *DeploymentController) rollbackToTemplate(deployment *extensions.Deployment, rs *extensions.ReplicaSet) (d *extensions.Deployment, performedRollback bool, err error) { func (dc *DeploymentController) rollbackToTemplate(deployment *extensions.Deployment, rs *extensions.ReplicaSet) (d *extensions.Deployment, performedRollback bool, err error) {
if !reflect.DeepEqual(deploymentutil.GetNewReplicaSetTemplate(deployment), rs.Spec.Template) { if !deploymentutil.EqualIgnoreHash(deployment.Spec.Template, rs.Spec.Template) {
glog.Infof("Rolling back deployment %s to template spec %+v", deployment.Name, rs.Spec.Template.Spec) glog.Infof("Rolling back deployment %s to template spec %+v", deployment.Name, rs.Spec.Template.Spec)
deploymentutil.SetFromReplicaSetTemplate(deployment, rs.Spec.Template) deploymentutil.SetFromReplicaSetTemplate(deployment, rs.Spec.Template)
// set RS (the old RS we'll rolling back to) annotations back to the deployment; // set RS (the old RS we'll rolling back to) annotations back to the deployment;

View File

@ -316,8 +316,9 @@ func (dc *DeploymentController) getNewReplicaSet(deployment *extensions.Deployme
// new ReplicaSet does not exist, create one. // new ReplicaSet does not exist, create one.
namespace := deployment.Namespace namespace := deployment.Namespace
podTemplateSpecHash := deploymentutil.GetPodTemplateSpecHash(deployment.Spec.Template) podTemplateSpecHash := fmt.Sprintf("%d", deploymentutil.GetPodTemplateSpecHash(deployment.Spec.Template))
newRSTemplate := deploymentutil.GetNewReplicaSetTemplate(deployment) newRSTemplate := deploymentutil.GetNewReplicaSetTemplate(deployment)
newRSTemplate.Labels = labelsutil.CloneAndAddLabel(deployment.Spec.Template.Labels, extensions.DefaultDeploymentUniqueLabelKey, podTemplateSpecHash)
// Add podTemplateHash label to selector. // Add podTemplateHash label to selector.
newRSSelector := labelsutil.CloneSelectorAndAddLabel(deployment.Spec.Selector, extensions.DefaultDeploymentUniqueLabelKey, podTemplateSpecHash) newRSSelector := labelsutil.CloneSelectorAndAddLabel(deployment.Spec.Selector, extensions.DefaultDeploymentUniqueLabelKey, podTemplateSpecHash)
@ -325,7 +326,7 @@ func (dc *DeploymentController) getNewReplicaSet(deployment *extensions.Deployme
newRS := extensions.ReplicaSet{ newRS := extensions.ReplicaSet{
ObjectMeta: v1.ObjectMeta{ ObjectMeta: v1.ObjectMeta{
// Make the name deterministic, to ensure idempotence // Make the name deterministic, to ensure idempotence
Name: deployment.Name + "-" + fmt.Sprintf("%d", podTemplateSpecHash), Name: deployment.Name + "-" + podTemplateSpecHash,
Namespace: namespace, Namespace: namespace,
}, },
Spec: extensions.ReplicaSetSpec{ Spec: extensions.ReplicaSetSpec{

View File

@ -587,43 +587,32 @@ func ListPods(deployment *extensions.Deployment, getPodList podListFunc) (*v1.Po
return getPodList(namespace, options) return getPodList(namespace, options)
} }
// 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 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 // Note that we assume input podTemplateSpecs contain non-empty labels
func equalIgnoreHash(template1, template2 v1.PodTemplateSpec) (bool, error) { func EqualIgnoreHash(template1, template2 v1.PodTemplateSpec) bool {
// First, compare template.Labels (ignoring hash) // First, compare template.Labels (ignoring hash)
labels1, labels2 := template1.Labels, template2.Labels labels1, labels2 := template1.Labels, template2.Labels
// The podTemplateSpec must have a non-empty label so that label selectors can find them.
// This is checked by validation (of resources contain a podTemplateSpec).
if len(labels1) == 0 || len(labels2) == 0 {
return false, fmt.Errorf("Unexpected empty labels found in given template")
}
if len(labels1) > len(labels2) { if len(labels1) > len(labels2) {
labels1, labels2 = labels2, labels1 labels1, labels2 = labels2, labels1
} }
// We make sure len(labels2) >= len(labels1) // We make sure len(labels2) >= len(labels1)
for k, v := range labels2 { for k, v := range labels2 {
if labels1[k] != v && k != extensions.DefaultDeploymentUniqueLabelKey { if labels1[k] != v && k != extensions.DefaultDeploymentUniqueLabelKey {
return false, nil return false
} }
} }
// Then, compare the templates without comparing their labels // Then, compare the templates without comparing their labels
template1.Labels, template2.Labels = nil, nil template1.Labels, template2.Labels = nil, nil
result := api.Semantic.DeepEqual(template1, template2) return api.Semantic.DeepEqual(template1, template2)
return result, nil
} }
// FindNewReplicaSet returns the new RS this given deployment targets (the one with the same pod template). // FindNewReplicaSet returns the new RS this given deployment targets (the one with the same pod template).
func FindNewReplicaSet(deployment *extensions.Deployment, rsList []*extensions.ReplicaSet) (*extensions.ReplicaSet, error) { func FindNewReplicaSet(deployment *extensions.Deployment, rsList []*extensions.ReplicaSet) (*extensions.ReplicaSet, error) {
newRSTemplate := GetNewReplicaSetTemplate(deployment) newRSTemplate := GetNewReplicaSetTemplate(deployment)
for i := range rsList { for i := range rsList {
equal, err := equalIgnoreHash(rsList[i].Spec.Template, newRSTemplate) if EqualIgnoreHash(rsList[i].Spec.Template, newRSTemplate) {
if err != nil {
return nil, err
}
if equal {
// This is the new ReplicaSet. // This is the new ReplicaSet.
return rsList[i], nil return rsList[i], nil
} }
@ -648,11 +637,7 @@ func FindOldReplicaSets(deployment *extensions.Deployment, rsList []*extensions.
return nil, nil, fmt.Errorf("invalid label selector: %v", err) return nil, nil, fmt.Errorf("invalid label selector: %v", err)
} }
// Filter out replica set that has the same pod template spec as the deployment - that is the new replica set. // Filter out replica set that has the same pod template spec as the deployment - that is the new replica set.
equal, err := equalIgnoreHash(rs.Spec.Template, newRSTemplate) if EqualIgnoreHash(rs.Spec.Template, newRSTemplate) {
if err != nil {
return nil, nil, err
}
if equal {
continue continue
} }
allOldRSs[rs.ObjectMeta.Name] = rs allOldRSs[rs.ObjectMeta.Name] = rs
@ -722,17 +707,13 @@ func LabelPodsWithHash(podList *v1.PodList, c clientset.Interface, podLister *ca
} }
// GetNewReplicaSetTemplate returns the desired PodTemplateSpec for the new ReplicaSet corresponding to the given ReplicaSet. // GetNewReplicaSetTemplate returns the desired PodTemplateSpec for the new ReplicaSet corresponding to the given ReplicaSet.
// Callers of this helper need to set the DefaultDeploymentUniqueLabelKey k/v pair.
func GetNewReplicaSetTemplate(deployment *extensions.Deployment) v1.PodTemplateSpec { func GetNewReplicaSetTemplate(deployment *extensions.Deployment) v1.PodTemplateSpec {
// newRS will have the same template as in deployment spec, plus a unique label in some cases. // newRS will have the same template as in deployment spec.
newRSTemplate := v1.PodTemplateSpec{ return v1.PodTemplateSpec{
ObjectMeta: deployment.Spec.Template.ObjectMeta, ObjectMeta: deployment.Spec.Template.ObjectMeta,
Spec: deployment.Spec.Template.Spec, Spec: deployment.Spec.Template.Spec,
} }
newRSTemplate.ObjectMeta.Labels = labelsutil.CloneAndAddLabel(
deployment.Spec.Template.ObjectMeta.Labels,
extensions.DefaultDeploymentUniqueLabelKey,
GetPodTemplateSpecHash(newRSTemplate))
return newRSTemplate
} }
// TODO: remove the duplicate // TODO: remove the duplicate
@ -746,7 +727,7 @@ func GetNewReplicaSetTemplateInternal(deployment *internalextensions.Deployment)
newRSTemplate.ObjectMeta.Labels = labelsutil.CloneAndAddLabel( newRSTemplate.ObjectMeta.Labels = labelsutil.CloneAndAddLabel(
deployment.Spec.Template.ObjectMeta.Labels, deployment.Spec.Template.ObjectMeta.Labels,
internalextensions.DefaultDeploymentUniqueLabelKey, internalextensions.DefaultDeploymentUniqueLabelKey,
GetInternalPodTemplateSpecHash(newRSTemplate)) fmt.Sprintf("%d", GetInternalPodTemplateSpecHash(newRSTemplate)))
return newRSTemplate return newRSTemplate
} }

View File

@ -446,11 +446,7 @@ func TestEqualIgnoreHash(t *testing.T) {
reverseString = " (reverse order)" reverseString = " (reverse order)"
} }
// Run // Run
equal, err := equalIgnoreHash(*t1, *t2) equal := EqualIgnoreHash(*t1, *t2)
// Check
if err != nil {
t.Errorf("In test case %q%s, expected no error, returned %v", test.test, reverseString, err)
}
if equal != test.expected { if equal != test.expected {
t.Errorf("In test case %q%s, expected %v", test.test, reverseString, test.expected) t.Errorf("In test case %q%s, expected %v", test.test, reverseString, test.expected)
} }

View File

@ -17,14 +17,12 @@ limitations under the License.
package labels package labels
import ( import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
// Clones the given map and returns a new map with the given key and value added. // Clones the given map and returns a new map with the given key and value added.
// Returns the given map, if labelKey is empty. // Returns the given map, if labelKey is empty.
func CloneAndAddLabel(labels map[string]string, labelKey string, labelValue uint32) map[string]string { func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map[string]string {
if labelKey == "" { if labelKey == "" {
// Don't need to add a label. // Don't need to add a label.
return labels return labels
@ -34,7 +32,7 @@ func CloneAndAddLabel(labels map[string]string, labelKey string, labelValue uint
for key, value := range labels { for key, value := range labels {
newLabels[key] = value newLabels[key] = value
} }
newLabels[labelKey] = fmt.Sprintf("%d", labelValue) newLabels[labelKey] = labelValue
return newLabels return newLabels
} }
@ -55,7 +53,7 @@ func CloneAndRemoveLabel(labels map[string]string, labelKey string) map[string]s
} }
// AddLabel returns a map with the given key and value added to the given map. // AddLabel returns a map with the given key and value added to the given map.
func AddLabel(labels map[string]string, labelKey string, labelValue string) map[string]string { func AddLabel(labels map[string]string, labelKey, labelValue string) map[string]string {
if labelKey == "" { if labelKey == "" {
// Don't need to add a label. // Don't need to add a label.
return labels return labels
@ -69,7 +67,7 @@ func AddLabel(labels map[string]string, labelKey string, labelValue string) map[
// Clones the given selector and returns a new selector with the given key and value added. // Clones the given selector and returns a new selector with the given key and value added.
// Returns the given selector, if labelKey is empty. // Returns the given selector, if labelKey is empty.
func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey string, labelValue uint32) *metav1.LabelSelector { func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector {
if labelKey == "" { if labelKey == "" {
// Don't need to add a label. // Don't need to add a label.
return selector return selector
@ -85,7 +83,7 @@ func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey string, l
newSelector.MatchLabels[key] = val newSelector.MatchLabels[key] = val
} }
} }
newSelector.MatchLabels[labelKey] = fmt.Sprintf("%d", labelValue) newSelector.MatchLabels[labelKey] = labelValue
if selector.MatchExpressions != nil { if selector.MatchExpressions != nil {
newMExps := make([]metav1.LabelSelectorRequirement, len(selector.MatchExpressions)) newMExps := make([]metav1.LabelSelectorRequirement, len(selector.MatchExpressions))
@ -108,7 +106,7 @@ func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey string, l
} }
// AddLabelToSelector returns a selector with the given key and value added to the given selector's MatchLabels. // AddLabelToSelector returns a selector with the given key and value added to the given selector's MatchLabels.
func AddLabelToSelector(selector *metav1.LabelSelector, labelKey string, labelValue string) *metav1.LabelSelector { func AddLabelToSelector(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector {
if labelKey == "" { if labelKey == "" {
// Don't need to add a label. // Don't need to add a label.
return selector return selector

View File

@ -33,7 +33,7 @@ func TestCloneAndAddLabel(t *testing.T) {
cases := []struct { cases := []struct {
labels map[string]string labels map[string]string
labelKey string labelKey string
labelValue uint32 labelValue string
want map[string]string want map[string]string
}{ }{
{ {
@ -43,7 +43,7 @@ func TestCloneAndAddLabel(t *testing.T) {
{ {
labels: labels, labels: labels,
labelKey: "foo4", labelKey: "foo4",
labelValue: uint32(42), labelValue: "42",
want: map[string]string{ want: map[string]string{
"foo1": "bar1", "foo1": "bar1",
"foo2": "bar2", "foo2": "bar2",
@ -122,7 +122,7 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
cases := []struct { cases := []struct {
labels map[string]string labels map[string]string
labelKey string labelKey string
labelValue uint32 labelValue string
want map[string]string want map[string]string
}{ }{
{ {
@ -132,7 +132,7 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
{ {
labels: labels, labels: labels,
labelKey: "foo4", labelKey: "foo4",
labelValue: 89, labelValue: "89",
want: map[string]string{ want: map[string]string{
"foo1": "bar1", "foo1": "bar1",
"foo2": "bar2", "foo2": "bar2",
@ -143,7 +143,7 @@ func TestCloneSelectorAndAddLabel(t *testing.T) {
{ {
labels: nil, labels: nil,
labelKey: "foo4", labelKey: "foo4",
labelValue: 12, labelValue: "12",
want: map[string]string{ want: map[string]string{
"foo4": "12", "foo4": "12",
}, },