Skip to content

Commit

Permalink
Add default image name
Browse files Browse the repository at this point in the history
  • Loading branch information
VOID404 committed Sep 16, 2024
1 parent e67a6d3 commit 84bc261
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion internal/gardener/shoot/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type GardenerConfig struct {
}

type MachineImageConfig struct {
DefaultName string `json:"defaultName" validate:"required"`
DefaultVersion string `json:"defaultVersion" validate:"required"`
}

Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions internal/gardener/shoot/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var testReader io.Reader = strings.NewReader(`{
}
},
"machineImage": {
"defaultName": "test-image-name",
"defaultVersion": "0.1.2.3.4"
},
"gardener": {
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 8 additions & 3 deletions internal/gardener/shoot/extender/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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,
}

Expand All @@ -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
}
}
11 changes: 7 additions & 4 deletions internal/gardener/shoot/extender/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
})
}
Expand All @@ -90,7 +92,7 @@ func TestProviderExtender(t *testing.T) {
}

// when
extender := NewProviderExtender(false, "")
extender := NewProviderExtender(false, "", "")
err := extender(runtime, &shoot)

// then
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 84bc261

Please sign in to comment.