2015-01-12 23:30:52 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
2015-01-12 23:30:52 +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 20:53:10 +00:00
"os"
2015-01-12 23:30:52 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
2015-04-07 18:21:25 +00:00
cmdutil "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.
2015-05-21 20:53:10 +00:00
$ kubectl run nginx -- image = nginx
2015-01-12 23:30:52 +00:00
2015-02-20 21:28:43 +00:00
// Starts a replicated instance of nginx.
2015-05-21 20:53:10 +00:00
$ kubectl run nginx -- image = 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.
2015-05-21 20:53:10 +00:00
$ kubectl run nginx -- image = nginx -- dry - run
2015-01-15 00:58:12 +00:00
2015-05-18 20:29:24 +00:00
// Start a single instance of nginx, but overload the spec of the replication controller with a partial set of values parsed from JSON.
2015-06-05 19:47:15 +00:00
$ kubectl run nginx -- image = nginx -- overrides = ' { "apiVersion" : "v1" , "spec" : { ... } } ' `
2015-02-20 21:28:43 +00:00
)
2015-02-03 17:59:21 +00:00
2015-05-21 20:53:10 +00:00
func NewCmdRun ( f * cmdutil . Factory , out io . Writer ) * cobra . Command {
2015-02-20 21:28:43 +00:00
cmd := & cobra . Command {
2015-05-21 20:53:10 +00:00
Use : "run NAME --image=image [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json]" ,
// run-container is deprecated
Aliases : [ ] string { "run-container" } ,
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-05-21 20:53:10 +00:00
err := Run ( f , out , cmd , args )
2015-04-07 18:21:25 +00:00
cmdutil . CheckErr ( err )
2015-01-12 23:30:52 +00:00
} ,
}
2015-04-07 18:21:25 +00:00
cmdutil . AddPrinterFlags ( cmd )
2015-05-21 20:53:10 +00:00
cmd . Flags ( ) . String ( "generator" , "run/v1" , "The name of the API generator to use. Default is 'run-controller/v1'." )
2015-02-03 17:59:21 +00:00
cmd . Flags ( ) . String ( "image" , "" , "The image for the container to run." )
2015-03-17 15:49:35 +00:00
cmd . MarkFlagRequired ( "image" )
2015-02-03 17:59:21 +00:00
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-04-29 23:54:23 +00:00
cmd . Flags ( ) . Int ( "hostport" , - 1 , "The host port mapping for the container port. To demonstrate a single-machine container." )
2015-05-21 20:53:10 +00:00
cmd . Flags ( ) . StringP ( "labels" , "l" , "" , "Labels to apply to the pod(s)." )
2015-01-12 23:30:52 +00:00
return cmd
}
2015-03-09 22:08:16 +00:00
2015-05-21 20:53:10 +00:00
func Run ( f * cmdutil . Factory , out io . Writer , cmd * cobra . Command , args [ ] string ) error {
if os . Args [ 1 ] == "run-container" {
printDeprecationWarning ( "run" , "run-container" )
}
2015-03-09 22:08:16 +00:00
if len ( args ) != 1 {
2015-05-21 20:53:10 +00:00
return cmdutil . UsageError ( cmd , "NAME is required for run" )
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
}
2015-04-07 18:21:25 +00:00
generatorName := cmdutil . GetFlagString ( cmd , "generator" )
2015-05-05 09:15:01 +00:00
generator , found := f . Generator ( generatorName )
2015-03-09 22:08:16 +00:00
if ! found {
2015-04-07 18:21:25 +00:00
return cmdutil . UsageError ( cmd , fmt . Sprintf ( "Generator: %s not found." , generator ) )
2015-03-09 22:08:16 +00:00
}
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
}
2015-04-07 18:21:25 +00:00
inline := cmdutil . GetFlagString ( cmd , "overrides" )
2015-03-09 22:08:16 +00:00
if len ( inline ) > 0 {
2015-04-07 18:21:25 +00:00
controller , err = cmdutil . Merge ( controller , inline , "ReplicationController" )
2015-03-09 22:08:16 +00:00
if err != nil {
return err
}
}
// TODO: extract this flag to a central location, when such a location exists.
2015-04-07 18:21:25 +00:00
if ! cmdutil . GetFlagBool ( cmd , "dry-run" ) {
2015-03-09 22:08:16 +00:00
controller , err = client . ReplicationControllers ( namespace ) . Create ( controller . ( * api . ReplicationController ) )
if err != nil {
return err
}
}
return f . PrintObject ( cmd , controller , out )
}