2015-01-12 23:30:52 +00:00
/ *
Copyright 2014 Google Inc . All rights reserved .
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"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
2015-02-05 00:14:48 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
2015-01-12 23:30:52 +00:00
"github.com/spf13/cobra"
)
2015-02-20 21:28:43 +00:00
const (
run_long = ` Create and run a particular image , possibly replicated .
Creates a replication controller to manage the created container ( s ) . `
run_example = ` // Starts a single instance of nginx.
$ kubectl run - container nginx -- image = dockerfile / nginx
2015-01-12 23:30:52 +00:00
2015-02-20 21:28:43 +00:00
// Starts a replicated instance of nginx.
$ kubectl run - container nginx -- image = dockerfile / nginx -- replicas = 5
2015-01-12 23:30:52 +00:00
2015-02-20 21:28:43 +00:00
// Dry run. Print the corresponding API objects without creating them.
$ kubectl run - container nginx -- image = dockerfile / nginx -- dry - run
2015-01-15 00:58:12 +00:00
2015-02-20 21:28:43 +00:00
// Start a single instance of nginx, but overload the desired state with a partial set of values parsed from JSON.
$ kubectl run - container nginx -- image = dockerfile / nginx -- overrides = ' { "apiVersion" : "v1beta1" , "desiredState" : { ... } } ' `
)
2015-02-03 17:59:21 +00:00
2015-02-20 21:28:43 +00:00
func ( f * Factory ) NewCmdRunContainer ( out io . Writer ) * cobra . Command {
cmd := & cobra . Command {
2015-03-11 17:22:08 +00:00
Use : "run-container NAME --image=image [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json]" ,
2015-02-20 21:28:43 +00:00
Short : "Run a particular image on the cluster." ,
Long : run_long ,
Example : run_example ,
2015-01-12 23:30:52 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2015-03-09 22:08:16 +00:00
err := RunRunContainer ( f , out , cmd , args )
2015-03-04 00:24:29 +00:00
util . CheckErr ( err )
2015-01-12 23:30:52 +00:00
} ,
}
2015-02-05 00:14:48 +00:00
util . AddPrinterFlags ( cmd )
2015-02-03 17:59:21 +00:00
cmd . Flags ( ) . String ( "generator" , "run-container/v1" , "The name of the API generator to use. Default is 'run-container-controller/v1'." )
cmd . Flags ( ) . String ( "image" , "" , "The image for the container to run." )
cmd . Flags ( ) . IntP ( "replicas" , "r" , 1 , "Number of replicas to create for this container. Default is 1." )
cmd . Flags ( ) . Bool ( "dry-run" , false , "If true, only print the object that would be sent, without sending it." )
cmd . Flags ( ) . String ( "overrides" , "" , "An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field." )
2015-01-17 01:52:27 +00:00
cmd . Flags ( ) . Int ( "port" , - 1 , "The port that this container exposes." )
2015-01-27 18:23:28 +00:00
cmd . Flags ( ) . StringP ( "labels" , "l" , "" , "Labels to apply to the pod(s) created by this call to run-container." )
2015-01-12 23:30:52 +00:00
return cmd
}
2015-03-09 22:08:16 +00:00
func RunRunContainer ( f * Factory , out io . Writer , cmd * cobra . Command , args [ ] string ) error {
if len ( args ) != 1 {
2015-03-11 17:22:08 +00:00
return util . UsageError ( cmd , "NAME is required for run-container" )
2015-03-09 22:08:16 +00:00
}
2015-03-14 10:45:18 +00:00
namespace , err := f . DefaultNamespace ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
2015-03-14 10:45:18 +00:00
client , err := f . Client ( )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
generatorName := util . GetFlagString ( cmd , "generator" )
generator , found := kubectl . Generators [ generatorName ]
if ! found {
return util . UsageError ( cmd , fmt . Sprintf ( "Generator: %s not found." , generator ) )
}
names := generator . ParamNames ( )
params := kubectl . MakeParams ( cmd , names )
params [ "name" ] = args [ 0 ]
err = kubectl . ValidateParams ( names , params )
if err != nil {
return err
}
controller , err := generator . Generate ( params )
if err != nil {
return err
}
inline := util . GetFlagString ( cmd , "overrides" )
if len ( inline ) > 0 {
controller , err = util . Merge ( controller , inline , "ReplicationController" )
if err != nil {
return err
}
}
// TODO: extract this flag to a central location, when such a location exists.
if ! util . GetFlagBool ( cmd , "dry-run" ) {
controller , err = client . ReplicationControllers ( namespace ) . Create ( controller . ( * api . ReplicationController ) )
if err != nil {
return err
}
}
return f . PrintObject ( cmd , controller , out )
}