Skip to content

Commit

Permalink
break up errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Feb 26, 2022
1 parent 4c684f2 commit ef9c8a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
7 changes: 3 additions & 4 deletions pkg/cloudprovider/aws/apis/v1alpha1/provider_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ func (c *Constraints) defaultBlockDeviceMappings() {
return
}
defaultEBS := BlockDevice{
DeleteOnTermination: aws.Bool(true),
Encrypted: aws.Bool(true),
VolumeType: aws.String(ec2.VolumeTypeGp3),
VolumeSize: resource.NewScaledQuantity(20, resource.Giga),
Encrypted: aws.Bool(true),
VolumeType: aws.String(ec2.VolumeTypeGp3),
VolumeSize: resource.NewScaledQuantity(20, resource.Giga),
}
switch aws.StringValue(c.AMIFamily) {
case AMIFamilyAL2, AMIFamilyUbuntu:
Expand Down
65 changes: 45 additions & 20 deletions pkg/cloudprovider/aws/apis/v1alpha1/provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ func (a *AWS) validateMetadataOptions() (errs *apis.FieldError) {
).ViaField(metadataOptionsPath)
}

func (a *AWS) validateHTTPEndpoint() (errs *apis.FieldError) {
func (a *AWS) validateHTTPEndpoint() *apis.FieldError {
if a.MetadataOptions.HTTPEndpoint == nil {
return nil
}
return a.validateStringEnum(*a.MetadataOptions.HTTPEndpoint, "httpEndpoint", ec2.LaunchTemplateInstanceMetadataEndpointState_Values())
}

func (a *AWS) validateHTTPProtocolIpv6() (errs *apis.FieldError) {
func (a *AWS) validateHTTPProtocolIpv6() *apis.FieldError {
if a.MetadataOptions.HTTPProtocolIPv6 == nil {
return nil
}
Expand Down Expand Up @@ -176,27 +176,52 @@ func (a *AWS) validateStringEnum(value, field string, validValues []string) *api
}

func (a *AWS) validateBlockDeviceMappings() (errs *apis.FieldError) {
if a.BlockDeviceMappings == nil {
return nil
}
for i, blockDeviceMapping := range a.BlockDeviceMappings {
if blockDeviceMapping.DeviceName == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].deviceName", blockDeviceMappingsPath, i)))
}
if blockDeviceMapping.EBS == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].ebs", blockDeviceMappingsPath, i)))
continue
if err := a.validateBlockDeviceMapping(blockDeviceMapping); err != nil {
errs = errs.Also(err.ViaFieldIndex(blockDeviceMappingsPath, i))
}
if blockDeviceMapping.EBS.VolumeType != nil {
if fieldErr := a.validateStringEnum(*blockDeviceMapping.EBS.VolumeType, fmt.Sprintf("%s[%d].ebs.volumeType", blockDeviceMappingsPath, i), ec2.VolumeType_Values()); fieldErr != nil {
errs = errs.Also(fieldErr)
}
}
if blockDeviceMapping.EBS.VolumeSize == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].ebs.volumeSize", blockDeviceMappingsPath, i)))
} else if blockDeviceMapping.EBS.VolumeSize.Cmp(minVolumeSize) == -1 || blockDeviceMapping.EBS.VolumeSize.Cmp(maxVolumeSize) == 1 {
errs = errs.Also(apis.ErrOutOfBoundsValue(blockDeviceMapping.EBS.VolumeSize.String(), minVolumeSize.String(), maxVolumeSize.String(), fmt.Sprintf("%s[%d].ebs.volumeSize", blockDeviceMappingsPath, i)))
}
return errs
}

func (a *AWS) validateBlockDeviceMapping(blockDeviceMapping *BlockDeviceMapping) (errs *apis.FieldError) {
return errs.Also(a.validateDeviceName(blockDeviceMapping), a.validateEBS(blockDeviceMapping))
}

func (a *AWS) validateDeviceName(blockDeviceMapping *BlockDeviceMapping) *apis.FieldError {
if blockDeviceMapping.DeviceName == nil {
return apis.ErrMissingField("deviceName")
}
return nil
}

func (a *AWS) validateEBS(blockDeviceMapping *BlockDeviceMapping) (errs *apis.FieldError) {
if blockDeviceMapping.EBS == nil {
return apis.ErrMissingField("ebs")
}
for _, err := range []*apis.FieldError{
a.validateVolumeType(blockDeviceMapping),
a.validateVolumeSize(blockDeviceMapping),
} {
if err != nil {
errs = errs.Also(err.ViaField("ebs"))
}
}
return errs
}

func (a *AWS) validateVolumeType(blockDeviceMapping *BlockDeviceMapping) *apis.FieldError {
if blockDeviceMapping.EBS.VolumeType != nil {
return a.validateStringEnum(*blockDeviceMapping.EBS.VolumeType, "volumeType", ec2.VolumeType_Values())
}
return nil
}

func (a *AWS) validateVolumeSize(blockDeviceMapping *BlockDeviceMapping) *apis.FieldError {
if blockDeviceMapping.EBS.VolumeSize == nil {
return apis.ErrMissingField("volumeSize")
} else if blockDeviceMapping.EBS.VolumeSize.Cmp(minVolumeSize) == -1 || blockDeviceMapping.EBS.VolumeSize.Cmp(maxVolumeSize) == 1 {
return apis.ErrOutOfBoundsValue(blockDeviceMapping.EBS.VolumeSize.String(), minVolumeSize.String(), maxVolumeSize.String(), "volumeSize")
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/controllers/provisioning/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func (c *Controller) Delete(name string) {
// Apply creates or updates the provisioner to the latest configuration
func (c *Controller) Apply(ctx context.Context, provisioner *v1alpha5.Provisioner) error {
provisioner.SetDefaults(ctx)
if err := provisioner.Validate(ctx); err != nil {
return err
}
// Refresh global requirements using instance type availability
instanceTypes, err := c.cloudProvider.GetInstanceTypes(ctx, provisioner.Spec.Provider)
if err != nil {
Expand Down

0 comments on commit ef9c8a3

Please sign in to comment.