2015-01-09 23:53:06 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2015-01-09 23:53:06 +00:00
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 (
"fmt"
"io"
2015-04-11 06:06:05 +00:00
"github.com/spf13/cobra"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/kubectl"
2016-10-07 22:24:42 +00:00
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2015-08-05 22:03:47 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2015-01-09 23:53:06 +00:00
)
2016-05-20 17:49:56 +00:00
var (
2017-02-16 03:47:00 +00:00
scaleLong = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Set a new size for a Deployment , ReplicaSet , Replication Controller , or Job .
2015-01-09 23:53:06 +00:00
2016-05-20 17:49:56 +00:00
Scale also allows users to specify one or more preconditions for the scale action .
2016-10-07 22:24:42 +00:00
2016-05-20 17:49:56 +00:00
If -- current - replicas or -- resource - version is specified , it is validated before the
scale is attempted , and it is guaranteed that the precondition holds true when the
2017-03-15 03:49:10 +00:00
scale is sent to the server . ` ) )
2016-10-07 22:24:42 +00:00
2017-02-16 03:47:00 +00:00
scaleExample = templates . Examples ( i18n . T ( `
2016-05-20 17:49:56 +00:00
# Scale a replicaset named ' foo ' to 3.
kubectl scale -- replicas = 3 rs / foo
2015-01-09 23:53:06 +00:00
2016-05-20 17:49:56 +00:00
# Scale a resource identified by type and name specified in "foo.yaml" to 3.
kubectl scale -- replicas = 3 - f foo . yaml
2015-08-11 09:05:28 +00:00
2016-05-20 17:49:56 +00:00
# If the deployment named mysql ' s current size is 2 , scale mysql to 3.
kubectl scale -- current - replicas = 2 -- replicas = 3 deployment / mysql
2015-06-29 09:37:11 +00:00
2016-05-20 17:49:56 +00:00
# Scale multiple replication controllers .
kubectl scale -- replicas = 5 rc / foo rc / bar rc / baz
2015-11-13 12:44:03 +00:00
2016-05-20 17:49:56 +00:00
# Scale job named ' cron ' to 3.
2017-03-15 03:49:10 +00:00
kubectl scale -- replicas = 3 job / cron ` ) )
2015-02-20 21:28:43 +00:00
)
2015-02-03 17:59:21 +00:00
2015-05-21 21:10:25 +00:00
// NewCmdScale returns a cobra command with the appropriate configuration and flags to run scale
2016-10-13 00:18:39 +00:00
func NewCmdScale ( f cmdutil . Factory , out io . Writer ) * cobra . Command {
2016-08-17 18:28:07 +00:00
options := & resource . FilenameOptions { }
2015-08-14 18:46:43 +00:00
2017-05-22 13:32:53 +00:00
validArgs := [ ] string { "deployment" , "replicaset" , "replicationcontroller" , "job" , "statefulset" }
2016-08-22 02:46:50 +00:00
argAliases := kubectl . ResourceAliases ( validArgs )
2015-02-20 21:28:43 +00:00
cmd := & cobra . Command {
2017-05-18 22:34:04 +00:00
Use : "scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)" ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job" ) ,
2017-02-16 03:47:00 +00:00
Long : scaleLong ,
Example : scaleExample ,
2015-01-09 23:53:06 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2015-07-01 22:47:43 +00:00
cmdutil . CheckErr ( cmdutil . ValidateOutputArgs ( cmd ) )
shortOutput := cmdutil . GetFlagString ( cmd , "output" ) == "name"
2015-08-14 18:46:43 +00:00
err := RunScale ( f , out , cmd , args , shortOutput , options )
2015-04-07 18:21:25 +00:00
cmdutil . CheckErr ( err )
2015-01-09 23:53:06 +00:00
} ,
2016-08-22 02:46:50 +00:00
ValidArgs : validArgs ,
ArgAliases : argAliases ,
2015-01-09 23:53:06 +00:00
}
2017-08-23 09:09:16 +00:00
cmd . Flags ( ) . StringP ( "selector" , "l" , "" , "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)" )
cmd . Flags ( ) . Bool ( "all" , false , "Select all resources in the namespace of the specified resource types" )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "resource-version" , "" , i18n . T ( "Precondition for resource version. Requires that the current resource version match this value in order to scale." ) )
2015-11-13 12:44:03 +00:00
cmd . Flags ( ) . Int ( "current-replicas" , - 1 , "Precondition for current size. Requires that the current size of the resource match this value in order to scale." )
2015-02-03 17:59:21 +00:00
cmd . Flags ( ) . Int ( "replicas" , - 1 , "The new desired number of replicas. Required." )
2015-03-17 15:49:35 +00:00
cmd . MarkFlagRequired ( "replicas" )
2016-08-03 20:36:51 +00:00
cmd . Flags ( ) . Duration ( "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)." )
2015-07-01 22:47:43 +00:00
cmdutil . AddOutputFlagsForMutation ( cmd )
2016-01-22 18:33:23 +00:00
cmdutil . AddRecordFlag ( cmd )
2016-03-10 01:27:19 +00:00
cmdutil . AddInclude3rdPartyFlags ( cmd )
2015-08-11 09:05:28 +00:00
2016-08-17 18:28:07 +00:00
usage := "identifying the resource to set a new size"
cmdutil . AddFilenameOptionFlags ( cmd , options , usage )
2015-01-09 23:53:06 +00:00
return cmd
}
2015-03-09 22:08:16 +00:00
2015-05-21 21:10:25 +00:00
// RunScale executes the scaling
2016-10-13 00:18:39 +00:00
func RunScale ( f cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string , shortOutput bool , options * resource . FilenameOptions ) error {
2015-08-11 09:05:28 +00:00
cmdNamespace , enforceNamespace , err := f . DefaultNamespace ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
2017-08-23 09:09:16 +00:00
selector := cmdutil . GetFlagString ( cmd , "selector" )
all := cmdutil . GetFlagBool ( cmd , "all" )
2017-05-16 21:52:51 +00:00
mapper , _ := f . Object ( )
2017-08-02 20:23:07 +00:00
r := f . NewBuilder ( ) .
2015-04-11 06:06:05 +00:00
ContinueOnError ( ) .
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
2016-08-17 18:28:07 +00:00
FilenameParam ( enforceNamespace , options ) .
2017-08-23 09:09:16 +00:00
ResourceTypeOrNameArgs ( all , args ... ) .
2015-04-11 06:06:05 +00:00
Flatten ( ) .
2017-08-04 06:54:17 +00:00
LabelSelectorParam ( selector ) .
2015-04-11 06:06:05 +00:00
Do ( )
err = r . Err ( )
2016-08-30 05:27:22 +00:00
if resource . IsUsageError ( err ) {
2017-06-14 21:14:42 +00:00
return cmdutil . UsageErrorf ( cmd , "%v" , err )
2016-08-30 05:27:22 +00:00
}
2015-04-11 06:06:05 +00:00
if err != nil {
return err
}
2016-08-30 05:27:22 +00:00
count := cmdutil . GetFlagInt ( cmd , "replicas" )
if count < 0 {
2017-06-14 21:14:42 +00:00
return cmdutil . UsageErrorf ( cmd , "The --replicas=COUNT flag is required, and COUNT must be greater than or equal to 0" )
2016-08-30 05:27:22 +00:00
}
2016-04-14 22:00:40 +00:00
infos := [ ] * resource . Info { }
err = r . Visit ( func ( info * resource . Info , err error ) error {
if err == nil {
infos = append ( infos , info )
}
return nil
} )
2015-03-09 22:08:16 +00:00
2015-04-07 18:21:25 +00:00
resourceVersion := cmdutil . GetFlagString ( cmd , "resource-version" )
2015-06-29 09:37:11 +00:00
if len ( resourceVersion ) != 0 && len ( infos ) > 1 {
2015-11-13 12:44:03 +00:00
return fmt . Errorf ( "cannot use --resource-version with multiple resources" )
2015-06-29 09:37:11 +00:00
}
2016-04-14 22:00:40 +00:00
counter := 0
err = r . Visit ( func ( info * resource . Info , err error ) error {
if err != nil {
return err
}
mapping := info . ResourceMapping ( )
scaler , err := f . Scaler ( mapping )
if err != nil {
return err
}
currentSize := cmdutil . GetFlagInt ( cmd , "current-replicas" )
precondition := & kubectl . ScalePrecondition { Size : currentSize , ResourceVersion : resourceVersion }
retry := kubectl . NewRetryParams ( kubectl . Interval , kubectl . Timeout )
var waitForReplicas * kubectl . RetryParams
if timeout := cmdutil . GetFlagDuration ( cmd , "timeout" ) ; timeout != 0 {
waitForReplicas = kubectl . NewRetryParams ( kubectl . Interval , timeout )
}
2015-06-29 09:37:11 +00:00
if err := scaler . Scale ( info . Namespace , info . Name , uint ( count ) , precondition , retry , waitForReplicas ) ; err != nil {
2016-04-14 22:00:40 +00:00
return err
2015-06-29 09:37:11 +00:00
}
2016-01-22 18:33:23 +00:00
if cmdutil . ShouldRecord ( cmd , info ) {
2017-02-25 15:40:50 +00:00
patchBytes , patchType , err := cmdutil . ChangeResourcePatch ( info , f . Command ( cmd , true ) )
2016-01-22 18:33:23 +00:00
if err != nil {
2016-04-14 22:00:40 +00:00
return err
2016-01-22 18:33:23 +00:00
}
mapping := info . ResourceMapping ( )
client , err := f . ClientForMapping ( mapping )
if err != nil {
return err
}
helper := resource . NewHelper ( client , mapping )
2017-01-23 02:15:10 +00:00
_ , err = helper . Patch ( info . Namespace , info . Name , patchType , patchBytes )
2016-01-22 18:33:23 +00:00
if err != nil {
2016-04-14 22:00:40 +00:00
return err
2016-01-22 18:33:23 +00:00
}
}
2016-04-14 22:00:40 +00:00
counter ++
2017-10-31 15:58:38 +00:00
f . PrintSuccess ( mapper , shortOutput , out , info . Mapping . Resource , info . Name , false , "scaled" )
2016-04-14 22:00:40 +00:00
return nil
} )
if err != nil {
return err
2015-03-09 22:08:16 +00:00
}
2016-04-14 22:00:40 +00:00
if counter == 0 {
return fmt . Errorf ( "no objects passed to scale" )
}
return nil
2015-03-09 22:08:16 +00:00
}