ceph-csi/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/rollingupdate.go

476 lines
17 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"bytes"
"fmt"
"time"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
2018-07-18 14:47:22 +00:00
scaleclient "k8s.io/client-go/scale"
2018-01-09 18:57:14 +00:00
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
2018-07-18 14:47:22 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
2018-01-09 18:57:14 +00:00
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2018-07-18 14:47:22 +00:00
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
2018-01-09 18:57:14 +00:00
"k8s.io/kubernetes/pkg/kubectl/util"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2018-07-18 14:47:22 +00:00
"k8s.io/kubernetes/pkg/kubectl/validation"
2018-01-09 18:57:14 +00:00
)
var (
rollingUpdateLong = templates.LongDesc(i18n.T(`
Perform a rolling update of the given ReplicationController.
Replaces the specified replication controller with a new replication controller by updating one pod at a time to use the
new PodTemplate. The new-controller.json must specify the same namespace as the
existing replication controller and overwrite at least one (common) label in its replicaSelector.
![Workflow](http://kubernetes.io/images/docs/kubectl_rollingupdate.svg)`))
rollingUpdateExample = templates.Examples(i18n.T(`
# Update pods of frontend-v1 using new replication controller data in frontend-v2.json.
kubectl rolling-update frontend-v1 -f frontend-v2.json
# Update pods of frontend-v1 using JSON data passed into stdin.
cat frontend-v2.json | kubectl rolling-update frontend-v1 -f -
# Update the pods of frontend-v1 to frontend-v2 by just changing the image, and switching the
# name of the replication controller.
kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2
# Update the pods of frontend by just changing the image, and keeping the old name.
kubectl rolling-update frontend --image=image:v2
# Abort and reverse an existing rollout in progress (from frontend-v1 to frontend-v2).
kubectl rolling-update frontend-v1 frontend-v2 --rollback`))
)
var (
updatePeriod, _ = time.ParseDuration("1m0s")
timeout, _ = time.ParseDuration("5m0s")
pollInterval, _ = time.ParseDuration("3s")
)
2018-07-18 14:47:22 +00:00
type RollingUpdateOptions struct {
FilenameOptions *resource.FilenameOptions
OldName string
KeepOldName bool
DeploymentKey string
Image string
Container string
PullPolicy string
Rollback bool
Period time.Duration
Timeout time.Duration
Interval time.Duration
DryRun bool
OutputFormat string
Namespace string
EnforceNamespace bool
ScaleClient scaleclient.ScalesGetter
ClientSet internalclientset.Interface
Builder *resource.Builder
ShouldValidate bool
Validator func(bool) (validation.Schema, error)
FindNewName func(*api.ReplicationController) string
PrintFlags *genericclioptions.PrintFlags
ToPrinter func(string) (printers.ResourcePrinter, error)
genericclioptions.IOStreams
}
func NewRollingUpdateOptions(streams genericclioptions.IOStreams) *RollingUpdateOptions {
return &RollingUpdateOptions{
PrintFlags: genericclioptions.NewPrintFlags("rolling updated").WithTypeSetter(scheme.Scheme),
FilenameOptions: &resource.FilenameOptions{},
DeploymentKey: "deployment",
Timeout: timeout,
Interval: pollInterval,
Period: updatePeriod,
IOStreams: streams,
}
}
func NewCmdRollingUpdate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewRollingUpdateOptions(ioStreams)
2018-01-09 18:57:14 +00:00
cmd := &cobra.Command{
2018-03-06 22:33:18 +00:00
Use: "rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] --image=NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC)",
DisableFlagsInUseLine: true,
2018-07-18 14:47:22 +00:00
Short: "Perform a rolling update. This command is deprecated, use rollout instead.",
Long: rollingUpdateLong,
Example: rollingUpdateExample,
Deprecated: `use "rollout" instead`,
Hidden: true,
2018-01-09 18:57:14 +00:00
Run: func(cmd *cobra.Command, args []string) {
2018-07-18 14:47:22 +00:00
cmdutil.CheckErr(o.Complete(f, cmd, args))
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run())
2018-01-09 18:57:14 +00:00
},
}
2018-07-18 14:47:22 +00:00
o.PrintFlags.AddFlags(cmd)
cmd.Flags().DurationVar(&o.Period, "update-period", o.Period, `Time to wait between updating pods. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`)
cmd.Flags().DurationVar(&o.Interval, "poll-interval", o.Interval, `Time delay between polling for replication controller status after the update. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`)
cmd.Flags().DurationVar(&o.Timeout, "timeout", o.Timeout, `Max time to wait for a replication controller to update before giving up. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`)
2018-01-09 18:57:14 +00:00
usage := "Filename or URL to file to use to create the new replication controller."
2018-07-18 14:47:22 +00:00
cmdutil.AddJsonFilenameFlag(cmd.Flags(), &o.FilenameOptions.Filenames, usage)
cmd.Flags().StringVar(&o.Image, "image", o.Image, i18n.T("Image to use for upgrading the replication controller. Must be distinct from the existing image (either new image or new image tag). Can not be used with --filename/-f"))
cmd.Flags().StringVar(&o.DeploymentKey, "deployment-label-key", o.DeploymentKey, i18n.T("The key to use to differentiate between two different controllers, default 'deployment'. Only relevant when --image is specified, ignored otherwise"))
cmd.Flags().StringVar(&o.Container, "container", o.Container, i18n.T("Container name which will have its image upgraded. Only relevant when --image is specified, ignored otherwise. Required when using --image on a multi-container pod"))
cmd.Flags().StringVar(&o.PullPolicy, "image-pull-policy", o.PullPolicy, i18n.T("Explicit policy for when to pull container images. Required when --image is same as existing image, ignored otherwise."))
cmd.Flags().BoolVar(&o.Rollback, "rollback", o.Rollback, "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")
2018-01-09 18:57:14 +00:00
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddValidateFlags(cmd)
return cmd
}
func validateArguments(cmd *cobra.Command, filenames, args []string) error {
deploymentKey := cmdutil.GetFlagString(cmd, "deployment-label-key")
image := cmdutil.GetFlagString(cmd, "image")
rollback := cmdutil.GetFlagBool(cmd, "rollback")
errors := []error{}
if len(deploymentKey) == 0 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "--deployment-label-key can not be empty"))
}
if len(filenames) > 1 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "May only specify a single filename for new controller"))
}
if !rollback {
if len(filenames) == 0 && len(image) == 0 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "Must specify --filename or --image for new controller"))
} else if len(filenames) != 0 && len(image) != 0 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "--filename and --image can not both be specified"))
}
} else {
if len(filenames) != 0 || len(image) != 0 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "Don't specify --filename or --image on rollback"))
}
}
if len(args) < 1 {
errors = append(errors, cmdutil.UsageErrorf(cmd, "Must specify the controller to update"))
}
return utilerrors.NewAggregate(errors)
}
2018-07-18 14:47:22 +00:00
func (o *RollingUpdateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
if len(args) > 0 {
o.OldName = args[0]
}
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.OutputFormat = cmdutil.GetFlagString(cmd, "output")
o.KeepOldName = len(args) == 1
o.ShouldValidate = cmdutil.GetFlagBool(cmd, "validate")
o.Validator = f.Validator
o.FindNewName = func(obj *api.ReplicationController) string {
return findNewName(args, obj)
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
var err error
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
o.ScaleClient, err = cmdutil.ScaleClientFn(f)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
o.ClientSet, err = f.ClientSet()
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
o.Builder = f.NewBuilder()
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
o.PrintFlags.NamePrintFlags.Operation = operation
if o.DryRun {
o.PrintFlags.Complete("%s (dry run)")
}
return o.PrintFlags.ToPrinter()
}
return nil
}
func (o *RollingUpdateOptions) Validate(cmd *cobra.Command, args []string) error {
return validateArguments(cmd, o.FilenameOptions.Filenames, args)
}
func (o *RollingUpdateOptions) Run() error {
filename := ""
if len(o.FilenameOptions.Filenames) > 0 {
filename = o.FilenameOptions.Filenames[0]
}
coreClient := o.ClientSet.Core()
2018-01-09 18:57:14 +00:00
var newRc *api.ReplicationController
// fetch rc
2018-07-18 14:47:22 +00:00
oldRc, err := coreClient.ReplicationControllers(o.Namespace).Get(o.OldName, metav1.GetOptions{})
2018-01-09 18:57:14 +00:00
if err != nil {
2018-07-18 14:47:22 +00:00
if !errors.IsNotFound(err) || len(o.Image) == 0 || !o.KeepOldName {
2018-01-09 18:57:14 +00:00
return err
}
// We're in the middle of a rename, look for an RC with a source annotation of oldName
2018-07-18 14:47:22 +00:00
newRc, err := kubectl.FindSourceController(coreClient, o.Namespace, o.OldName)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
return kubectl.Rename(coreClient, newRc, o.OldName)
2018-01-09 18:57:14 +00:00
}
var replicasDefaulted bool
if len(filename) != 0 {
2018-07-18 14:47:22 +00:00
schema, err := o.Validator(o.ShouldValidate)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
request := o.Builder.
2018-01-09 18:57:14 +00:00
Unstructured().
Schema(schema).
2018-07-18 14:47:22 +00:00
NamespaceParam(o.Namespace).DefaultNamespace().
FilenameParam(o.EnforceNamespace, &resource.FilenameOptions{Recursive: false, Filenames: []string{filename}}).
2018-01-09 18:57:14 +00:00
Flatten().
Do()
infos, err := request.Infos()
if err != nil {
return err
}
// Handle filename input from stdin.
if len(infos) > 1 {
2018-07-18 14:47:22 +00:00
return fmt.Errorf("%s specifies multiple items", filename)
2018-01-09 18:57:14 +00:00
}
if len(infos) == 0 {
2018-07-18 14:47:22 +00:00
return fmt.Errorf("please make sure %s exists and is not empty", filename)
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
uncastVersionedObj, err := legacyscheme.Scheme.ConvertToVersion(infos[0].Object, v1.SchemeGroupVersion)
if err != nil {
glog.V(4).Infof("Object %T is not a ReplicationController", infos[0].Object)
return fmt.Errorf("%s contains a %v not a ReplicationController", filename, infos[0].Object.GetObjectKind().GroupVersionKind())
}
switch t := uncastVersionedObj.(type) {
2018-01-09 18:57:14 +00:00
case *v1.ReplicationController:
replicasDefaulted = t.Spec.Replicas == nil
2018-07-18 14:47:22 +00:00
// previous code ignored the error. Seem like it's very unlikely to fail, so ok for now.
uncastObj, err := legacyscheme.Scheme.ConvertToVersion(uncastVersionedObj, api.SchemeGroupVersion)
if err == nil {
newRc, _ = uncastObj.(*api.ReplicationController)
}
2018-01-09 18:57:14 +00:00
}
if newRc == nil {
glog.V(4).Infof("Object %T is not a ReplicationController", infos[0].Object)
2018-07-18 14:47:22 +00:00
return fmt.Errorf("%s contains a %v not a ReplicationController", filename, infos[0].Object.GetObjectKind().GroupVersionKind())
2018-01-09 18:57:14 +00:00
}
}
// If the --image option is specified, we need to create a new rc with at least one different selector
// than the old rc. This selector is the hash of the rc, with a suffix to provide uniqueness for
// same-image updates.
2018-07-18 14:47:22 +00:00
if len(o.Image) != 0 {
2018-01-09 18:57:14 +00:00
codec := legacyscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion)
2018-07-18 14:47:22 +00:00
newName := o.FindNewName(oldRc)
if newRc, err = kubectl.LoadExistingNextReplicationController(coreClient, o.Namespace, newName); err != nil {
2018-01-09 18:57:14 +00:00
return err
}
if newRc != nil {
2018-07-18 14:47:22 +00:00
if inProgressImage := newRc.Spec.Template.Spec.Containers[0].Image; inProgressImage != o.Image {
return fmt.Errorf("Found existing in-progress update to image (%s).\nEither continue in-progress update with --image=%s or rollback with --rollback", inProgressImage, inProgressImage)
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
fmt.Fprintf(o.Out, "Found existing update in progress (%s), resuming.\n", newRc.Name)
2018-01-09 18:57:14 +00:00
} else {
config := &kubectl.NewControllerConfig{
2018-07-18 14:47:22 +00:00
Namespace: o.Namespace,
OldName: o.OldName,
2018-01-09 18:57:14 +00:00
NewName: newName,
2018-07-18 14:47:22 +00:00
Image: o.Image,
Container: o.Container,
DeploymentKey: o.DeploymentKey,
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
if oldRc.Spec.Template.Spec.Containers[0].Image == o.Image {
if len(o.PullPolicy) == 0 {
return fmt.Errorf("--image-pull-policy (Always|Never|IfNotPresent) must be provided when --image is the same as existing container image")
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
config.PullPolicy = api.PullPolicy(o.PullPolicy)
2018-01-09 18:57:14 +00:00
}
newRc, err = kubectl.CreateNewControllerFromCurrentController(coreClient, codec, config)
if err != nil {
return err
}
}
// Update the existing replication controller with pointers to the 'next' controller
// and adding the <deploymentKey> label if necessary to distinguish it from the 'next' controller.
oldHash, err := util.HashObject(oldRc, codec)
if err != nil {
return err
}
// If new image is same as old, the hash may not be distinct, so add a suffix.
oldHash += "-orig"
2018-07-18 14:47:22 +00:00
oldRc, err = kubectl.UpdateExistingReplicationController(coreClient, coreClient, oldRc, o.Namespace, newRc.Name, o.DeploymentKey, oldHash, o.Out)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
}
2018-07-18 14:47:22 +00:00
if o.Rollback {
newName := o.FindNewName(oldRc)
if newRc, err = kubectl.LoadExistingNextReplicationController(coreClient, o.Namespace, newName); err != nil {
2018-01-09 18:57:14 +00:00
return err
}
if newRc == nil {
2018-07-18 14:47:22 +00:00
return fmt.Errorf("Could not find %s to rollback.\n", newName)
2018-01-09 18:57:14 +00:00
}
}
2018-07-18 14:47:22 +00:00
if o.OldName == newRc.Name {
return fmt.Errorf("%s cannot have the same name as the existing ReplicationController %s",
filename, o.OldName)
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
updater := kubectl.NewRollingUpdater(newRc.Namespace, coreClient, coreClient, o.ScaleClient)
2018-01-09 18:57:14 +00:00
// To successfully pull off a rolling update the new and old rc have to differ
// by at least one selector. Every new pod should have the selector and every
// old pod should not have the selector.
var hasLabel bool
for key, oldValue := range oldRc.Spec.Selector {
if newValue, ok := newRc.Spec.Selector[key]; ok && newValue != oldValue {
hasLabel = true
break
}
}
if !hasLabel {
2018-07-18 14:47:22 +00:00
return fmt.Errorf("%s must specify a matching key with non-equal value in Selector for %s",
filename, o.OldName)
2018-01-09 18:57:14 +00:00
}
// TODO: handle scales during rolling update
if replicasDefaulted {
newRc.Spec.Replicas = oldRc.Spec.Replicas
}
2018-07-18 14:47:22 +00:00
if o.DryRun {
2018-01-09 18:57:14 +00:00
oldRcData := &bytes.Buffer{}
newRcData := &bytes.Buffer{}
2018-07-18 14:47:22 +00:00
if o.OutputFormat == "" {
2018-01-09 18:57:14 +00:00
oldRcData.WriteString(oldRc.Name)
newRcData.WriteString(newRc.Name)
} else {
2018-07-18 14:47:22 +00:00
printer, err := o.ToPrinter("rolling updated")
if err != nil {
2018-01-09 18:57:14 +00:00
return err
}
2018-07-18 14:47:22 +00:00
if err := printer.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(oldRc, nil), oldRcData); err != nil {
return err
}
if err := printer.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(newRc, nil), newRcData); err != nil {
2018-01-09 18:57:14 +00:00
return err
}
}
2018-07-18 14:47:22 +00:00
fmt.Fprintf(o.Out, "Rolling from:\n%s\nTo:\n%s\n", string(oldRcData.Bytes()), string(newRcData.Bytes()))
2018-01-09 18:57:14 +00:00
return nil
}
updateCleanupPolicy := kubectl.DeleteRollingUpdateCleanupPolicy
2018-07-18 14:47:22 +00:00
if o.KeepOldName {
2018-01-09 18:57:14 +00:00
updateCleanupPolicy = kubectl.RenameRollingUpdateCleanupPolicy
}
config := &kubectl.RollingUpdaterConfig{
2018-07-18 14:47:22 +00:00
Out: o.Out,
2018-01-09 18:57:14 +00:00
OldRc: oldRc,
NewRc: newRc,
2018-07-18 14:47:22 +00:00
UpdatePeriod: o.Period,
Interval: o.Interval,
2018-01-09 18:57:14 +00:00
Timeout: timeout,
CleanupPolicy: updateCleanupPolicy,
MaxUnavailable: intstr.FromInt(0),
MaxSurge: intstr.FromInt(1),
}
2018-07-18 14:47:22 +00:00
if o.Rollback {
2018-01-09 18:57:14 +00:00
err = kubectl.AbortRollingUpdate(config)
if err != nil {
return err
}
coreClient.ReplicationControllers(config.NewRc.Namespace).Update(config.NewRc)
}
err = updater.Update(config)
if err != nil {
return err
}
message := "rolling updated"
2018-07-18 14:47:22 +00:00
if o.KeepOldName {
newRc.Name = o.OldName
2018-01-09 18:57:14 +00:00
} else {
message = fmt.Sprintf("rolling updated to %q", newRc.Name)
}
2018-07-18 14:47:22 +00:00
newRc, err = coreClient.ReplicationControllers(o.Namespace).Get(newRc.Name, metav1.GetOptions{})
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
printer, err := o.ToPrinter(message)
if err != nil {
return err
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
return printer.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(newRc, nil), o.Out)
2018-01-09 18:57:14 +00:00
}
func findNewName(args []string, oldRc *api.ReplicationController) string {
if len(args) >= 2 {
return args[1]
}
if oldRc != nil {
newName, _ := kubectl.GetNextControllerAnnotation(oldRc)
return newName
}
return ""
}