mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 15:05:27 +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"
|
||||
"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)
|
||||
|
@ -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 {
|
||||
|
@ -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]
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user