Skip to content

Commit 7b75af6

Browse files
committed
propagate k8s version from AzureASOManagedControlPlane to ManagedCluster
1 parent e5e73a2 commit 7b75af6

File tree

4 files changed

+314
-2
lines changed

4 files changed

+314
-2
lines changed

exp/controllers/azureasomanagedcontrolplane_controller.go

Lines changed: 2 additions & 2 deletions
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)
183+
resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane))
184184
if err != nil {
185185
return ctrl.Result{}, err
186186
}
@@ -194,7 +194,7 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileNormal(ctx context.Cont
194194
}
195195
}
196196
if managedClusterName == "" {
197-
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)
198198
}
199199

200200
resourceReconciler := r.newResourceReconciler(asoManagedControlPlane, resources)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
29+
)
30+
31+
var (
32+
// ErrNoManagedClusterDefined describes an AzureASOManagedControlPlane without a ManagedCluster.
33+
ErrNoManagedClusterDefined = fmt.Errorf("no %s ManagedCluster defined in AzureASOManagedControlPlane spec.resources", asocontainerservicev1.GroupVersion.Group)
34+
)
35+
36+
// SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster.
37+
func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane) ResourcesMutator {
38+
return func(ctx context.Context, us []*unstructured.Unstructured) error {
39+
ctx, _, done := tele.StartSpanWithLogger(ctx, "mutators.SetManagedClusterDefaults")
40+
defer done()
41+
42+
var managedCluster *unstructured.Unstructured
43+
var managedClusterPath string
44+
for i, u := range us {
45+
if u.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group &&
46+
u.GroupVersionKind().Kind == "ManagedCluster" {
47+
managedCluster = u
48+
managedClusterPath = fmt.Sprintf("spec.resources[%d]", i)
49+
break
50+
}
51+
}
52+
if managedCluster == nil {
53+
return reconcile.TerminalError(ErrNoManagedClusterDefined)
54+
}
55+
56+
if err := setManagedClusterKubernetesVersion(ctx, asoManagedControlPlane, managedClusterPath, managedCluster); err != nil {
57+
return err
58+
}
59+
60+
return nil
61+
}
62+
}
63+
64+
func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, managedClusterPath string, managedCluster *unstructured.Unstructured) error {
65+
_, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterKubernetesVersion")
66+
defer done()
67+
68+
capzK8sVersion := strings.TrimPrefix(asoManagedControlPlane.Spec.Version, "v")
69+
if capzK8sVersion == "" {
70+
// When the CAPI contract field isn't set, any value for version in the embedded ASO resource may be specified.
71+
return nil
72+
}
73+
74+
k8sVersionPath := []string{"spec", "kubernetesVersion"}
75+
userK8sVersion, k8sVersionFound, err := unstructured.NestedString(managedCluster.UnstructuredContent(), k8sVersionPath...)
76+
if err != nil {
77+
return err
78+
}
79+
setK8sVersion := mutation{
80+
location: managedClusterPath + "." + strings.Join(k8sVersionPath, "."),
81+
val: capzK8sVersion,
82+
reason: "because spec.version is set to " + asoManagedControlPlane.Spec.Version,
83+
}
84+
if k8sVersionFound && userK8sVersion != capzK8sVersion {
85+
return Incompatible{
86+
mutation: setK8sVersion,
87+
userVal: userK8sVersion,
88+
}
89+
}
90+
logMutation(log, setK8sVersion)
91+
return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capzK8sVersion, k8sVersionPath...)
92+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
"encoding/json"
22+
"testing"
23+
24+
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
25+
"github.com/google/go-cmp/cmp"
26+
. "github.com/onsi/gomega"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/utils/ptr"
30+
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
31+
)
32+
33+
func TestSetManagedClusterDefaults(t *testing.T) {
34+
ctx := context.Background()
35+
g := NewGomegaWithT(t)
36+
37+
tests := []struct {
38+
name string
39+
asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane
40+
expected []*unstructured.Unstructured
41+
expectedErr error
42+
}{
43+
{
44+
name: "no ManagedCluster",
45+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{
46+
Spec: infrav1exp.AzureASOManagedControlPlaneSpec{
47+
AzureASOManagedControlPlaneTemplateResourceSpec: infrav1exp.AzureASOManagedControlPlaneTemplateResourceSpec{
48+
Resources: []runtime.RawExtension{},
49+
},
50+
},
51+
},
52+
expectedErr: ErrNoManagedClusterDefined,
53+
},
54+
{
55+
name: "success",
56+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{
57+
Spec: infrav1exp.AzureASOManagedControlPlaneSpec{
58+
AzureASOManagedControlPlaneTemplateResourceSpec: infrav1exp.AzureASOManagedControlPlaneTemplateResourceSpec{
59+
Version: "vCAPI k8s version",
60+
Resources: []runtime.RawExtension{
61+
{
62+
Raw: mcJSON(g, &asocontainerservicev1.ManagedCluster{}),
63+
},
64+
},
65+
},
66+
},
67+
},
68+
expected: []*unstructured.Unstructured{
69+
mcUnstructured(g, &asocontainerservicev1.ManagedCluster{
70+
Spec: asocontainerservicev1.ManagedCluster_Spec{
71+
KubernetesVersion: ptr.To("CAPI k8s version"),
72+
},
73+
}),
74+
},
75+
},
76+
}
77+
78+
for _, test := range tests {
79+
t.Run(test.name, func(t *testing.T) {
80+
g := NewGomegaWithT(t)
81+
82+
mutator := SetManagedClusterDefaults(test.asoManagedControlPlane)
83+
actual, err := ApplyMutators(ctx, test.asoManagedControlPlane.Spec.Resources, mutator)
84+
if test.expectedErr != nil {
85+
g.Expect(err).To(MatchError(test.expectedErr))
86+
} else {
87+
g.Expect(err).NotTo(HaveOccurred())
88+
}
89+
g.Expect(cmp.Diff(test.expected, actual)).To(BeEmpty())
90+
})
91+
}
92+
}
93+
94+
func TestSetManagedClusterKubernetesVersion(t *testing.T) {
95+
ctx := context.Background()
96+
97+
tests := []struct {
98+
name string
99+
asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane
100+
managedCluster *asocontainerservicev1.ManagedCluster
101+
expected *asocontainerservicev1.ManagedCluster
102+
expectedErr error
103+
}{
104+
{
105+
name: "no CAPI opinion",
106+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{},
107+
managedCluster: &asocontainerservicev1.ManagedCluster{
108+
Spec: asocontainerservicev1.ManagedCluster_Spec{
109+
KubernetesVersion: ptr.To("user k8s version"),
110+
},
111+
},
112+
expected: &asocontainerservicev1.ManagedCluster{
113+
Spec: asocontainerservicev1.ManagedCluster_Spec{
114+
KubernetesVersion: ptr.To("user k8s version"),
115+
},
116+
},
117+
},
118+
{
119+
name: "set from CAPI opinion",
120+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{
121+
Spec: infrav1exp.AzureASOManagedControlPlaneSpec{
122+
AzureASOManagedControlPlaneTemplateResourceSpec: infrav1exp.AzureASOManagedControlPlaneTemplateResourceSpec{
123+
Version: "vCAPI k8s version",
124+
},
125+
},
126+
},
127+
managedCluster: &asocontainerservicev1.ManagedCluster{},
128+
expected: &asocontainerservicev1.ManagedCluster{
129+
Spec: asocontainerservicev1.ManagedCluster_Spec{
130+
KubernetesVersion: ptr.To("CAPI k8s version"),
131+
},
132+
},
133+
},
134+
{
135+
name: "user value matching CAPI ok",
136+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{
137+
Spec: infrav1exp.AzureASOManagedControlPlaneSpec{
138+
AzureASOManagedControlPlaneTemplateResourceSpec: infrav1exp.AzureASOManagedControlPlaneTemplateResourceSpec{
139+
Version: "vCAPI k8s version",
140+
},
141+
},
142+
},
143+
managedCluster: &asocontainerservicev1.ManagedCluster{
144+
Spec: asocontainerservicev1.ManagedCluster_Spec{
145+
KubernetesVersion: ptr.To("CAPI k8s version"),
146+
},
147+
},
148+
expected: &asocontainerservicev1.ManagedCluster{
149+
Spec: asocontainerservicev1.ManagedCluster_Spec{
150+
KubernetesVersion: ptr.To("CAPI k8s version"),
151+
},
152+
},
153+
},
154+
{
155+
name: "incompatible",
156+
asoManagedControlPlane: &infrav1exp.AzureASOManagedControlPlane{
157+
Spec: infrav1exp.AzureASOManagedControlPlaneSpec{
158+
AzureASOManagedControlPlaneTemplateResourceSpec: infrav1exp.AzureASOManagedControlPlaneTemplateResourceSpec{
159+
Version: "vCAPI k8s version",
160+
},
161+
},
162+
},
163+
managedCluster: &asocontainerservicev1.ManagedCluster{
164+
Spec: asocontainerservicev1.ManagedCluster_Spec{
165+
KubernetesVersion: ptr.To("user k8s version"),
166+
},
167+
},
168+
expectedErr: Incompatible{
169+
mutation: mutation{
170+
location: ".spec.kubernetesVersion",
171+
val: "CAPI k8s version",
172+
reason: "because spec.version is set to vCAPI k8s version",
173+
},
174+
userVal: "user k8s version",
175+
},
176+
},
177+
}
178+
179+
s := runtime.NewScheme()
180+
NewGomegaWithT(t).Expect(asocontainerservicev1.AddToScheme(s)).To(Succeed())
181+
182+
for _, test := range tests {
183+
t.Run(test.name, func(t *testing.T) {
184+
g := NewGomegaWithT(t)
185+
186+
before := test.managedCluster.DeepCopy()
187+
umc := mcUnstructured(g, test.managedCluster)
188+
189+
err := setManagedClusterKubernetesVersion(ctx, test.asoManagedControlPlane, "", umc)
190+
g.Expect(s.Convert(umc, test.managedCluster, nil)).To(Succeed())
191+
if test.expectedErr != nil {
192+
g.Expect(err).To(MatchError(test.expectedErr))
193+
g.Expect(cmp.Diff(before, test.managedCluster)).To(BeEmpty()) // errors should never modify the resource.
194+
} else {
195+
g.Expect(err).NotTo(HaveOccurred())
196+
g.Expect(cmp.Diff(test.expected, test.managedCluster)).To(BeEmpty())
197+
}
198+
})
199+
}
200+
}
201+
202+
func mcJSON(g Gomega, mc *asocontainerservicev1.ManagedCluster) []byte {
203+
mc.SetGroupVersionKind(asocontainerservicev1.GroupVersion.WithKind("ManagedCluster"))
204+
j, err := json.Marshal(mc)
205+
g.Expect(err).NotTo(HaveOccurred())
206+
return j
207+
}
208+
209+
func mcUnstructured(g Gomega, mc *asocontainerservicev1.ManagedCluster) *unstructured.Unstructured {
210+
s := runtime.NewScheme()
211+
g.Expect(asocontainerservicev1.AddToScheme(s)).To(Succeed())
212+
u := &unstructured.Unstructured{}
213+
g.Expect(s.Convert(mc, u, nil)).To(Succeed())
214+
return u
215+
}

exp/mutators/mutator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
"github.com/go-logr/logr"
2425
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -37,6 +38,10 @@ type mutation struct {
3738
reason string
3839
}
3940

41+
func logMutation(log logr.Logger, mutation mutation) {
42+
log.V(4).Info(fmt.Sprintf("setting %s to %v %s", mutation.location, mutation.val, mutation.reason))
43+
}
44+
4045
// Incompatible describes an error where a piece of user-defined configuration does not match what CAPZ
4146
// requires.
4247
type Incompatible struct {

0 commit comments

Comments
 (0)