Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #15267 upstream release 1.1 #15565

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/apis/extensions/v1beta1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func addDefaultingFuncs() {
}
},
func(obj *Deployment) {
// Default labels and selector to labels from pod template spec.
var labels map[string]string
if obj.Spec.Template != nil {
labels = obj.Spec.Template.Labels
}
if labels != nil {
if len(obj.Spec.Selector) == 0 {
obj.Spec.Selector = labels
}
if len(obj.Labels) == 0 {
obj.Labels = labels
}
}
// Set DeploymentSpec.Replicas to 1 if it is not set.
if obj.Spec.Replicas == nil {
obj.Spec.Replicas = new(int)
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/deployment/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,17 @@ func (d *DeploymentController) getNewRC(deployment extensions.Deployment) (*api.
podTemplateSpecHash := deploymentUtil.GetPodTemplateSpecHash(deployment.Spec.Template)
rcName := fmt.Sprintf("deploymentrc-%d", podTemplateSpecHash)
newRCTemplate := deploymentUtil.GetNewRCTemplate(deployment)
// Add podTemplateHash label to selector.
newRCSelector := deploymentUtil.CloneAndAddLabel(deployment.Spec.Selector, deployment.Spec.UniqueLabelKey, podTemplateSpecHash)

newRC := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: rcName,
Namespace: namespace,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: newRCTemplate.ObjectMeta.Labels,
Selector: newRCSelector,
Template: newRCTemplate,
},
}
Expand Down
29 changes: 20 additions & 9 deletions pkg/util/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,29 @@ func GetNewRCTemplate(deployment extensions.Deployment) *api.PodTemplateSpec {
ObjectMeta: deployment.Spec.Template.ObjectMeta,
Spec: deployment.Spec.Template.Spec,
}
podTemplateSpecHash := GetPodTemplateSpecHash(newRCTemplate)
if deployment.Spec.UniqueLabelKey != "" {
newLabels := map[string]string{}
for key, value := range deployment.Spec.Template.ObjectMeta.Labels {
newLabels[key] = value
}
newLabels[deployment.Spec.UniqueLabelKey] = fmt.Sprintf("%d", podTemplateSpecHash)
newRCTemplate.ObjectMeta.Labels = newLabels
}
newRCTemplate.ObjectMeta.Labels = CloneAndAddLabel(
deployment.Spec.Template.ObjectMeta.Labels,
deployment.Spec.UniqueLabelKey,
GetPodTemplateSpecHash(newRCTemplate))
return newRCTemplate
}

// Clones the given map and returns a new map with the given key and value added.
// Returns the given map, if labelKey is empty.
func CloneAndAddLabel(labels map[string]string, labelKey string, labelValue uint32) map[string]string {
if labelKey == "" {
// Dont need to add a label.
return labels
}
// Clone.
newLabels := map[string]string{}
for key, value := range labels {
newLabels[key] = value
}
newLabels[labelKey] = fmt.Sprintf("%d", labelValue)
return newLabels
}

func GetPodTemplateSpecHash(template *api.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New()
util.DeepHashObject(podTemplateSpecHasher, template)
Expand Down