From 8fd7862e6974fda28bb91286ca79dc6fa236f2f8 Mon Sep 17 00:00:00 2001 From: Hong Xu Date: Wed, 24 Mar 2021 13:59:08 -0700 Subject: [PATCH] Validate scale's --current-replicas flag --current-replicas is only meaningful when it's -1 or greater. Also add an error message to clarify the range of --current-replicas. It is unclear that --current-replicas=-1 means no precondition. This info may be useful for programming purposes (i.e., shell scripting, etc.). --- staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go b/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go index f46be57e8fd..1607ef3ff59 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go @@ -127,7 +127,7 @@ func NewCmdScale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") cmd.Flags().BoolVar(&o.All, "all", o.All, "Select all resources in the namespace of the specified resource types") cmd.Flags().StringVar(&o.ResourceVersion, "resource-version", o.ResourceVersion, i18n.T("Precondition for resource version. Requires that the current resource version match this value in order to scale.")) - cmd.Flags().IntVar(&o.CurrentReplicas, "current-replicas", o.CurrentReplicas, "Precondition for current size. Requires that the current size of the resource match this value in order to scale.") + cmd.Flags().IntVar(&o.CurrentReplicas, "current-replicas", o.CurrentReplicas, "Precondition for current size. Requires that the current size of the resource match this value in order to scale. -1 (default) for no condition.") cmd.Flags().IntVar(&o.Replicas, "replicas", o.Replicas, "The new desired number of replicas. Required.") cmd.MarkFlagRequired("replicas") cmd.Flags().DurationVar(&o.Timeout, "timeout", 0, "The length of time to wait before giving up on a scale operation, zero means don't wait. Any other values should contain a corresponding time unit (e.g. 1s, 2m, 3h).") @@ -185,6 +185,10 @@ func (o *ScaleOptions) Validate(cmd *cobra.Command) error { return fmt.Errorf("The --replicas=COUNT flag is required, and COUNT must be greater than or equal to 0") } + if o.CurrentReplicas < -1 { + return fmt.Errorf("The --current-replicas must specify an integer of -1 or greater") + } + return nil }