Skip to content

Commit 64fc535

Browse files
committed
Initial changes for supporting HostResourceGroupArn parameter in AWSMachine.Spec
Signed-off-by: Pankaj Walke <punkwalker@gmail.com>
1 parent 8ff7054 commit 64fc535

15 files changed

+215
-6
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
6565
dst.Status.Bastion.MarketType = restored.Status.Bastion.MarketType
6666
dst.Status.Bastion.HostAffinity = restored.Status.Bastion.HostAffinity
6767
dst.Status.Bastion.HostID = restored.Status.Bastion.HostID
68+
dst.Status.Bastion.HostResourceGroupArn = restored.Status.Bastion.HostResourceGroupArn
6869
dst.Status.Bastion.CapacityReservationPreference = restored.Status.Bastion.CapacityReservationPreference
6970
dst.Status.Bastion.CPUOptions = restored.Status.Bastion.CPUOptions
7071
if restored.Status.Bastion.DynamicHostAllocation != nil {

api/v1beta1/awsmachine_conversion.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
4545
dst.Spec.CapacityReservationID = restored.Spec.CapacityReservationID
4646
dst.Spec.MarketType = restored.Spec.MarketType
4747
dst.Spec.HostID = restored.Spec.HostID
48+
dst.Spec.HostResourceGroupArn = restored.Spec.HostResourceGroupArn
4849
dst.Spec.HostAffinity = restored.Spec.HostAffinity
4950
dst.Spec.CapacityReservationPreference = restored.Spec.CapacityReservationPreference
5051
dst.Spec.NetworkInterfaceType = restored.Spec.NetworkInterfaceType
@@ -117,6 +118,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
117118
dst.Spec.Template.Spec.CapacityReservationID = restored.Spec.Template.Spec.CapacityReservationID
118119
dst.Spec.Template.Spec.MarketType = restored.Spec.Template.Spec.MarketType
119120
dst.Spec.Template.Spec.HostID = restored.Spec.Template.Spec.HostID
121+
dst.Spec.Template.Spec.HostResourceGroupArn = restored.Spec.Template.Spec.HostResourceGroupArn
120122
dst.Spec.Template.Spec.HostAffinity = restored.Spec.Template.Spec.HostAffinity
121123
dst.Spec.Template.Spec.CapacityReservationPreference = restored.Spec.Template.Spec.CapacityReservationPreference
122124
dst.Spec.Template.Spec.NetworkInterfaceType = restored.Spec.Template.Spec.NetworkInterfaceType

api/v1beta1/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/awsmachine_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ type AWSMachineSpec struct {
250250
// +optional
251251
HostID *string `json:"hostID,omitempty"`
252252

253+
// HostResourceGroupArn specifies the Dedicated Host Resource Group ARN on which the instance must be started.
254+
// This field is mutually exclusive with DynamicHostAllocation and HostID.
255+
// Note: The instance's AMI licenses must match the licenses associated with the host resource group.
256+
// If the host resource group has no associated licenses, ensure the AMI also has no special licensing requirements.
257+
// +kubebuilder:validation:Pattern=`^arn:aws[a-z\-]*:resource-groups:[a-z0-9\-]+:[0-9]{12}:group/[a-zA-Z0-9\-_]+$`
258+
// +optional
259+
HostResourceGroupArn *string `json:"hostResourceGroupArn,omitempty"`
260+
253261
// HostAffinity specifies the dedicated host affinity setting for the instance.
254262
// When HostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
255263
// When HostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.

api/v1beta2/awsmachine_webhook.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,24 @@ func (r *AWSMachine) validateAdditionalSecurityGroups() field.ErrorList {
477477
func (r *AWSMachine) validateHostAllocation() field.ErrorList {
478478
var allErrs field.ErrorList
479479

480-
// Check if both hostID and dynamicHostAllocation are specified
480+
// Check if multiple host allocation options are specified
481481
hasHostID := r.Spec.HostID != nil && len(*r.Spec.HostID) > 0
482+
hasHostResourceGroupArn := r.Spec.HostResourceGroupArn != nil && len(*r.Spec.HostResourceGroupArn) > 0
482483
hasDynamicHostAllocation := r.Spec.DynamicHostAllocation != nil
483484

484-
if hasHostID && hasDynamicHostAllocation {
485-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.hostID"), "hostID and dynamicHostAllocation are mutually exclusive"), field.Forbidden(field.NewPath("spec.dynamicHostAllocation"), "hostID and dynamicHostAllocation are mutually exclusive"))
485+
count := 0
486+
if hasHostID {
487+
count++
488+
}
489+
if hasHostResourceGroupArn {
490+
count++
491+
}
492+
if hasDynamicHostAllocation {
493+
count++
494+
}
495+
496+
if count > 1 {
497+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "hostID, hostResourceGroupArn, and dynamicHostAllocation are mutually exclusive"))
486498
}
487499

488500
return allErrs

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,58 @@ func TestAWSMachineCreate(t *testing.T) {
596596
},
597597
wantErr: false,
598598
},
599+
{
600+
name: "hostResourceGroupArn alone is valid",
601+
machine: &AWSMachine{
602+
Spec: AWSMachineSpec{
603+
InstanceType: "test",
604+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
605+
},
606+
},
607+
wantErr: false,
608+
},
609+
{
610+
name: "hostID and hostResourceGroupArn are mutually exclusive",
611+
machine: &AWSMachine{
612+
Spec: AWSMachineSpec{
613+
InstanceType: "test",
614+
HostID: aws.String("h-1234567890abcdef0"),
615+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
616+
},
617+
},
618+
wantErr: true,
619+
},
620+
{
621+
name: "hostResourceGroupArn and dynamicHostAllocation are mutually exclusive",
622+
machine: &AWSMachine{
623+
Spec: AWSMachineSpec{
624+
InstanceType: "test",
625+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
626+
DynamicHostAllocation: &DynamicHostAllocationSpec{
627+
Tags: map[string]string{
628+
"Environment": "test",
629+
},
630+
},
631+
},
632+
},
633+
wantErr: true,
634+
},
635+
{
636+
name: "all three host allocation options are mutually exclusive",
637+
machine: &AWSMachine{
638+
Spec: AWSMachineSpec{
639+
InstanceType: "test",
640+
HostID: aws.String("h-1234567890abcdef0"),
641+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
642+
DynamicHostAllocation: &DynamicHostAllocationSpec{
643+
Tags: map[string]string{
644+
"Environment": "test",
645+
},
646+
},
647+
},
648+
},
649+
wantErr: true,
650+
},
599651
}
600652
for _, tt := range tests {
601653
t.Run(tt.name, func(t *testing.T) {

api/v1beta2/awsmachinetemplate_webhook.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,24 @@ func (r *AWSMachineTemplate) validateHostAllocation() field.ErrorList {
177177

178178
spec := r.Spec.Template.Spec
179179

180-
// Check if both hostID and dynamicHostAllocation are specified
180+
// Check if multiple host allocation options are specified
181181
hasHostID := spec.HostID != nil && len(*spec.HostID) > 0
182+
hasHostResourceGroupArn := spec.HostResourceGroupArn != nil && len(*spec.HostResourceGroupArn) > 0
182183
hasDynamicHostAllocation := spec.DynamicHostAllocation != nil
183184

184-
if hasHostID && hasDynamicHostAllocation {
185-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.template.spec.hostID"), "hostID and dynamicHostAllocation are mutually exclusive"), field.Forbidden(field.NewPath("spec.template.spec.dynamicHostAllocation"), "hostID and dynamicHostAllocation are mutually exclusive"))
185+
count := 0
186+
if hasHostID {
187+
count++
188+
}
189+
if hasHostResourceGroupArn {
190+
count++
191+
}
192+
if hasDynamicHostAllocation {
193+
count++
194+
}
195+
196+
if count > 1 {
197+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.template.spec"), "hostID, hostResourceGroupArn, and dynamicHostAllocation are mutually exclusive"))
186198
}
187199

188200
return allErrs

api/v1beta2/awsmachinetemplate_webhook_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,57 @@ func TestAWSMachineTemplateValidateCreate(t *testing.T) {
100100
},
101101
wantError: true,
102102
},
103+
{
104+
name: "hostResourceGroupArn alone is valid",
105+
inputTemplate: &AWSMachineTemplate{
106+
ObjectMeta: metav1.ObjectMeta{},
107+
Spec: AWSMachineTemplateSpec{
108+
Template: AWSMachineTemplateResource{
109+
Spec: AWSMachineSpec{
110+
InstanceType: "test",
111+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
112+
},
113+
},
114+
},
115+
},
116+
wantError: false,
117+
},
118+
{
119+
name: "hostID and hostResourceGroupArn are mutually exclusive",
120+
inputTemplate: &AWSMachineTemplate{
121+
ObjectMeta: metav1.ObjectMeta{},
122+
Spec: AWSMachineTemplateSpec{
123+
Template: AWSMachineTemplateResource{
124+
Spec: AWSMachineSpec{
125+
InstanceType: "test",
126+
HostID: aws.String("h-1234567890abcdef0"),
127+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
128+
},
129+
},
130+
},
131+
},
132+
wantError: true,
133+
},
134+
{
135+
name: "hostResourceGroupArn and dynamicHostAllocation are mutually exclusive",
136+
inputTemplate: &AWSMachineTemplate{
137+
ObjectMeta: metav1.ObjectMeta{},
138+
Spec: AWSMachineTemplateSpec{
139+
Template: AWSMachineTemplateResource{
140+
Spec: AWSMachineSpec{
141+
InstanceType: "test",
142+
HostResourceGroupArn: aws.String("arn:aws:resource-groups:us-west-2:123456789012:group/test-group"),
143+
DynamicHostAllocation: &DynamicHostAllocationSpec{
144+
Tags: map[string]string{
145+
"Environment": "test",
146+
},
147+
},
148+
},
149+
},
150+
},
151+
},
152+
wantError: true,
153+
},
103154
}
104155
for _, tt := range tests {
105156
t.Run(tt.name, func(t *testing.T) {

api/v1beta2/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ type Instance struct {
286286
// +optional
287287
HostID *string `json:"hostID,omitempty"`
288288

289+
// HostResourceGroupArn specifies the Dedicated Host Resource Group ARN on which the instance should be started.
290+
// Note: The instance's AMI licenses must match the licenses associated with the host resource group.
291+
// +optional
292+
HostResourceGroupArn *string `json:"hostResourceGroupArn,omitempty"`
293+
289294
// DynamicHostAllocation enables automatic allocation of dedicated hosts.
290295
// This field is mutually exclusive with HostID.
291296
// +optional

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)