Skip to content

Commit abba169

Browse files
authored
Merge pull request #4793 from nojnhuh/v2-mutators
ASOAPI: add resource mutator framework
2 parents d817ab3 + dc69aa6 commit abba169

7 files changed

+878
-28
lines changed

exp/controllers/azureasomanagedcluster_controller.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323

2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26-
"k8s.io/apimachinery/pkg/runtime"
2726
infracontroller "sigs.k8s.io/cluster-api-provider-azure/controllers"
2827
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
28+
"sigs.k8s.io/cluster-api-provider-azure/exp/mutators"
2929
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3030
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3131
"sigs.k8s.io/cluster-api/controllers/external"
@@ -227,11 +227,11 @@ func (r *AzureASOManagedClusterReconciler) reconcileNormal(ctx context.Context,
227227
return ctrl.Result{Requeue: true}, nil
228228
}
229229

230-
us, err := resourcesToUnstructured(asoManagedCluster.Spec.Resources)
230+
resources, err := mutators.ToUnstructured(ctx, asoManagedCluster.Spec.Resources)
231231
if err != nil {
232232
return ctrl.Result{}, err
233233
}
234-
resourceReconciler := r.newResourceReconciler(asoManagedCluster, us)
234+
resourceReconciler := r.newResourceReconciler(asoManagedCluster, resources)
235235
err = resourceReconciler.Reconcile(ctx)
236236
if err != nil {
237237
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)
@@ -279,11 +279,11 @@ func (r *AzureASOManagedClusterReconciler) reconcileDelete(ctx context.Context,
279279
defer done()
280280
log.V(4).Info("reconciling delete")
281281

282-
us, err := resourcesToUnstructured(asoManagedCluster.Spec.Resources)
282+
resources, err := mutators.ToUnstructured(ctx, asoManagedCluster.Spec.Resources)
283283
if err != nil {
284284
return ctrl.Result{}, err
285285
}
286-
resourceReconciler := r.newResourceReconciler(asoManagedCluster, us)
286+
resourceReconciler := r.newResourceReconciler(asoManagedCluster, resources)
287287
err = resourceReconciler.Delete(ctx)
288288
if err != nil {
289289
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)
@@ -295,15 +295,3 @@ func (r *AzureASOManagedClusterReconciler) reconcileDelete(ctx context.Context,
295295
controllerutil.RemoveFinalizer(asoManagedCluster, clusterv1.ClusterFinalizer)
296296
return ctrl.Result{}, nil
297297
}
298-
299-
func resourcesToUnstructured(resources []runtime.RawExtension) ([]*unstructured.Unstructured, error) {
300-
var us []*unstructured.Unstructured
301-
for _, resource := range resources {
302-
u := &unstructured.Unstructured{}
303-
if err := u.UnmarshalJSON(resource.Raw); err != nil {
304-
return nil, fmt.Errorf("failed to unmarshal resource JSON: %w", err)
305-
}
306-
us = append(us, u)
307-
}
308-
return us, nil
309-
}

exp/controllers/azureasomanagedcontrolplane_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2929
infracontroller "sigs.k8s.io/cluster-api-provider-azure/controllers"
3030
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
31+
"sigs.k8s.io/cluster-api-provider-azure/exp/mutators"
3132
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3233
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3334
"sigs.k8s.io/cluster-api/controllers/external"
@@ -179,24 +180,24 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileNormal(ctx context.Cont
179180
return ctrl.Result{Requeue: true}, nil
180181
}
181182

182-
us, err := resourcesToUnstructured(asoManagedControlPlane.Spec.Resources)
183+
resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane, cluster))
183184
if err != nil {
184185
return ctrl.Result{}, err
185186
}
186187

