Skip to content

Commit 525d5a3

Browse files
authored
fix: ClusterAutoscaler addon ownership for move (#830)
This commit changes ownership of the CA addon to be owned by the management cluster (either the bootstrap cluster or the managemen cluster) which fixes moving the resources only after the cluster exists on the target cluster.
1 parent 51910ac commit 525d5a3

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

pkg/handlers/generic/lifecycle/clusterautoscaler/strategy_crs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (s crsStrategy) apply(
119119
cm.Name,
120120
s.client,
121121
targetCluster,
122-
utils.EnsureCRSForClusterFromObjectsOptions{SetClusterOwnership: false},
122+
utils.DefaultEnsureCRSForClusterFromObjectsOptions().WithOwnerCluster(targetCluster),
123123
cm,
124124
); err != nil {
125125
return fmt.Errorf(

pkg/handlers/generic/lifecycle/clusterautoscaler/strategy_helmaddon.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1414
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1516

1617
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
1718
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
@@ -105,7 +106,16 @@ func (s helmAddonStrategy) apply(
105106

106107
// NOTE Unlike other addons, the cluster-autoscaler HelmChartProxy is created in the management cluster
107108
// namespace and thus cannot be owned by the workload cluster which will commonly exist in a different namespace.
108-
// Deletion is handled by a BeforeClusterDelete hook instead of relying on Kubernetes GC.
109+
// Ownership is set up to be owned by the management cluster so that move will work correctly but deletion is handled
110+
// by a BeforeClusterDelete hook instead of relying on Kubernetes GC.
111+
112+
if err = controllerutil.SetOwnerReference(targetCluster, hcp, s.client.Scheme()); err != nil {
113+
return fmt.Errorf(
114+
"failed to set owner reference on HelmChartProxy %q: %w",
115+
hcp.Name,
116+
err,
117+
)
118+
}
109119

110120
if err = client.ServerSideApply(ctx, s.client, hcp, client.ForceOwnership); err != nil {
111121
return fmt.Errorf("failed to apply cluster-autoscaler installation HelmChartProxy: %w", err)

pkg/handlers/utils/utils.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ import (
2222
)
2323

2424
type EnsureCRSForClusterFromObjectsOptions struct {
25-
// SetClusterOwnership will set the ownership of the CRS to the cluster when set to true
26-
SetClusterOwnership bool
25+
// OwnerCluster holds the owning cluster for the ClusterResourceSet.
26+
// This allows setting the owner to something other than the workload cluster, which is
27+
// needed specifically for the ClusterAutoscaler addon which can be deployed in a different
28+
// namespace to the target cluster as it must exist in the management cluster.
29+
OwnerCluster *clusterv1.Cluster
2730
}
2831

2932
func DefaultEnsureCRSForClusterFromObjectsOptions() EnsureCRSForClusterFromObjectsOptions {
30-
return EnsureCRSForClusterFromObjectsOptions{
31-
SetClusterOwnership: true,
32-
}
33+
return EnsureCRSForClusterFromObjectsOptions{}
34+
}
35+
36+
func (o EnsureCRSForClusterFromObjectsOptions) WithOwnerCluster(
37+
cluster *clusterv1.Cluster,
38+
) EnsureCRSForClusterFromObjectsOptions {
39+
o.OwnerCluster = cluster
40+
return o
3341
}
3442

3543
func EnsureCRSForClusterFromObjects(
@@ -83,10 +91,12 @@ func EnsureCRSForClusterFromObjects(
8391
},
8492
}
8593

86-
if opts.SetClusterOwnership {
87-
if err := controllerutil.SetOwnerReference(cluster, crs, c.Scheme()); err != nil {
88-
return fmt.Errorf("failed to set owner reference: %w", err)
89-
}
94+
ownerCluster := cluster
95+
if opts.OwnerCluster != nil {
96+
ownerCluster = opts.OwnerCluster
97+
}
98+
if err := controllerutil.SetOwnerReference(ownerCluster, crs, c.Scheme()); err != nil {
99+
return fmt.Errorf("failed to set owner reference: %w", err)
90100
}
91101

92102
err := client.ServerSideApply(ctx, c, crs, client.ForceOwnership)

0 commit comments

Comments
 (0)