From e3cb44aaffca4eefceec6e3754a44d419c770736 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Mon, 1 Feb 2016 17:34:48 -0800 Subject: [PATCH] Copy deployment's annotations to its RC --- .../deployment/deployment_controller.go | 28 +++++++++++++------ pkg/kubectl/cmd/annotate.go | 8 +++--- pkg/kubectl/history.go | 21 ++++++-------- test/e2e/deployment.go | 7 +++-- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/pkg/controller/deployment/deployment_controller.go b/pkg/controller/deployment/deployment_controller.go index 0024378e669..5d2ec0d3bdc 100644 --- a/pkg/controller/deployment/deployment_controller.go +++ b/pkg/controller/deployment/deployment_controller.go @@ -34,7 +34,6 @@ import ( unversioned_legacy "k8s.io/kubernetes/pkg/client/typed/generated/legacy/unversioned" "k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller/framework" - "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/util" deploymentutil "k8s.io/kubernetes/pkg/util/deployment" @@ -679,9 +678,21 @@ func (dc *DeploymentController) getNewRC(deployment extensions.Deployment, maxOl if existingNewRC.Annotations == nil { existingNewRC.Annotations = make(map[string]string) } + // Copy deployment's annotations to existing new RC + annotationChanged := false + for k, v := range deployment.Annotations { + if existingNewRC.Annotations[k] != v { + annotationChanged = true + existingNewRC.Annotations[k] = v + } + } + // Update existing new RC's revision annotation if existingNewRC.Annotations[deploymentutil.RevisionAnnotation] != newRevision { existingNewRC.Annotations[deploymentutil.RevisionAnnotation] = newRevision + annotationChanged = true glog.V(4).Infof("update existingNewRC %s revision to %s - %+v\n", existingNewRC.Name, newRevision) + } + if annotationChanged { return dc.client.Legacy().ReplicationControllers(deployment.ObjectMeta.Namespace).Update(existingNewRC) } return existingNewRC, nil @@ -708,13 +719,20 @@ func (dc *DeploymentController) getNewRC(deployment extensions.Deployment, maxOl return nil, fmt.Errorf("couldn't get key for deployment controller %#v: %v", deployment, err) } dc.rcExpectations.ExpectCreations(dKey, 1) + // Copy deployment's annotations to new RC + annotations := deployment.Annotations + if annotations == nil { + annotations = make(map[string]string) + } + // Set new RC's revision annotation + annotations[deploymentutil.RevisionAnnotation] = newRevision // Create new RC newRC := api.ReplicationController{ ObjectMeta: api.ObjectMeta{ GenerateName: deployment.Name + "-", Namespace: namespace, - Annotations: map[string]string{deploymentutil.RevisionAnnotation: newRevision}, + Annotations: annotations, }, Spec: api.ReplicationControllerSpec{ Replicas: 0, @@ -722,12 +740,6 @@ func (dc *DeploymentController) getNewRC(deployment extensions.Deployment, maxOl Template: &newRCTemplate, }, } - if _, ok := deployment.Annotations[kubectl.ChangeCauseAnnotation]; ok { - if newRC.Annotations == nil { - newRC.Annotations = make(map[string]string) - } - newRC.Annotations[kubectl.ChangeCauseAnnotation] = deployment.Annotations[kubectl.ChangeCauseAnnotation] - } createdRC, err := dc.client.Legacy().ReplicationControllers(namespace).Create(&newRC) if err != nil { dc.rcExpectations.DeleteExpectations(dKey) diff --git a/pkg/kubectl/cmd/annotate.go b/pkg/kubectl/cmd/annotate.go index 69921f90895..daebf57d1f6 100644 --- a/pkg/kubectl/cmd/annotate.go +++ b/pkg/kubectl/cmd/annotate.go @@ -46,8 +46,8 @@ type AnnotateOptions struct { all bool resourceVersion string - changeCause string - recordFlag bool + changeCause string + recordChangeCause bool f *cmdutil.Factory out io.Writer @@ -155,7 +155,7 @@ func (o *AnnotateOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra return err } - o.recordFlag = cmdutil.GetRecordFlag(cmd) + o.recordChangeCause = cmdutil.GetRecordFlag(cmd) o.changeCause = f.Command() mapper, typer := f.Object() @@ -207,7 +207,7 @@ func (o AnnotateOptions) RunAnnotate() error { return err } // If we should record change-cause, add it to new annotations - if cmdutil.ContainsChangeCause(info) || o.recordFlag { + if cmdutil.ContainsChangeCause(info) || o.recordChangeCause { o.newAnnotations[kubectl.ChangeCauseAnnotation] = o.changeCause } if err := o.updateAnnotations(obj); err != nil { diff --git a/pkg/kubectl/history.go b/pkg/kubectl/history.go index f864e535c2f..fc0b9e809f2 100644 --- a/pkg/kubectl/history.go +++ b/pkg/kubectl/history.go @@ -79,19 +79,14 @@ func (h *DeploymentHistoryViewer) History(namespace, name string) (HistoryInfo, for _, rc := range allRCs { v, err := deploymentutil.Revision(rc) if err != nil { - return historyInfo, fmt.Errorf("failed to retrieve revision out of RC %s from deployment %s: %v", rc.Name, name, err) + continue } historyInfo.RevisionToTemplate[v] = rc.Spec.Template - changeCause, err := getChangeCause(rc) - if err != nil { - return historyInfo, fmt.Errorf("failed to retrieve change-cause out of RC %s from deployment %s: %v", rc.Name, name, err) - } - if len(changeCause) > 0 { - if historyInfo.RevisionToTemplate[v].Annotations == nil { - historyInfo.RevisionToTemplate[v].Annotations = make(map[string]string) - } - historyInfo.RevisionToTemplate[v].Annotations[ChangeCauseAnnotation] = changeCause + changeCause := getChangeCause(rc) + if historyInfo.RevisionToTemplate[v].Annotations == nil { + historyInfo.RevisionToTemplate[v].Annotations = make(map[string]string) } + historyInfo.RevisionToTemplate[v].Annotations[ChangeCauseAnnotation] = changeCause } return historyInfo, nil } @@ -130,10 +125,10 @@ func PrintRolloutHistory(historyInfo HistoryInfo, resource, name string) (string } // getChangeCause returns the change-cause annotation of the input object -func getChangeCause(obj runtime.Object) (string, error) { +func getChangeCause(obj runtime.Object) string { meta, err := api.ObjectMetaFor(obj) if err != nil { - return "", err + return "" } - return meta.Annotations[ChangeCauseAnnotation], nil + return meta.Annotations[ChangeCauseAnnotation] } diff --git a/test/e2e/deployment.go b/test/e2e/deployment.go index 1dc155875ff..90f50ce1099 100644 --- a/test/e2e/deployment.go +++ b/test/e2e/deployment.go @@ -160,7 +160,9 @@ func testNewDeployment(f *Framework) { podLabels := map[string]string{"name": "nginx"} replicas := 1 Logf("Creating simple deployment %s", deploymentName) - _, err := c.Extensions().Deployments(ns).Create(newDeployment(deploymentName, replicas, podLabels, "nginx", "nginx", extensions.RollingUpdateDeploymentStrategyType, nil)) + d := newDeployment(deploymentName, replicas, podLabels, "nginx", "nginx", extensions.RollingUpdateDeploymentStrategyType, nil) + d.Annotations = map[string]string{"test": "annotation-should-copy-to-RC"} + _, err := c.Extensions().Deployments(ns).Create(d) Expect(err).NotTo(HaveOccurred()) defer func() { deployment, err := c.Extensions().Deployments(ns).Get(deploymentName) @@ -189,7 +191,8 @@ func testNewDeployment(f *Framework) { Expect(deployment.Status.UpdatedReplicas).Should(Equal(replicas)) // Check if it's updated to revision 1 correctly - checkDeploymentRevision(c, ns, deploymentName, "1", "nginx", "nginx") + _, newRC := checkDeploymentRevision(c, ns, deploymentName, "1", "nginx", "nginx") + Expect(newRC.Annotations["test"]).Should(Equal("annotation-should-copy-to-RC")) } func testRollingUpdateDeployment(f *Framework) {