|
| 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 | +} |
0 commit comments