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"
)
func ( f * Factory ) NewCmdRunContainer ( out io . Writer ) * cobra . Command {
cmd := & cobra . Command {
2015-01-17 01:52:27 +00:00
Use : "run-container <name> --image=<image> [--port=<port>] [--replicas=replicas] [--dry-run=<bool>] [--overrides=<inline-json>]" ,
2015-01-12 23:30:52 +00:00
Short : "Run a particular image on the cluster." ,
Long : ` Create and run a particular image , possibly replicated .
2015-02-03 17:59:21 +00:00
Creates a replication controller to manage the created container ( s ) .
2015-01-12 23:30:52 +00:00
Examples :
2015-02-03 17:59:21 +00:00
// Starts a single instance of nginx.
2015-02-12 23:49:25 +00:00
$ kubectl run - container nginx -- image = dockerfile / nginx
2015-01-15 00:58:12 +00:00
2015-02-03 17:59:21 +00:00
// Starts a replicated instance of nginx.
2015-02-12 23:49:25 +00:00
$ kubectl run - container nginx -- image = dockerfile / nginx -- replicas = 5
2015-02-03 17:59:21 +00:00
// Dry run. Print the corresponding API objects without creating them.
2015-02-12 23:49:25 +00:00
$ kubectl run - container nginx -- image = dockerfile / nginx -- dry - run
2015-01-15 23:43:49 +00:00
2015-02-12 23:49:25 +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-01-12 23:30:52 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
if len ( args ) != 1 {
2015-01-17 01:52:27 +00:00
usageError ( cmd , "<name> is required for run-container" )
2015-01-12 23:30:52 +00:00
}
2015-01-02 18:08:37 +00:00
namespace , err := f . DefaultNamespace ( cmd )
checkErr ( err )
2015-01-12 23:30:52 +00:00
client , err := f . Client ( cmd )
checkErr ( err )
2015-02-05 00:14:48 +00:00
generatorName := util . GetFlagString ( cmd , "generator" )
2015-01-12 23:30:52 +00:00
generator , found := kubectl . Generators [ generatorName ]
if ! found {
usageError ( cmd , fmt . Sprintf ( "Generator: %s not found." , generator ) )
}
names := generator . ParamNames ( )
2015-01-14 15:11:31 +00:00
params := kubectl . MakeParams ( cmd , names )
2015-01-12 23:30:52 +00:00
params [ "name" ] = args [ 0 ]
err = kubectl . ValidateParams ( names , params )
checkErr ( err )
controller , err := generator . Generate ( params )
checkErr ( err )
2015-02-05 00:14:48 +00:00
inline := util . GetFlagString ( cmd , "overrides" )
2015-01-15 03:29:13 +00:00
if len ( inline ) > 0 {
2015-01-31 18:59:08 +00:00
controller , err = util . Merge ( controller , inline , "ReplicationController" )
checkErr ( err )
2015-01-15 03:29:13 +00:00
}
2015-01-12 23:30:52 +00:00
// TODO: extract this flag to a central location, when such a location exists.
2015-02-05 00:14:48 +00:00
if ! util . GetFlagBool ( cmd , "dry-run" ) {
2015-01-12 23:30:52 +00:00
controller , err = client . ReplicationControllers ( namespace ) . Create ( controller . ( * api . ReplicationController ) )
checkErr ( err )
}
2015-01-15 00:58:12 +00:00
2015-02-05 00:14:48 +00:00
err = f . PrintObject ( cmd , controller , out )
2015-01-15 00:58:12 +00:00
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
}