diff --git a/exp/addons/internal/controllers/clusterresourceset_controller.go b/exp/addons/internal/controllers/clusterresourceset_controller.go index 336ceaf082a4..8167bee4a9b8 100644 --- a/exp/addons/internal/controllers/clusterresourceset_controller.go +++ b/exp/addons/internal/controllers/clusterresourceset_controller.go @@ -264,6 +264,9 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte } }() + // Ensure that the owner references are set on the ClusterResourceSetBinding. + clusterResourceSetBinding.OwnerReferences = ensureOwnerRefs(clusterResourceSetBinding, clusterResourceSet, cluster) + errList := []error{} resourceSetBinding := clusterResourceSetBinding.GetOrCreateBinding(clusterResourceSet) diff --git a/exp/addons/internal/controllers/clusterresourceset_helpers.go b/exp/addons/internal/controllers/clusterresourceset_helpers.go index a45c8fbddc86..0fee1dc263ca 100644 --- a/exp/addons/internal/controllers/clusterresourceset_helpers.go +++ b/exp/addons/internal/controllers/clusterresourceset_helpers.go @@ -123,13 +123,8 @@ func (r *ClusterResourceSetReconciler) getOrCreateClusterResourceSetBinding(ctx clusterResourceSetBinding.Name = cluster.Name clusterResourceSetBinding.Namespace = cluster.Namespace - clusterResourceSetBinding.OwnerReferences = util.EnsureOwnerRef(clusterResourceSetBinding.OwnerReferences, metav1.OwnerReference{ - APIVersion: clusterv1.GroupVersion.String(), - Kind: "Cluster", - Name: cluster.Name, - UID: cluster.UID, - }) - clusterResourceSetBinding.OwnerReferences = util.EnsureOwnerRef(clusterResourceSetBinding.OwnerReferences, *metav1.NewControllerRef(clusterResourceSet, clusterResourceSet.GroupVersionKind())) + + clusterResourceSetBinding.OwnerReferences = ensureOwnerRefs(clusterResourceSetBinding, clusterResourceSet, cluster) clusterResourceSetBinding.Spec.Bindings = []*addonsv1.ResourceSetBinding{} if err := r.Client.Create(ctx, clusterResourceSetBinding); err != nil { @@ -146,6 +141,20 @@ func (r *ClusterResourceSetReconciler) getOrCreateClusterResourceSetBinding(ctx return clusterResourceSetBinding, nil } +// ensureOwnerRefs ensures Cluster and ClusterResourceSet owner references are set on the ClusterResourceSetBinding +func ensureOwnerRefs(clusterResourceSetBinding *addonsv1.ClusterResourceSetBinding, clusterResourceSet *addonsv1.ClusterResourceSet, cluster *clusterv1.Cluster) []metav1.OwnerReference { + clusterResourceSetBinding.Name = cluster.Name + clusterResourceSetBinding.Namespace = cluster.Namespace + clusterResourceSetBinding.OwnerReferences = util.EnsureOwnerRef(clusterResourceSetBinding.OwnerReferences, metav1.OwnerReference{ + APIVersion: clusterv1.GroupVersion.String(), + Kind: "Cluster", + Name: cluster.Name, + UID: cluster.UID, + }) + clusterResourceSetBinding.OwnerReferences = util.EnsureOwnerRef(clusterResourceSetBinding.OwnerReferences, *metav1.NewControllerRef(clusterResourceSet, clusterResourceSet.GroupVersionKind())) + return clusterResourceSetBinding.OwnerReferences +} + // getConfigMap retrieves any ConfigMap from the given name and namespace. func getConfigMap(ctx context.Context, c client.Client, configmapName types.NamespacedName) (*corev1.ConfigMap, error) { configMap := &corev1.ConfigMap{}