Skip to content

✨feat(awsmachinepool): custom lifecyclehooks for machinepools #4875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
"elasticloadbalancing:DeleteListener",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeInstanceRefreshes",
"autoscaling:DeleteLifecycleHook",
"autoscaling:DescribeLifecycleHooks",
"autoscaling:PutLifecycleHook",
"ec2:CreateLaunchTemplate",
"ec2:CreateLaunchTemplateVersion",
"ec2:DescribeLaunchTemplates",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ Resources:
- elasticloadbalancing:DeleteListener
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeInstanceRefreshes
- autoscaling:DeleteLifecycleHook
- autoscaling:DescribeLifecycleHooks
- autoscaling:PutLifecycleHook
- ec2:CreateLaunchTemplate
- ec2:CreateLaunchTemplateVersion
- ec2:DescribeLaunchTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,55 @@ spec:
after it enters the InService state.
If no value is supplied by user a default value of 300 seconds is set
type: string
lifecycleHooks:
description: AWSLifecycleHooks specifies lifecycle hooks for the autoscaling
group.
items:
description: AWSLifecycleHook describes an AWS lifecycle hook
properties:
defaultResult:
description: The default result for the lifecycle hook. The
possible values are CONTINUE and ABANDON.
enum:
- CONTINUE
- ABANDON
type: string
heartbeatTimeout:
description: |-
The maximum time, in seconds, that an instance can remain in a Pending:Wait or
Terminating:Wait state. The maximum is 172800 seconds (48 hours) or 100 times
HeartbeatTimeout, whichever is smaller.
format: duration
type: string
lifecycleTransition:
description: The state of the EC2 instance to which to attach
the lifecycle hook.
enum:
- autoscaling:EC2_INSTANCE_LAUNCHING
- autoscaling:EC2_INSTANCE_TERMINATING
type: string
name:
description: The name of the lifecycle hook.
type: string
notificationMetadata:
description: Contains additional metadata that will be passed
to the notification target.
type: string
notificationTargetARN:
description: |-
The ARN of the notification target that Amazon EC2 Auto Scaling uses to
notify you when an instance is in the transition state for the lifecycle hook.
type: string
roleARN:
description: |-
The ARN of the IAM role that allows the Auto Scaling group to publish to the
specified notification target.
type: string
required:
- lifecycleTransition
- name
type: object
type: array
maxSize:
default: 1
description: MaxSize defines the maximum size of the group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,55 @@ spec:
type: string
description: Labels specifies labels for the Kubernetes node objects
type: object
lifecycleHooks:
description: AWSLifecycleHooks specifies lifecycle hooks for the managed
node group.
items:
description: AWSLifecycleHook describes an AWS lifecycle hook
properties:
defaultResult:
description: The default result for the lifecycle hook. The
possible values are CONTINUE and ABANDON.
enum:
- CONTINUE
- ABANDON
type: string
heartbeatTimeout:
description: |-
The maximum time, in seconds, that an instance can remain in a Pending:Wait or
Terminating:Wait state. The maximum is 172800 seconds (48 hours) or 100 times
HeartbeatTimeout, whichever is smaller.
format: duration
type: string
lifecycleTransition:
description: The state of the EC2 instance to which to attach
the lifecycle hook.
enum:
- autoscaling:EC2_INSTANCE_LAUNCHING
- autoscaling:EC2_INSTANCE_TERMINATING
type: string
name:
description: The name of the lifecycle hook.
type: string
notificationMetadata:
description: Contains additional metadata that will be passed
to the notification target.
type: string
notificationTargetARN:
description: |-
The ARN of the notification target that Amazon EC2 Auto Scaling uses to
notify you when an instance is in the transition state for the lifecycle hook.
type: string
roleARN:
description: |-
The ARN of the IAM role that allows the Auto Scaling group to publish to the
specified notification target.
type: string
required:
- lifecycleTransition
- name
type: object
type: array
providerIDList:
description: |-
ProviderIDList are the provider IDs of instances in the
Expand Down
6 changes: 6 additions & 0 deletions exp/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (src *AWSMachinePool) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType
}
dst.Status.InfrastructureMachineKind = restored.Status.InfrastructureMachineKind
if restored.Spec.AWSLifecycleHooks != nil {
dst.Spec.AWSLifecycleHooks = restored.Spec.AWSLifecycleHooks
}

