mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-27 19:16:16 +00:00
Add support for --rollback.
This commit is contained in:
@@ -82,6 +82,7 @@ func NewCmdRollingUpdate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmd.Flags().String("image", "", "Image to upgrade the controller to. Can not be used with --filename/-f")
|
||||
cmd.Flags().String("deployment-label-key", "deployment", "The key to use to differentiate between two different controllers, default 'deployment'. Only relevant when --image is specified, ignored otherwise")
|
||||
cmd.Flags().Bool("dry-run", false, "If true, print out the changes that would be made, but don't actually make them.")
|
||||
cmd.Flags().Bool("rollback", false, "If true, this is a request to abort an existing rollout that is partially rolled out. It effectively reverses current and next and runs a rollout")
|
||||
cmdutil.AddPrinterFlags(cmd)
|
||||
return cmd
|
||||
}
|
||||
@@ -171,10 +172,15 @@ func RunRollingUpdate(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, arg
|
||||
|
||||
if len(newName) > 0 {
|
||||
newRc, err = client.ReplicationControllers(cmdNamespace).Get(newName)
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
if err != nil {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
} else {
|
||||
newRc = nil
|
||||
}
|
||||
} else {
|
||||
fmt.Fprint(out, "Found existing update in progress (%s), resuming.\n", newName)
|
||||
}
|
||||
fmt.Fprint(out, "Found existing update in progress (%s), resuming.\n", newName)
|
||||
}
|
||||
if newRc == nil {
|
||||
// load the old RC into the "new" RC
|
||||
@@ -257,7 +263,7 @@ func RunRollingUpdate(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, arg
|
||||
if keepOldName {
|
||||
updateCleanupPolicy = kubectl.RenameRollingUpdateCleanupPolicy
|
||||
}
|
||||
err = updater.Update(&kubectl.RollingUpdaterConfig{
|
||||
config := &kubectl.RollingUpdaterConfig{
|
||||
Out: out,
|
||||
OldRc: oldRc,
|
||||
NewRc: newRc,
|
||||
@@ -265,7 +271,12 @@ func RunRollingUpdate(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, arg
|
||||
Interval: interval,
|
||||
Timeout: timeout,
|
||||
CleanupPolicy: updateCleanupPolicy,
|
||||
})
|
||||
}
|
||||
if cmdutil.GetFlagBool(cmd, "rollback") {
|
||||
kubectl.AbortRollingUpdate(config)
|
||||
client.ReplicationControllers(config.NewRc.Namespace).Update(config.NewRc)
|
||||
}
|
||||
err = updater.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -85,6 +85,24 @@ const (
|
||||
nextControllerAnnotation = kubectlAnnotationPrefix + "next-controller-id"
|
||||
)
|
||||
|
||||
func AbortRollingUpdate(c *RollingUpdaterConfig) {
|
||||
// Swap the controllers
|
||||
tmp := c.OldRc
|
||||
c.OldRc = c.NewRc
|
||||
c.NewRc = tmp
|
||||
|
||||
if c.NewRc.Annotations == nil {
|
||||
c.NewRc.Annotations = map[string]string{}
|
||||
}
|
||||
c.NewRc.Annotations[sourceIdAnnotation] = fmt.Sprintf("%s:%s", c.OldRc.Name, c.OldRc.UID)
|
||||
desiredSize, found := c.OldRc.Annotations[desiredReplicasAnnotation]
|
||||
if found {
|
||||
fmt.Printf("Found desired replicas.")
|
||||
c.NewRc.Annotations[desiredReplicasAnnotation] = desiredSize
|
||||
}
|
||||
c.CleanupPolicy = DeleteRollingUpdateCleanupPolicy
|
||||
}
|
||||
|
||||
func GetNextControllerAnnotation(rc *api.ReplicationController) (string, bool) {
|
||||
res, found := rc.Annotations[nextControllerAnnotation]
|
||||
return res, found
|
||||
@@ -237,7 +255,7 @@ func (r *RollingUpdater) getExistingNewRc(sourceId, name string) (rc *api.Replic
|
||||
source := annotations[sourceIdAnnotation]
|
||||
_, ok := annotations[desiredReplicasAnnotation]
|
||||
if source != sourceId || !ok {
|
||||
err = fmt.Errorf("Missing/unexpected annotations for controller %s: %s", name, annotations)
|
||||
err = fmt.Errorf("Missing/unexpected annotations for controller %s, expected %s : %s", name, sourceId, annotations)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user