|
| 1 | +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package mutator_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook" |
| 12 | + "github.com/gardener/gardener/pkg/apis/core/v1beta1" |
| 13 | + "github.com/gardener/gardener/pkg/utils/test" |
| 14 | + . "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/apimachinery/pkg/runtime" |
| 18 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 19 | + "k8s.io/utils/ptr" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | + fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 23 | + |
| 24 | + "github.com/gardener/gardener-extension-provider-aws/pkg/admission/mutator" |
| 25 | + "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws/install" |
| 26 | +) |
| 27 | + |
| 28 | +var _ = Describe("CloudProfile Mutator", func() { |
| 29 | + var ( |
| 30 | + fakeClient client.Client |
| 31 | + fakeManager manager.Manager |
| 32 | + ctx = context.Background() |
| 33 | + |
| 34 | + cloudProfileMutator extensionswebhook.Mutator |
| 35 | + cloudProfile *v1beta1.CloudProfile |
| 36 | + ) |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + scheme := runtime.NewScheme() |
| 40 | + utilruntime.Must(install.AddToScheme(scheme)) |
| 41 | + utilruntime.Must(v1beta1.AddToScheme(scheme)) |
| 42 | + fakeClient = fakeclient.NewClientBuilder().WithScheme(scheme).Build() |
| 43 | + fakeManager = &test.FakeManager{ |
| 44 | + Client: fakeClient, |
| 45 | + Scheme: scheme, |
| 46 | + } |
| 47 | + |
| 48 | + cloudProfileMutator = mutator.NewCloudProfileMutator(fakeManager) |
| 49 | + |
| 50 | + imageVersion := "1.0.0" |
| 51 | + latestImageVersion := "1.0.1" |
| 52 | + imageName := "os-1" |
| 53 | + |
| 54 | + machineImages := []v1beta1.MachineImage{ |
| 55 | + { |
| 56 | + Name: imageName, |
| 57 | + Versions: []v1beta1.MachineImageVersion{{ |
| 58 | + ExpirableVersion: v1beta1.ExpirableVersion{ |
| 59 | + Version: imageVersion, |
| 60 | + }, |
| 61 | + }, { |
| 62 | + ExpirableVersion: v1beta1.ExpirableVersion{ |
| 63 | + Version: latestImageVersion, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + cloudProfile = &v1beta1.CloudProfile{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{ |
| 72 | + Name: "aws", |
| 73 | + }, |
| 74 | + Spec: v1beta1.CloudProfileSpec{ |
| 75 | + MachineImages: machineImages, |
| 76 | + }, |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + Describe("#Mutate", func() { |
| 81 | + Context("CloudProfile without machineCapabilities", func() { |
| 82 | + BeforeEach(func() { |
| 83 | + cloudProfile.Spec.ProviderConfig = nil |
| 84 | + }) |
| 85 | + |
| 86 | + It("should succeed and not modify the CloudProfile", func() { |
| 87 | + cloudProfile.Spec.ProviderConfig = &runtime.RawExtension{Raw: []byte(`{ |
| 88 | +"apiVersion":"aws.provider.extensions.gardener.cloud/v1alpha1", |
| 89 | +"kind":"CloudProfileConfig", |
| 90 | +"machineImages":[ |
| 91 | + {"name":"image-1","versions":[{"version":"1.1","regions":[{"name":"eu2","ami":"ami-124","architecture":"armhf"}]}]} |
| 92 | +]}`)} |
| 93 | + expectedProfileSpec := cloudProfile.Spec.DeepCopy() |
| 94 | + Expect(cloudProfileMutator.Mutate(ctx, cloudProfile, nil)).To(Succeed()) |
| 95 | + |
| 96 | + Expect(cloudProfile.Spec.MachineImages).To(Equal(expectedProfileSpec.MachineImages)) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + Context("CloudProfile with machineCapabilities", func() { |
| 101 | + BeforeEach(func() { |
| 102 | + cloudProfile.Spec.MachineCapabilities = []v1beta1.CapabilityDefinition{{ |
| 103 | + Name: "architecture", |
| 104 | + Values: []string{"amd64", "arm64", "armhf"}, |
| 105 | + }, { |
| 106 | + Name: "gpu", |
| 107 | + Values: []string{"true", "false"}, |
| 108 | + }} |
| 109 | + }) |
| 110 | + It("should succeed for CloudProfile without provider config", func() { |
| 111 | + expectedProfile := cloudProfile.DeepCopy() |
| 112 | + Expect(cloudProfileMutator.Mutate(ctx, cloudProfile, nil)).To(Succeed()) |
| 113 | + Expect(cloudProfile).To(Equal(expectedProfile)) |
| 114 | + |
| 115 | + }) |
| 116 | + |
| 117 | + It("should skip if CloudProfile is in deletion phase", func() { |
| 118 | + cloudProfile.DeletionTimestamp = ptr.To(metav1.Now()) |
| 119 | + expectedProfile := cloudProfile.DeepCopy() |
| 120 | + |
| 121 | + Expect(cloudProfileMutator.Mutate(ctx, cloudProfile, nil)).To(Succeed()) |
| 122 | + |
| 123 | + Expect(cloudProfile).To(Equal(expectedProfile)) |
| 124 | + }) |
| 125 | + |
| 126 | + It("should fill capabilityFlavors based on provider config", func() { |
| 127 | + image1AmiMappings := `"capabilityFlavors":[ |
| 128 | +{"capabilities":{"architecture":["arm64"]},"regions":[{"name":"image-region-1","ami":"id-img-reg-1"}]}, |
| 129 | +{"capabilities":{"architecture":["amd64"]},"regions":[{"name":"image-region-2","ami":"id-img-reg-2"}]} |
| 130 | +]` |
| 131 | + image1FallbackMappings := `"capabilityFlavors":[ |
| 132 | +{"capabilities":{"architecture":["amd64"]},"regions":[{"name":"image-region-2","ami":"id-img-reg-2"}]} |
| 133 | +]` |
| 134 | + |
| 135 | + cloudProfile.Spec.ProviderConfig = &runtime.RawExtension{Raw: []byte(fmt.Sprintf(`{ |
| 136 | +"apiVersion":"aws.provider.extensions.gardener.cloud/v1alpha1", |
| 137 | +"kind":"CloudProfileConfig", |
| 138 | +"machineImages":[ |
| 139 | + {"name":"os-1","versions":[ |
| 140 | + {"version":"1.0.0",%s}, |
| 141 | + {"version":"1.0.1",%s} |
| 142 | + ]} |
| 143 | +]}`, image1AmiMappings, image1FallbackMappings))} |
| 144 | + Expect(cloudProfileMutator.Mutate(ctx, cloudProfile, nil)).To(Succeed()) |
| 145 | + Expect(cloudProfile.Spec.MachineImages).To(Equal([]v1beta1.MachineImage{ |
| 146 | + { |
| 147 | + Name: "os-1", |
| 148 | + Versions: []v1beta1.MachineImageVersion{ |
| 149 | + { |
| 150 | + ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.0"}, |
| 151 | + CapabilityFlavors: []v1beta1.MachineImageFlavor{ |
| 152 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"arm64"}}}, |
| 153 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 154 | + }, |
| 155 | + }, |
| 156 | + { |
| 157 | + ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.1"}, |
| 158 | + CapabilityFlavors: []v1beta1.MachineImageFlavor{ |
| 159 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + })) |
| 165 | + }) |
| 166 | + |
| 167 | + It("should overwrite capabilityFlavors when some versions already have them", func() { |
| 168 | + twoFlavors := `"capabilityFlavors":[ |
| 169 | +{"capabilities":{"architecture":["arm64"]},"regions":[{"name":"image-region-1","ami":"id-img-reg-1"}]}, |
| 170 | +{"capabilities":{"architecture":["amd64"]},"regions":[{"name":"image-region-2","ami":"id-img-reg-2"}]} |
| 171 | +]` |
| 172 | + oneFlavors := `"capabilityFlavors":[ |
| 173 | +{"capabilities":{"architecture":["amd64"]},"regions":[{"name":"image-region-2","ami":"id-img-reg-2"}]} |
| 174 | +]` |
| 175 | + cloudProfile.Spec.MachineImages = []v1beta1.MachineImage{ |
| 176 | + { |
| 177 | + Name: "os-1", |
| 178 | + Versions: []v1beta1.MachineImageVersion{ |
| 179 | + { |
| 180 | + ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.0"}, |
| 181 | + CapabilityFlavors: []v1beta1.MachineImageFlavor{ |
| 182 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"not-existing"}}}, |
| 183 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 184 | + }, |
| 185 | + }, |
| 186 | + {ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.1"}}, |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + Name: "os-2", |
| 191 | + Versions: []v1beta1.MachineImageVersion{ |
| 192 | + {ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.0"}}, |
| 193 | + {ExpirableVersion: v1beta1.ExpirableVersion{Version: "1.0.1"}}, |
| 194 | + }, |
| 195 | + }, |
| 196 | + } |
| 197 | + cloudProfile.Spec.ProviderConfig = &runtime.RawExtension{Raw: []byte(fmt.Sprintf(`{ |
| 198 | +"apiVersion":"aws.provider.extensions.gardener.cloud/v1alpha1", |
| 199 | +"kind":"CloudProfileConfig", |
| 200 | +"machineImages":[ |
| 201 | + {"name":"os-1","versions":[ |
| 202 | + {"version":"1.0.0",%s}, |
| 203 | + {"version":"1.0.1",%s} |
| 204 | + ]}, |
| 205 | + {"name":"os-2","versions":[ |
| 206 | + {"version":"1.0.0",%s}, |
| 207 | + {"version":"1.0.1",%s} |
| 208 | + ]} |
| 209 | +]}`, twoFlavors, oneFlavors, oneFlavors, twoFlavors))} |
| 210 | + Expect(cloudProfileMutator.Mutate(ctx, cloudProfile, nil)).To(Succeed()) |
| 211 | + Expect(cloudProfile.Spec.MachineImages).To(HaveLen(2)) |
| 212 | + Expect(cloudProfile.Spec.MachineImages[0].Name).To(Equal("os-1")) |
| 213 | + Expect(cloudProfile.Spec.MachineImages[0].Versions).To(HaveLen(2)) |
| 214 | + Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Version).To(Equal("1.0.0")) |
| 215 | + // the existing capabilityFlavors should be overwritten. |
| 216 | + Expect(cloudProfile.Spec.MachineImages[0].Versions[0].CapabilityFlavors).To(ConsistOf([]v1beta1.MachineImageFlavor{ |
| 217 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"arm64"}}}, |
| 218 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 219 | + })) |
| 220 | + Expect(cloudProfile.Spec.MachineImages[0].Versions[1].Version).To(Equal("1.0.1")) |
| 221 | + Expect(cloudProfile.Spec.MachineImages[0].Versions[1].CapabilityFlavors).To(ConsistOf([]v1beta1.MachineImageFlavor{ |
| 222 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 223 | + })) |
| 224 | + // The second machine image should be added completely. |
| 225 | + Expect(cloudProfile.Spec.MachineImages[1].Versions).To(HaveLen(2)) |
| 226 | + Expect(cloudProfile.Spec.MachineImages[1].Versions[0].Version).To(Equal("1.0.0")) |
| 227 | + Expect(cloudProfile.Spec.MachineImages[1].Versions[0].CapabilityFlavors).To(ConsistOf([]v1beta1.MachineImageFlavor{ |
| 228 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 229 | + })) |
| 230 | + Expect(cloudProfile.Spec.MachineImages[1].Versions[1].Version).To(Equal("1.0.1")) |
| 231 | + Expect(cloudProfile.Spec.MachineImages[1].Versions[1].CapabilityFlavors).To(ConsistOf([]v1beta1.MachineImageFlavor{ |
| 232 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"arm64"}}}, |
| 233 | + {Capabilities: v1beta1.Capabilities{"architecture": []string{"amd64"}}}, |
| 234 | + })) |
| 235 | + |
| 236 | + }) |
| 237 | + }) |
| 238 | + |
| 239 | + }) |
| 240 | +}) |
0 commit comments