From 52eef42e27917e2f68a333645992351c409db0f4 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Mon, 25 Mar 2024 10:25:42 +0800 Subject: [PATCH] Use install strategy in addon example and e2e (#253) Signed-off-by: Jian Qiu --- cmd/example/helloworld/main.go | 7 +++++-- cmd/example/helloworld_helm/main.go | 4 ++-- .../addon/helloworld/kustomization.yaml | 4 +++- .../resources/addon_deployment_config.yaml | 6 ++++++ .../helloworld_clustermanagementaddon.yaml | 12 +++++++++++ .../resources/managed_clusterset_binding.yaml | 6 ++++++ .../addon/helloworld/resources/placement.yaml | 10 +++++++++ examples/helloworld/helloworld.go | 21 +++++++------------ test/e2e/helloworld_helm_test.go | 6 ++---- test/e2e/helloworld_test.go | 19 ++++++++++------- 10 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 examples/deploy/addon/helloworld/resources/addon_deployment_config.yaml create mode 100644 examples/deploy/addon/helloworld/resources/managed_clusterset_binding.yaml create mode 100644 examples/deploy/addon/helloworld/resources/placement.yaml diff --git a/cmd/example/helloworld/main.go b/cmd/example/helloworld/main.go index 53fabf6ba..b933922a5 100644 --- a/cmd/example/helloworld/main.go +++ b/cmd/example/helloworld/main.go @@ -21,7 +21,6 @@ import ( "open-cluster-management.io/addon-framework/examples/helloworld_agent" "open-cluster-management.io/addon-framework/pkg/addonfactory" "open-cluster-management.io/addon-framework/pkg/addonmanager" - addonagent "open-cluster-management.io/addon-framework/pkg/agent" cmdfactory "open-cluster-management.io/addon-framework/pkg/cmd/factory" "open-cluster-management.io/addon-framework/pkg/utils" "open-cluster-management.io/addon-framework/pkg/version" @@ -109,7 +108,11 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error { ), ). WithAgentRegistrationOption(registrationOption). - WithInstallStrategy(addonagent.InstallAllStrategy(helloworld.InstallationNamespace)). + WithAgentInstallNamespace( + utils.AgentInstallNamespaceFromDeploymentConfigFunc( + utils.NewAddOnDeploymentConfigGetter(addonClient), + ), + ). WithAgentHealthProber(helloworld.AgentHealthProber()). BuildTemplateAgentAddon() if err != nil { diff --git a/cmd/example/helloworld_helm/main.go b/cmd/example/helloworld_helm/main.go index 2e416adf6..099523226 100644 --- a/cmd/example/helloworld_helm/main.go +++ b/cmd/example/helloworld_helm/main.go @@ -115,12 +115,12 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error { WithGetValuesFuncs( helloworld_helm.GetDefaultValues, addonfactory.GetAddOnDeploymentConfigValues( - addonfactory.NewAddOnDeploymentConfigGetter(addonClient), + utils.NewAddOnDeploymentConfigGetter(addonClient), addonfactory.ToAddOnNodePlacementValues, addonfactory.ToAddOnProxyConfigValues, ), addonfactory.GetAgentImageValues( - addonfactory.NewAddOnDeploymentConfigGetter(addonClient), + utils.NewAddOnDeploymentConfigGetter(addonClient), "global.imageOverrides.helloWorldHelm", helloworld.DefaultHelloWorldExampleImage, ), diff --git a/examples/deploy/addon/helloworld/kustomization.yaml b/examples/deploy/addon/helloworld/kustomization.yaml index 53f17d7fc..1b32ca87f 100644 --- a/examples/deploy/addon/helloworld/kustomization.yaml +++ b/examples/deploy/addon/helloworld/kustomization.yaml @@ -4,10 +4,12 @@ resources: - resources/cluster_role.yaml - resources/cluster_role_binding.yaml - resources/service_account.yaml +- resources/managed_clusterset_binding.yaml +- resources/placement.yaml +- resources/addon_deployment_config.yaml - resources/helloworld_clustermanagementaddon.yaml - resources/helloworld_controller.yaml - images: - name: quay.io/open-cluster-management/addon-examples newName: quay.io/open-cluster-management/addon-examples diff --git a/examples/deploy/addon/helloworld/resources/addon_deployment_config.yaml b/examples/deploy/addon/helloworld/resources/addon_deployment_config.yaml new file mode 100644 index 000000000..d29abd622 --- /dev/null +++ b/examples/deploy/addon/helloworld/resources/addon_deployment_config.yaml @@ -0,0 +1,6 @@ +apiVersion: addon.open-cluster-management.io/v1alpha1 +kind: AddOnDeploymentConfig +metadata: + name: global +spec: + agentInstallNamespace: open-cluster-management-agent-addon diff --git a/examples/deploy/addon/helloworld/resources/helloworld_clustermanagementaddon.yaml b/examples/deploy/addon/helloworld/resources/helloworld_clustermanagementaddon.yaml index ba46bb733..77bf16d68 100644 --- a/examples/deploy/addon/helloworld/resources/helloworld_clustermanagementaddon.yaml +++ b/examples/deploy/addon/helloworld/resources/helloworld_clustermanagementaddon.yaml @@ -2,6 +2,8 @@ apiVersion: addon.open-cluster-management.io/v1alpha1 kind: ClusterManagementAddOn metadata: name: helloworld + annotations: + "addon.open-cluster-management.io/lifecycle": "addon-manager" spec: addOnMeta: displayName: helloworld @@ -9,3 +11,13 @@ spec: supportedConfigs: - group: addon.open-cluster-management.io resource: addondeploymentconfigs + installStrategy: + type: Placements + placements: + - name: global + namespace: open-cluster-management + configs: + - group: addon.open-cluster-management.io + resource: addondeploymentconfigs + name: global + namespace: open-cluster-management diff --git a/examples/deploy/addon/helloworld/resources/managed_clusterset_binding.yaml b/examples/deploy/addon/helloworld/resources/managed_clusterset_binding.yaml new file mode 100644 index 000000000..7b1ce9ca7 --- /dev/null +++ b/examples/deploy/addon/helloworld/resources/managed_clusterset_binding.yaml @@ -0,0 +1,6 @@ +apiVersion: cluster.open-cluster-management.io/v1beta2 +kind: ManagedClusterSetBinding +metadata: + name: global +spec: + clusterSet: global diff --git a/examples/deploy/addon/helloworld/resources/placement.yaml b/examples/deploy/addon/helloworld/resources/placement.yaml new file mode 100644 index 000000000..da500bac2 --- /dev/null +++ b/examples/deploy/addon/helloworld/resources/placement.yaml @@ -0,0 +1,10 @@ +apiVersion: cluster.open-cluster-management.io/v1beta1 +kind: Placement +metadata: + name: global +spec: + clusterSets: + - global + predicates: + - requiredClusterSelector: + labelSelector: {} diff --git a/examples/helloworld/helloworld.go b/examples/helloworld/helloworld.go index efcb53c80..36455e195 100644 --- a/examples/helloworld/helloworld.go +++ b/examples/helloworld/helloworld.go @@ -18,7 +18,7 @@ import ( const ( DefaultHelloWorldExampleImage = "quay.io/open-cluster-management/addon-examples:latest" AddonName = "helloworld" - InstallationNamespace = "default" + InstallationNamespace = "open-cluster-management-agent-addon" ) //go:embed manifests @@ -30,16 +30,11 @@ func NewRegistrationOption(kubeConfig *rest.Config, addonName, agentName string) CSRConfigurations: agent.KubeClientSignerConfigurations(addonName, agentName), CSRApproveCheck: utils.DefaultCSRApprover(agentName), PermissionConfig: rbac.AddonRBAC(kubeConfig), - Namespace: InstallationNamespace, } } func GetDefaultValues(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) (addonfactory.Values, error) { - installNamespace := addon.Spec.InstallNamespace - if len(installNamespace) == 0 { - installNamespace = InstallationNamespace - } image := os.Getenv("EXAMPLE_IMAGE_NAME") if len(image) == 0 { @@ -47,15 +42,13 @@ func GetDefaultValues(cluster *clusterv1.ManagedCluster, } manifestConfig := struct { - KubeConfigSecret string - ClusterName string - AddonInstallNamespace string - Image string + KubeConfigSecret string + ClusterName string + Image string }{ - KubeConfigSecret: fmt.Sprintf("%s-hub-kubeconfig", addon.Name), - AddonInstallNamespace: installNamespace, - ClusterName: cluster.Name, - Image: image, + KubeConfigSecret: fmt.Sprintf("%s-hub-kubeconfig", addon.Name), + ClusterName: cluster.Name, + Image: image, } return addonfactory.StructToValues(manifestConfig), nil diff --git a/test/e2e/helloworld_helm_test.go b/test/e2e/helloworld_helm_test.go index c36d50e3a..d4f9a91d7 100644 --- a/test/e2e/helloworld_helm_test.go +++ b/test/e2e/helloworld_helm_test.go @@ -30,9 +30,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld helm addons", func() { ObjectMeta: metav1.ObjectMeta{ Name: helloWorldHelmAddonName, }, - Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{ - InstallNamespace: addonInstallNamespace, - }, + Spec: addonapiv1alpha1.ManagedClusterAddOnSpec{}, } ginkgo.BeforeEach(func() { @@ -135,7 +133,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld helm addons", func() { if err == nil { errd := hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), agentInstallNamespaceConfig, metav1.DeleteOptions{}) - if errd != nil { + if errd != nil && !errors.IsNotFound(errd) { return errd } return fmt.Errorf("ns is deleting, need re-check if namespace is not found") diff --git a/test/e2e/helloworld_test.go b/test/e2e/helloworld_test.go index 88a0f434f..2bc46d3ee 100644 --- a/test/e2e/helloworld_test.go +++ b/test/e2e/helloworld_test.go @@ -108,7 +108,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { return err } - if cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey] != addonapiv1alpha1.AddonLifecycleSelfManageAnnotationValue { + if cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey] != addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue { return fmt.Errorf("addon should have annotation, but get %v", cma.Annotations) } @@ -149,7 +149,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { gomega.Expect(err).ToNot(gomega.HaveOccurred()) gomega.Eventually(func() error { - copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps("default").Get(context.Background(), configmap.Name, metav1.GetOptions{}) + copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(context.Background(), configmap.Name, metav1.GetOptions{}) if err != nil { return err } @@ -194,7 +194,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { ginkgo.By("Make sure addon is configured") gomega.Eventually(func() error { - agentDeploy, err := hubKubeClient.AppsV1().Deployments("default").Get(context.Background(), "helloworld-agent", metav1.GetOptions{}) + agentDeploy, err := hubKubeClient.AppsV1().Deployments(addonInstallNamespace).Get(context.Background(), "helloworld-agent", metav1.GetOptions{}) if err != nil { return err } @@ -250,7 +250,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { gomega.Expect(err).ToNot(gomega.HaveOccurred()) gomega.Eventually(func() error { - copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps("default").Get(context.Background(), configmap.Name, metav1.GetOptions{}) + copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(context.Background(), configmap.Name, metav1.GetOptions{}) if err != nil { return err } @@ -295,7 +295,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { ginkgo.By("Make sure addon is configured") gomega.Eventually(func() error { - agentDeploy, err := hubKubeClient.AppsV1().Deployments("default").Get(context.Background(), "helloworld-agent", metav1.GetOptions{}) + agentDeploy, err := hubKubeClient.AppsV1().Deployments(addonInstallNamespace).Get(context.Background(), "helloworld-agent", metav1.GetOptions{}) if err != nil { return err } @@ -368,7 +368,7 @@ var _ = ginkgo.Describe("install/uninstall helloworld addons", func() { gomega.Expect(err).ToNot(gomega.HaveOccurred()) gomega.Eventually(func() error { - copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps("default").Get(context.Background(), configmap.Name, metav1.GetOptions{}) + copyiedConfig, err := hubKubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(context.Background(), configmap.Name, metav1.GetOptions{}) if err != nil { return err } @@ -441,6 +441,7 @@ func prepareAddOnDeploymentConfig(namespace string) error { NodeSelector: nodeSelector, Tolerations: tolerations, }, + AgentInstallNamespace: addonInstallNamespace, }, }, metav1.CreateOptions{}, @@ -466,7 +467,8 @@ func prepareImageOverrideAddOnDeploymentConfig(namespace string) error { Namespace: namespace, }, Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{ - Registries: registries, + Registries: registries, + AgentInstallNamespace: addonInstallNamespace, }, }, metav1.CreateOptions{}, @@ -491,7 +493,8 @@ func prepareProxyConfigAddOnDeploymentConfig(namespace string) error { Namespace: namespace, }, Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{ - ProxyConfig: proxyConfig, + ProxyConfig: proxyConfig, + AgentInstallNamespace: addonInstallNamespace, }, }, metav1.CreateOptions{},