Skip to content

Commit 72ecf00

Browse files
committed
wip: testing, refactoring
1 parent 217f6eb commit 72ecf00

13 files changed

+108
-104
lines changed

api/v1beta1/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ type AWSMachineSpec struct {
228228
// +optional
229229
HostID *string `json:"hostID,omitempty"`
230230

231-
// Affinity specifies the dedicated host affinity setting for the instance.
232-
// When affinity is set to Host, an instance started onto a specific host always restarts on the same host if stopped.
231+
// HostAffinity specifies the dedicated host affinity setting for the instance.
232+
// When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
233+
// When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
234+
// When HostAffinity is defined, HostID is required.
233235
// +optional
234-
// +kubebuilder:validation:Enum:=Default;Host
236+
// +kubebuilder:validation:Enum:=default;host
235237
HostAffinity *string `json:"hostAffinity,omitempty"`
236238
}
237239

api/v1beta2/awsmachine_webhook.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (r *AWSMachine) ValidateCreate() (admission.Warnings, error) {
6464
allErrs = append(allErrs, r.validateNonRootVolumes()...)
6565
allErrs = append(allErrs, r.validateSSHKeyName()...)
6666
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
67+
allErrs = append(allErrs, r.validateHostAffinity()...)
6768
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
6869
allErrs = append(allErrs, r.validateNetworkElasticIPPool()...)
6970
allErrs = append(allErrs, r.validateInstanceMarketType()...)
@@ -91,6 +92,8 @@ func (r *AWSMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, err
9192
allErrs = append(allErrs, r.validateCloudInitSecret()...)
9293
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
9394
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
95+
allErrs = append(allErrs, r.validateHostAffinity()...)
96+
9497

9598
newAWSMachineSpec := newAWSMachine["spec"].(map[string]interface{})
9699
oldAWSMachineSpec := oldAWSMachine["spec"].(map[string]interface{})
@@ -433,6 +436,17 @@ func (r *AWSMachine) validateAdditionalSecurityGroups() field.ErrorList {
433436
return allErrs
434437
}
435438

439+
func (r *AWSMachine) validateHostAffinity() field.ErrorList {
440+
var allErrs field.ErrorList
441+
442+
if r.Spec.HostAffinity != nil {
443+
if r.Spec.HostID == nil || len(*r.Spec.HostID) == 0 {
444+
allErrs = append(allErrs, field.Required(field.NewPath("spec.hostID"), "hostID must be set when hostAffinity is configured"))
445+
}
446+
}
447+
return allErrs
448+
}
449+
436450
func (r *AWSMachine) validateSSHKeyName() field.ErrorList {
437451
return validateSSHKeyName(r.Spec.SSHKeyName)
438452
}

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,37 @@ func TestAWSMachineCreate(t *testing.T) {
411411
},
412412
wantErr: true,
413413
},
414+
{
415+
name: "configure host affinity with Host ID",
416+
machine: &AWSMachine{
417+
Spec: AWSMachineSpec{
418+
InstanceType: "test",
419+
HostAffinity: ptr.To("default"),
420+
HostID: ptr.To("h-09dcf61cb388b0149"),
421+
},
422+
},
423+
wantErr: false,
424+
},
425+
{
426+
name: "configure host affinity with invalid affinity",
427+
machine: &AWSMachine{
428+
Spec: AWSMachineSpec{
429+
InstanceType: "test",
430+
HostAffinity: ptr.To("invalid"),
431+
},
432+
},
433+
wantErr: true,
434+
},
435+
{
436+
name: "configure host affinity without Host ID",
437+
machine: &AWSMachine{
438+
Spec: AWSMachineSpec{
439+
InstanceType: "test",
440+
HostAffinity: ptr.To("default"),
441+
},
442+
},
443+
wantErr: true,
444+
},
414445
{
415446
name: "create with valid BYOIPv4",
416447
machine: &AWSMachine{

api/v1beta2/types.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,17 @@ type Instance struct {
274274
// +optional
275275
MarketType MarketType `json:"marketType,omitempty"`
276276

277-
// HostID specifies the dedicated host on which the instance should be started
277+
// HostAffinity specifies the dedicated host affinity setting for the instance.
278+
// When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
279+
// When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
280+
// When HostAffinity is defined, HostID is required.
278281
// +optional
279-
HostID *string `json:"hostID,omitempty"`
282+
// +kubebuilder:validation:Enum:=default;host
283+
HostAffinity *string `json:"hostAffinity,omitempty"`
280284

281-
// Affinity specifies the dedicated host affinity setting for the instance.
285+
// HostID specifies the dedicated host on which the instance should be started
282286
// +optional
283-
HostAffinity *string `json:"hostAffinity,omitempty"`
287+
HostID *string `json:"hostID,omitempty"`
284288
}
285289

286290
// MarketType describes the market type of an Instance

api/v1beta2/zz_generated.deepcopy.go

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

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,14 @@ spec:
12141214
enabled.
12151215
type: boolean
12161216
hostAffinity:
1217-
description: Affinity specifies the dedicated host affinity setting
1218-
for the instance.
1217+
description: |-
1218+
HostAffinity specifies the dedicated host affinity setting for the instance.
1219+
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
1220+
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
1221+
When HostAffinity is defined, HostID is required.
1222+
enum:
1223+
- default
1224+
- host
12191225
type: string
12201226
hostID:
12211227
description: HostID specifies the dedicated host on which the
@@ -3387,8 +3393,14 @@ spec:
33873393
enabled.
33883394
type: boolean
33893395
hostAffinity:
3390-
description: Affinity specifies the dedicated host affinity setting
3391-
for the instance.
3396+
description: |-
3397+
HostAffinity specifies the dedicated host affinity setting for the instance.
3398+
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
3399+
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
3400+
When HostAffinity is defined, HostID is required.
3401+
enum:
3402+
- default
3403+
- host
33923404
type: string
33933405
hostID:
33943406
description: HostID specifies the dedicated host on which the

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,8 +2181,14 @@ spec:
21812181
enabled.
21822182
type: boolean
21832183
hostAffinity:
2184-
description: Affinity specifies the dedicated host affinity setting
2185-
for the instance.
2184+
description: |-
2185+
HostAffinity specifies the dedicated host affinity setting for the instance.
2186+
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
2187+
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
2188+
When HostAffinity is defined, HostID is required.
2189+
enum:
2190+
- default
2191+
- host
21862192
type: string
21872193
hostID:
21882194
description: HostID specifies the dedicated host on which the

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,13 @@ spec:
688688
type: object
689689
hostAffinity:
690690
description: |-
691-
Affinity specifies the dedicated host affinity setting for the instance.
692-
When affinity is set to Host, an instance started onto a specific host always restarts on the same host if stopped.
691+
HostAffinity specifies the dedicated host affinity setting for the instance.
692+
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
693+
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
694+
When HostAffinity is defined, HostID is required.
693695
enum:
694-
- Default
695-
- Host
696+
- default
697+
- host
696698
type: string
697699
hostID:
698700
description: HostID specifies the Dedicated Host on which the instance

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,13 @@ spec:
622622
type: object
623623
hostAffinity:
624624
description: |-
625-
Affinity specifies the dedicated host affinity setting for the instance.
626-
When affinity is set to Host, an instance started onto a specific host always restarts on the same host if stopped.
625+
HostAffinity specifies the dedicated host affinity setting for the instance.
626+
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
627+
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
628+
When HostAffinity is defined, HostID is required.
627629
enum:
628-
- Default
629-
- Host
630+
- default
631+
- host
630632
type: string
631633
hostID:
632634
description: HostID specifies the Dedicated Host on which

0 commit comments

Comments
 (0)