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"
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"
"k8s.io/kubernetes/pkg/util/errors"
2015-01-09 23:53:06 +00:00
)
2015-02-20 21:28:43 +00:00
const (
2015-05-21 21:10:25 +00:00
scale_long = ` Set a new size for a Replication Controller .
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 . `
2015-08-12 16:50:09 +00:00
scale_example = ` # Scale replication controller named ' foo ' to 3.
2015-05-21 21:10:25 +00:00
$ kubectl scale -- replicas = 3 replicationcontrollers foo
2015-01-09 23:53:06 +00:00
2015-08-12 16:50:09 +00:00
# If the replication controller named foo ' s current size is 2 , scale foo to 3.
2015-06-29 09:37:11 +00:00
$ kubectl scale -- current - replicas = 2 -- replicas = 3 replicationcontrollers foo
2015-08-12 16:50:09 +00:00
# Scale multiple replication controllers .
2015-06-29 09:37:11 +00:00
$ kubectl scale -- replicas = 5 rc / foo rc / bar `
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-02-20 21:28:43 +00:00
cmd := & cobra . Command {
2015-07-06 11:31:27 +00:00
Use : "scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT TYPE NAME" ,
2015-05-21 21:10:25 +00:00
// resize is deprecated
Aliases : [ ] string { "resize" } ,
2015-02-20 21:28:43 +00:00
Short : "Set a new size for a Replication Controller." ,
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"
err := RunScale ( f , out , cmd , args , shortOutput )
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." )
cmd . Flags ( ) . Int ( "current-replicas" , - 1 , "Precondition for current size. Requires that the current size of the replication controller 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-06-29 09:37:11 +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-03-17 15:49:35 +00:00
cmd . MarkFlagRequired ( "replicas" )
2015-07-01 22:47:43 +00:00
cmdutil . AddOutputFlagsForMutation ( cmd )
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-07-01 22:47:43 +00:00
func RunScale ( f * cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string , shortOutput bool ) 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-07-06 11:31:27 +00:00
return cmdutil . UsageError ( cmd , "--replicas=COUNT TYPE NAME" )
2015-03-09 22:08:16 +00:00
}
2015-06-26 20:49:34 +00:00
cmdNamespace , _ , 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 ( )
r := resource . NewBuilder ( mapper , typer , f . ClientMapperForCommand ( ) ) .
ContinueOnError ( ) .
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
ResourceTypeOrNameArgs ( false , args ... ) .
Flatten ( ) .
Do ( )
err = r . Err ( )
if err != nil {
return err
}
mapping , err := r . ResourceMapping ( )
if err != nil {
return err
}
infos , err := r . Infos ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
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 {
return fmt . Errorf ( "cannot use --resource-version with multiple controllers" )
}
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 {
return fmt . Errorf ( "cannot use --current-replicas with multiple controllers" )
}
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
}
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
return errors . NewAggregate ( errs )
2015-03-09 22:08:16 +00:00
}