diff --git a/pkg/cmd/addon/enable/cmd.go b/pkg/cmd/addon/enable/cmd.go index 28d7c0019..cef7edbe5 100644 --- a/pkg/cmd/addon/enable/cmd.go +++ b/pkg/cmd/addon/enable/cmd.go @@ -45,9 +45,9 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream }, } - cmd.Flags().StringVar(&o.names, "name", "", "Names of the add-on to deploy (comma separated)") + cmd.Flags().StringSliceVar(&o.names, "name", []string{}, "Names of the add-on to deploy (comma separated)") cmd.Flags().StringVarP(&o.namespace, "namespace", "n", "open-cluster-management-agent-addon", "Specified namespace to addon addon") - cmd.Flags().StringVar(&o.clusters, "cluster", "", "Names of the managed cluster to deploy the add-on to (comma separated)") + cmd.Flags().StringSliceVar(&o.clusters, "cluster", []string{}, "Names of the managed cluster to deploy the add-on to (comma separated)") cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file") return cmd diff --git a/pkg/cmd/addon/enable/exec.go b/pkg/cmd/addon/enable/exec.go index f5a60371a..78e72f61d 100644 --- a/pkg/cmd/addon/enable/exec.go +++ b/pkg/cmd/addon/enable/exec.go @@ -4,13 +4,13 @@ package enable import ( "context" "fmt" - "strings" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned" @@ -42,11 +42,11 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { } func (o *Options) validate() error { - if o.names == "" { + if len(o.names) == 0 { return fmt.Errorf("names is missing") } - if o.clusters == "" { + if len(o.clusters) == 0 { return fmt.Errorf("clusters is misisng") } @@ -54,25 +54,8 @@ func (o *Options) validate() error { } func (o *Options) run() error { - alreadyProvidedAddons := make(map[string]bool) - addons := make([]string, 0) - names := strings.Split(o.names, ",") - for _, n := range names { - if _, ok := alreadyProvidedAddons[n]; !ok { - alreadyProvidedAddons[n] = true - addons = append(addons, strings.TrimSpace(n)) - } - } - - alreadyProvidedClusters := make(map[string]bool) - clusters := make([]string, 0) - cs := strings.Split(o.clusters, ",") - for _, c := range cs { - if _, ok := alreadyProvidedClusters[c]; !ok { - alreadyProvidedClusters[c] = true - clusters = append(clusters, strings.TrimSpace(c)) - } - } + addons := sets.NewString(o.names...) + clusters := sets.NewString(o.clusters...) klog.V(3).InfoS("values:", "addon", addons, "clusters", clusters) @@ -90,7 +73,7 @@ func (o *Options) run() error { return err } - return o.runWithClient(clusterClient, kubeClient, apiExtensionsClient, dynamicClient, o.ClusteradmFlags.DryRun, addons, clusters) + return o.runWithClient(clusterClient, kubeClient, apiExtensionsClient, dynamicClient, o.ClusteradmFlags.DryRun, addons.List(), clusters.List()) } func (o *Options) runWithClient(clusterClient clusterclientset.Interface, diff --git a/pkg/cmd/addon/enable/exec_test.go b/pkg/cmd/addon/enable/exec_test.go index 00b83b32e..033365831 100644 --- a/pkg/cmd/addon/enable/exec_test.go +++ b/pkg/cmd/addon/enable/exec_test.go @@ -63,7 +63,7 @@ var _ = ginkgo.Describe("addon enable", func() { } addons := []string{appMgrAddonName} - clusters := []string{cluster1Name} + clusters := []string{cluster1Name, cluster1Name, cluster1Name} err := o.runWithClient(clusterClient, kubeClient, apiExtensionsClient, dynamicClient, false, addons, clusters) gomega.Expect(err).ToNot(gomega.HaveOccurred()) @@ -87,7 +87,7 @@ var _ = ginkgo.Describe("addon enable", func() { } addons := []string{appMgrAddonName} - clusters := []string{cluster1Name, cluster2Name} + clusters := []string{cluster1Name, cluster2Name, cluster1Name} err := o.runWithClient(clusterClient, kubeClient, apiExtensionsClient, dynamicClient, false, addons, clusters) gomega.Expect(err).ToNot(gomega.HaveOccurred()) diff --git a/pkg/cmd/addon/enable/options.go b/pkg/cmd/addon/enable/options.go index 004cd170a..d91bff27a 100644 --- a/pkg/cmd/addon/enable/options.go +++ b/pkg/cmd/addon/enable/options.go @@ -10,11 +10,11 @@ type Options struct { //ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime. ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags //A list of comma separated addon names - names string + names []string //The sepcified namespace for addon to install namespace string //A list of comma separated cluster names - clusters string + clusters []string //The file to output the resources will be sent to the file. outputFile string //