From 1c85bad80fa8dc91cfb7ff3a62928784146f4dbb Mon Sep 17 00:00:00 2001 From: yipeng1030 <58055769+yipeng1030@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:25:18 +0800 Subject: [PATCH] fix: specifying namespace on ops cmds. (#490) (cherry picked from commit 0c1c1e371c3e35fa1984319f1a8d99e5b34f2dc8) --- pkg/cmd/cluster/operations.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 2d0db6577..109a25735 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -135,7 +135,7 @@ func newBaseOperationsOptions(f cmdutil.Factory, streams genericiooptions.IOStre HasComponentNamesFlag: hasComponentNamesFlag, AutoApprove: false, CreateOptions: action.CreateOptions{ - Factory: getFactory(f), + Factory: f, IOStreams: streams, CueTemplateName: "cluster_operations_template.cue", GVR: types.OpsGVR(), @@ -148,18 +148,6 @@ func newBaseOperationsOptions(f cmdutil.Factory, streams genericiooptions.IOStre return o } -// getFactory get a new factory when given factory isn't a TestFactory. -func getFactory(f cmdutil.Factory) cmdutil.Factory { - if factory, ok := f.(*testing.TestFactory); ok { - return factory - } else { - kubeConfigFlags := genericclioptions.NewConfigFlags(true) - matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) - newFactory := cmdutil.NewFactory(matchVersionKubeConfigFlags) - return newFactory - } -} - // addCommonFlags adds common flags for operations command func (o *OperationsOptions) addCommonFlags(cmd *cobra.Command, f cmdutil.Factory) { o.AddCommonFlags(cmd) @@ -1083,8 +1071,20 @@ func buildCustomOpsExamples(t unstructured.Unstructured) string { return templates.Examples(baseCommand) } +// getTempFactory get a new factory when given factory isn't a TestFactory. +func getTempFactory(f cmdutil.Factory) cmdutil.Factory { + if factory, ok := f.(*testing.TestFactory); ok { + return factory + } else { + kubeConfigFlags := genericclioptions.NewConfigFlags(true) + matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) + newFactory := cmdutil.NewFactory(matchVersionKubeConfigFlags) + return newFactory + } +} + func buildCustomOpsCmds(option *OperationsOptions) []*cobra.Command { - dynamic, _ := option.Factory.DynamicClient() + dynamic, _ := getTempFactory(option.Factory).DynamicClient() opsDefs, _ := dynamic.Resource(types.OpsDefinitionGVR()).List(context.TODO(), metav1.ListOptions{}) if opsDefs == nil { return nil