update kubectl subcommand with -k changes

pull/564/head
Jingfang Liu 2019-02-25 16:30:50 -08:00
parent 0c026bfac1
commit b38cf738d6
18 changed files with 47 additions and 22 deletions

View File

@ -208,7 +208,7 @@ func (o AnnotateOptions) Validate() error {
if o.all && len(o.fieldSelector) > 0 { if o.all && len(o.fieldSelector) > 0 {
return fmt.Errorf("cannot set --all and --field-selector at the same time") return fmt.Errorf("cannot set --all and --field-selector at the same time")
} }
if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>") return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>")
} }
if len(o.newAnnotations) < 1 && len(o.removeAnnotations) < 1 { if len(o.newAnnotations) < 1 && len(o.removeAnnotations) < 1 {

View File

@ -115,6 +115,9 @@ var (
# Apply the configuration in pod.json to a pod. # Apply the configuration in pod.json to a pod.
kubectl apply -f ./pod.json kubectl apply -f ./pod.json
# Apply resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml.
kubectl apply -k dir/
# Apply the JSON passed into stdin to a pod. # Apply the JSON passed into stdin to a pod.
cat pod.json | kubectl apply -f - cat pod.json | kubectl apply -f -
@ -152,7 +155,7 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
o.cmdBaseName = baseName o.cmdBaseName = baseName
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "apply -f FILENAME", Use: "apply (-f FILENAME | -k DIRECTORY)",
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: i18n.T("Apply a configuration to a resource by filename or stdin"), Short: i18n.T("Apply a configuration to a resource by filename or stdin"),
Long: applyLong, Long: applyLong,
@ -170,7 +173,6 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
o.RecordFlags.AddFlags(cmd) o.RecordFlags.AddFlags(cmd)
o.PrintFlags.AddFlags(cmd) o.PrintFlags.AddFlags(cmd)
cmd.MarkFlagRequired("filename")
cmd.Flags().BoolVar(&o.Overwrite, "overwrite", o.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration") cmd.Flags().BoolVar(&o.Overwrite, "overwrite", o.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
cmd.Flags().BoolVar(&o.Prune, "prune", o.Prune, "Automatically delete resource objects, including the uninitialized ones, that do not appear in the configs and are created by either apply or create --save-config. Should be used with either -l or --all.") cmd.Flags().BoolVar(&o.Prune, "prune", o.Prune, "Automatically delete resource objects, including the uninitialized ones, that do not appear in the configs and are created by either apply or create --save-config. Should be used with either -l or --all.")
cmdutil.AddValidateFlags(cmd) cmdutil.AddValidateFlags(cmd)
@ -237,6 +239,10 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
return err return err
} }
o.DeleteOptions = o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams) o.DeleteOptions = o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
err = o.DeleteOptions.FilenameOptions.RequireFilenameOrKustomize()
if err != nil {
return err
}
o.OpenAPISchema, _ = f.OpenAPISchema() o.OpenAPISchema, _ = f.OpenAPISchema()
o.Validator, err = f.Validator(cmdutil.GetFlagBool(cmd, "validate")) o.Validator, err = f.Validator(cmdutil.GetFlagBool(cmd, "validate"))

View File

@ -107,13 +107,16 @@ func NewCmdReconcile(f cmdutil.Factory, streams genericclioptions.IOStreams) *co
cmd.Flags().BoolVar(&o.DryRun, "dry-run", o.DryRun, "If true, display results but do not submit changes") cmd.Flags().BoolVar(&o.DryRun, "dry-run", o.DryRun, "If true, display results but do not submit changes")
cmd.Flags().BoolVar(&o.RemoveExtraPermissions, "remove-extra-permissions", o.RemoveExtraPermissions, "If true, removes extra permissions added to roles") cmd.Flags().BoolVar(&o.RemoveExtraPermissions, "remove-extra-permissions", o.RemoveExtraPermissions, "If true, removes extra permissions added to roles")
cmd.Flags().BoolVar(&o.RemoveExtraSubjects, "remove-extra-subjects", o.RemoveExtraSubjects, "If true, removes extra subjects added to rolebindings") cmd.Flags().BoolVar(&o.RemoveExtraSubjects, "remove-extra-subjects", o.RemoveExtraSubjects, "If true, removes extra subjects added to rolebindings")
cmd.MarkFlagRequired("filename")
return cmd return cmd
} }
// Complete completes all the required options // Complete completes all the required options
func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args []string) error { func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args []string) error {
if err := o.FilenameOptions.RequireFilenameOrKustomize(); err != nil {
return err
}
if len(args) > 0 { if len(args) > 0 {
return errors.New("no arguments are allowed") return errors.New("no arguments are allowed")
} }

View File

@ -104,7 +104,7 @@ func (o *CertificateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
} }
func (o *CertificateOptions) Validate() error { func (o *CertificateOptions) Validate() error {
if len(o.csrNames) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.csrNames) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("one or more CSRs must be specified as <name> or -f <filename>") return fmt.Errorf("one or more CSRs must be specified as <name> or -f <filename>")
} }
return nil return nil

