Skip to content

Commit 0ee855d

Browse files
committed
Added the AWS Capacity Reservation Webhook
1 parent 57b7917 commit 0ee855d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pkg/webhooks/machine_webhook.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package webhooks
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"regexp"
89
goruntime "runtime"
@@ -729,6 +730,12 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
729730
warnings = append(warnings, "providerSpec.iamInstanceProfile: no IAM instance profile provided: nodes may be unable to join the cluster")
730731
}
731732

733+
if providerSpec.CapacityReservationID != "" {
734+
if err := validateAwsCapacityReservationId(providerSpec.CapacityReservationID); err != nil {
735+
errs = append(errs, field.Invalid(field.NewPath("providerSpec", "capacityReservationId"), providerSpec.CapacityReservationID, err.Error()))
736+
}
737+
}
738+
732739
// TODO(alberto): Validate providerSpec.BlockDevices.
733740
// https://github.com/openshift/cluster-api-provider-aws/pull/299#discussion_r433920532
734741

@@ -2269,3 +2276,16 @@ func appendNextAzureResourceIDValidation(parts []string, id string) error {
22692276
}
22702277
return fmt.Errorf("invalid resource ID: %s", id)
22712278
}
2279+
2280+
// validateAWScapacityReservationId validate capacity reservation group ID.
2281+
func validateAwsCapacityReservationId(capacityReservationId string) error {
2282+
if len(capacityReservationId) == 0 {
2283+
return errors.New("invalid capacityReservationId: capacityReservationId cannot be empty")
2284+
}
2285+
// It must starts with cr-xxxxxxxxxxxxxxxxx with length of 17 characters excluding cr-
2286+
re := regexp.MustCompile(`^cr-[0-9a-f]{17}$`)
2287+
if !re.MatchString(capacityReservationId) {
2288+
return fmt.Errorf("invalid value for capacityReservationId: %q, it must start with 'cr-' and be exactly 20 characters long with 17 hexadecimal characters", capacityReservationId)
2289+
}
2290+
return nil
2291+
}

pkg/webhooks/machine_webhook_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,48 @@ func TestMachineCreation(t *testing.T) {
152152
},
153153
expectedError: "",
154154
},
155+
{
156+
name: "with AWS and CapacityReservationID is empty",
157+
platformType: osconfigv1.AWSPlatformType,
158+
clusterID: "aws-cluster",
159+
providerSpecValue: &kruntime.RawExtension{
160+
Object: &machinev1beta1.AWSMachineProviderConfig{
161+
AMI: machinev1beta1.AWSResourceReference{
162+
ID: ptr.To[string]("ami"),
163+
},
164+
CapacityReservationID: "",
165+
},
166+
},
167+
expectedError: "",
168+
},
169+
{
170+
name: "with AWS and CapacityReservationID is valid",
171+
platformType: osconfigv1.AWSPlatformType,
172+
clusterID: "aws-cluster",
173+
providerSpecValue: &kruntime.RawExtension{
174+
Object: &machinev1beta1.AWSMachineProviderConfig{
175+
AMI: machinev1beta1.AWSResourceReference{
176+
ID: ptr.To[string]("ami"),
177+
},
178+
CapacityReservationID: "cr-12345678901234567",
179+
},
180+
},
181+
expectedError: "",
182+
},
183+
{
184+
name: "with AWS and CapacityReservationID is not valid",
185+
platformType: osconfigv1.AWSPlatformType,
186+
clusterID: "aws-cluster",
187+
providerSpecValue: &kruntime.RawExtension{
188+
Object: &machinev1beta1.AWSMachineProviderConfig{
189+
AMI: machinev1beta1.AWSResourceReference{
190+
ID: ptr.To[string]("ami"),
191+
},
192+
CapacityReservationID: "cr-123",
193+
},
194+
},
195+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: providerSpec.capacityReservationId: Invalid value: \"cr-123\": invalid value for capacityReservationId: \"cr-123\", it must start with 'cr-' and be exactly 20 characters long with 17 hexadecimal characters",
196+
},
155197
{
156198
name: "with Azure and a nil provider spec value",
157199
platformType: osconfigv1.AzurePlatformType,

0 commit comments

Comments
 (0)