From 84bc2618ff097ca5065710a14faab39e0c6ba331 Mon Sep 17 00:00:00 2001 From: VOID404 Date: Mon, 16 Sep 2024 09:38:47 +0200 Subject: [PATCH] Add default image name --- internal/gardener/shoot/converter.go | 3 ++- internal/gardener/shoot/converter_test.go | 2 ++ internal/gardener/shoot/extender/provider.go | 11 ++++++++--- internal/gardener/shoot/extender/provider_test.go | 11 +++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/gardener/shoot/converter.go b/internal/gardener/shoot/converter.go index aa819a47..35899efd 100644 --- a/internal/gardener/shoot/converter.go +++ b/internal/gardener/shoot/converter.go @@ -67,6 +67,7 @@ type GardenerConfig struct { } type MachineImageConfig struct { + DefaultName string `json:"defaultName" validate:"required"` DefaultVersion string `json:"defaultVersion" validate:"required"` } @@ -75,7 +76,7 @@ func NewConverter(config ConverterConfig) Converter { extender.ExtendWithAnnotations, extender.ExtendWithLabels, extender.NewKubernetesVersionExtender(config.Kubernetes.DefaultVersion), - extender.NewProviderExtender(config.Provider.AWS.EnableIMDSv2, config.MachineImage.DefaultVersion), + extender.NewProviderExtender(config.Provider.AWS.EnableIMDSv2, config.MachineImage.DefaultName, config.MachineImage.DefaultVersion), extender.NewDNSExtender(config.DNS.SecretName, config.DNS.DomainPrefix, config.DNS.ProviderType), extender.ExtendWithOIDC, extender.ExtendWithCloudProfile, diff --git a/internal/gardener/shoot/converter_test.go b/internal/gardener/shoot/converter_test.go index 51c1d4d3..67389c12 100644 --- a/internal/gardener/shoot/converter_test.go +++ b/internal/gardener/shoot/converter_test.go @@ -151,6 +151,7 @@ var testReader io.Reader = strings.NewReader(`{ } }, "machineImage": { + "defaultName": "test-image-name", "defaultVersion": "0.1.2.3.4" }, "gardener": { @@ -188,6 +189,7 @@ func Test_ConverterConfig_Load_OK(t *testing.T) { }, }, MachineImage: MachineImageConfig{ + DefaultName: "test-image-name", DefaultVersion: "0.1.2.3.4", }, Gardener: GardenerConfig{ diff --git a/internal/gardener/shoot/extender/provider.go b/internal/gardener/shoot/extender/provider.go index 5796b355..3f23a489 100644 --- a/internal/gardener/shoot/extender/provider.go +++ b/internal/gardener/shoot/extender/provider.go @@ -14,7 +14,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) -func NewProviderExtender(enableIMDSv2 bool, defaultMachineImageVersion string) func(runtime imv1.Runtime, shoot *gardener.Shoot) error { +func NewProviderExtender(enableIMDSv2 bool, defaultMachineImageName, defaultMachineImageVersion string) func(runtime imv1.Runtime, shoot *gardener.Shoot) error { return func(runtime imv1.Runtime, shoot *gardener.Shoot) error { provider := &shoot.Spec.Provider provider.Type = runtime.Spec.Shoot.Provider.Type @@ -26,7 +26,7 @@ func NewProviderExtender(enableIMDSv2 bool, defaultMachineImageVersion string) f return err } - setDefaultMachineImageVersion(provider, defaultMachineImageVersion) + setDefaultMachineImage(provider, defaultMachineImageName, defaultMachineImageVersion) err = setWorkerConfig(provider, provider.Type, enableIMDSv2) setWorkerSettings(provider) @@ -122,12 +122,13 @@ func setWorkerSettings(provider *gardener.Provider) { } } -func setDefaultMachineImageVersion(provider *gardener.Provider, defaultMachineImageVersion string) { +func setDefaultMachineImage(provider *gardener.Provider, defaultMachineImageName, defaultMachineImageVersion string) { for i := 0; i < len(provider.Workers); i++ { worker := &provider.Workers[i] if worker.Machine.Image == nil { worker.Machine.Image = &gardener.ShootMachineImage{ + Name: defaultMachineImageName, Version: &defaultMachineImageVersion, } @@ -138,6 +139,10 @@ func setDefaultMachineImageVersion(provider *gardener.Provider, defaultMachineIm machineImageVersion = &defaultMachineImageVersion } + if worker.Machine.Image.Name == "" { + worker.Machine.Image.Name = defaultMachineImageName + } + worker.Machine.Image.Version = machineImageVersion } } diff --git a/internal/gardener/shoot/extender/provider_test.go b/internal/gardener/shoot/extender/provider_test.go index 999790f8..1d24ff3f 100644 --- a/internal/gardener/shoot/extender/provider_test.go +++ b/internal/gardener/shoot/extender/provider_test.go @@ -18,6 +18,8 @@ func TestProviderExtender(t *testing.T) { EnableIMDSv2 bool DefaultMachineImageVersion string ExpectedMachineImageVersion string + DefaultMachineImageName string + ExpectedMachineImageName string ExpectedZonesCount int }{ "Create provider specific config for AWS without worker config": { @@ -65,13 +67,13 @@ func TestProviderExtender(t *testing.T) { shoot := fixEmptyGardenerShoot("cluster", "kcp-system") // when - extender := NewProviderExtender(testCase.EnableIMDSv2, testCase.DefaultMachineImageVersion) + extender := NewProviderExtender(testCase.EnableIMDSv2, testCase.DefaultMachineImageName, testCase.DefaultMachineImageVersion) err := extender(testCase.Runtime, &shoot) // then require.NoError(t, err) - assertProvider(t, testCase.Runtime.Spec.Shoot, shoot, testCase.EnableIMDSv2, testCase.ExpectedMachineImageVersion) + assertProvider(t, testCase.Runtime.Spec.Shoot, shoot, testCase.EnableIMDSv2, testCase.ExpectedMachineImageName, testCase.ExpectedMachineImageVersion) assertProviderSpecificConfig(t, shoot, testCase.ExpectedZonesCount) }) } @@ -90,7 +92,7 @@ func TestProviderExtender(t *testing.T) { } // when - extender := NewProviderExtender(false, "") + extender := NewProviderExtender(false, "", "") err := extender(runtime, &shoot) // then @@ -175,7 +177,7 @@ func fixAWSProviderWithMultipleWorkers() imv1.Provider { } } -func assertProvider(t *testing.T, runtimeShoot imv1.RuntimeShoot, shoot gardener.Shoot, expectWorkerConfig bool, expectedMachineImageVersion string) { +func assertProvider(t *testing.T, runtimeShoot imv1.RuntimeShoot, shoot gardener.Shoot, expectWorkerConfig bool, expectedMachineImageName, expectedMachineImageVersion string) { assert.Equal(t, runtimeShoot.Provider.Type, shoot.Spec.Provider.Type) assert.Equal(t, runtimeShoot.Provider.Workers, shoot.Spec.Provider.Workers) assert.Equal(t, false, shoot.Spec.Provider.WorkersSettings.SSHAccess.Enabled) @@ -192,6 +194,7 @@ func assertProvider(t *testing.T, runtimeShoot imv1.RuntimeShoot, shoot gardener assert.Empty(t, worker.ProviderConfig) } assert.Equal(t, expectedMachineImageVersion, *worker.Machine.Image.Version) + assert.Equal(t, expectedMachineImageName, worker.Machine.Image.Name) } }