Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 87 additions & 3 deletions machine/v1beta1/types_awsprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,16 @@ type Filter struct {

// TagSpecification is the name/value pair for a tag
type TagSpecification struct {
// name of the tag
// name of the tag.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we currently validate any pattern for these? I would assume AWS has some sort of valid character set that we would have to observe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the existing logic i found for instances (which i am using same func for dedicated hosts) states not sensitive and just removes duplicates. Online i found:

Key Tag Character & Usage Rules:
Valid Characters: A-Z, a-z, 0-9, space, and _ . : / = + - @.
Case Sensitivity: Yes (e.g., Project and project are distinct).
Prefix Restrictions: Tag keys cannot start with aws:.
Length Limits: Maximum key length is 128 Unicode characters; maximum value length is 256.
Allowed Special Characters: Hyphen (-), Underscore (_), Colon (:), Forward Slash (/), Equal sign (=), Plus (+), Period (.), At sign (@).
Prohibited Characters: Commas are generally not permitted in tag values.
Maximum Tags: Up to 50 tags per resource. 

// This field is required and must be a non-empty string.
// Must be between 1 and 128 characters in length.
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=128
// +required
Name string `json:"name"`
// value of the tag
// When omitted, this creates a tag with an empty string as the value.
// +optional
Value string `json:"value"`
}

Expand Down Expand Up @@ -407,6 +414,27 @@ type AWSMachineProviderStatus struct {
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty"`
// dedicatedHost tracks the dynamically allocated dedicated host.
// This field is populated when allocationStrategy is Dynamic (with or without DynamicHostAllocation).
// +optional
DedicatedHost *DedicatedHostStatus `json:"dedicatedHost,omitempty"`
}

// DedicatedHostStatus defines the observed state of a dynamically allocated dedicated host
// associated with an AWSMachine. This struct is used to track the ID of the dedicated host.
// +kubebuilder:validation:MinProperties=1
type DedicatedHostStatus struct {
// id tracks the dynamically allocated dedicated host ID.
// This field is populated when allocationStrategy is Dynamic (with or without DynamicHostAllocation).
// When omitted, this indicates that the dedicated host has not yet been allocated, or allocation is in progress.
// The value must start with "h-" followed by either 8 or 17 lowercase hexadecimal characters (0-9 and a-f).
// The use of 8 lowercase hexadecimal characters is for older legacy hosts that may not have been migrated to newer format.
// Must be either 10 or 19 characters in length.
// +kubebuilder:validation:XValidation:rule="self.matches('^h-([0-9a-f]{8}|[0-9a-f]{17})$')",message="id must start with 'h-' followed by either 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"
// +kubebuilder:validation:MinLength=10
// +kubebuilder:validation:MaxLength=19
// +optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was keeping this consistent with upstream version: https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/26c35866af2d601760438bd177e10dbfd359b111/api/v1beta2/awsmachine_types.go#L473

I can make this required if you think it should be. I am not set on this.

ID *string `json:"id,omitempty"`
}

// MarketType describes the market type of an EC2 Instance
Expand Down Expand Up @@ -454,21 +482,77 @@ type HostAffinity string

const (
// HostAffinityAnyAvailable lets the platform select any available dedicated host.

HostAffinityAnyAvailable HostAffinity = "AnyAvailable"

// HostAffinityDedicatedHost requires specifying a particular host via dedicatedHost.host.hostID.
HostAffinityDedicatedHost HostAffinity = "DedicatedHost"
)

// AllocationStrategy selects how a dedicated host is provided to the system for assigning to the instance.
// +kubebuilder:validation:Enum:=UserProvided;Dynamic
// +enum
type AllocationStrategy string

const (
// AllocationStrategyUserProvided specifies that the system should assign instances to a user-provided dedicated host.
AllocationStrategyUserProvided AllocationStrategy = "UserProvided"

// AllocationStrategyDynamic specifies that the system should dynamically allocate a dedicated host for instances.
AllocationStrategyDynamic AllocationStrategy = "Dynamic"
)

// DedicatedHost represents the configuration for the usage of dedicated host.
// +kubebuilder:validation:XValidation:rule="self.allocationStrategy == 'UserProvided' ? has(self.id) : !has(self.id)",message="id is required when allocationStrategy is UserProvided, and forbidden otherwise"
// +kubebuilder:validation:XValidation:rule="self.allocationStrategy == 'Dynamic' ? has(self.dynamicHostAllocation) : !has(self.dynamicHostAllocation)",message="dynamicHostAllocation is required when allocationStrategy is Dynamic, and forbidden otherwise"
// +union
type DedicatedHost struct {
// allocationStrategy specifies if the dedicated host will be provided by the admin through the id field or if the host will be dynamically allocated.
// Valid values are UserProvided and Dynamic.
// When omitted, the value defaults to "UserProvided", which requires the id field to be set.
// When allocationStrategy is set to UserProvided, an ID of the dedicated host to assign must be provided.
// When allocationStrategy is set to Dynamic, a dedicated host will be allocated and used to assign instances.
// When allocationStrategy is set to Dynamic, and dynamicHostAllocation is configured, a dedicated host will be allocated and the tags in dynamicHostAllocation will be assigned to that host.
// +optional
// +unionDiscriminator
// +default="UserProvided"
AllocationStrategy *AllocationStrategy `json:"allocationStrategy,omitempty"`

// id identifies the AWS Dedicated Host on which the instance must run.
// The value must start with "h-" followed by either 8 or 17 lowercase hexadecimal characters (0-9 and a-f).
// The use of 8 lowercase hexadecimal characters is for older legacy hosts that may not have been migrated to newer format.
// Must be either 10 or 19 characters in length.
// +kubebuilder:validation:XValidation:rule="self.matches('^h-([0-9a-f]{8}|[0-9a-f]{17})$')",message="hostID must start with 'h-' followed by either 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"
// This field is required when allocationStrategy is UserProvided, and forbidden otherwise.
// When omitted with allocationStrategy set to Dynamic, the platform will dynamically allocate a dedicated host.
// +kubebuilder:validation:XValidation:rule="self.matches('^h-([0-9a-f]{8}|[0-9a-f]{17})$')",message="id must start with 'h-' followed by either 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"
// +kubebuilder:validation:MinLength=10
// +kubebuilder:validation:MaxLength=19
// +required
// +optional
// +unionMember=UserProvided
ID string `json:"id,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID is already part of 4.21, right? If so, then it's a breaking change:

4.21 will ship:

type DedicatedHost struct {
    // +required
    ID string `json:"id,omitempty"`
}

4.22 will ship:

type DedicatedHost struct {
    // +required
    AllocationStrategy AllocationStrategy `json:"allocationStrategy,omitempty"`

    // +optional (changed from +required)
    ID string `json:"id,omitempty"`

    // +optional (new field)
    DynamicHostAllocation *DynamicHostAllocationSpec `json:"dynamicHostAllocation,omitempty"`
}

Means that a valid 4.21 manifest will be invalid in 4.22.

dedicatedHost:
  id: "h-1234567890abcdef0"

Do we have a webhook or similar solution in the works for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there is webhook changes, that PR is almost ready. For backwards compatability, if its "" (emptry string) it is defaulting to UserProvided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, I can change the field to be optional but have it default to UserProvided. Just let me know what you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current implementation addresses the raised concerns.


// dynamicHostAllocation specifies tags to apply to a dynamically allocated dedicated host.
// This field is only allowed when allocationStrategy is Dynamic, and is mutually exclusive with id.
// When specified, a dedicated host will be allocated with the provided tags applied.
// When omitted (and allocationStrategy is Dynamic), a dedicated host will be allocated without any additional tags.
// +optional
// +unionMember=Dynamic
DynamicHostAllocation *DynamicHostAllocationSpec `json:"dynamicHostAllocation,omitempty"`
}
Comment on lines +506 to +541
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

XValidation rule contradicts field documentation.

The XValidation rule on line 507 enforces that dynamicHostAllocation is required when allocationStrategy is Dynamic:

rule="self.allocationStrategy == 'Dynamic' ? has(self.dynamicHostAllocation) : !has(self.dynamicHostAllocation)"

However, the comment on lines 537-538 states:

"When omitted (and allocationStrategy is Dynamic), a dedicated host will be allocated without any additional tags."

This is contradictory—the rule mandates dynamicHostAllocation but the docs say it's optional.

Additionally, since DynamicHostAllocationSpec has +kubebuilder:validation:MinProperties=1 and Tags is its only field (marked optional), users cannot provide an empty dynamicHostAllocation. Combined with the XValidation requiring it, this effectively makes Tags required when using Dynamic allocation—contrary to the stated intent.

Resolution options:

  1. If dynamicHostAllocation should be optional when Dynamic, update the XValidation rule:

    -// +kubebuilder:validation:XValidation:rule="self.allocationStrategy == 'Dynamic' ? has(self.dynamicHostAllocation) : !has(self.dynamicHostAllocation)",message="dynamicHostAllocation is required when allocationStrategy is Dynamic, and forbidden otherwise"
    +// +kubebuilder:validation:XValidation:rule="has(self.dynamicHostAllocation) ? self.allocationStrategy == 'Dynamic' : true",message="dynamicHostAllocation is only allowed when allocationStrategy is Dynamic"

    And remove MinProperties=1 from DynamicHostAllocationSpec.

  2. If dynamicHostAllocation (with tags) is indeed required for Dynamic, update the comment to match the rule.

🤖 Prompt for AI Agents
In `@machine/v1beta1/types_awsprovider.go` around lines 506 - 541, The XValidation
on DedicatedHost currently requires dynamicHostAllocation when
AllocationStrategy == Dynamic, contradicting the comment and the intended
optionality; to make dynamicHostAllocation optional when Dynamic, change the
XValidation rule on the DedicatedHost type (the existing rule referencing
self.allocationStrategy and self.dynamicHostAllocation) to allow omission when
allocationStrategy == 'Dynamic' (for example replace with an expression like
"self.allocationStrategy == 'Dynamic' || !has(self.dynamicHostAllocation)" so
the field is only forbidden when allocationStrategy != 'Dynamic'), and remove
+kubebuilder:validation:MinProperties=1 from the DynamicHostAllocationSpec so an
empty spec (no Tags) is permitted; alternatively, if you want
dynamicHostAllocation to be required, update the DedicatedHost comments to state
it is required and keep the existing XValidation/MinProperties.


// DynamicHostAllocationSpec defines the configuration for dynamic dedicated host allocation.
// This specification always allocates exactly one dedicated host per machine.
// At least one property must be specified when this struct is used.
// Currently only Tags are available for configuring, but in the future more configs may become available.
// +kubebuilder:validation:MinProperties=1
type DynamicHostAllocationSpec struct {
// tags specifies a set of key-value pairs to apply to the allocated dedicated host.
// When omitted, no additional user-defined tags will be applied to the allocated host.
// A maximum of 50 tags can be specified.
// +kubebuilder:validation:MinItems=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The combination of those are confusing:

  • MinProperties=1 on the struct
  • MinItems=1 on the tags field
  • +optional marker on the field

Tags is the only field in the struct, and MinProperties=1 requires at least one property, the field is effectively required when the struct exists. But MinItems=1 means if you do provide tags, it cannot be an empty array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, i think that is what linter / coderabbit kept forcing me torwards. I do believe it would be crazy to provide tags w/o any items so requiring 1 min item would make sense. I can try to remove the MinProperties but coderabbit was the one requiring it (or maybe it was linter.) I'll see if I can trim this again .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

machine/v1beta1/types_awsprovider.go:537:2: minlength: field DedicatedHost.DynamicHostAllocation type DynamicHostAllocationSpec must have a minimum properties, add kubebuilder:validation:MinProperties marker (kubeapilinter)
	DynamicHostAllocation *DynamicHostAllocationSpec `json:"dynamicHostAllocation,omitempty"`

Currently this is the only field in this struct. In the future we may add more for user to provide configuration settings for the dedicated host. I would hate to make this field required and change in the future to optional. Just let me know which way you want to go.

// +kubebuilder:validation:MaxItems=50
// +listType=map
// +listMapKey=name
// +optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this required?

Copy link
Contributor Author

@vr4manta vr4manta Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the future, if we add any additonal fields, we may allow something such as host type or something like that. If that is the case, we may not want to force tags to be required.

Tags *[]TagSpecification `json:"tags,omitempty"`
}
63 changes: 62 additions & 1 deletion machine/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions machine/v1beta1/zz_generated.swagger_doc_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading