Skip to content

Commit

Permalink
addon ownerref to pass integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <[email protected]>
  • Loading branch information
haoqing0110 committed Mar 14, 2024
1 parent d6636fd commit 7caafd5
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 142 deletions.
1 change: 0 additions & 1 deletion cmd/example/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error {
),
).
WithAgentRegistrationOption(registrationOption).
// WithInstallStrategy(addonagent.InstallAllStrategy(helloworld.InstallationNamespace)).
WithAgentHealthProber(helloworld.AgentHealthProber()).
BuildTemplateAgentAddon()
if err != nil {
Expand Down
18 changes: 2 additions & 16 deletions pkg/addonfactory/addonfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ func NewAgentAddonFactory(addonName string, fs embed.FS, dir string) *AgentAddon
fs: fs,
dir: dir,
agentAddonOptions: agent.AgentAddonOptions{
AddonName: addonName,
Registration: nil,
// InstallStrategy: nil,
AddonName: addonName,
Registration: nil,
HealthProber: nil,
SupportedConfigGVRs: []schema.GroupVersionResource{},
},
Expand All @@ -78,19 +77,6 @@ func (f *AgentAddonFactory) WithGetValuesFuncs(getValuesFuncs ...GetValuesFunc)
return f
}

// WithInstallStrategy defines the installation strategy of the manifests prescribed by Manifests(..).
// Deprecated: add annotation "addon.open-cluster-management.io/lifecycle: addon-manager" to ClusterManagementAddon
// and define install strategy in ClusterManagementAddon spec.installStrategy instead.
// The migration plan refer to https://github.com/open-cluster-management-io/ocm/issues/355.
/*func (f *AgentAddonFactory) WithInstallStrategy(strategy *agent.InstallStrategy) *AgentAddonFactory {
if strategy.InstallNamespace == "" {
strategy.InstallNamespace = AddonDefaultInstallNamespace
}
f.agentAddonOptions.InstallStrategy = strategy
return f
}*/

// WithAgentRegistrationOption defines how agent is registered to the hub cluster.
func (f *AgentAddonFactory) WithAgentRegistrationOption(option *agent.RegistrationOption) *AgentAddonFactory {
f.agentAddonOptions.Registration = option
Expand Down
73 changes: 0 additions & 73 deletions pkg/agent/inteface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ type AgentAddonOptions struct {
// +optional
Registration *RegistrationOption

// InstallStrategy defines that addon should be created in which clusters.
// Addon will not be installed automatically until a ManagedClusterAddon is applied to the cluster's
// namespace if InstallStrategy is nil.
// Deprecated: use installStrategy config in ClusterManagementAddOn API instead
// The migration plan refer to https://github.com/open-cluster-management-io/ocm/issues/355.
// +optional
// InstallStrategy *InstallStrategy

// Updaters select a set of resources and define the strategies to update them.
// UpdateStrategy is Update if no Updater is defined for a resource.
// +optional
Expand Down Expand Up @@ -154,23 +146,6 @@ type RegistrationOption struct {
CSRSign CSRSignerFunc
}

// InstallStrategy is the installation strategy of the manifests prescribed by Manifests(..).
// type InstallStrategy struct {
// *installStrategy
// }

// type installStrategy struct {
// // InstallNamespace is target deploying namespace in the managed cluster upon automatic addon installation.
// InstallNamespace string
//
// // managedClusterFilter will filter the clusters to install the addon to.
// managedClusterFilter func(cluster *clusterv1.ManagedCluster) bool
// }

// func (s *InstallStrategy) GetManagedClusterFilter() func(cluster *clusterv1.ManagedCluster) bool {
// return s.managedClusterFilter
// }

type Updater struct {
// ResourceIdentifier sets what resources the strategy applies to
ResourceIdentifier workapiv1.ResourceIdentifier
Expand Down Expand Up @@ -255,54 +230,6 @@ func DefaultGroups(clusterName, addonName string) []string {
}
}

// // InstallAllStrategy indicate to install addon to all clusters
// func InstallAllStrategy(installNamespace string) *InstallStrategy {
// return &InstallStrategy{
// &installStrategy{
// InstallNamespace: installNamespace,
// managedClusterFilter: func(cluster *clusterv1.ManagedCluster) bool {
// return true
// },
// },
// }
// }

// InstallByLabelStrategy indicate to install addon based on clusters' label
// func InstallByLabelStrategy(installNamespace string, selector metav1.LabelSelector) *InstallStrategy {
// return &InstallStrategy{
// &installStrategy{
// InstallNamespace: installNamespace,
// managedClusterFilter: func(cluster *clusterv1.ManagedCluster) bool {
// selector, err := metav1.LabelSelectorAsSelector(&selector)
// if err != nil {
// klog.Warningf("labels selector is not correct: %v", err)
// return false
// }
//
// if !selector.Matches(labels.Set(cluster.Labels)) {
// return false
// }
// return true
// },
// },
// }
// }

// InstallByFilterFunctionStrategy indicate to install addon based on a filter function, and it will also install addons if the filter function is nil.
// func InstallByFilterFunctionStrategy(installNamespace string, f func(cluster *clusterv1.ManagedCluster) bool) *InstallStrategy {
// if f == nil {
// f = func(cluster *clusterv1.ManagedCluster) bool {
// return true
// }
// }
// return &InstallStrategy{
// &installStrategy{
// InstallNamespace: installNamespace,
// managedClusterFilter: f,
// },
// }
// }

// ApprovalAllCSRs returns true for all csrs.
func ApprovalAllCSRs(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn, csr *certificatesv1.CertificateSigningRequest) bool {
return true
Expand Down
20 changes: 8 additions & 12 deletions test/integration/agent_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
var managedClusterName string
var err error
var manifestWorkName string
var cma *addonapiv1alpha1.ClusterManagementAddOn
ginkgo.BeforeEach(func() {
suffix := rand.String(5)
managedClusterName = fmt.Sprintf("managedcluster-%s", suffix)
Expand All @@ -140,8 +141,8 @@ var _ = ginkgo.Describe("Agent deploy", func() {
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

cma := newClusterManagementAddon(testAddonImpl.name)
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma = newClusterManagementAddon(testAddonImpl.name)
cma, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

Expand Down Expand Up @@ -175,8 +176,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
Expand Down Expand Up @@ -270,8 +270,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
Expand Down Expand Up @@ -422,8 +421,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
Expand Down Expand Up @@ -571,8 +569,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), manifestWorkName, metav1.GetOptions{})
Expand Down Expand Up @@ -637,8 +634,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

var work *workapiv1.ManifestWork
gomega.Eventually(func() error {
Expand Down
9 changes: 5 additions & 4 deletions test/integration/agent_hook_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var jobCompleteValue = "True"

var _ = ginkgo.Describe("Agent hook deploy", func() {
var managedClusterName string
var cma *addonapiv1alpha1.ClusterManagementAddOn
var err error

ginkgo.BeforeEach(func() {
Expand All @@ -93,8 +94,8 @@ var _ = ginkgo.Describe("Agent hook deploy", func() {
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

cma := newClusterManagementAddon(testAddonImpl.name)
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma = newClusterManagementAddon(testAddonImpl.name)
cma, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
Expand Down Expand Up @@ -126,8 +127,8 @@ var _ = ginkgo.Describe("Agent hook deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

// deploy manifest is deployed
gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(managedClusterName).Get(context.Background(), fmt.Sprintf("addon-%s-deploy-0", testAddonImpl.name), metav1.GetOptions{})
Expand Down
13 changes: 5 additions & 8 deletions test/integration/agent_hosting_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
var managedClusterName, hostingClusterName string
var err error
var hostingManifestWorkName string
var cma *addonapiv1alpha1.ClusterManagementAddOn
ginkgo.BeforeEach(func() {
suffix := rand.String(5)
managedClusterName = fmt.Sprintf("managedcluster-%s", suffix)
Expand Down Expand Up @@ -110,8 +111,8 @@ var _ = ginkgo.Describe("Agent deploy", func() {
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

cma := newClusterManagementAddon(testHostedAddonImpl.name)
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma = newClusterManagementAddon(testHostedAddonImpl.name)
cma, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
Expand Down Expand Up @@ -156,9 +157,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(
context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(hostingClusterName).Get(context.Background(),
Expand Down Expand Up @@ -278,9 +277,7 @@ var _ = ginkgo.Describe("Agent deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(
context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(hostingClusterName).Get(context.Background(),
Expand Down
9 changes: 4 additions & 5 deletions test/integration/agent_hosting_hook_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var _ = ginkgo.Describe("Agent hook deploy", func() {
var err error
var hostingManifestWorkName string
var hostingJobCompleteValue = "True"
var cma *addonapiv1alpha1.ClusterManagementAddOn
ginkgo.BeforeEach(func() {
suffix := rand.String(5)
managedClusterName = fmt.Sprintf("managedcluster-%s", suffix)
Expand Down Expand Up @@ -113,8 +114,8 @@ var _ = ginkgo.Describe("Agent hook deploy", func() {
_, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

cma := newClusterManagementAddon(testHostedAddonImpl.name)
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma = newClusterManagementAddon(testHostedAddonImpl.name)
cma, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(),
cma, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
Expand Down Expand Up @@ -163,9 +164,7 @@ var _ = ginkgo.Describe("Agent hook deploy", func() {
InstallNamespace: "default",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(
context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
work, err := hubWorkClient.WorkV1().ManifestWorks(hostingClusterName).Get(context.Background(),
Expand Down
16 changes: 16 additions & 0 deletions test/integration/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"open-cluster-management.io/addon-framework/pkg/utils"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
)

Expand Down Expand Up @@ -102,6 +103,21 @@ func updateClusterManagementAddOn(ctx context.Context, new *addonapiv1alpha1.Clu
}, eventuallyTimeout, eventuallyInterval).Should(gomega.BeTrue())
}

func createManagedClusterAddOnwithOwnerRefs(namespace string, addon *addonapiv1alpha1.ManagedClusterAddOn, cma *addonapiv1alpha1.ClusterManagementAddOn) {
addon, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(namespace).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

addonCopy := addon.DeepCopy()

// This is to assume that addon-manager has already added the OwnerReferences.
owner := metav1.NewControllerRef(cma, addonapiv1alpha1.GroupVersion.WithKind("ClusterManagementAddOn"))
modified := utils.MergeOwnerRefs(&addonCopy.OwnerReferences, *owner, false)
if modified {
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(addonCopy.Namespace).Update(context.Background(), addonCopy, metav1.UpdateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
}

func updateManagedClusterAddOnStatus(ctx context.Context, new *addonapiv1alpha1.ManagedClusterAddOn) {
gomega.Eventually(func() bool {
old, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(new.Namespace).Get(context.Background(), new.Name, metav1.GetOptions{})
Expand Down
7 changes: 3 additions & 4 deletions test/integration/cluster_management_addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = ginkgo.Describe("ClusterManagementAddon", func() {

ginkgo.It("Should update config related object successfully", func() {
// Create clustermanagement addon
clusterManagementAddon := &addonapiv1alpha1.ClusterManagementAddOn{
cma := &addonapiv1alpha1.ClusterManagementAddOn{
ObjectMeta: metav1.ObjectMeta{
Name: testAddonImpl.name,
},
Expand All @@ -67,7 +67,7 @@ var _ = ginkgo.Describe("ClusterManagementAddon", func() {
},
},
}
_, err = hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(), clusterManagementAddon, metav1.CreateOptions{})
cma, err := hubAddonClient.AddonV1alpha1().ClusterManagementAddOns().Create(context.Background(), cma, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

// Create managed cluster addon
Expand All @@ -79,8 +79,7 @@ var _ = ginkgo.Describe("ClusterManagementAddon", func() {
InstallNamespace: "test",
},
}
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), addon, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createManagedClusterAddOnwithOwnerRefs(managedClusterName, addon, cma)

gomega.Eventually(func() error {
actual, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Get(context.Background(), testAddonImpl.name, metav1.GetOptions{})
Expand Down
Loading

0 comments on commit 7caafd5

Please sign in to comment.