mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-01 02:38:12 +00:00
Add kubectl create [SUBCOMMAND] pattern
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
@@ -56,6 +57,10 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
Long: create_long,
|
||||
Example: create_example,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(options.Filenames) == 0 {
|
||||
cmd.Help()
|
||||
return
|
||||
}
|
||||
cmdutil.CheckErr(ValidateArgs(cmd, args))
|
||||
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
||||
cmdutil.CheckErr(RunCreate(f, cmd, out, options))
|
||||
@@ -68,6 +73,10 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmdutil.AddValidateFlags(cmd)
|
||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||
cmdutil.AddApplyAnnotationFlags(cmd)
|
||||
|
||||
// create subcommands
|
||||
cmd.AddCommand(NewCmdCreateNamespace(f, out))
|
||||
cmd.AddCommand(NewCmdCreateSecret(f, out))
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -172,3 +181,66 @@ func createAndRefresh(info *resource.Info) error {
|
||||
info.Refresh(obj, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NameFromCommandArgs is a utility function for commands that assume the first argument is a resource name
|
||||
func NameFromCommandArgs(cmd *cobra.Command, args []string) (string, error) {
|
||||
if len(args) == 0 {
|
||||
return "", cmdutil.UsageError(cmd, "NAME is required")
|
||||
}
|
||||
return args[0], nil
|
||||
}
|
||||
|
||||
// CreateSubcommandOptions is an options struct to support create subcommands
|
||||
type CreateSubcommandOptions struct {
|
||||
// Name of resource being created
|
||||
Name string
|
||||
// StructuredGenerator is the resource generator for the object being created
|
||||
StructuredGenerator kubectl.StructuredGenerator
|
||||
// DryRun is true if the command should be simulated but not run against the server
|
||||
DryRun bool
|
||||
// OutputFormat
|
||||
OutputFormat string
|
||||
}
|
||||
|
||||
// RunCreateSubcommand executes a create subcommand using the specified options
|
||||
func RunCreateSubcommand(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateSubcommandOptions) error {
|
||||
namespace, _, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
obj, err := options.StructuredGenerator.StructuredGenerate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapper, typer := f.Object()
|
||||
gvk, err := typer.ObjectKind(obj)
|
||||
mapping, err := mapper.RESTMapping(unversioned.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client, err := f.RESTClient(mapping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resourceMapper := &resource.Mapper{ObjectTyper: typer, RESTMapper: mapper, ClientMapper: f.ClientMapperForCommand()}
|
||||
info, err := resourceMapper.InfoForObject(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
|
||||
return err
|
||||
}
|
||||
if !options.DryRun {
|
||||
obj, err = resource.NewHelper(client, mapping).Create(namespace, false, info.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if useShortOutput := options.OutputFormat == "name"; useShortOutput || len(options.OutputFormat) == 0 {
|
||||
cmdutil.PrintSuccess(mapper, useShortOutput, out, mapping.Resource, options.Name, "created")
|
||||
return nil
|
||||
}
|
||||
|
||||
return f.PrintObject(cmd, obj, out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user