From 36ff3d613b3607bac4d267b1baeeaea61e9fc5f4 Mon Sep 17 00:00:00 2001 From: mengyipeng <413183498@qq.com> Date: Tue, 19 Nov 2024 16:00:23 +0800 Subject: [PATCH 1/2] fix specifying namespace on ops cmds. --- pkg/cmd/cluster/operations.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 2d0db6577..eac7e15ea 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -22,6 +22,8 @@ package cluster import ( "context" "fmt" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/cmd/testing" "strings" appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" @@ -38,10 +40,8 @@ import ( apiruntime "k8s.io/apimachinery/pkg/runtime" apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" - "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/kube-openapi/pkg/validation/spec" - "k8s.io/kubectl/pkg/cmd/testing" cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" @@ -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 From a8ea2ae636449c1843b2102d43de6cadad7d6bc4 Mon Sep 17 00:00:00 2001 From: mengyipeng <413183498@qq.com> Date: Tue, 19 Nov 2024 16:07:01 +0800 Subject: [PATCH 2/2] fix lint --- pkg/cmd/cluster/operations.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index eac7e15ea..109a25735 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -22,8 +22,6 @@ package cluster import ( "context" "fmt" - "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/kubectl/pkg/cmd/testing" "strings" appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" @@ -40,8 +38,10 @@ import ( apiruntime "k8s.io/apimachinery/pkg/runtime" apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" + "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/kube-openapi/pkg/validation/spec" + "k8s.io/kubectl/pkg/cmd/testing" cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client"