From ba0a94fee488c294693393c64f165fac65c2e595 Mon Sep 17 00:00:00 2001 From: Aldo Fuster Turpin Date: Tue, 7 Jan 2025 14:43:16 +0100 Subject: [PATCH] fix the random order of block_device_mappings render Include a new function to get the keys of the map used for block_device_mappings to access elements in deterministic order. --- .../launchtemplate_target_terraform.go | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/upup/pkg/fi/cloudup/awstasks/launchtemplate_target_terraform.go b/upup/pkg/fi/cloudup/awstasks/launchtemplate_target_terraform.go index d8786b87069f3..c3c14db0cb196 100644 --- a/upup/pkg/fi/cloudup/awstasks/launchtemplate_target_terraform.go +++ b/upup/pkg/fi/cloudup/awstasks/launchtemplate_target_terraform.go @@ -22,6 +22,7 @@ import ( "k8s.io/kops/upup/pkg/fi/cloudup/awsup" "k8s.io/kops/upup/pkg/fi/cloudup/terraform" "k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter" + "k8s.io/kops/util/pkg/maps" ) type terraformLaunchTemplateNetworkInterface struct { @@ -262,55 +263,39 @@ func (t *LaunchTemplate) RenderTerraform(target *terraform.TerraformTarget, a, e } } } + devices, err := e.buildRootDevice(cloud) if err != nil { return err } - for n, x := range devices { - tf.BlockDeviceMappings = append(tf.BlockDeviceMappings, &terraformLaunchTemplateBlockDevice{ - DeviceName: fi.PtrTo(n), - EBS: []*terraformLaunchTemplateBlockDeviceEBS{ - { - DeleteOnTermination: fi.PtrTo(true), - Encrypted: x.EbsEncrypted, - KmsKeyID: x.EbsKmsKey, - IOPS: x.EbsVolumeIops, - Throughput: x.EbsVolumeThroughput, - VolumeSize: x.EbsVolumeSize, - VolumeType: fi.PtrTo(string(x.EbsVolumeType)), - }, - }, - }) + + devicesKeys := maps.SortedKeys(devices) + for _, key := range devicesKeys { + terraformLaunchTemplateBlockDevice := createTerraformLaunchTemplateBlockDevice(key, devices[key]) + tf.BlockDeviceMappings = append(tf.BlockDeviceMappings, terraformLaunchTemplateBlockDevice) } + additionals, err := buildAdditionalDevices(e.BlockDeviceMappings) if err != nil { return err } - for n, x := range additionals { - tf.BlockDeviceMappings = append(tf.BlockDeviceMappings, &terraformLaunchTemplateBlockDevice{ - DeviceName: fi.PtrTo(n), - EBS: []*terraformLaunchTemplateBlockDeviceEBS{ - { - DeleteOnTermination: fi.PtrTo(true), - Encrypted: x.EbsEncrypted, - IOPS: x.EbsVolumeIops, - Throughput: x.EbsVolumeThroughput, - KmsKeyID: x.EbsKmsKey, - VolumeSize: x.EbsVolumeSize, - VolumeType: fi.PtrTo(string(x.EbsVolumeType)), - }, - }, - }) + + additionalsKeys := maps.SortedKeys(additionals) + for _, key := range additionalsKeys { + terraformLaunchTemplateBlockDevice := createTerraformLaunchTemplateBlockDevice(key, additionals[key]) + tf.BlockDeviceMappings = append(tf.BlockDeviceMappings, terraformLaunchTemplateBlockDevice) } devices, err = buildEphemeralDevices(cloud, fi.ValueOf(e.InstanceType)) if err != nil { return err } - for n, x := range devices { + + devicesKeys = maps.SortedKeys(devices) + for _, key := range devicesKeys { tf.BlockDeviceMappings = append(tf.BlockDeviceMappings, &terraformLaunchTemplateBlockDevice{ - VirtualName: x.VirtualName, - DeviceName: fi.PtrTo(n), + VirtualName: devices[key].VirtualName, + DeviceName: fi.PtrTo(key), }) } @@ -328,3 +313,20 @@ func (t *LaunchTemplate) RenderTerraform(target *terraform.TerraformTarget, a, e return target.RenderResource("aws_launch_template", fi.ValueOf(e.Name), tf) } + +func createTerraformLaunchTemplateBlockDevice(deviceName string, v *BlockDeviceMapping) *terraformLaunchTemplateBlockDevice { + return &terraformLaunchTemplateBlockDevice{ + DeviceName: fi.PtrTo(deviceName), + EBS: []*terraformLaunchTemplateBlockDeviceEBS{ + { + DeleteOnTermination: fi.PtrTo(true), + Encrypted: v.EbsEncrypted, + KmsKeyID: v.EbsKmsKey, + IOPS: v.EbsVolumeIops, + Throughput: v.EbsVolumeThroughput, + VolumeSize: v.EbsVolumeSize, + VolumeType: fi.PtrTo(string(v.EbsVolumeType)), + }, + }, + } +}