diff --git a/pkg/cmd/install/cmd.go b/pkg/cmd/install/cmd.go index a64ba30e6..2eece412f 100644 --- a/pkg/cmd/install/cmd.go +++ b/pkg/cmd/install/cmd.go @@ -8,7 +8,7 @@ import ( genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" ) -// NewCmd provides a cobra command wrapping NewCmdImportCluster +// NewCmd provides a cobra command wrapping addon install cmd func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "install", diff --git a/pkg/cmd/install/hubaddon/exec.go b/pkg/cmd/install/hubaddon/exec.go index 06c08a96c..c4f643a74 100644 --- a/pkg/cmd/install/hubaddon/exec.go +++ b/pkg/cmd/install/hubaddon/exec.go @@ -15,11 +15,6 @@ import ( "open-cluster-management.io/clusteradm/pkg/version" ) -const ( - appMgrAddonName = "application-manager" - policyFrameworkAddonName = "governance-policy-framework" -) - func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { klog.V(1).InfoS("addon options:", "dry-run", o.ClusteradmFlags.DryRun, "names", o.names, "output-file", o.outputFile) return nil @@ -37,12 +32,7 @@ func (o *Options) validate() (err error) { names := strings.Split(o.names, ",") for _, n := range names { - switch n { - case appMgrAddonName: - continue - case policyFrameworkAddonName: - continue - default: + if _, ok := scenario.AddonDeploymentFiles[n]; !ok { return fmt.Errorf("invalid add-on name %s", n) } } @@ -67,9 +57,9 @@ func (o *Options) run() error { addons = append(addons, strings.TrimSpace(n)) } } - o.values.hubAddons = addons + o.values.HubAddons = addons - klog.V(3).InfoS("values:", "addon", o.values.hubAddons) + klog.V(3).InfoS("values:", "addon", o.values.HubAddons) return o.runWithClient() } @@ -78,35 +68,26 @@ func (o *Options) runWithClient() error { r := reader.NewResourceReader(o.ClusteradmFlags.KubectlFactory, o.ClusteradmFlags.DryRun, o.Streams) - for _, addon := range o.values.hubAddons { - switch addon { - // Install the Application Management Addon - case appMgrAddonName: - err := r.Apply(scenario.Files, o.values, scenario.AppManagerConfigFiles...) - if err != nil { - return err - } - err = r.Apply(scenario.Files, o.values, scenario.AppManagerDeploymentFiles...) - if err != nil { - return err - } - - fmt.Fprintf(o.Streams.Out, "Installing built-in %s add-on to the Hub cluster...\n", appMgrAddonName) - - // Install the Policy Framework Addon - case policyFrameworkAddonName: - err := r.Apply(scenario.Files, o.values, scenario.PolicyFrameworkConfigFiles...) - if err != nil { - return fmt.Errorf("Error deploying framework deployment dependencies: %w", err) - } - - err = r.Apply(scenario.Files, o.values, scenario.PolicyFrameworkDeploymentFiles...) - if err != nil { - return fmt.Errorf("Error deploying framework deployments: %w", err) - } - - fmt.Fprintf(o.Streams.Out, "Installing built-in %s add-on to the Hub cluster...\n", policyFrameworkAddonName) + for _, addon := range o.values.HubAddons { + files, ok := scenario.AddonDeploymentFiles[addon] + if !ok { + continue + } + err := r.Apply(scenario.Files, o.values, files.CRDFiles...) + if err != nil { + return fmt.Errorf("Error deploying %s CRDs: %w", addon, err) } + err = r.Apply(scenario.Files, o.values, files.ConfigFiles...) + if err != nil { + return fmt.Errorf("Error deploying %s dependencies: %w", addon, err) + } + err = r.Apply(scenario.Files, o.values, files.DeploymentFiles...) + if err != nil { + return fmt.Errorf("Error deploying %s deployments: %w", addon, err) + } + + fmt.Fprintf(o.Streams.Out, "Installing built-in %s add-on to the Hub cluster...\n", addon) + } if len(o.outputFile) > 0 { diff --git a/pkg/cmd/install/hubaddon/exec_test.go b/pkg/cmd/install/hubaddon/exec_test.go index 96b5168c1..d20085334 100644 --- a/pkg/cmd/install/hubaddon/exec_test.go +++ b/pkg/cmd/install/hubaddon/exec_test.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" + "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario" "open-cluster-management.io/clusteradm/pkg/version" ) @@ -76,8 +77,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{ ClusteradmFlags: clusteradmFlags, - values: Values{ - hubAddons: []string{invalidAddon}, + values: scenario.Values{ + HubAddons: []string{invalidAddon}, }, } @@ -98,9 +99,9 @@ var _ = ginkgo.Describe("install hub-addon", func() { o := Options{ ClusteradmFlags: clusteradmFlags, bundleVersion: ocmVersion, - values: Values{ + values: scenario.Values{ Namespace: invalidNamespace, - hubAddons: []string{appMgrAddonName}, + HubAddons: []string{scenario.AppMgrAddonName}, }, Streams: genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}, } @@ -122,9 +123,9 @@ var _ = ginkgo.Describe("install hub-addon", func() { o := Options{ ClusteradmFlags: clusteradmFlags, bundleVersion: ocmVersion, - values: Values{ + values: scenario.Values{ Namespace: ocmNamespace, - hubAddons: []string{hubAddon}, + HubAddons: []string{hubAddon}, BundleVersion: ocmBundleVersion, }, Streams: genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}, diff --git a/pkg/cmd/install/hubaddon/options.go b/pkg/cmd/install/hubaddon/options.go index bbc2deec1..d3005353f 100644 --- a/pkg/cmd/install/hubaddon/options.go +++ b/pkg/cmd/install/hubaddon/options.go @@ -3,8 +3,8 @@ package hubaddon import ( "k8s.io/cli-runtime/pkg/genericclioptions" + "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario" genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" - "open-cluster-management.io/clusteradm/pkg/version" ) type Options struct { @@ -14,21 +14,12 @@ type Options struct { names string //The file to output the resources will be sent to the file. outputFile string - values Values + values scenario.Values bundleVersion string Streams genericclioptions.IOStreams } -// Values: The values used in the template -type Values struct { - hubAddons []string - // Namespace to install - Namespace string - // Version to install - BundleVersion version.VersionBundle -} - func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options { return &Options{ ClusteradmFlags: clusteradmFlags, diff --git a/pkg/cmd/install/hubaddon/scenario/resources.go b/pkg/cmd/install/hubaddon/scenario/resources.go index 681b566f5..79f9e2fd2 100644 --- a/pkg/cmd/install/hubaddon/scenario/resources.go +++ b/pkg/cmd/install/hubaddon/scenario/resources.go @@ -3,60 +3,89 @@ package scenario import ( "embed" + + "open-cluster-management.io/clusteradm/pkg/version" ) //go:embed addon var Files embed.FS -var ( - PolicyFrameworkConfigFiles = []string{ - "addon/policy/addon-controller_clusterrole.yaml", - "addon/policy/addon-controller_clusterrolebinding.yaml", - "addon/policy/addon-controller_role.yaml", - "addon/policy/addon-controller_rolebinding.yaml", - "addon/policy/addon-controller_serviceaccount.yaml", - "addon/policy/policy.open-cluster-management.io_placementbindings.yaml", - "addon/policy/policy.open-cluster-management.io_policies.yaml", - "addon/policy/policy.open-cluster-management.io_policyautomations.yaml", - "addon/policy/policy.open-cluster-management.io_policysets.yaml", - "addon/policy/propagator_clusterrole.yaml", - "addon/policy/propagator_clusterrolebinding.yaml", - "addon/policy/propagator_role.yaml", - "addon/policy/propagator_rolebinding.yaml", - "addon/policy/propagator_service.yaml", - "addon/policy/propagator_serviceaccount.yaml", - "addon/policy/clustermanagementaddon_configpolicy.yaml", - "addon/policy/clustermanagementaddon_policyframework.yaml", - "addon/appmgr/crd_placementrule.yaml", - } +const ( + AppMgrAddonName = "application-manager" + PolicyFrameworkAddonName = "governance-policy-framework" +) - PolicyFrameworkDeploymentFiles = []string{ - "addon/policy/addon-controller_deployment.yaml", - "addon/policy/propagator_deployment.yaml", - } +type AddonDeploymentFile struct { + ConfigFiles []string + DeploymentFiles []string + CRDFiles []string +} - AppManagerConfigFiles = []string{ - "addon/appmgr/clustermanagementaddon_appmgr.yaml", - "addon/appmgr/clusterrole_agent.yaml", - "addon/appmgr/clusterrole_binding.yaml", - "addon/appmgr/clusterrole.yaml", - "addon/appmgr/crd_channel.yaml", - "addon/appmgr/crd_helmrelease.yaml", - "addon/appmgr/crd_placementrule.yaml", - "addon/appmgr/crd_subscription.yaml", - "addon/appmgr/crd_subscriptionstatuses.yaml", - "addon/appmgr/crd_report.yaml", - "addon/appmgr/crd_clusterreport.yaml", - "addon/appmgr/service_account.yaml", - "addon/appmgr/service_metrics.yaml", - "addon/appmgr/service_operator.yaml", - "addon/appmgr/mutatingwebhookconfiguration.yaml", - } +// Values: The values used in the template +type Values struct { + HubAddons []string + // Namespace to install + Namespace string + // Version to install + BundleVersion version.VersionBundle +} - AppManagerDeploymentFiles = []string{ - "addon/appmgr/deployment_channel.yaml", - "addon/appmgr/deployment_subscription.yaml", - "addon/appmgr/deployment_placementrule.yaml", - "addon/appmgr/deployment_appsubsummary.yaml", +var ( + AddonDeploymentFiles = map[string]AddonDeploymentFile{ + PolicyFrameworkAddonName: { + ConfigFiles: []string{ + "addon/policy/addon-controller_clusterrole.yaml", + "addon/policy/addon-controller_clusterrolebinding.yaml", + "addon/policy/addon-controller_role.yaml", + "addon/policy/addon-controller_rolebinding.yaml", + "addon/policy/addon-controller_serviceaccount.yaml", + "addon/policy/propagator_clusterrole.yaml", + "addon/policy/propagator_clusterrolebinding.yaml", + "addon/policy/propagator_role.yaml", + "addon/policy/propagator_rolebinding.yaml", + "addon/policy/propagator_service.yaml", + "addon/policy/propagator_serviceaccount.yaml", + "addon/policy/clustermanagementaddon_configpolicy.yaml", + "addon/policy/clustermanagementaddon_policyframework.yaml", + }, + CRDFiles: []string{ + "addon/policy/policy.open-cluster-management.io_placementbindings.yaml", + "addon/policy/policy.open-cluster-management.io_policies.yaml", + "addon/policy/policy.open-cluster-management.io_policyautomations.yaml", + "addon/policy/policy.open-cluster-management.io_policysets.yaml", + "addon/appmgr/crd_placementrule.yaml", + }, + DeploymentFiles: []string{ + "addon/policy/addon-controller_deployment.yaml", + "addon/policy/propagator_deployment.yaml", + }, + }, + AppMgrAddonName: { + ConfigFiles: []string{ + "addon/appmgr/clustermanagementaddon_appmgr.yaml", + "addon/appmgr/clusterrole_agent.yaml", + "addon/appmgr/clusterrole_binding.yaml", + "addon/appmgr/clusterrole.yaml", + "addon/appmgr/service_account.yaml", + "addon/appmgr/service_metrics.yaml", + "addon/appmgr/service_operator.yaml", + "addon/appmgr/mutatingwebhookconfiguration.yaml", + }, + CRDFiles: []string{ + "addon/appmgr/crd_channel.yaml", + "addon/appmgr/crd_helmrelease.yaml", + "addon/appmgr/crd_placementrule.yaml", + "addon/appmgr/crd_subscription.yaml", + "addon/appmgr/crd_subscriptionstatuses.yaml", + "addon/appmgr/crd_report.yaml", + "addon/appmgr/crd_clusterreport.yaml", + }, + DeploymentFiles: []string{ + "addon/appmgr/deployment_channel.yaml", + "addon/appmgr/deployment_subscription.yaml", + "addon/appmgr/deployment_placementrule.yaml", + "addon/appmgr/deployment_appsubsummary.yaml", + }, + }, } ) diff --git a/pkg/cmd/uninstall/cmd.go b/pkg/cmd/uninstall/cmd.go index 45915d36e..f42f4f724 100644 --- a/pkg/cmd/uninstall/cmd.go +++ b/pkg/cmd/uninstall/cmd.go @@ -4,11 +4,11 @@ package uninstall import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" - hubaddon "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon" + "open-cluster-management.io/clusteradm/pkg/cmd/uninstall/hubaddon" genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" ) -// NewCmd provides a cobra command wrapping NewCmdImportCluster +// NewCmd provides a cobra command wrapping addon uninstall cmd func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "uninstall", diff --git a/pkg/cmd/uninstall/hubaddon/cmd.go b/pkg/cmd/uninstall/hubaddon/cmd.go index 4a4adc07d..14ee024e5 100644 --- a/pkg/cmd/uninstall/hubaddon/cmd.go +++ b/pkg/cmd/uninstall/hubaddon/cmd.go @@ -13,8 +13,8 @@ import ( var example = ` # Uninstall built-in add-ons from the hub cluster -%[1]s install hub-addon --names application-manager -%[1]s install hub-addon --names governance-policy-framework +%[1]s uninstall hub-addon --names application-manager +%[1]s uninstall hub-addon --names governance-policy-framework ` // NewCmd... @@ -23,8 +23,8 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream cmd := &cobra.Command{ Use: "hub-addon", - Short: "install hub-addon", - Long: "Install specific built-in add-on(s) to the hub cluster", + Short: "uninstall hub-addon", + Long: "Uninstall specific built-in add-on(s) to the hub cluster", Example: fmt.Sprintf(example, clusteradmhelpers.GetExampleHeader()), SilenceUsage: true, PreRunE: func(c *cobra.Command, args []string) error { @@ -47,8 +47,8 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream }, } - cmd.Flags().StringVar(&o.names, "names", "", "Names of the built-in add-on to install (comma separated). The built-in add-ons are: application-manager, governance-policy-framework") - cmd.Flags().StringVar(&o.values.Namespace, "namespace", "open-cluster-management", "Namespace of the built-in add-on to install. Defaults to open-cluster-management") + cmd.Flags().StringVar(&o.names, "names", "", "Names of the built-in add-on to uninstall (comma separated). The built-in add-ons are: application-manager, governance-policy-framework") + cmd.Flags().StringVar(&o.values.Namespace, "namespace", "open-cluster-management", "Namespace of the built-in add-on to uninstall. Defaults to open-cluster-management") return cmd } diff --git a/pkg/cmd/uninstall/hubaddon/exec.go b/pkg/cmd/uninstall/hubaddon/exec.go index 92cd326bc..f178bb3e1 100644 --- a/pkg/cmd/uninstall/hubaddon/exec.go +++ b/pkg/cmd/uninstall/hubaddon/exec.go @@ -16,11 +16,6 @@ import ( "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario" ) -const ( - appMgrAddonName = "application-manager" - policyFrameworkAddonName = "governance-policy-framework" -) - func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { klog.V(1).InfoS("addon options:", "dry-run", o.ClusteradmFlags.DryRun, "names", o.names) return nil @@ -38,12 +33,7 @@ func (o *Options) validate() (err error) { names := strings.Split(o.names, ",") for _, n := range names { - switch n { - case appMgrAddonName: - continue - case policyFrameworkAddonName: - continue - default: + if _, ok := scenario.AddonDeploymentFiles[n]; !ok { return fmt.Errorf("invalid add-on name %s", n) } } @@ -61,9 +51,9 @@ func (o *Options) run() error { addons = append(addons, strings.TrimSpace(n)) } } - o.values.hubAddons = addons + o.values.HubAddons = addons - klog.V(3).InfoS("values:", "addon", o.values.hubAddons) + klog.V(3).InfoS("values:", "addon", o.values.HubAddons) return o.runWithClient() } @@ -72,39 +62,26 @@ func (o *Options) runWithClient() error { r := reader.NewResourceReader(o.ClusteradmFlags.KubectlFactory, o.ClusteradmFlags.DryRun, o.Streams) - for _, addon := range o.values.hubAddons { + for _, addon := range o.values.HubAddons { if err := o.checkExistingAddon(addon); err != nil { return err } - switch addon { - // Install the Application Management Addon - case appMgrAddonName: - err := r.Delete(scenario.Files, o.values, scenario.AppManagerConfigFiles...) - if err != nil { - return err - } - - err = r.Delete(scenario.Files, o.values, scenario.AppManagerDeploymentFiles...) - if err != nil { - return err - } - - fmt.Fprintf(o.Streams.Out, "Uninstalling built-in %s add-on from the Hub cluster...\n", appMgrAddonName) - - // Install the Policy Framework Addon - case policyFrameworkAddonName: - err := r.Delete(scenario.Files, o.values, scenario.PolicyFrameworkConfigFiles...) - if err != nil { - return fmt.Errorf("Error deploying framework deployment dependencies: %w", err) - } - - err = r.Delete(scenario.Files, o.values, scenario.PolicyFrameworkDeploymentFiles...) - if err != nil { - return fmt.Errorf("Error deploying framework deployments: %w", err) - } - - fmt.Fprintf(o.Streams.Out, "Uninstalling built-in %s add-on from the Hub cluster...\n", policyFrameworkAddonName) + files, ok := scenario.AddonDeploymentFiles[addon] + if !ok { + continue + } + + err := r.Delete(scenario.Files, o.values, files.ConfigFiles...) + if err != nil { + return err + } + + err = r.Delete(scenario.Files, o.values, files.DeploymentFiles...) + if err != nil { + return err } + + fmt.Fprintf(o.Streams.Out, "Uninstalling built-in %s add-on from the Hub cluster...\n", addon) } return nil diff --git a/pkg/cmd/uninstall/hubaddon/options.go b/pkg/cmd/uninstall/hubaddon/options.go index 5cc34760d..87bf2d8eb 100644 --- a/pkg/cmd/uninstall/hubaddon/options.go +++ b/pkg/cmd/uninstall/hubaddon/options.go @@ -3,6 +3,7 @@ package hubaddon import ( "k8s.io/cli-runtime/pkg/genericclioptions" + "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario" genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" ) @@ -12,18 +13,11 @@ type Options struct { //A list of comma separated addon names names string //The file to output the resources will be sent to the file. - values Values + values scenario.Values Streams genericclioptions.IOStreams } -// Values: The values used in the template -type Values struct { - hubAddons []string - // Namespace to install - Namespace string -} - func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options { return &Options{ ClusteradmFlags: clusteradmFlags,