mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-28 21:55:48 +00:00
Make kubectl commands return errors and centralize exit handling
This commit is contained in:
@@ -49,44 +49,7 @@ func (f *Factory) NewCmdRunContainer(out io.Writer) *cobra.Command {
|
||||
Long: run_long,
|
||||
Example: run_example,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
usageError(cmd, "<name> is required for run-container")
|
||||
}
|
||||
|
||||
namespace, err := f.DefaultNamespace(cmd)
|
||||
util.CheckErr(err)
|
||||
|
||||
client, err := f.Client(cmd)
|
||||
util.CheckErr(err)
|
||||
|
||||
generatorName := util.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)
|
||||
util.CheckErr(err)
|
||||
|
||||
controller, err := generator.Generate(params)
|
||||
util.CheckErr(err)
|
||||
|
||||
inline := util.GetFlagString(cmd, "overrides")
|
||||
if len(inline) > 0 {
|
||||
controller, err = util.Merge(controller, inline, "ReplicationController")
|
||||
util.CheckErr(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))
|
||||
util.CheckErr(err)
|
||||
}
|
||||
|
||||
err = f.PrintObject(cmd, controller, out)
|
||||
err := RunRunContainer(f, out, cmd, args)
|
||||
util.CheckErr(err)
|
||||
},
|
||||
}
|
||||
@@ -100,3 +63,56 @@ func (f *Factory) NewCmdRunContainer(out io.Writer) *cobra.Command {
|
||||
cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run-container.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunRunContainer(f *Factory, out io.Writer, cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return util.UsageError(cmd, "<name> is required for run-container")
|
||||
}
|
||||
|
||||
namespace, err := f.DefaultNamespace(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := f.Client(cmd)
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user