diff --git a/pkg/webhooks/machine_webhook.go b/pkg/webhooks/machine_webhook.go index 3b5d491c99..16c7da2487 100644 --- a/pkg/webhooks/machine_webhook.go +++ b/pkg/webhooks/machine_webhook.go @@ -794,6 +794,29 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st // TODO(alberto): Validate providerSpec.BlockDevices. // https://github.com/openshift/cluster-api-provider-aws/pull/299#discussion_r433920532 + for i, blockDevice := range providerSpec.BlockDevices { + if blockDevice.EBS == nil || blockDevice.EBS.VolumeType == nil || blockDevice.EBS.ThroughputMib == nil { + continue + } else if *blockDevice.EBS.VolumeType != "gp3" { + errs = append( + errs, + field.Invalid( + field.NewPath("providerSpec", "blockDevices", "ebs", "throughputMib"), + *providerSpec.BlockDevices[i].EBS.ThroughputMib, + fmt.Sprintf("providerSpec.blockDevices[%d].ebs.throughputMib is only valid for gp3 volumes", i), + ), + ) + } else if *blockDevice.EBS.ThroughputMib < 125 || *blockDevice.EBS.ThroughputMib > 2000 { + errs = append( + errs, + field.Invalid( + field.NewPath("providerSpec", "blockDevices", "ebs", "throughputMib"), + *providerSpec.BlockDevices[i].EBS.ThroughputMib, + fmt.Sprintf("providerSpec.blockDevices[%d].ebs.throughputMib must be a value between 125 and 2000", i), + ), + ) + } + } switch providerSpec.Placement.Tenancy { case "", machinev1beta1.DefaultTenancy, machinev1beta1.DedicatedTenancy, machinev1beta1.HostTenancy: diff --git a/pkg/webhooks/machine_webhook_test.go b/pkg/webhooks/machine_webhook_test.go index cd0a84ecc9..67e272f7c5 100644 --- a/pkg/webhooks/machine_webhook_test.go +++ b/pkg/webhooks/machine_webhook_test.go @@ -316,6 +316,110 @@ func TestMachineCreation(t *testing.T) { }, expectedError: "", }, + { + name: "with VolumeType set to gp3 and Throughput not set", + platformType: osconfigv1.AWSPlatformType, + clusterID: "aws-cluster", + providerSpecValue: &kruntime.RawExtension{ + Object: &machinev1beta1.AWSMachineProviderConfig{ + AMI: machinev1beta1.AWSResourceReference{ + ID: ptr.To[string]("ami"), + }, + BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{ + { + EBS: &machinev1beta1.EBSBlockDeviceSpec{ + VolumeType: ptr.To[string]("gp3"), + }, + }, + }, + }, + }, + expectedError: "", + }, + { + name: "with VolumeType set to gp3 and Throughput set under minium value", + platformType: osconfigv1.AWSPlatformType, + clusterID: "aws-cluster", + providerSpecValue: &kruntime.RawExtension{ + Object: &machinev1beta1.AWSMachineProviderConfig{ + AMI: machinev1beta1.AWSResourceReference{ + ID: ptr.To[string]("ami"), + }, + BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{ + { + EBS: &machinev1beta1.EBSBlockDeviceSpec{ + VolumeType: ptr.To[string]("gp3"), + ThroughputMib: ptr.To[int32](124), + }, + }, + }, + }, + }, + expectedError: "providerSpec.blockDevices[0].ebs.throughputMib must be a value between 125 and 2000", + }, + { + name: "with VolumeType set to gp3 and Throughput set over maxium value", + platformType: osconfigv1.AWSPlatformType, + clusterID: "aws-cluster", + providerSpecValue: &kruntime.RawExtension{ + Object: &machinev1beta1.AWSMachineProviderConfig{ + AMI: machinev1beta1.AWSResourceReference{ + ID: ptr.To[string]("ami"), + }, + BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{ + { + EBS: &machinev1beta1.EBSBlockDeviceSpec{ + VolumeType: ptr.To[string]("gp3"), + ThroughputMib: ptr.To[int32](2001), + }, + }, + }, + }, + }, + expectedError: "providerSpec.blockDevices[0].ebs.throughputMib must be a value between 125 and 2000", + }, + { + name: "with VolumeType set to gp3 and Throughput set within range", + platformType: osconfigv1.AWSPlatformType, + clusterID: "aws-cluster", + providerSpecValue: &kruntime.RawExtension{ + Object: &machinev1beta1.AWSMachineProviderConfig{ + AMI: machinev1beta1.AWSResourceReference{ + ID: ptr.To[string]("ami"), + }, + BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{ + { + EBS: &machinev1beta1.EBSBlockDeviceSpec{ + VolumeType: ptr.To[string]("gp3"), + ThroughputMib: ptr.To[int32](1000), + }, + }, + }, + }, + }, + expectedError: "", + }, + { + name: "with Throughput set on non gp3 volume", + platformType: osconfigv1.AWSPlatformType, + clusterID: "aws-cluster", + providerSpecValue: &kruntime.RawExtension{ + Object: &machinev1beta1.AWSMachineProviderConfig{ + AMI: machinev1beta1.AWSResourceReference{ + ID: ptr.To[string]("ami"), + }, + BlockDevices: []machinev1beta1.BlockDeviceMappingSpec{ + { + EBS: &machinev1beta1.EBSBlockDeviceSpec{ + VolumeType: ptr.To[string]("io1"), + ThroughputMib: ptr.To[int32](124), + }, + }, + }, + }, + }, + expectedError: "providerSpec.blockDevices[0].ebs.throughputMib is only valid for gp3 volumes", + }, { name: "with Azure and a nil provider spec value", platformType: osconfigv1.AzurePlatformType,