From cdd84a28bfc2e1e7ad552de128cd1644c7deeae7 Mon Sep 17 00:00:00 2001 From: Chok Yip Lau Date: Sun, 20 Nov 2022 15:53:05 -0500 Subject: [PATCH 1/2] Removed factory from apply command flag struct --- .../src/k8s.io/kubectl/pkg/cmd/apply/apply.go | 29 +++++++++---------- .../kubectl/pkg/cmd/apply/apply_test.go | 4 +-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go index ac42ef7da48..ada4b7f4337 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -55,8 +55,6 @@ import ( // reflect the runtime requirements for the command. This structure reduces the transformation to wiring and makes // the logic itself easy to unit test type ApplyFlags struct { - Factory cmdutil.Factory - RecordFlags *genericclioptions.RecordFlags PrintFlags *genericclioptions.PrintFlags @@ -172,9 +170,8 @@ var ( ) // NewApplyFlags returns a default ApplyFlags -func NewApplyFlags(f cmdutil.Factory, streams genericclioptions.IOStreams) *ApplyFlags { +func NewApplyFlags(streams genericclioptions.IOStreams) *ApplyFlags { return &ApplyFlags{ - Factory: f, RecordFlags: genericclioptions.NewRecordFlags(), DeleteFlags: delete.NewDeleteFlags("The files that contain the configurations to apply."), PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme), @@ -188,7 +185,7 @@ func NewApplyFlags(f cmdutil.Factory, streams genericclioptions.IOStreams) *Appl // NewCmdApply creates the `apply` command func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { - flags := NewApplyFlags(f, ioStreams) + flags := NewApplyFlags(ioStreams) cmd := &cobra.Command{ Use: "apply (-f FILENAME | -k DIRECTORY)", @@ -197,7 +194,7 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions Long: applyLong, Example: applyExample, Run: func(cmd *cobra.Command, args []string) { - o, err := flags.ToOptions(cmd, baseName, args) + o, err := flags.ToOptions(f, cmd, baseName, args) cmdutil.CheckErr(err) cmdutil.CheckErr(o.Validate()) cmdutil.CheckErr(o.Run()) @@ -207,9 +204,9 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions flags.AddFlags(cmd) // apply subcommands - cmd.AddCommand(NewCmdApplyViewLastApplied(flags.Factory, flags.IOStreams)) - cmd.AddCommand(NewCmdApplySetLastApplied(flags.Factory, flags.IOStreams)) - cmd.AddCommand(NewCmdApplyEditLastApplied(flags.Factory, flags.IOStreams)) + cmd.AddCommand(NewCmdApplyViewLastApplied(f, flags.IOStreams)) + cmd.AddCommand(NewCmdApplySetLastApplied(f, flags.IOStreams)) + cmd.AddCommand(NewCmdApplyEditLastApplied(f, flags.IOStreams)) return cmd } @@ -237,7 +234,7 @@ func (flags *ApplyFlags) AddFlags(cmd *cobra.Command) { } // ToOptions converts from CLI inputs to runtime inputs -func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []string) (*ApplyOptions, error) { +func (flags *ApplyFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, baseName string, args []string) (*ApplyOptions, error) { if len(args) != 0 { return nil, cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args) } @@ -249,7 +246,7 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s return nil, err } - dynamicClient, err := flags.Factory.DynamicClient() + dynamicClient, err := f.DynamicClient() if err != nil { return nil, err } @@ -279,23 +276,23 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s return nil, err } - openAPISchema, _ := flags.Factory.OpenAPISchema() + openAPISchema, _ := f.OpenAPISchema() validationDirective, err := cmdutil.GetValidationDirective(cmd) if err != nil { return nil, err } - validator, err := flags.Factory.Validator(validationDirective) + validator, err := f.Validator(validationDirective, fieldValidationVerifier) if err != nil { return nil, err } - builder := flags.Factory.NewBuilder() - mapper, err := flags.Factory.ToRESTMapper() + builder := f.NewBuilder() + mapper, err := f.ToRESTMapper() if err != nil { return nil, err } - namespace, enforceNamespace, err := flags.Factory.ToRawKubeConfigLoader().Namespace() + namespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace() if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go index 1ffcbf965ef..b0cf1fd6972 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go @@ -151,13 +151,13 @@ func TestApplyFlagValidation(t *testing.T) { for _, test := range tests { cmd := &cobra.Command{} - flags := NewApplyFlags(f, genericclioptions.NewTestIOStreamsDiscard()) + flags := NewApplyFlags(genericclioptions.NewTestIOStreamsDiscard()) flags.AddFlags(cmd) cmd.Flags().Set("filename", "unused") for _, arg := range test.args { cmd.Flags().Set(arg[0], arg[1]) } - o, err := flags.ToOptions(cmd, "kubectl", []string{}) + o, err := flags.ToOptions(f, cmd, "kubectl", []string{}) if err != nil { t.Fatalf("unexpected error creating apply options: %s", err) } From 65eb7f1e0c452a0ce00d9db874c54a5de2932564 Mon Sep 17 00:00:00 2001 From: Chok Yip Lau Date: Sun, 18 Dec 2022 22:58:23 -0500 Subject: [PATCH 2/2] fixed error --- staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go index ada4b7f4337..0370f0e0e21 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -282,7 +282,7 @@ func (flags *ApplyFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, baseNa if err != nil { return nil, err } - validator, err := f.Validator(validationDirective, fieldValidationVerifier) + validator, err := f.Validator(validationDirective) if err != nil { return nil, err }