// History returns a revision-to-RC map as the revision history of a deployment func (h *DeploymentHistoryViewer) History(namespace, name string) (HistoryInfo, error) { historyInfo := HistoryInfo{ RevisionToTemplate: make(map[int64]*api.PodTemplateSpec), } deployment, err := h.c.Extensions().Deployments(namespace).Get(name) if err != nil { return historyInfo, fmt.Errorf("failed to retrieve deployment %s: %v", name, err) } _, allOldRCs, err := deploymentutil.GetOldRCs(*deployment, h.c) if err != nil { return historyInfo, fmt.Errorf("failed to retrieve old RCs from deployment %s: %v", name, err) } newRC, err := deploymentutil.GetNewRC(*deployment, h.c) if err != nil { return historyInfo, fmt.Errorf("failed to retrieve new RC from deployment %s: %v", name, err) } allRCs := append(allOldRCs, newRC) for _, rc := range allRCs { v, err := deploymentutil.Revision(rc) if err != nil { continue } historyInfo.RevisionToTemplate[v] = rc.Spec.Template 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 }
func (d *DeploymentController) getOldRCs(deployment extensions.Deployment) ([]*api.ReplicationController, error) { return deploymentutil.GetOldRCs(deployment, d.client) }
// testRollbackDeploymentRCNoRevision tests that deployment supports rollback even when there's old RC without revision. // An old RC without revision is created, and then a deployment is created (v1). The deployment shouldn't add revision // annotation to the old RC. Then rollback the deployment to last revision, and it should fail and emit related event. // Then update the deployment to v2 and rollback it to v1 should succeed and emit related event, now the deployment // becomes v3. Then rollback the deployment to v10 (doesn't exist in history) should fail and emit related event. // Finally, rollback the deployment (v3) to v3 should be no-op and emit related event. func testRollbackDeploymentRCNoRevision(f *Framework) { ns := f.Namespace.Name unversionedClient := f.Client c := clientset.FromUnversionedClient(f.Client) podName := "nginx" deploymentPodLabels := map[string]string{"name": podName} rcPodLabels := map[string]string{ "name": podName, "pod": "nginx", } rcName := "nginx-controller" rcReplicas := 0 rc := newRC(rcName, rcReplicas, rcPodLabels, "nginx", "nginx") rc.Annotations = make(map[string]string) rc.Annotations["make"] = "difference" _, err := c.Legacy().ReplicationControllers(ns).Create(rc) Expect(err).NotTo(HaveOccurred()) defer func() { Logf("deleting replication controller %s", rcName) Expect(c.Legacy().ReplicationControllers(ns).Delete(rcName, nil)).NotTo(HaveOccurred()) }() // Create a deployment to create nginx pods, which have different template than the rc created above. deploymentName, deploymentImageName := "nginx-deployment", "nginx" deploymentReplicas := 1 deploymentImage := "nginx" deploymentStrategyType := extensions.RollingUpdateDeploymentStrategyType Logf("Creating deployment %s", deploymentName) d := newDeployment(deploymentName, deploymentReplicas, deploymentPodLabels, deploymentImageName, deploymentImage, deploymentStrategyType, nil) _, err = c.Extensions().Deployments(ns).Create(d) Expect(err).NotTo(HaveOccurred()) defer func() { deployment, err := c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) Logf("deleting deployment %s", deploymentName) Expect(c.Extensions().Deployments(ns).Delete(deploymentName, nil)).NotTo(HaveOccurred()) // TODO: remove this once we can delete rcs with deployment newRC, err := deploymentutil.GetNewRC(*deployment, c) Expect(err).NotTo(HaveOccurred()) Expect(c.Legacy().ReplicationControllers(ns).Delete(newRC.Name, nil)).NotTo(HaveOccurred()) oldRCs, _, err := deploymentutil.GetOldRCs(*deployment, c) Expect(err).NotTo(HaveOccurred()) for _, oldRC := range oldRCs { Expect(c.Legacy().ReplicationControllers(ns).Delete(oldRC.Name, nil)).NotTo(HaveOccurred()) } }() // Check that deployment is created fine. deployment, err := c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) // Verify that the required pods have come up. err = verifyPods(unversionedClient, ns, "nginx", false, deploymentReplicas) if err != nil { Logf("error in waiting for pods to come up: %s", err) Expect(err).NotTo(HaveOccurred()) } deployment, err = c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) // DeploymentStatus should be appropriately updated. Expect(deployment.Status.Replicas).Should(Equal(deploymentReplicas)) Expect(deployment.Status.UpdatedReplicas).Should(Equal(deploymentReplicas)) // Check if it's updated to revision 1 correctly checkDeploymentRevision(c, ns, deploymentName, "1", deploymentImageName, deploymentImage) // Check that the rc we created still doesn't contain revision information rc, err = c.Legacy().ReplicationControllers(ns).Get(rcName) Expect(rc.Annotations[deploymentutil.RevisionAnnotation]).Should(Equal("")) // Update the deploymentRollback to rollback to last revision // Since there's only 1 revision in history, it should stay as revision 1 revision := int64(0) Logf("rolling back deployment %s to last revision", deploymentName) rollback := newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) // There should be revision not found event since there's no last revision waitForEvents(unversionedClient, ns, deployment, 2) events, err := c.Events(ns).Search(deployment) Expect(err).NotTo(HaveOccurred()) Expect(events.Items[1].Reason).Should(Equal(deploymentutil.RollbackRevisionNotFound)) // Check if it's still revision 1 checkDeploymentRevision(c, ns, deploymentName, "1", deploymentImageName, deploymentImage) // Update the deployment to create redis pods. updatedDeploymentImage := "redis" updatedDeploymentImageName := "redis" d.Spec.Template.Spec.Containers[0].Name = updatedDeploymentImageName d.Spec.Template.Spec.Containers[0].Image = updatedDeploymentImage Logf("updating deployment %s", deploymentName) _, err = c.Extensions().Deployments(ns).Update(d) Expect(err).NotTo(HaveOccurred()) err = waitForDeploymentStatus(c, ns, deploymentName, deploymentReplicas, deploymentReplicas-1, deploymentReplicas+1, 0) Expect(err).NotTo(HaveOccurred()) // Check if it's updated to revision 2 correctly checkDeploymentRevision(c, ns, deploymentName, "2", updatedDeploymentImageName, updatedDeploymentImage) // Update the deploymentRollback to rollback to revision 1 revision = 1 Logf("rolling back deployment %s to revision %d", deploymentName, revision) rollback = newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) err = waitForDeploymentStatus(c, ns, deploymentName, deploymentReplicas, deploymentReplicas-1, deploymentReplicas+1, 0) Expect(err).NotTo(HaveOccurred()) // There should be rollback event after we rollback to revision 1 waitForEvents(unversionedClient, ns, deployment, 5) events, err = c.Events(ns).Search(deployment) Expect(err).NotTo(HaveOccurred()) Expect(events.Items[4].Reason).Should(Equal(deploymentutil.RollbackDone)) // Check if it's updated to revision 3 correctly checkDeploymentRevision(c, ns, deploymentName, "3", deploymentImageName, deploymentImage) // Update the deploymentRollback to rollback to revision 10 // Since there's no revision 10 in history, it should stay as revision 3, and emit an event revision = 10 Logf("rolling back deployment %s to revision %d", deploymentName, revision) rollback = newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) // There should be revision not found event since there's no revision 10 waitForEvents(unversionedClient, ns, deployment, 7) events, err = c.Events(ns).Search(deployment) Expect(err).NotTo(HaveOccurred()) Expect(events.Items[6].Reason).Should(Equal(deploymentutil.RollbackRevisionNotFound)) // Check if it's still revision 3 checkDeploymentRevision(c, ns, deploymentName, "3", deploymentImageName, deploymentImage) // Update the deploymentRollback to rollback to revision 3 // Since it's already revision 3, it should be no-op and emit an event revision = 3 Logf("rolling back deployment %s to revision %d", deploymentName, revision) rollback = newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) // There should be revision template unchanged event since it's already revision 3 waitForEvents(unversionedClient, ns, deployment, 8) events, err = c.Events(ns).Search(deployment) Expect(err).NotTo(HaveOccurred()) Expect(events.Items[7].Reason).Should(Equal(deploymentutil.RollbackTemplateUnchanged)) // Check if it's still revision 3 checkDeploymentRevision(c, ns, deploymentName, "3", deploymentImageName, deploymentImage) }
// testRollbackDeployment tests that a deployment is created (revision 1) and updated (revision 2), and // then rollback to revision 1 (should update template to revision 1, and then update revision 1 to 3), // and then rollback to last revision. func testRollbackDeployment(f *Framework) { ns := f.Namespace.Name unversionedClient := f.Client c := clientset.FromUnversionedClient(unversionedClient) podName := "nginx" deploymentPodLabels := map[string]string{"name": podName} // Create a deployment to create nginx pods. deploymentName, deploymentImageName := "nginx-deployment", "nginx" deploymentReplicas := 1 deploymentImage := "nginx" deploymentStrategyType := extensions.RollingUpdateDeploymentStrategyType Logf("Creating deployment %s", deploymentName) d := newDeployment(deploymentName, deploymentReplicas, deploymentPodLabels, deploymentImageName, deploymentImage, deploymentStrategyType, nil) _, err := c.Extensions().Deployments(ns).Create(d) Expect(err).NotTo(HaveOccurred()) defer func() { deployment, err := c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) Logf("deleting deployment %s", deploymentName) Expect(c.Extensions().Deployments(ns).Delete(deploymentName, nil)).NotTo(HaveOccurred()) // TODO: remove this once we can delete rcs with deployment newRC, err := deploymentutil.GetNewRC(*deployment, c) Expect(err).NotTo(HaveOccurred()) Expect(c.Legacy().ReplicationControllers(ns).Delete(newRC.Name, nil)).NotTo(HaveOccurred()) oldRCs, _, err := deploymentutil.GetOldRCs(*deployment, c) Expect(err).NotTo(HaveOccurred()) for _, oldRC := range oldRCs { Expect(c.Legacy().ReplicationControllers(ns).Delete(oldRC.Name, nil)).NotTo(HaveOccurred()) } }() // Check that deployment is created fine. deployment, err := c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) // Verify that the required pods have come up. err = verifyPods(unversionedClient, ns, "nginx", false, deploymentReplicas) if err != nil { Logf("error in waiting for pods to come up: %s", err) Expect(err).NotTo(HaveOccurred()) } deployment, err = c.Extensions().Deployments(ns).Get(deploymentName) Expect(err).NotTo(HaveOccurred()) // DeploymentStatus should be appropriately updated. Expect(deployment.Status.Replicas).Should(Equal(deploymentReplicas)) Expect(deployment.Status.UpdatedReplicas).Should(Equal(deploymentReplicas)) // Check if it's updated to revision 1 correctly checkDeploymentRevision(c, ns, deploymentName, "1", deploymentImageName, deploymentImage) // Update the deployment to create redis pods. updatedDeploymentImage := "redis" updatedDeploymentImageName := "redis" d.Spec.Template.Spec.Containers[0].Name = updatedDeploymentImageName d.Spec.Template.Spec.Containers[0].Image = updatedDeploymentImage Logf("updating deployment %s", deploymentName) _, err = c.Extensions().Deployments(ns).Update(d) Expect(err).NotTo(HaveOccurred()) err = waitForDeploymentStatus(c, ns, deploymentName, deploymentReplicas, deploymentReplicas-1, deploymentReplicas+1, 0) Expect(err).NotTo(HaveOccurred()) // Check if it's updated to revision 2 correctly checkDeploymentRevision(c, ns, deploymentName, "2", updatedDeploymentImageName, updatedDeploymentImage) // Update the deploymentRollback to rollback to revision 1 revision := int64(1) Logf("rolling back deployment %s to revision %d", deploymentName, revision) rollback := newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) err = waitForDeploymentStatus(c, ns, deploymentName, deploymentReplicas, deploymentReplicas-1, deploymentReplicas+1, 0) Expect(err).NotTo(HaveOccurred()) // Check if it's updated to revision 3 correctly checkDeploymentRevision(c, ns, deploymentName, "3", deploymentImageName, deploymentImage) // Update the deploymentRollback to rollback to last revision revision = 0 Logf("rolling back deployment %s to last revision", deploymentName) rollback = newDeploymentRollback(deploymentName, nil, revision) err = c.Extensions().Deployments(ns).Rollback(rollback) Expect(err).NotTo(HaveOccurred()) err = waitForDeploymentStatus(c, ns, deploymentName, deploymentReplicas, deploymentReplicas-1, deploymentReplicas+1, 0) Expect(err).NotTo(HaveOccurred()) // Check if it's updated to revision 4 correctly checkDeploymentRevision(c, ns, deploymentName, "4", updatedDeploymentImageName, updatedDeploymentImage) }
func (d *DeploymentController) getOldRCs(deployment experimental.Deployment) ([]*api.ReplicationController, error) { return deploymentUtil.GetOldRCs(deployment, d.client) }