if restored.Spec.AWSLaunchTemplate.PrivateDNSName != nil {
dst.Spec.AWSLaunchTemplate.PrivateDNSName = restored.Spec.AWSLaunchTemplate.PrivateDNSName
Expand Down Expand Up @@ -129,6 +132,9 @@ func (src *AWSManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error {
if restored.Spec.AvailabilityZoneSubnetType != nil {
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType
}
if restored.Spec.AWSLifecycleHooks != nil {
dst.Spec.AWSLifecycleHooks = restored.Spec.AWSLifecycleHooks
}

dst.Spec.RolePath = restored.Spec.RolePath
dst.Spec.RolePermissionsBoundary = restored.Spec.RolePermissionsBoundary
Expand Down
2 changes: 2 additions & 0 deletions exp/api/v1beta1/zz_generated.conversion.go

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

4 changes: 4 additions & 0 deletions exp/api/v1beta2/awsmachinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type AWSMachinePoolSpec struct {
// SuspendProcesses defines a list of processes to suspend for the given ASG. This is constantly reconciled.
// If a process is removed from this list it will automatically be resumed.
SuspendProcesses *SuspendProcessesTypes `json:"suspendProcesses,omitempty"`

// AWSLifecycleHooks specifies lifecycle hooks for the autoscaling group.
// +optional
AWSLifecycleHooks []AWSLifecycleHook `json:"lifecycleHooks,omitempty"`
}

// SuspendProcessesTypes contains user friendly auto-completable values for suspended process names.
Expand Down
8 changes: 7 additions & 1 deletion exp/api/v1beta2/awsmachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (r *AWSMachinePool) validateRefreshPreferences() field.ErrorList {
return allErrs
}

func (r *AWSMachinePool) validateLifecycleHooks() field.ErrorList {
return validateLifecycleHooks(r.Spec.AWSLifecycleHooks)
}

// ValidateCreate will do any extra validation when creating a AWSMachinePool.
func (r *AWSMachinePool) ValidateCreate() (admission.Warnings, error) {
log.Info("AWSMachinePool validate create", "machine-pool", klog.KObj(r))
Expand All @@ -178,6 +182,7 @@ func (r *AWSMachinePool) ValidateCreate() (admission.Warnings, error) {
allErrs = append(allErrs, r.validateSpotInstances()...)
allErrs = append(allErrs, r.validateRefreshPreferences()...)
allErrs = append(allErrs, r.validateInstanceMarketType()...)
allErrs = append(allErrs, r.validateLifecycleHooks()...)

if len(allErrs) == 0 {
return nil, nil
Expand Down Expand Up @@ -205,7 +210,7 @@ func (r *AWSMachinePool) validateInstanceMarketType() field.ErrorList {
switch r.Spec.AWSLaunchTemplate.MarketType {
case "", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock:
default:
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.awsLaunchTemplate.MarketType"), r.Spec.AWSLaunchTemplate.MarketType, fmt.Sprintf("Valid values are: %s, %s, %s and omitted", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock)))
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.awsLaunchTemplate.marketType"), r.Spec.AWSLaunchTemplate.MarketType, fmt.Sprintf("Valid values are: %s, %s, %s and omitted", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock)))
}
if r.Spec.AWSLaunchTemplate.MarketType == v1beta2.MarketTypeSpot && r.Spec.AWSLaunchTemplate.CapacityReservationID != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.awsLaunchTemplate.marketType"), "cannot be set to 'Spot' when CapacityReservationID is specified"))
Expand All @@ -228,6 +233,7 @@ func (r *AWSMachinePool) ValidateUpdate(_ runtime.Object) (admission.Warnings, e
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
allErrs = append(allErrs, r.validateSpotInstances()...)
allErrs = append(allErrs, r.validateRefreshPreferences()...)
allErrs = append(allErrs, r.validateLifecycleHooks()...)

if len(allErrs) == 0 {
return nil, nil
Expand Down
Loading
Loading