Skip to content

Commit dc69aa6

Browse files
committed
ASOAPI: propagate Cluster spec.clusterNetwork to ManagedCluster
1 parent 7b75af6 commit dc69aa6

File tree

3 files changed

+357
-3
lines changed

3 files changed

+357
-3
lines changed

exp/controllers/azureasomanagedcontrolplane_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileNormal(ctx context.Cont
180180
return ctrl.Result{Requeue: true}, nil
181181
}
182182

183-
resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane))
183+
resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane, cluster))
184184
if err != nil {
185185
return ctrl.Result{}, err
186186
}

exp/mutators/azureasomanagedcontrolplane.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2626
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
2727
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2829
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2930
)
3031

@@ -34,7 +35,7 @@ var (
3435
)
3536

3637
// SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster.
37-
func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane) ResourcesMutator {
38+
func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, cluster *clusterv1.Cluster) ResourcesMutator {
3839
return func(ctx context.Context, us []*unstructured.Unstructured) error {
3940
ctx, _, done := tele.StartSpanWithLogger(ctx, "mutators.SetManagedClusterDefaults")
4041
defer done()
@@ -57,6 +58,14 @@ func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManage
5758
return err
5859
}
5960

61+
if err := setManagedClusterServiceCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil {
62+
return err
63+
}
64+
65+
if err := setManagedClusterPodCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil {
66+
return err
67+
}
68+
6069
return nil
6170
}
6271
}
@@ -90,3 +99,69 @@ func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPl
9099
logMutation(log, setK8sVersion)
91100
return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capzK8sVersion, k8sVersionPath...)
92101
}
102+
103+
func setManagedClusterServiceCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error {
104+
_, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterServiceCIDR")
105+
defer done()
106+
107+
if cluster.Spec.ClusterNetwork == nil ||
108+
cluster.Spec.ClusterNetwork.Services == nil ||
109+
len(cluster.Spec.ClusterNetwork.Services.CIDRBlocks) == 0 {
110+
return nil
111+
}
112+
113+
capiCIDR := cluster.Spec.ClusterNetwork.Services.CIDRBlocks[0]
114+
115+
// ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural serviceCidrs field.
116+
svcCIDRPath := []string{"spec", "networkProfile", "serviceCidr"}
117+
userSvcCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), svcCIDRPath...)
118+
if err != nil {
119+
return err
120+
}
121+
setSvcCIDR := mutation{
122+
location: managedClusterPath + "." + strings.Join(svcCIDRPath, "."),
123+
val: capiCIDR,
124+
reason: fmt.Sprintf("because spec.clusterNetwork.services.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR),
125+
}
126+
if found && userSvcCIDR != capiCIDR {
127+
return Incompatible{
128+
mutation: setSvcCIDR,
129+
userVal: userSvcCIDR,
130+
}
131+
}
132+
logMutation(log, setSvcCIDR)
133+
return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, svcCIDRPath...)
134+
}
135+
136+
func setManagedClusterPodCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error {
137+
_, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterPodCIDR")
138+
defer done()
139+
140+
if cluster.Spec.ClusterNetwork == nil ||
141+
cluster.Spec.ClusterNetwork.Pods == nil ||
142+
len(cluster.Spec.ClusterNetwork.Pods.CIDRBlocks) == 0 {
143+
return nil
144+
}
145+
146+
capiCIDR := cluster.Spec.ClusterNetwork.Pods.CIDRBlocks[0]
147+
148+
// ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural podCidrs field.
149+
podCIDRPath := []string{"spec", "networkProfile", "podCidr"}
150+
userPodCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), podCIDRPath...)
151+
if err != nil {
152+
return err
153+
}
154+
setPodCIDR := mutation{
155+
location: managedClusterPath + "." + strings.Join(podCIDRPath, "."),
156+
val: capiCIDR,
157+
reason: fmt.Sprintf("because spec.clusterNetwork.pods.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR),
158+
}
159+
if found && userPodCIDR != capiCIDR {
160+
return Incompatible{
161+
mutation: setPodCIDR,
162+
userVal: userPodCIDR,
163+
}
164+
}
165+
logMutation(log, setPodCIDR)
166+
return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, podCIDRPath...)
167+
}

0 commit comments

Comments
 (0)