mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Copy deployment's annotations to its RC
This commit is contained in:
parent
5088d0e147
commit
e3cb44aaff
@ -34,7 +34,6 @@ import (
|
|||||||
unversioned_legacy "k8s.io/kubernetes/pkg/client/typed/generated/legacy/unversioned"
|
unversioned_legacy "k8s.io/kubernetes/pkg/client/typed/generated/legacy/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/controller"
|
"k8s.io/kubernetes/pkg/controller"
|
||||||
"k8s.io/kubernetes/pkg/controller/framework"
|
"k8s.io/kubernetes/pkg/controller/framework"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util"
|
||||||
deploymentutil "k8s.io/kubernetes/pkg/util/deployment"
|
deploymentutil "k8s.io/kubernetes/pkg/util/deployment"
|
||||||
@ -679,9 +678,21 @@ func (dc *DeploymentController) getNewRC(deployment extensions.Deployment, maxOl
|
|||||||
if existingNewRC.Annotations == nil {
|
if existingNewRC.Annotations == nil {
|
||||||
existingNewRC.Annotations = make(map[string]string)
|
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 {
|
if existingNewRC.Annotations[deploymentutil.RevisionAnnotation] != newRevision {
|
||||||
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)
|
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 dc.client.Legacy().ReplicationControllers(deployment.ObjectMeta.Namespace).Update(existingNewRC)
|
||||||
}
|
}
|
||||||
return existingNewRC, nil
|
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)
|
return nil, fmt.Errorf("couldn't get key for deployment controller %#v: %v", deployment, err)
|
||||||
}
|
}
|
||||||
dc.rcExpectations.ExpectCreations(dKey, 1)
|
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
|
// Create new RC
|
||||||
newRC := api.ReplicationController{
|
newRC := api.ReplicationController{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
GenerateName: deployment.Name + "-",
|
GenerateName: deployment.Name + "-",
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Annotations: map[string]string{deploymentutil.RevisionAnnotation: newRevision},
|
Annotations: annotations,
|
||||||
},
|
},
|
||||||
Spec: api.ReplicationControllerSpec{
|
Spec: api.ReplicationControllerSpec{
|
||||||
Replicas: 0,
|
Replicas: 0,
|
||||||
@ -722,12 +740,6 @@ func (dc *DeploymentController) getNewRC(deployment extensions.Deployment, maxOl
|
|||||||
Template: &newRCTemplate,
|
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)
|
createdRC, err := dc.client.Legacy().ReplicationControllers(namespace).Create(&newRC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dc.rcExpectations.DeleteExpectations(dKey)
|
dc.rcExpectations.DeleteExpectations(dKey)
|
||||||
|
@ -46,8 +46,8 @@ type AnnotateOptions struct {
|
|||||||
all bool
|
all bool
|
||||||
resourceVersion string
|
resourceVersion string
|
||||||
|
|
||||||
changeCause string
|
changeCause string
|
||||||
recordFlag bool
|
recordChangeCause bool
|
||||||
|
|
||||||
f *cmdutil.Factory
|
f *cmdutil.Factory
|
||||||
out io.Writer
|
out io.Writer
|
||||||
@ -155,7 +155,7 @@ func (o *AnnotateOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
o.recordFlag = cmdutil.GetRecordFlag(cmd)
|
o.recordChangeCause = cmdutil.GetRecordFlag(cmd)
|
||||||
o.changeCause = f.Command()
|
o.changeCause = f.Command()
|
||||||
|
|
||||||
mapper, typer := f.Object()
|
mapper, typer := f.Object()
|
||||||
@ -207,7 +207,7 @@ func (o AnnotateOptions) RunAnnotate() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If we should record change-cause, add it to new annotations
|
// 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
|
o.newAnnotations[kubectl.ChangeCauseAnnotation] = o.changeCause
|
||||||
}
|
}
|
||||||
if err := o.updateAnnotations(obj); err != nil {
|
if err := o.updateAnnotations(obj); err != nil {
|
||||||
|
@ -79,19 +79,14 @@ func (h *DeploymentHistoryViewer) History(namespace, name string) (HistoryInfo,
|
|||||||
for _, rc := range allRCs {
|
for _, rc := range allRCs {
|
||||||
v, err := deploymentutil.Revision(rc)
|
v, err := deploymentutil.Revision(rc)
|
||||||
if err != nil {
|
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
|
historyInfo.RevisionToTemplate[v] = rc.Spec.Template
|
||||||
changeCause, err := getChangeCause(rc)
|
changeCause := getChangeCause(rc)
|
||||||
if err != nil {
|
if historyInfo.RevisionToTemplate[v].Annotations == nil {
|
||||||
return historyInfo, fmt.Errorf("failed to retrieve change-cause out of RC %s from deployment %s: %v", rc.Name, name, err)
|
historyInfo.RevisionToTemplate[v].Annotations = make(map[string]string)
|
||||||
}
|
|
||||||
if len(changeCause) > 0 {
|
|
||||||
if historyInfo.RevisionToTemplate[v].Annotations == nil {
|
|
||||||
historyInfo.RevisionToTemplate[v].Annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
historyInfo.RevisionToTemplate[v].Annotations[ChangeCauseAnnotation] = changeCause
|
|
||||||
}
|
}
|
||||||
|
historyInfo.RevisionToTemplate[v].Annotations[ChangeCauseAnnotation] = changeCause
|
||||||
}
|
}
|
||||||
return historyInfo, nil
|
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
|
// 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)
|
meta, err := api.ObjectMetaFor(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return ""
|
||||||
}
|
}
|
||||||
return meta.Annotations[ChangeCauseAnnotation], nil
|
return meta.Annotations[ChangeCauseAnnotation]
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,9 @@ func testNewDeployment(f *Framework) {
|
|||||||
podLabels := map[string]string{"name": "nginx"}
|
podLabels := map[string]string{"name": "nginx"}
|
||||||
replicas := 1
|
replicas := 1
|
||||||
Logf("Creating simple deployment %s", deploymentName)
|
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())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer func() {
|
defer func() {
|
||||||
deployment, err := c.Extensions().Deployments(ns).Get(deploymentName)
|
deployment, err := c.Extensions().Deployments(ns).Get(deploymentName)
|
||||||
@ -189,7 +191,8 @@ func testNewDeployment(f *Framework) {
|
|||||||
Expect(deployment.Status.UpdatedReplicas).Should(Equal(replicas))
|
Expect(deployment.Status.UpdatedReplicas).Should(Equal(replicas))
|
||||||
|
|
||||||
// Check if it's updated to revision 1 correctly
|
// 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) {
|
func testRollingUpdateDeployment(f *Framework) {
|
||||||
|
Loading…
Reference in New Issue
Block a user