func Run(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { if len(os.Args) > 1 && os.Args[1] == "run-container" { printDeprecationWarning("run", "run-container") } if len(args) != 1 { return cmdutil.UsageError(cmd, "NAME is required for run") } namespace, _, err := f.DefaultNamespace() if err != nil { return err } client, err := f.Client() if err != nil { return err } generatorName := cmdutil.GetFlagString(cmd, "generator") generator, found := f.Generator(generatorName) if !found { return cmdutil.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 := cmdutil.GetFlagString(cmd, "overrides") if len(inline) > 0 { controller, err = cmdutil.Merge(controller, inline, "ReplicationController") if err != nil { return err } } // TODO: extract this flag to a central location, when such a location exists. if !cmdutil.GetFlagBool(cmd, "dry-run") { controller, err = client.ReplicationControllers(namespace).Create(controller.(*api.ReplicationController)) if err != nil { return err } } return f.PrintObject(cmd, controller, out) }
func (f *Factory) NewCmdRunContainer(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>]", Short: "Run a particular image on the cluster.", Long: `Create and run a particular image, possibly replicated. Creates a replication controller to manage the created container(s) Examples: $ kubectl run-container nginx --image=dockerfile/nginx <starts a single instance of nginx> $ kubectl run-container nginx --image=dockerfile/nginx --replicas=5 <starts a replicated instance of nginx> $ kubectl run-container nginx --image=dockerfile/nginx --dry-run <just print the corresponding API objects, don't actually send them to the apiserver>`, Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { usageError(cmd, "<name> is required for run") } namespace := GetKubeNamespace(cmd) client, err := f.Client(cmd) checkErr(err) generatorName := GetFlagString(cmd, "generator") generator, found := kubectl.Generators[generatorName] if !found { 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) checkErr(err) controller, err := generator.Generate(params) checkErr(err) // TODO: extract this flag to a central location, when such a location exists. if !GetFlagBool(cmd, "dry-run") { controller, err = client.ReplicationControllers(namespace).Create(controller.(*api.ReplicationController)) checkErr(err) } PrintObject(cmd, controller, f, out) }, } AddPrinterFlags(cmd) cmd.Flags().String("generator", "run-container/v1", "The name of the api generator that you want to use. Default 'run-container-controller-v1'") cmd.Flags().String("image", "", "The image for the container you wish to run.") cmd.Flags().IntP("replicas", "r", 1, "Number of replicas to create for this container. Default 1") cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, don't actually do anything") cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run.") return cmd }
func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { namespace, _, err := f.DefaultNamespace() if err != nil { return err } mapper, typer := f.Object() r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). ContinueOnError(). NamespaceParam(namespace).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() if err != nil { return err } if len(infos) > 1 { return fmt.Errorf("multiple resources provided: %v", args) } info := infos[0] // Get the input object client, err := f.RESTClient(mapping) if err != nil { return err } inputObject, err := resource.NewHelper(client, mapping).Get(info.Namespace, info.Name) if err != nil { return err } // Get the generator, setup and validate all required parameters generatorName := cmdutil.GetFlagString(cmd, "generator") generator, found := f.Generator(generatorName) if !found { return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generatorName)) } names := generator.ParamNames() params := kubectl.MakeParams(cmd, names) params["default-name"] = info.Name if s, found := params["selector"]; !found || len(s) == 0 || cmdutil.GetFlagInt(cmd, "port") < 1 { if len(s) == 0 { s, err := f.PodSelectorForObject(inputObject) if err != nil { return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find selectors via --selector flag or introspection: %s", err)) } params["selector"] = s } noPorts := true for _, param := range names { if param.Name == "port" { noPorts = false break } } if cmdutil.GetFlagInt(cmd, "port") < 0 && !noPorts { ports, err := f.PortsForObject(inputObject) if err != nil { return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find port via --port flag or introspection: %s", err)) } switch len(ports) { case 0: return cmdutil.UsageError(cmd, "couldn't find port via --port flag or introspection") case 1: params["port"] = ports[0] default: return cmdutil.UsageError(cmd, fmt.Sprintf("multiple ports to choose from: %v, please explicitly specify a port using the --port flag.", ports)) } } } if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") { params["create-external-load-balancer"] = "true" } if len(params["labels"]) == 0 { labels, err := f.LabelsForObject(inputObject) if err != nil { return err } params["labels"] = kubectl.MakeLabels(labels) } if v := cmdutil.GetFlagString(cmd, "type"); v != "" { params["type"] = v } err = kubectl.ValidateParams(names, params) if err != nil { return err } // Expose new object object, err := generator.Generate(params) if err != nil { return err } inline := cmdutil.GetFlagString(cmd, "overrides") if len(inline) > 0 { object, err = cmdutil.Merge(object, inline, mapping.Kind) if err != nil { return err } } // TODO: extract this flag to a central location, when such a location exists. if !cmdutil.GetFlagBool(cmd, "dry-run") { resourceMapper := &resource.Mapper{typer, mapper, f.ClientMapperForCommand()} info, err := resourceMapper.InfoForObject(object) if err != nil { return err } data, err := info.Mapping.Codec.Encode(object) if err != nil { return err } _, err = resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, data) if err != nil { return err } } return f.PrintObject(cmd, object, out) }
func RunExpose(f *Factory, out io.Writer, cmd *cobra.Command, args []string) error { if len(args) != 1 { return util.UsageError(cmd, "<name> is required for expose") } namespace, err := f.DefaultNamespace() if err != nil { return err } client, err := f.Client() 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)) } if util.GetFlagInt(cmd, "port") < 1 { return util.UsageError(cmd, "--port is required and must be a positive integer.") } names := generator.ParamNames() params := kubectl.MakeParams(cmd, names) if len(util.GetFlagString(cmd, "service-name")) == 0 { params["name"] = args[0] } else { params["name"] = util.GetFlagString(cmd, "service-name") } if _, found := params["selector"]; !found { rc, err := client.ReplicationControllers(namespace).Get(args[0]) if err != nil { return err } params["selector"] = kubectl.MakeLabels(rc.Spec.Selector) } if util.GetFlagBool(cmd, "create-external-load-balancer") { params["create-external-load-balancer"] = "true" } err = kubectl.ValidateParams(names, params) if err != nil { return err } service, err := generator.Generate(params) if err != nil { return err } inline := util.GetFlagString(cmd, "overrides") if len(inline) > 0 { service, err = util.Merge(service, inline, "Service") if err != nil { return err } } // TODO: extract this flag to a central location, when such a location exists. if !util.GetFlagBool(cmd, "dry-run") { service, err = client.Services(namespace).Create(service.(*api.Service)) if err != nil { return err } } return f.PrintObject(cmd, service, out) }
func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { var name, resource string switch l := len(args); { case l == 2: resource, name = args[0], args[1] default: return cmdutil.UsageError(cmd, "the type and name of a resource to expose are required arguments") } namespace, err := f.DefaultNamespace() if err != nil { return err } client, err := f.Client() if err != nil { return err } generatorName := cmdutil.GetFlagString(cmd, "generator") generator, found := kubectl.Generators[generatorName] if !found { return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generator)) } names := generator.ParamNames() params := kubectl.MakeParams(cmd, names) if len(cmdutil.GetFlagString(cmd, "service-name")) == 0 { params["name"] = name } else { params["name"] = cmdutil.GetFlagString(cmd, "service-name") } if s, found := params["selector"]; !found || len(s) == 0 || cmdutil.GetFlagInt(cmd, "port") < 1 { mapper, _ := f.Object() v, k, err := mapper.VersionAndKindForResource(resource) if err != nil { return err } mapping, err := mapper.RESTMapping(k, v) if err != nil { return err } if len(s) == 0 { s, err := f.PodSelectorForResource(mapping, namespace, name) if err != nil { return err } params["selector"] = s } if cmdutil.GetFlagInt(cmd, "port") < 0 { ports, err := f.PortsForResource(mapping, namespace, name) if err != nil { return err } if len(ports) == 0 { return cmdutil.UsageError(cmd, "couldn't find a suitable port via --port flag or introspection") } if len(ports) > 1 { return cmdutil.UsageError(cmd, "more than one port to choose from, please explicitly specify a port using the --port flag.") } params["port"] = ports[0] } } if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") { params["create-external-load-balancer"] = "true" } err = kubectl.ValidateParams(names, params) if err != nil { return err } service, err := generator.Generate(params) if err != nil { return err } inline := cmdutil.GetFlagString(cmd, "overrides") if len(inline) > 0 { service, err = cmdutil.Merge(service, inline, "Service") if err != nil { return err } } // TODO: extract this flag to a central location, when such a location exists. if !cmdutil.GetFlagBool(cmd, "dry-run") { service, err = client.Services(namespace).Create(service.(*api.Service)) if err != nil { return err } } return f.PrintObject(cmd, service, out) }