187188
var managedClusterName string
188-
for _, resource := range us {
189+
for _, resource := range resources {
189190
if resource.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group &&
190191
resource.GroupVersionKind().Kind == "ManagedCluster" {
191192
managedClusterName = resource.GetName()
192193
break
193194
}
194195
}
195196
if managedClusterName == "" {
196-
return ctrl.Result{}, reconcile.TerminalError(fmt.Errorf("no %s ManagedCluster defined in AzureASOManagedControlPlane spec.resources", asocontainerservicev1.GroupVersion.Group))
197+
return ctrl.Result{}, reconcile.TerminalError(mutators.ErrNoManagedClusterDefined)
197198
}
198199

199-
resourceReconciler := r.newResourceReconciler(asoManagedControlPlane, us)
200+
resourceReconciler := r.newResourceReconciler(asoManagedControlPlane, resources)
200201
err = resourceReconciler.Reconcile(ctx)
201202
if err != nil {
202203
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)
@@ -295,11 +296,11 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileDelete(ctx context.Cont
295296
defer done()
296297
log.V(4).Info("reconciling delete")
297298

298-
us, err := resourcesToUnstructured(asoManagedControlPlane.Spec.Resources)
299+
resources, err := mutators.ToUnstructured(ctx, asoManagedControlPlane.Spec.Resources)
299300
if err != nil {
300301
return ctrl.Result{}, err
301302
}
302-
resourceReconciler := r.newResourceReconciler(asoManagedControlPlane, us)
303+
resourceReconciler := r.newResourceReconciler(asoManagedControlPlane, resources)
303304
err = resourceReconciler.Delete(ctx)
304305
if err != nil {
305306
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)

exp/controllers/azureasomanagedmachinepool_controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/utils/ptr"
2929
infracontroller "sigs.k8s.io/cluster-api-provider-azure/controllers"
3030
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
31+
"sigs.k8s.io/cluster-api-provider-azure/exp/mutators"
3132
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3233
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3334
"sigs.k8s.io/cluster-api/controllers/external"
@@ -208,13 +209,13 @@ func (r *AzureASOManagedMachinePoolReconciler) reconcileNormal(ctx context.Conte
208209
return ctrl.Result{Requeue: true}, nil
209210
}
210211

211-
us, err := resourcesToUnstructured(asoManagedMachinePool.Spec.Resources)
212+
resources, err := mutators.ApplyMutators(ctx, asoManagedMachinePool.Spec.Resources)
212213
if err != nil {
213214
return ctrl.Result{}, err
214215
}
215216

216217
var agentPoolName string
217-
for _, resource := range us {
218+
for _, resource := range resources {
218219
if resource.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group &&
219220
resource.GroupVersionKind().Kind == "ManagedClustersAgentPool" {
220221
agentPoolName = resource.GetName()
@@ -225,7 +226,7 @@ func (r *AzureASOManagedMachinePoolReconciler) reconcileNormal(ctx context.Conte
225226
return ctrl.Result{}, reconcile.TerminalError(fmt.Errorf("no %s ManagedClustersAgentPools defined in AzureASOManagedMachinePool spec.resources", asocontainerservicev1.GroupVersion.Group))
226227
}
227228

228-
resourceReconciler := r.newResourceReconciler(asoManagedMachinePool, us)
229+
resourceReconciler := r.newResourceReconciler(asoManagedMachinePool, resources)
229230
err = resourceReconciler.Reconcile(ctx)
230231
if err != nil {
231232
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)
@@ -311,11 +312,11 @@ func (r *AzureASOManagedMachinePoolReconciler) reconcileDelete(ctx context.Conte
311312
// If the entire cluster is being deleted, this ASO ManagedClustersAgentPool will be deleted with the rest
312313
// of the ManagedCluster.
313314
if cluster.DeletionTimestamp.IsZero() {
314-
us, err := resourcesToUnstructured(asoManagedMachinePool.Spec.Resources)
315+
resources, err := mutators.ToUnstructured(ctx, asoManagedMachinePool.Spec.Resources)
315316
if err != nil {
316317
return ctrl.Result{}, err
317318
}
318-
resourceReconciler := r.newResourceReconciler(asoManagedMachinePool, us)
319+
resourceReconciler := r.newResourceReconciler(asoManagedMachinePool, resources)
319320
err = resourceReconciler.Delete(ctx)
320321
if err != nil {
321322
return ctrl.Result{}, fmt.Errorf("failed to reconcile resources: %w", err)
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mutators
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strings"
23+
24+
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
27+
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
29+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
30+
)
31+
32+
var (
33+
// ErrNoManagedClusterDefined describes an AzureASOManagedControlPlane without a ManagedCluster.
34+
ErrNoManagedClusterDefined = fmt.Errorf("no %s ManagedCluster defined in AzureASOManagedControlPlane spec.resources", asocontainerservicev1.GroupVersion.Group)
35+
)
36+
37+
// SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster.
38+
func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, cluster *clusterv1.Cluster) ResourcesMutator {
39+
return func(ctx context.Context, us []*unstructured.Unstructured) error {
40+
ctx, _, done := tele.StartSpanWithLogger(ctx, "mutators.SetManagedClusterDefaults")
41+
defer done()
42+
43+
var managedCluster *unstructured.Unstructured
44+
var managedClusterPath string
45+
for i, u := range us {
46+
if u.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group &&
47+
u.GroupVersionKind().Kind == "ManagedCluster" {
48+
managedCluster = u
49+
managedClusterPath = fmt.Sprintf("spec.resources[%d]", i)
50+
break
51+
}
52+
}
53+
if managedCluster == nil {
54+
return reconcile.TerminalError(ErrNoManagedClusterDefined)
55+
}
56+
57+
if err := setManagedClusterKubernetesVersion(ctx, asoManagedControlPlane, managedClusterPath, managedCluster); err != nil {
58+
return err
59+
}
60+
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+
69+
return nil
70+
}
71+
}
72+
73+
func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, managedClusterPath string, managedCluster *unstructured.Unstructured) error {
74+
_, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterKubernetesVersion")
75+
defer done()
76+
77+
capzK8sVersion := strings.TrimPrefix(asoManagedControlPlane.Spec.Version, "v")
78+
if capzK8sVersion == "" {
79+
// When the CAPI contract field isn't set, any value for version in the embedded ASO resource may be specified.
80+
return nil
81+
}
82+
83+
k8sVersionPath := []string{"spec", "kubernetesVersion"}
84+
userK8sVersion, k8sVersionFound, err := unstructured.NestedString(managedCluster.UnstructuredContent(), k8sVersionPath...)
85+
if err != nil {
86+
return err
87+
}
88+
setK8sVersion := mutation{
89+
location: managedClusterPath + "." + strings.Join(k8sVersionPath, "."),
90+
val: capzK8sVersion,
91+
reason: "because spec.version is set to " + asoManagedControlPlane.Spec.Version,
92+
}
93+
if k8sVersionFound && userK8sVersion != capzK8sVersion {
94+
return Incompatible{
95+
mutation: setK8sVersion,
96+
userVal: userK8sVersion,
97+
}
98+
}
99+
logMutation(log, setK8sVersion)
100+
return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capzK8sVersion, k8sVersionPath...)
101+
}
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)