From 5ac819300515623b12126c6218707e20914e783e Mon Sep 17 00:00:00 2001 From: ycyaoxdu Date: Thu, 27 Oct 2022 10:51:29 +0000 Subject: [PATCH] error while run command against wrong context Signed-off-by: ycyaoxdu --- pkg/cmd/accept/exec.go | 14 +++-- pkg/cmd/addon/disable/exec.go | 9 +++- pkg/cmd/addon/enable/exec.go | 12 +++-- pkg/cmd/clean/exec.go | 6 +-- pkg/cmd/clusterset/bind/exec.go | 6 +++ pkg/cmd/clusterset/set/exec.go | 6 +++ pkg/cmd/clusterset/unbind/exec.go | 6 +++ pkg/cmd/create/clusterset/exec.go | 7 ++- pkg/cmd/create/sampleapp/exec.go | 4 ++ pkg/cmd/create/work/exec.go | 6 +++ pkg/cmd/delete/clusterset/exec.go | 6 +++ pkg/cmd/delete/token/exec.go | 9 ++-- pkg/cmd/delete/work/exec.go | 13 +++-- pkg/cmd/get/addon/exec.go | 5 ++ pkg/cmd/get/cluster/exec.go | 5 ++ pkg/cmd/get/clusterset/exec.go | 5 ++ pkg/cmd/get/hubinfo/exec.go | 8 ++- pkg/cmd/get/klusterletinfo/exec.go | 8 ++- pkg/cmd/get/placement/exec.go | 5 ++ pkg/cmd/get/token/exec.go | 23 ++------- pkg/cmd/get/work/exec.go | 5 ++ pkg/cmd/init/exec.go | 7 ++- pkg/cmd/init/preflight/checks.go | 2 +- pkg/cmd/install/hubaddon/exec.go | 7 ++- pkg/cmd/install/hubaddon/exec_test.go | 6 ++- pkg/cmd/install/hubaddon/suite_test.go | 10 ++++ pkg/cmd/join/exec.go | 3 +- pkg/cmd/unjoin/exec.go | 8 ++- pkg/cmd/upgrade/clustermanager/exec.go | 16 +++--- pkg/cmd/upgrade/klusterlet/exec.go | 16 +++--- pkg/genericclioptions/clusteradm_flags.go | 31 +++++++++++ pkg/helpers/check/check.go | 63 +++++++++++++++++++++++ 32 files changed, 262 insertions(+), 75 deletions(-) create mode 100644 pkg/helpers/check/check.go diff --git a/pkg/cmd/accept/exec.go b/pkg/cmd/accept/exec.go index 606501e0a..34180e227 100644 --- a/pkg/cmd/accept/exec.go +++ b/pkg/cmd/accept/exec.go @@ -7,20 +7,19 @@ import ( "strings" "time" + "github.com/spf13/cobra" + certificatesv1 "k8s.io/api/certificates/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" - "open-cluster-management.io/clusteradm/pkg/helpers" - - "github.com/spf13/cobra" - certificatesv1 "k8s.io/api/certificates/v1" - utilerrors "k8s.io/apimachinery/pkg/util/errors" clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned" + "open-cluster-management.io/clusteradm/pkg/helpers" ) const ( @@ -52,6 +51,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() error { + err := o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + return nil } diff --git a/pkg/cmd/addon/disable/exec.go b/pkg/cmd/addon/disable/exec.go index 22aa2ee9c..92e85037b 100644 --- a/pkg/cmd/addon/disable/exec.go +++ b/pkg/cmd/addon/disable/exec.go @@ -5,10 +5,9 @@ import ( "context" "fmt" - "k8s.io/apimachinery/pkg/api/errors" - "github.com/spf13/cobra" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/dynamic" @@ -26,6 +25,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Names) == 0 { return fmt.Errorf("names is missing") } @@ -37,6 +41,7 @@ func (o *Options) Validate() (err error) { if o.Allclusters && len(o.Clusters) != 0 { return fmt.Errorf("flag --all-clusters and --clusters can not be set together") } + return nil } diff --git a/pkg/cmd/addon/enable/exec.go b/pkg/cmd/addon/enable/exec.go index 3cd6856fb..63d53f38a 100644 --- a/pkg/cmd/addon/enable/exec.go +++ b/pkg/cmd/addon/enable/exec.go @@ -7,14 +7,13 @@ import ( "strings" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/klog/v2" - "github.com/stolostron/applier/pkg/apply" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" + "k8s.io/klog/v2" clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned" "open-cluster-management.io/clusteradm/pkg/cmd/addon/enable/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" @@ -52,7 +51,12 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { return nil } -func (o *Options) Validate() error { +func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Names) == 0 { return fmt.Errorf("names is missing") } diff --git a/pkg/cmd/clean/exec.go b/pkg/cmd/clean/exec.go index 22f52b3b1..8b327c1c4 100644 --- a/pkg/cmd/clean/exec.go +++ b/pkg/cmd/clean/exec.go @@ -8,11 +8,7 @@ import ( "log" "time" - clustermanagerclient "open-cluster-management.io/api/client/operator/clientset/versioned" - "open-cluster-management.io/clusteradm/pkg/helpers" - "github.com/spf13/cobra" - apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -21,6 +17,8 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/util/retry" "k8s.io/klog/v2" + clustermanagerclient "open-cluster-management.io/api/client/operator/clientset/versioned" + "open-cluster-management.io/clusteradm/pkg/helpers" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { diff --git a/pkg/cmd/clusterset/bind/exec.go b/pkg/cmd/clusterset/bind/exec.go index 7e4c5bb86..30bc28d0d 100644 --- a/pkg/cmd/clusterset/bind/exec.go +++ b/pkg/cmd/clusterset/bind/exec.go @@ -27,9 +27,15 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Namespace) == 0 { return fmt.Errorf("namespace name must be specified in --namespace") } + return nil } diff --git a/pkg/cmd/clusterset/set/exec.go b/pkg/cmd/clusterset/set/exec.go index 472952cfe..f3878aab0 100644 --- a/pkg/cmd/clusterset/set/exec.go +++ b/pkg/cmd/clusterset/set/exec.go @@ -25,9 +25,15 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Clusters) == 0 { return fmt.Errorf("cluster name must be specified in --clusters") } + return nil } diff --git a/pkg/cmd/clusterset/unbind/exec.go b/pkg/cmd/clusterset/unbind/exec.go index 4cfccaa49..810c5b900 100644 --- a/pkg/cmd/clusterset/unbind/exec.go +++ b/pkg/cmd/clusterset/unbind/exec.go @@ -26,9 +26,15 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Namespace) == 0 { return fmt.Errorf("namespace name must be specified in --namespace") } + return nil } diff --git a/pkg/cmd/create/clusterset/exec.go b/pkg/cmd/create/clusterset/exec.go index eaf267432..ed9b190f8 100644 --- a/pkg/cmd/create/clusterset/exec.go +++ b/pkg/cmd/create/clusterset/exec.go @@ -12,19 +12,24 @@ import ( ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { - o.Clustersets = args return nil } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Clustersets) == 0 { return fmt.Errorf("the name of the clusterset must be specified") } if len(o.Clustersets) > 1 { return fmt.Errorf("only one clusterset can be created") } + return nil } diff --git a/pkg/cmd/create/sampleapp/exec.go b/pkg/cmd/create/sampleapp/exec.go index 5cb6adbeb..46f481b3f 100644 --- a/pkg/cmd/create/sampleapp/exec.go +++ b/pkg/cmd/create/sampleapp/exec.go @@ -45,6 +45,10 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) Validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } return nil } diff --git a/pkg/cmd/create/work/exec.go b/pkg/cmd/create/work/exec.go index 4ce119730..4ed523648 100644 --- a/pkg/cmd/create/work/exec.go +++ b/pkg/cmd/create/work/exec.go @@ -34,6 +34,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Cluster) == 0 && len(o.Placement) == 0 { return fmt.Errorf("--clusters or --placement must be specified") } @@ -46,6 +51,7 @@ func (o *Options) validate() (err error) { if len(*o.FileNameFlags.Filenames) == 0 { return fmt.Errorf("manifest files must be specified") } + return nil } diff --git a/pkg/cmd/delete/clusterset/exec.go b/pkg/cmd/delete/clusterset/exec.go index d442066f5..8352ad006 100644 --- a/pkg/cmd/delete/clusterset/exec.go +++ b/pkg/cmd/delete/clusterset/exec.go @@ -21,12 +21,18 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Clustersets) == 0 { return fmt.Errorf("the name of the clusterset must be specified") } if len(o.Clustersets) > 1 { return fmt.Errorf("only one clusterset can be deleted") } + return nil } diff --git a/pkg/cmd/delete/token/exec.go b/pkg/cmd/delete/token/exec.go index f57232deb..1c1364177 100644 --- a/pkg/cmd/delete/token/exec.go +++ b/pkg/cmd/delete/token/exec.go @@ -5,16 +5,13 @@ import ( "context" "fmt" + "github.com/spf13/cobra" + apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - + "k8s.io/client-go/kubernetes" "open-cluster-management.io/clusteradm/pkg/config" "open-cluster-management.io/clusteradm/pkg/helpers" - - "github.com/spf13/cobra" - - apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - "k8s.io/client-go/kubernetes" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { diff --git a/pkg/cmd/delete/work/exec.go b/pkg/cmd/delete/work/exec.go index dba98c82d..a34df1626 100644 --- a/pkg/cmd/delete/work/exec.go +++ b/pkg/cmd/delete/work/exec.go @@ -6,14 +6,12 @@ import ( "fmt" "time" - "k8s.io/apimachinery/pkg/watch" - + "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/watch" workclientset "open-cluster-management.io/api/client/work/clientset/versioned" "open-cluster-management.io/clusteradm/pkg/helpers" - - "github.com/spf13/cobra" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { @@ -30,7 +28,12 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { return nil } -func (o *Options) validate() error { +func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.Cluster) == 0 { return fmt.Errorf("the name of the cluster must be specified") } diff --git a/pkg/cmd/get/addon/exec.go b/pkg/cmd/get/addon/exec.go index e055e5875..8f35ee578 100644 --- a/pkg/cmd/get/addon/exec.go +++ b/pkg/cmd/get/addon/exec.go @@ -28,6 +28,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + return nil } diff --git a/pkg/cmd/get/cluster/exec.go b/pkg/cmd/get/cluster/exec.go index 2a7dbe203..3b24e1007 100644 --- a/pkg/cmd/get/cluster/exec.go +++ b/pkg/cmd/get/cluster/exec.go @@ -21,6 +21,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate(args []string) (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(args) != 0 { return fmt.Errorf("there should be no argument") } diff --git a/pkg/cmd/get/clusterset/exec.go b/pkg/cmd/get/clusterset/exec.go index d686fef69..e0a6b545c 100644 --- a/pkg/cmd/get/clusterset/exec.go +++ b/pkg/cmd/get/clusterset/exec.go @@ -32,6 +32,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate(args []string) (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(args) != 0 { return fmt.Errorf("there should be no argument") } diff --git a/pkg/cmd/get/hubinfo/exec.go b/pkg/cmd/get/hubinfo/exec.go index 03de58c2a..860aa9442 100644 --- a/pkg/cmd/get/hubinfo/exec.go +++ b/pkg/cmd/get/hubinfo/exec.go @@ -39,10 +39,16 @@ func (o *Options) complete(cmd *cobra.Command, args []string) error { return nil } -func (o *Options) validate(args []string) error { +func (o *Options) validate(args []string) (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(args) != 0 { return fmt.Errorf("there should be no argument") } + return nil } diff --git a/pkg/cmd/get/klusterletinfo/exec.go b/pkg/cmd/get/klusterletinfo/exec.go index 449eab4d9..a09c785ae 100644 --- a/pkg/cmd/get/klusterletinfo/exec.go +++ b/pkg/cmd/get/klusterletinfo/exec.go @@ -39,10 +39,16 @@ func (o *Options) complete(cmd *cobra.Command, args []string) error { return nil } -func (o *Options) validate(args []string) error { +func (o *Options) validate(args []string) (err error) { + err = o.ClusteradmFlags.ValidateManagedCluster() + if err != nil { + return err + } + if len(args) != 0 { return fmt.Errorf("there should be no argument") } + return nil } diff --git a/pkg/cmd/get/placement/exec.go b/pkg/cmd/get/placement/exec.go index 66ab8e52b..16827a715 100644 --- a/pkg/cmd/get/placement/exec.go +++ b/pkg/cmd/get/placement/exec.go @@ -25,6 +25,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate(args []string) (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(args) > 1 { return fmt.Errorf("the number of placement name should be 0 or 1") } diff --git a/pkg/cmd/get/token/exec.go b/pkg/cmd/get/token/exec.go index 679a6119a..82290587f 100644 --- a/pkg/cmd/get/token/exec.go +++ b/pkg/cmd/get/token/exec.go @@ -5,16 +5,12 @@ import ( "context" "fmt" - "k8s.io/apimachinery/pkg/api/errors" - + "github.com/spf13/cobra" "github.com/stolostron/applier/pkg/apply" "github.com/stolostron/applier/pkg/asset" + "k8s.io/apimachinery/pkg/api/errors" "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" - - "github.com/spf13/cobra" - - apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { @@ -27,23 +23,12 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { return nil } -func (o *Options) validate() error { - restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() +func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() if err != nil { return err } - apiExtensionsClient, err := apiextensionsclient.NewForConfig(restConfig) - if err != nil { - return err - } - installed, err := helpers.IsClusterManagerInstalled(apiExtensionsClient) - if err != nil { - return err - } - if !installed { - return fmt.Errorf("this is not a hub") - } return err } diff --git a/pkg/cmd/get/work/exec.go b/pkg/cmd/get/work/exec.go index 477989902..79b779e5a 100644 --- a/pkg/cmd/get/work/exec.go +++ b/pkg/cmd/get/work/exec.go @@ -30,6 +30,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if len(o.cluster) == 0 { return fmt.Errorf("cluster name must be specified") } diff --git a/pkg/cmd/init/exec.go b/pkg/cmd/init/exec.go index f1abb9be0..8a3723b5d 100644 --- a/pkg/cmd/init/exec.go +++ b/pkg/cmd/init/exec.go @@ -6,16 +6,15 @@ import ( "fmt" "os" + "github.com/spf13/cobra" "github.com/stolostron/applier/pkg/apply" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/klog/v2" "open-cluster-management.io/clusteradm/pkg/cmd/init/preflight" "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" - helperwait "open-cluster-management.io/clusteradm/pkg/helpers/wait" - - "github.com/spf13/cobra" - "k8s.io/klog/v2" version "open-cluster-management.io/clusteradm/pkg/helpers/version" + helperwait "open-cluster-management.io/clusteradm/pkg/helpers/wait" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { diff --git a/pkg/cmd/init/preflight/checks.go b/pkg/cmd/init/preflight/checks.go index f0c847ed4..2130ef661 100644 --- a/pkg/cmd/init/preflight/checks.go +++ b/pkg/cmd/init/preflight/checks.go @@ -162,7 +162,7 @@ func createClusterInfo(client kubernetes.Interface, cluster *clientcmdapi.Cluste return CreateOrUpdateConfigMap(client, clusterInfo) } -// RunChecks runs each check, display it's warning/errors, +// RunChecks runs each check, display it's check/errors, // and once all are processed will exist if any errors occured. func RunChecks(checks []Checker, ww io.Writer) error { var errsBuffer bytes.Buffer diff --git a/pkg/cmd/install/hubaddon/exec.go b/pkg/cmd/install/hubaddon/exec.go index 13f81e820..5702ff77f 100644 --- a/pkg/cmd/install/hubaddon/exec.go +++ b/pkg/cmd/install/hubaddon/exec.go @@ -28,7 +28,12 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { return nil } -func (o *Options) validate() error { +func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } + if o.names == "" { return fmt.Errorf("names is missing") } diff --git a/pkg/cmd/install/hubaddon/exec_test.go b/pkg/cmd/install/hubaddon/exec_test.go index 76a179d35..26fc54504 100644 --- a/pkg/cmd/install/hubaddon/exec_test.go +++ b/pkg/cmd/install/hubaddon/exec_test.go @@ -27,7 +27,8 @@ var _ = ginkgo.Describe("install hub-addon", func() { ginkgo.It("Should not create any built-in add-on deployment(s) because it's not a valid add-on name", func() { o := Options{ - names: invalidAddon, + ClusteradmFlags: clusteradmFlags, + names: invalidAddon, } err := o.validate() @@ -36,7 +37,8 @@ var _ = ginkgo.Describe("install hub-addon", func() { ginkgo.It("Should not create any built-in add-on deployment(s) because it's not a valid version", func() { o := Options{ - bundleVersion: "invalid", + ClusteradmFlags: clusteradmFlags, + bundleVersion: "invalid", } err := o.validate() diff --git a/pkg/cmd/install/hubaddon/suite_test.go b/pkg/cmd/install/hubaddon/suite_test.go index 3af0ddf9a..f8ec751b3 100644 --- a/pkg/cmd/install/hubaddon/suite_test.go +++ b/pkg/cmd/install/hubaddon/suite_test.go @@ -8,9 +8,12 @@ import ( "github.com/onsi/gomega" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + cmdutil "k8s.io/kubectl/pkg/cmd/util" + genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" "sigs.k8s.io/controller-runtime/pkg/envtest" ) @@ -26,6 +29,7 @@ var restConfig *rest.Config var kubeClient kubernetes.Interface var apiExtensionsClient apiextensionsclient.Interface var dynamicClient dynamic.Interface +var clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags func TestIntegrationInstallAddons(t *testing.T) { gomega.RegisterFailHandler(ginkgo.Fail) @@ -50,6 +54,12 @@ var _ = ginkgo.BeforeSuite(func() { gomega.Expect(err).NotTo(gomega.HaveOccurred()) restConfig = cfg + + // add clusteradm flags + kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() + matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) + f := cmdutil.NewFactory(matchVersionKubeConfigFlags) + clusteradmFlags = genericclioptionsclusteradm.NewClusteradmFlags(f) }) var _ = ginkgo.AfterSuite(func() { diff --git a/pkg/cmd/join/exec.go b/pkg/cmd/join/exec.go index 27335d95f..5fa97ab09 100644 --- a/pkg/cmd/join/exec.go +++ b/pkg/cmd/join/exec.go @@ -9,6 +9,7 @@ import ( "github.com/ghodss/yaml" "github.com/spf13/cobra" + "github.com/stolostron/applier/pkg/apply" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -20,8 +21,6 @@ import ( clientcmdapiv1 "k8s.io/client-go/tools/clientcmd/api/v1" "k8s.io/klog/v2" "k8s.io/kubectl/pkg/cmd/util" - - "github.com/stolostron/applier/pkg/apply" "open-cluster-management.io/clusteradm/pkg/cmd/join/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" "open-cluster-management.io/clusteradm/pkg/helpers/printer" diff --git a/pkg/cmd/unjoin/exec.go b/pkg/cmd/unjoin/exec.go index 0453214a8..3df53b5b9 100644 --- a/pkg/cmd/unjoin/exec.go +++ b/pkg/cmd/unjoin/exec.go @@ -8,16 +8,14 @@ import ( "time" "github.com/spf13/cobra" + apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/klog/v2" - + utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/client-go/util/retry" - - apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - utilerrors "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/klog/v2" klusterletclient "open-cluster-management.io/api/client/operator/clientset/versioned" appliedworkclient "open-cluster-management.io/api/client/work/clientset/versioned" "open-cluster-management.io/clusteradm/pkg/helpers" diff --git a/pkg/cmd/upgrade/clustermanager/exec.go b/pkg/cmd/upgrade/clustermanager/exec.go index f4c6cce39..1dbd96a75 100644 --- a/pkg/cmd/upgrade/clustermanager/exec.go +++ b/pkg/cmd/upgrade/clustermanager/exec.go @@ -4,15 +4,14 @@ package clustermanager import ( "fmt" - "github.com/stolostron/applier/pkg/apply" - init_scenario "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario" - "open-cluster-management.io/clusteradm/pkg/helpers" - "open-cluster-management.io/clusteradm/pkg/helpers/wait" - "github.com/spf13/cobra" + "github.com/stolostron/applier/pkg/apply" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/klog/v2" + init_scenario "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario" + "open-cluster-management.io/clusteradm/pkg/helpers" version "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/helpers/wait" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { @@ -40,12 +39,17 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { return nil } -func (o *Options) validate() error { +func (o *Options) validate() (err error) { + err = o.ClusteradmFlags.ValidateHub() + if err != nil { + return err + } restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() if err != nil { return err } + apiExtensionsClient, err := apiextensionsclient.NewForConfig(restConfig) if err != nil { return err diff --git a/pkg/cmd/upgrade/klusterlet/exec.go b/pkg/cmd/upgrade/klusterlet/exec.go index 526420b34..88c002310 100644 --- a/pkg/cmd/upgrade/klusterlet/exec.go +++ b/pkg/cmd/upgrade/klusterlet/exec.go @@ -5,17 +5,16 @@ import ( "context" "fmt" - "github.com/stolostron/applier/pkg/apply" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - join_scenario "open-cluster-management.io/clusteradm/pkg/cmd/join/scenario" - "open-cluster-management.io/clusteradm/pkg/helpers" - "open-cluster-management.io/clusteradm/pkg/helpers/wait" - "github.com/spf13/cobra" + "github.com/stolostron/applier/pkg/apply" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" operatorclient "open-cluster-management.io/api/client/operator/clientset/versioned" + join_scenario "open-cluster-management.io/clusteradm/pkg/cmd/join/scenario" + "open-cluster-management.io/clusteradm/pkg/helpers" version "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/helpers/wait" ) //TODO add to a common folder @@ -29,6 +28,11 @@ const ( ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { + err = o.ClusteradmFlags.ValidateManagedCluster() + if err != nil { + return err + } + cfg, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() if err != nil { return err diff --git a/pkg/genericclioptions/clusteradm_flags.go b/pkg/genericclioptions/clusteradm_flags.go index 18afb3e2c..c3a91790a 100644 --- a/pkg/genericclioptions/clusteradm_flags.go +++ b/pkg/genericclioptions/clusteradm_flags.go @@ -2,8 +2,12 @@ package genericclioptions import ( + "fmt" + "github.com/spf13/pflag" cmdutil "k8s.io/kubectl/pkg/cmd/util" + clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned" + "open-cluster-management.io/clusteradm/pkg/helpers/check" ) type ClusteradmFlags struct { @@ -32,3 +36,30 @@ func (f *ClusteradmFlags) SetContext(context *string) { f.Context = *context } } + +func (f *ClusteradmFlags) ValidateHub() error { + client, err := f.buildClusterClientset() + if err != nil { + return err + } + return check.CheckForHub(client) +} +func (f *ClusteradmFlags) ValidateManagedCluster() error { + client, err := f.buildClusterClientset() + if err != nil { + return err + } + return check.CheckForManagedCluster(client) +} + +func (f *ClusteradmFlags) buildClusterClientset() (*clusterclientset.Clientset, error) { + config, err := f.KubectlFactory.ToRESTConfig() + if err != nil { + return nil, fmt.Errorf("Build ClusteradmFlags failed: %v", err) + } + client, err := clusterclientset.NewForConfig(config) + if err != nil { + return nil, fmt.Errorf("Build ClusteradmFlags failed: %v", err) + } + return client, nil +} diff --git a/pkg/helpers/check/check.go b/pkg/helpers/check/check.go new file mode 100644 index 000000000..6ea41e018 --- /dev/null +++ b/pkg/helpers/check/check.go @@ -0,0 +1,63 @@ +// Copyright Contributors to the Open Cluster Management project +package check + +import ( + "fmt" + + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clusterclient "open-cluster-management.io/api/client/cluster/clientset/versioned" + clusterv1 "open-cluster-management.io/api/cluster/v1" +) + +const ( + ManagedClusterResourceName = "managedclusters" + ClusterClaimResourceName = "clusterclaims" +) + +func CheckForHub(client clusterclient.Interface) error { + msg := "hub oriented command should not running against non-hub cluster" + + list, err := client.Discovery().ServerResourcesForGroupVersion(clusterv1.GroupVersion.String()) + if err != nil { + if errors.IsNotFound(err) { + return fmt.Errorf(msg) + + } + return fmt.Errorf("failed to list GroupVersion: %s", clusterv1.GroupVersion.String()) + + } + flag := findResource(list, ManagedClusterResourceName) + if flag { + return nil + } + return fmt.Errorf(msg) +} + +func CheckForManagedCluster(client clusterclient.Interface) error { + msg := "managed cluster oriented command should not running against non-managed cluster" + + list, err := client.Discovery().ServerResourcesForGroupVersion(clusterv1.GroupVersion.String()) + if err != nil { + if errors.IsNotFound(err) { + return fmt.Errorf(msg) + + } + return fmt.Errorf("failed to list GroupVersion: %s", clusterv1.GroupVersion.String()) + + } + flag := findResource(list, ClusterClaimResourceName) + if flag { + return nil + } + return fmt.Errorf(msg) +} + +func findResource(list *metav1.APIResourceList, resourceName string) bool { + for _, item := range list.APIResources { + if item.Name == resourceName { + return true + } + } + return false +}