Skip to content

Commit

Permalink
Use install strategy in addon example and e2e
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 committed Mar 21, 2024
1 parent e8778a9 commit 4444cd3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 27 deletions.
7 changes: 5 additions & 2 deletions cmd/example/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/example/helloworld_helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
4 changes: 3 additions & 1 deletion examples/deploy/addon/helloworld/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: addon.open-cluster-management.io/v1alpha1
kind: AddOnDeploymentConfig
metadata:
name: global
spec:
agentInstallNamespace: default
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ 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
description: "helloworld is an example addon created by go template"
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: cluster.open-cluster-management.io/v1beta2
kind: ManagedClusterSetBinding
metadata:
name: global
spec:
clusterSet: global
10 changes: 10 additions & 0 deletions examples/deploy/addon/helloworld/resources/placement.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: cluster.open-cluster-management.io/v1beta1
kind: Placement
metadata:
name: global
spec:
clusterSets:
- global
predicates:
- requiredClusterSelector:
labelSelector: {}
21 changes: 7 additions & 14 deletions examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,32 +30,25 @@ 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 {
image = DefaultHelloWorldExampleImage
}

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
Expand Down
6 changes: 2 additions & 4 deletions test/e2e/helloworld_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 7 additions & 4 deletions test/e2e/helloworld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -441,6 +441,7 @@ func prepareAddOnDeploymentConfig(namespace string) error {
NodeSelector: nodeSelector,
Tolerations: tolerations,
},
AgentInstallNamespace: addonInstallNamespace,
},
},
metav1.CreateOptions{},
Expand All @@ -466,7 +467,8 @@ func prepareImageOverrideAddOnDeploymentConfig(namespace string) error {
Namespace: namespace,
},
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
Registries: registries,
Registries: registries,
AgentInstallNamespace: addonInstallNamespace,
},
},
metav1.CreateOptions{},
Expand All @@ -491,7 +493,8 @@ func prepareProxyConfigAddOnDeploymentConfig(namespace string) error {
Namespace: namespace,
},
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
ProxyConfig: proxyConfig,
ProxyConfig: proxyConfig,
AgentInstallNamespace: addonInstallNamespace,
},
},
metav1.CreateOptions{},
Expand Down

0 comments on commit 4444cd3

Please sign in to comment.