View File

@ -107,12 +107,15 @@ func NewCmdConvert(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *co
cmdutil.AddValidateFlags(cmd) cmdutil.AddValidateFlags(cmd)
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "to need to get converted.") cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "to need to get converted.")
cmd.MarkFlagRequired("filename")
return cmd return cmd
} }
// Complete collects information required to run Convert command from command line. // Complete collects information required to run Convert command from command line.
func (o *ConvertOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) (err error) { func (o *ConvertOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) (err error) {
err = o.FilenameOptions.RequireFilenameOrKustomize()
if err != nil {
return err
}
o.builder = f.NewBuilder o.builder = f.NewBuilder
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace() o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()

View File

@ -103,7 +103,8 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cob
Long: createLong, Long: createLong,
Example: createExample, Example: createExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) { if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut) defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
defaultRunFunc(cmd, args) defaultRunFunc(cmd, args)
return return
@ -119,7 +120,6 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cob
usage := "to use to create the resource" usage := "to use to create the resource"
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage) cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
cmd.MarkFlagRequired("filename")
cmdutil.AddValidateFlags(cmd) cmdutil.AddValidateFlags(cmd)
cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating") cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating")
cmd.Flags().Bool("windows-line-endings", runtime.GOOS == "windows", cmd.Flags().Bool("windows-line-endings", runtime.GOOS == "windows",
@ -184,7 +184,6 @@ func (o *CreateOptions) ValidateArgs(cmd *cobra.Command, args []string) error {
// Complete completes all the required options // Complete completes all the required options
func (o *CreateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error { func (o *CreateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
var err error var err error
o.RecordFlags.Complete(cmd) o.RecordFlags.Complete(cmd)
o.Recorder, err = o.RecordFlags.ToRecorder() o.Recorder, err = o.RecordFlags.ToRecorder()
if err != nil { if err != nil {

View File

@ -71,6 +71,9 @@ var (
# Delete a pod using the type and name specified in pod.json. # Delete a pod using the type and name specified in pod.json.
kubectl delete -f ./pod.json kubectl delete -f ./pod.json
# Delete resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml.
kubectl delete -k dir
# Delete a pod based on the type and name in the JSON passed into stdin. # Delete a pod based on the type and name in the JSON passed into stdin.
cat pod.json | kubectl delete -f - cat pod.json | kubectl delete -f -
@ -119,7 +122,7 @@ func NewCmdDelete(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra
deleteFlags := NewDeleteCommandFlags("containing the resource to delete.") deleteFlags := NewDeleteCommandFlags("containing the resource to delete.")
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])", Use: "delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)])",
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: i18n.T("Delete resources by filenames, stdin, resources and names, or by resources and label selector"), Short: i18n.T("Delete resources by filenames, stdin, resources and names, or by resources and label selector"),
Long: deleteLong, Long: deleteLong,

View File

@ -131,7 +131,7 @@ func (o *DescribeOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
o.EnforceNamespace = false o.EnforceNamespace = false
} }
if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) { if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
return fmt.Errorf("You must specify the type of resource to describe. %s\n", cmdutil.SuggestAPIResources(o.CmdParent)) return fmt.Errorf("You must specify the type of resource to describe. %s\n", cmdutil.SuggestAPIResources(o.CmdParent))
} }

View File

@ -118,7 +118,6 @@ func NewCmdDiff(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.C
usage := "contains the configuration to diff" usage := "contains the configuration to diff"
cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage) cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage)
cmdutil.AddServerSideApplyFlags(cmd) cmdutil.AddServerSideApplyFlags(cmd)
cmd.MarkFlagRequired("filename")
return cmd return cmd
} }
@ -395,6 +394,11 @@ func isConflict(err error) bool {
func (o *DiffOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error { func (o *DiffOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
var err error var err error
err = o.FilenameOptions.RequireFilenameOrKustomize()
if err != nil {
return err
}
o.ServerSideApply = cmdutil.GetServerSideApplyFlag(cmd) o.ServerSideApply = cmdutil.GetServerSideApplyFlag(cmd)
o.ForceConflicts = cmdutil.GetForceConflictsFlag(cmd) o.ForceConflicts = cmdutil.GetForceConflictsFlag(cmd)
if o.ForceConflicts && !o.ServerSideApply { if o.ForceConflicts && !o.ServerSideApply {

View File

@ -115,6 +115,9 @@ var (
# List a pod identified by type and name specified in "pod.yaml" in JSON output format. # List a pod identified by type and name specified in "pod.yaml" in JSON output format.
kubectl get -f pod.yaml -o json kubectl get -f pod.yaml -o json
# List resources from a directory with kustomization.yaml - e.g. dir/kustomization.yaml.
kubectl get -k dir/
# Return only the phase value of the specified pod. # Return only the phase value of the specified pod.
kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}} kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}
@ -257,7 +260,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
switch { switch {
case o.Watch || o.WatchOnly: case o.Watch || o.WatchOnly:
default: default:
if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
fmt.Fprintf(o.ErrOut, "You must specify the type of resource to get. %s\n\n", cmdutil.SuggestAPIResources(o.CmdParent)) fmt.Fprintf(o.ErrOut, "You must specify the type of resource to get. %s\n\n", cmdutil.SuggestAPIResources(o.CmdParent))
fullCmdName := cmd.Parent().CommandPath() fullCmdName := cmd.Parent().CommandPath()
usageString := "Required resource not specified." usageString := "Required resource not specified."

View File

@ -205,7 +205,7 @@ func (o *LabelOptions) Validate() error {
if o.all && len(o.fieldSelector) > 0 { if o.all && len(o.fieldSelector) > 0 {
return fmt.Errorf("cannot set --all and --field-selector at the same time") return fmt.Errorf("cannot set --all and --field-selector at the same time")
} }
if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) { if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>") return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>")
} }
if len(o.newLabels) < 1 && len(o.removeLabels) < 1 && !o.list { if len(o.newLabels) < 1 && len(o.removeLabels) < 1 && !o.list {

View File

@ -117,7 +117,6 @@ func NewCmdReplace(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobr
o.DeleteFlags.AddFlags(cmd) o.DeleteFlags.AddFlags(cmd)
o.RecordFlags.AddFlags(cmd) o.RecordFlags.AddFlags(cmd)
cmd.MarkFlagRequired("filename")
cmdutil.AddValidateFlags(cmd) cmdutil.AddValidateFlags(cmd)
cmdutil.AddApplyAnnotationFlags(cmd) cmdutil.AddApplyAnnotationFlags(cmd)
@ -163,6 +162,11 @@ func (o *ReplaceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
} }
o.DeleteOptions = deleteOpts o.DeleteOptions = deleteOpts
err = o.DeleteOptions.FilenameOptions.RequireFilenameOrKustomize()
if err != nil {
return err
}
schema, err := f.Validator(o.validate) schema, err := f.Validator(o.validate)
if err != nil { if err != nil {
return err return err
@ -189,7 +193,7 @@ func (o *ReplaceOptions) Validate(cmd *cobra.Command) error {
return fmt.Errorf("--timeout must have --force specified") return fmt.Errorf("--timeout must have --force specified")
} }
if cmdutil.IsFilenameSliceEmpty(o.DeleteOptions.FilenameOptions.Filenames) { if cmdutil.IsFilenameSliceEmpty(o.DeleteOptions.FilenameOptions.Filenames, o.DeleteOptions.FilenameOptions.Kustomize) {
return cmdutil.UsageErrorf(cmd, "Must specify --filename to replace") return cmdutil.UsageErrorf(cmd, "Must specify --filename to replace")
} }

View File

@ -123,7 +123,7 @@ func (o *RolloutHistoryOptions) Complete(f cmdutil.Factory, cmd *cobra.Command,
// Validate makes sure all the provided values for command-line options are valid // Validate makes sure all the provided values for command-line options are valid
func (o *RolloutHistoryOptions) Validate() error { func (o *RolloutHistoryOptions) Validate() error {
if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("required resource not specified") return fmt.Errorf("required resource not specified")
} }
if o.Revision < 0 { if o.Revision < 0 {

View File

@ -117,7 +117,7 @@ func (o *PauseOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
} }
func (o *PauseOptions) Validate() error { func (o *PauseOptions) Validate() error {
if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("required resource not specified") return fmt.Errorf("required resource not specified")
} }
return nil return nil

View File

@ -121,7 +121,7 @@ func (o *ResumeOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s
} }
func (o *ResumeOptions) Validate() error { func (o *ResumeOptions) Validate() error {
if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("required resource not specified") return fmt.Errorf("required resource not specified")
} }
return nil return nil

View File

@ -148,7 +148,7 @@ func (o *RolloutStatusOptions) Complete(f cmdutil.Factory, args []string) error
// Validate makes sure all the provided values for command-line options are valid // Validate makes sure all the provided values for command-line options are valid
func (o *RolloutStatusOptions) Validate() error { func (o *RolloutStatusOptions) Validate() error {
if len(o.BuilderArgs) == 0 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) { if len(o.BuilderArgs) == 0 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
return fmt.Errorf("required resource not specified") return fmt.Errorf("required resource not specified")
} }

View File

@ -126,7 +126,7 @@ func (o *UndoOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []str
} }
func (o *UndoOptions) Validate() error { func (o *UndoOptions) Validate() error {
if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("required resource not specified") return fmt.Errorf("required resource not specified")
} }
return nil return nil

View File

@ -198,7 +198,7 @@ func (o *SetImageOptions) Validate() error {
if o.All && len(o.Selector) > 0 { if o.All && len(o.Selector) > 0 {
errors = append(errors, fmt.Errorf("cannot set --all and --selector at the same time")) errors = append(errors, fmt.Errorf("cannot set --all and --selector at the same time"))
} }
if len(o.Resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames) { if len(o.Resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
errors = append(errors, fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>")) errors = append(errors, fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>"))
} }
if len(o.ContainerImages) < 1 { if len(o.ContainerImages) < 1 {