2015-01-09 23:53:06 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
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-05-21 21:10:25 +00:00
"os"
2015-01-09 23:53:06 +00:00
2015-04-11 06:06:05 +00:00
"github.com/spf13/cobra"
2016-01-22 18:33:23 +00:00
"k8s.io/kubernetes/pkg/api"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2015-10-14 05:18:37 +00:00
utilerrors "k8s.io/kubernetes/pkg/util/errors"
2015-01-09 23:53:06 +00:00
)
2015-08-14 18:46:43 +00:00
// ScaleOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type ScaleOptions struct {
Filenames [ ] string
2016-03-28 19:44:21 +00:00
Recursive bool
2015-08-14 18:46:43 +00:00
}
2015-02-20 21:28:43 +00:00
const (
2016-03-21 21:00:43 +00:00
scale_long = ` Set a new size for a Deployment , ReplicaSet , Replication Controller , or Job .
2015-01-09 23:53:06 +00:00
2015-05-21 21:10:25 +00:00
Scale also allows users to specify one or more preconditions for the scale action .
2015-02-03 17:59:21 +00:00
If -- current - replicas or -- resource - version is specified , it is validated before the
2015-05-21 21:10:25 +00:00
scale is attempted , and it is guaranteed that the precondition holds true when the
scale is sent to the server . `
2016-03-21 21:00:43 +00:00
scale_example = ` # Scale a replicaset named ' foo ' to 3.
kubectl scale -- replicas = 3 rs / foo
2015-01-09 23:53:06 +00:00
2015-11-13 12:44:03 +00:00
# Scale a resource identified by type and name specified in "foo.yaml" to 3.
2016-02-29 14:41:09 +00:00
kubectl scale -- replicas = 3 - f foo . yaml
2015-08-11 09:05:28 +00:00
2015-11-13 12:44:03 +00:00
# If the deployment named mysql ' s current size is 2 , scale mysql to 3.
2016-02-29 14:41:09 +00:00
kubectl scale -- current - replicas = 2 -- replicas = 3 deployment / mysql
2015-06-29 09:37:11 +00:00
2015-08-12 16:50:09 +00:00
# Scale multiple replication controllers .
2016-02-29 14:41:09 +00:00
kubectl scale -- replicas = 5 rc / foo rc / bar rc / baz
2015-11-13 12:44:03 +00:00
# Scale job named ' cron ' to 3.
2016-02-29 14:41:09 +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
func NewCmdScale ( f * cmdutil . Factory , out io . Writer ) * cobra . Command {
2015-08-14 18:46:43 +00:00
options := & ScaleOptions { }
2015-02-20 21:28:43 +00:00
cmd := & cobra . Command {
2015-08-11 09:05:28 +00:00
Use : "scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)" ,
2015-05-21 21:10:25 +00:00
// resize is deprecated
Aliases : [ ] string { "resize" } ,
2016-03-21 21:00:43 +00:00
Short : "Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job." ,
2015-05-21 21:10:25 +00:00
Long : scale_long ,
Example : scale_example ,
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
} ,
}
2015-05-21 21:10:25 +00:00
cmd . Flags ( ) . String ( "resource-version" , "" , "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" )
2015-09-11 06:38:52 +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." )
2015-07-01 22:47:43 +00:00
cmdutil . AddOutputFlagsForMutation ( cmd )
2016-01-22 18:33:23 +00:00
cmdutil . AddRecordFlag ( cmd )
2015-08-11 09:05:28 +00:00
2015-11-13 12:44:03 +00:00
usage := "Filename, directory, or URL to a file identifying the resource to set a new size"
2015-08-14 18:46:43 +00:00
kubectl . AddJsonFilenameFlag ( cmd , & options . Filenames , usage )
2016-03-28 19:44:21 +00:00
cmdutil . AddRecursiveFlag ( cmd , & options . Recursive )
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
2015-08-14 18:46:43 +00:00
func RunScale ( f * cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string , shortOutput bool , options * ScaleOptions ) error {
2015-07-09 23:15:42 +00:00
if len ( os . Args ) > 1 && os . Args [ 1 ] == "resize" {
2015-05-21 21:10:25 +00:00
printDeprecationWarning ( "scale" , "resize" )
}
2015-04-07 18:21:25 +00:00
count := cmdutil . GetFlagInt ( cmd , "replicas" )
2015-05-21 13:05:34 +00:00
if count < 0 {
2015-09-11 06:38:52 +00:00
return cmdutil . UsageError ( cmd , "--replicas=COUNT is required, and COUNT must be greater than or equal to 0" )
2015-03-09 22:08:16 +00:00
}
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
}
2015-04-11 06:06:05 +00:00
mapper , typer := f . Object ( )
2015-12-21 05:37:49 +00:00
r := resource . NewBuilder ( mapper , typer , resource . ClientMapperFunc ( f . ClientForMapping ) , f . Decoder ( true ) ) .
2015-04-11 06:06:05 +00:00
ContinueOnError ( ) .
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
2016-03-28 19:44:21 +00:00
FilenameParam ( enforceNamespace , options . Recursive , options . Filenames ... ) .
2015-04-11 06:06:05 +00:00
ResourceTypeOrNameArgs ( false , args ... ) .
Flatten ( ) .
Do ( )
err = r . Err ( )
if err != nil {
return err
}
infos , err := r . Infos ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
2015-08-11 09:05:28 +00:00
info := infos [ 0 ]
mapping := info . ResourceMapping ( )
2015-05-21 21:10:25 +00:00
scaler , err := f . Scaler ( mapping )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
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
}
2015-04-07 18:21:25 +00:00
currentSize := cmdutil . GetFlagInt ( cmd , "current-replicas" )
2015-06-29 09:37:11 +00:00
if currentSize != - 1 && len ( infos ) > 1 {
2015-11-13 12:44:03 +00:00
return fmt . Errorf ( "cannot use --current-replicas with multiple resources" )
2015-06-29 09:37:11 +00:00
}
2015-08-08 01:52:23 +00:00
precondition := & kubectl . ScalePrecondition { Size : currentSize , ResourceVersion : resourceVersion }
2015-05-19 14:07:58 +00:00
retry := kubectl . NewRetryParams ( kubectl . Interval , kubectl . Timeout )
2015-06-29 09:37:11 +00:00
var waitForReplicas * kubectl . RetryParams
if timeout := cmdutil . GetFlagDuration ( cmd , "timeout" ) ; timeout != 0 {
waitForReplicas = kubectl . NewRetryParams ( kubectl . Interval , timeout )
}
errs := [ ] error { }
for _ , info := range infos {
if err := scaler . Scale ( info . Namespace , info . Name , uint ( count ) , precondition , retry , waitForReplicas ) ; err != nil {
errs = append ( errs , err )
continue
}
2016-01-22 18:33:23 +00:00
if cmdutil . ShouldRecord ( cmd , info ) {
patchBytes , err := cmdutil . ChangeResourcePatch ( info , f . Command ( ) )
if err != nil {
errs = append ( errs , err )
continue
}
mapping := info . ResourceMapping ( )
client , err := f . ClientForMapping ( mapping )
if err != nil {
return err
}
helper := resource . NewHelper ( client , mapping )
_ , err = helper . Patch ( info . Namespace , info . Name , api . StrategicMergePatchType , patchBytes )
if err != nil {
errs = append ( errs , err )
continue
}
}
2015-06-29 09:37:11 +00:00
cmdutil . PrintSuccess ( mapper , shortOutput , out , info . Mapping . Resource , info . Name , "scaled" )
2015-03-09 22:08:16 +00:00
}
2015-06-29 09:37:11 +00:00
2015-10-14 05:18:37 +00:00
return utilerrors . NewAggregate ( errs )
2015-03-09 22:08:16 +00:00
}