diff --git a/cmd/kubectl/kubectlcobra/comands.go b/cmd/kubectl/kubectlcobra/comands.go index fbe2aadfb..62492b404 100644 --- a/cmd/kubectl/kubectlcobra/comands.go +++ b/cmd/kubectl/kubectlcobra/comands.go @@ -14,6 +14,8 @@ import ( "k8s.io/kubectl/pkg/cmd/apply" "k8s.io/kubectl/pkg/cmd/diff" "k8s.io/kubectl/pkg/cmd/util" + cmdutil "k8s.io/kubectl/pkg/cmd/util" + "k8s.io/kubectl/pkg/util/i18n" "sigs.k8s.io/kustomize/kyaml/commandutil" // initialize auth @@ -58,7 +60,7 @@ func GetCommand(parent *cobra.Command) *cobra.Command { } names := []string{"apply", "diff"} - applyCmd := apply.NewCmdApply("kustomize", f, ioStreams) + applyCmd := NewCmdApply("kustomize", f, ioStreams) updateHelp(names, applyCmd) diffCmd := diff.NewCmdDiff(f, ioStreams) updateHelp(names, diffCmd) @@ -76,3 +78,40 @@ func updateHelp(names []string, c *cobra.Command) { c.Example = strings.ReplaceAll(c.Example, "kubectl "+name, "kustomize "+name) } } + +// NewCmdApply creates the `apply` command +func NewCmdApply(baseName string, f util.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { + o := apply.NewApplyOptions(ioStreams) + + cmd := &cobra.Command{ + Use: "apply (-f FILENAME | -k DIRECTORY)", + DisableFlagsInUseLine: true, + Short: i18n.T("Apply a configuration to a resource by filename or stdin"), + //Long: applyLong, + //Example: applyExample, + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + if len(args) > 0 { + // check is kustomize, if so update + o.DeleteFlags.FileNameFlags.Kustomize = &args[0] + } + + cmdutil.CheckErr(o.Complete(f, cmd)) + cmdutil.CheckErr(o.Run()) + }, + } + + // bind flag structs + o.DeleteFlags.AddFlags(cmd) + o.RecordFlags.AddFlags(cmd) + o.PrintFlags.AddFlags(cmd) + + o.Overwrite = true + + cmdutil.AddValidateFlags(cmd) + cmd.Flags().BoolVar(&o.ServerDryRun, "server-dry-run", o.ServerDryRun, "If true, request will be sent to server with dry-run flag, which means the modifications won't be persisted. This is an alpha feature and flag.") + cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, without sending it. Warning: --dry-run cannot accurately output the result of merging the local manifest and the server-side data. Use --server-dry-run to get the merged result instead.") + cmdutil.AddServerSideApplyFlags(cmd) + + return cmd +}