Skip to content

Commit b4ceefd

Browse files
committed
refactor: adopt improvements from gardener/gardener provider local
1 parent 83db92c commit b4ceefd

4 files changed

Lines changed: 134 additions & 91 deletions

File tree

pkg/admission/validator/cloudprofile.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ import (
2222

2323
// NewCloudProfileValidator returns a new instance of a cloud profile validator.
2424
func NewCloudProfileValidator(mgr manager.Manager) extensionswebhook.Validator {
25-
return &cloudProfile{
25+
return &cloudProfileValidator{
2626
decoder: serializer.NewCodecFactory(mgr.GetScheme(), serializer.EnableStrict).UniversalDecoder(),
2727
}
2828
}
2929

30-
type cloudProfile struct {
30+
type cloudProfileValidator struct {
3131
decoder runtime.Decoder
3232
}
3333

3434
// Validate validates the given cloud profile objects.
35-
func (cp *cloudProfile) Validate(_ context.Context, newObj, _ client.Object) error {
35+
func (cp *cloudProfileValidator) Validate(_ context.Context, newObj, _ client.Object) error {
3636
cloudProfile, ok := newObj.(*core.CloudProfile)
3737
if !ok {
3838
return fmt.Errorf("wrong object type %T", newObj)
@@ -45,13 +45,13 @@ func (cp *cloudProfile) Validate(_ context.Context, newObj, _ client.Object) err
4545

4646
cpConfig, err := decodeCloudProfileConfig(cp.decoder, cloudProfile.Spec.ProviderConfig)
4747
if err != nil {
48-
return err
48+
return fmt.Errorf("could not decode providerConfig of CloudProfile %q: %w", cloudProfile.Name, err)
4949
}
5050

51-
capabilitiesDefinitions, err := gardencorev1beta1helper.ConvertV1beta1CapabilityDefinitions(cloudProfile.Spec.MachineCapabilities)
51+
capabilityDefinitions, err := gardencorev1beta1helper.ConvertV1beta1CapabilityDefinitions(cloudProfile.Spec.MachineCapabilities)
5252
if err != nil {
5353
return field.InternalError(field.NewPath("spec").Child("machineCapabilities"), err)
5454
}
5555

56-
return awsvalidation.ValidateCloudProfileConfig(cpConfig, cloudProfile.Spec.MachineImages, capabilitiesDefinitions, providerConfigPath).ToAggregate()
56+
return awsvalidation.ValidateCloudProfileConfig(cpConfig, cloudProfile.Spec.MachineImages, capabilityDefinitions, providerConfigPath).ToAggregate()
5757
}

pkg/admission/validator/namespacedcloudprofile.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,25 @@ type namespacedCloudProfile struct {
4242

4343
// Validate validates the given NamespacedCloudProfile objects.
4444
func (p *namespacedCloudProfile) Validate(ctx context.Context, newObj, _ client.Object) error {
45-
profile, ok := newObj.(*core.NamespacedCloudProfile)
45+
cloudProfile, ok := newObj.(*core.NamespacedCloudProfile)
4646
if !ok {
4747
return fmt.Errorf("wrong object type %T", newObj)
4848
}
4949

50-
if profile.DeletionTimestamp != nil {
50+
if cloudProfile.DeletionTimestamp != nil {
5151
return nil
5252
}
5353

54-
cpConfig := &api.CloudProfileConfig{}
55-
if profile.Spec.ProviderConfig != nil {
54+
cloudProfileConfig := &api.CloudProfileConfig{}
55+
if cloudProfile.Spec.ProviderConfig != nil {
5656
var err error
57-
cpConfig, err = decodeCloudProfileConfig(p.decoder, profile.Spec.ProviderConfig)
57+
cloudProfileConfig, err = decodeCloudProfileConfig(p.decoder, cloudProfile.Spec.ProviderConfig)
5858
if err != nil {
59-
return err
59+
return fmt.Errorf("could not decode providerConfig of NamespacedCloudProfile for '%s': %w", cloudProfile.Name, err)
6060
}
6161
}
6262

63-
parentCloudProfile := profile.Spec.Parent
63+
parentCloudProfile := cloudProfile.Spec.Parent
6464
if parentCloudProfile.Kind != constants.CloudProfileReferenceKindCloudProfile {
6565
return fmt.Errorf("parent reference must be of kind CloudProfile (unsupported kind: %s)", parentCloudProfile.Kind)
6666
}
@@ -69,16 +69,7 @@ func (p *namespacedCloudProfile) Validate(ctx context.Context, newObj, _ client.
6969
return err
7070
}
7171

72-
return p.validateNamespacedCloudProfileProviderConfig(cpConfig, profile.Spec, parentProfile.Spec).ToAggregate()
73-
}
74-
75-
// validateNamespacedCloudProfileProviderConfig validates the CloudProfileConfig passed with a NamespacedCloudProfile.
76-
func (p *namespacedCloudProfile) validateNamespacedCloudProfileProviderConfig(providerConfig *api.CloudProfileConfig, profileSpec core.NamespacedCloudProfileSpec, parentSpec gardencorev1beta1.CloudProfileSpec) field.ErrorList {
77-
allErrs := field.ErrorList{}
78-
79-
allErrs = append(allErrs, p.validateMachineImages(providerConfig, profileSpec.MachineImages, parentSpec)...)
80-
81-
return allErrs
72+
return p.validateMachineImages(cloudProfileConfig, cloudProfile.Spec.MachineImages, parentProfile.Spec).ToAggregate()
8273
}
8374

8475
func (p *namespacedCloudProfile) validateMachineImages(providerConfig *api.CloudProfileConfig, machineImages []core.MachineImage, parentSpec gardencorev1beta1.CloudProfileSpec) field.ErrorList {
@@ -87,7 +78,7 @@ func (p *namespacedCloudProfile) validateMachineImages(providerConfig *api.Cloud
8778
machineImagesPath := field.NewPath("spec.providerConfig.machineImages")
8879
for i, machineImage := range providerConfig.MachineImages {
8980
idxPath := machineImagesPath.Index(i)
90-
allErrs = append(allErrs, validation.ValidateProviderMachineImage(idxPath, machineImage, parentSpec.MachineCapabilities)...)
81+
allErrs = append(allErrs, validation.ValidateProviderMachineImage(machineImage, parentSpec.MachineCapabilities, idxPath)...)
9182
}
9283

9384
profileImages := gutil.NewCoreImagesContext(machineImages)

pkg/apis/aws/helper/helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func FindImageInCloudProfile(
9494
}
9595
machineImages := cloudProfileConfig.MachineImages
9696

97-
capabilitySet, err := findCapabilitySetFromMachineImages(machineImages, name, version, region, arch, machineCapabilities, capabilityDefinitions)
97+
capabilitySet, err := findMachineImageFlavor(machineImages, name, version, region, arch, machineCapabilities, capabilityDefinitions)
9898
if err != nil {
9999
return nil, fmt.Errorf("could not find an AMI for region %q, image %q, version %q that supports %v: %w", region, name, version, machineCapabilities, err)
100100
}
@@ -135,7 +135,7 @@ func FindImageInWorkerStatus(machineImages []api.MachineImage, name string, vers
135135
return nil, fmt.Errorf("no machine image found for image %q with version %q and capabilities %v", name, version, machineCapabilities)
136136
}
137137

138-
func findCapabilitySetFromMachineImages(
138+
func findMachineImageFlavor(
139139
machineImages []api.MachineImages,
140140
imageName, imageVersion, region string,
141141
arch *string,
@@ -166,7 +166,7 @@ func findCapabilitySetFromMachineImages(
166166
filteredCapabilityFlavors := filterCapabilityFlavorsByRegion(version.CapabilityFlavors, region)
167167
bestMatch, err := worker.FindBestImageFlavor(filteredCapabilityFlavors, machineCapabilities, capabilityDefinitions)
168168
if err != nil {
169-
return nil, fmt.Errorf("could not determine best capabilitySet %w", err)
169+
return nil, fmt.Errorf("could not determine best flavor %w", err)
170170
}
171171

172172
return bestMatch, nil

0 commit comments

Comments
 (0)