Skip to content

Commit bee9fa4

Browse files
sebltmAndiDog
andcommitted
feat: custom lifecyclehooks for machinepools
Taken and rebased from unfinished PR #4875 at commit 2421ec3 Co-authored-by: Andreas Sommer <[email protected]>
1 parent c57821f commit bee9fa4

39 files changed

+1199
-102
lines changed

cmd/clusterawsadm/cloudformation/bootstrap/cluster_api_controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
177177
"elasticloadbalancing:DeleteListener",
178178
"autoscaling:DescribeAutoScalingGroups",
179179
"autoscaling:DescribeInstanceRefreshes",
180+
"autoscaling:DeleteLifecycleHook",
181+
"autoscaling:DescribeLifecycleHooks",
182+
"autoscaling:PutLifecycleHook",
180183
"ec2:CreateLaunchTemplate",
181184
"ec2:CreateLaunchTemplateVersion",
182185
"ec2:DescribeLaunchTemplates",

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/customsuffix.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/default.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_all_secret_backends.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ Resources:
243243
- elasticloadbalancing:DeleteListener
244244
- autoscaling:DescribeAutoScalingGroups
245245
- autoscaling:DescribeInstanceRefreshes
246+
- autoscaling:DeleteLifecycleHook
247+
- autoscaling:DescribeLifecycleHooks
248+
- autoscaling:PutLifecycleHook
246249
- ec2:CreateLaunchTemplate
247250
- ec2:CreateLaunchTemplateVersion
248251
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_allow_assume_role.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_bootstrap_user.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ Resources:
243243
- elasticloadbalancing:DeleteListener
244244
- autoscaling:DescribeAutoScalingGroups
245245
- autoscaling:DescribeInstanceRefreshes
246+
- autoscaling:DeleteLifecycleHook
247+
- autoscaling:DescribeLifecycleHooks
248+
- autoscaling:PutLifecycleHook
246249
- ec2:CreateLaunchTemplate
247250
- ec2:CreateLaunchTemplateVersion
248251
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_custom_bootstrap_user.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ Resources:
243243
- elasticloadbalancing:DeleteListener
244244
- autoscaling:DescribeAutoScalingGroups
245245
- autoscaling:DescribeInstanceRefreshes
246+
- autoscaling:DeleteLifecycleHook
247+
- autoscaling:DescribeLifecycleHooks
248+
- autoscaling:PutLifecycleHook
246249
- ec2:CreateLaunchTemplate
247250
- ec2:CreateLaunchTemplateVersion
248251
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_different_instance_profiles.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_console.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_default_roles.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_disable.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_kms_prefix.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_extra_statements.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ Resources:
243243
- elasticloadbalancing:DeleteListener
244244
- autoscaling:DescribeAutoScalingGroups
245245
- autoscaling:DescribeInstanceRefreshes
246+
- autoscaling:DeleteLifecycleHook
247+
- autoscaling:DescribeLifecycleHooks
248+
- autoscaling:PutLifecycleHook
246249
- ec2:CreateLaunchTemplate
247250
- ec2:CreateLaunchTemplateVersion
248251
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_s3_bucket.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_ssm_secret_backend.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Resources:
237237
- elasticloadbalancing:DeleteListener
238238
- autoscaling:DescribeAutoScalingGroups
239239
- autoscaling:DescribeInstanceRefreshes
240+
- autoscaling:DeleteLifecycleHook
241+
- autoscaling:DescribeLifecycleHooks
242+
- autoscaling:PutLifecycleHook
240243
- ec2:CreateLaunchTemplate
241244
- ec2:CreateLaunchTemplateVersion
242245
- ec2:DescribeLaunchTemplates

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

+49
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,55 @@ spec:
889889
after it enters the InService state.
890890
If no value is supplied by user a default value of 300 seconds is set
891891
type: string
892+
lifecycleHooks:
893+
description: AWSLifecycleHooks specifies lifecycle hooks for the autoscaling
894+
group.
895+
items:
896+
description: AWSLifecycleHook describes an AWS lifecycle hook
897+
properties:
898+
defaultResult:
899+
description: The default result for the lifecycle hook. The
900+
possible values are CONTINUE and ABANDON.
901+
enum:
902+
- CONTINUE
903+
- ABANDON
904+
type: string
905+
heartbeatTimeout:
906+
description: |-
907+
The maximum time, in seconds, that an instance can remain in a Pending:Wait or
908+
Terminating:Wait state. The maximum is 172800 seconds (48 hours) or 100 times
909+
HeartbeatTimeout, whichever is smaller.
910+
format: duration
911+
type: string
912+
lifecycleTransition:
913+
description: The state of the EC2 instance to which to attach
914+
the lifecycle hook.
915+
enum:
916+
- autoscaling:EC2_INSTANCE_LAUNCHING
917+
- autoscaling:EC2_INSTANCE_TERMINATING
918+
type: string
919+
name:
920+
description: The name of the lifecycle hook.
921+
type: string
922+
notificationMetadata:
923+
description: Contains additional metadata that will be passed
924+
to the notification target.
925+
type: string
926+
notificationTargetARN:
927+
description: |-
928+
The ARN of the notification target that Amazon EC2 Auto Scaling uses to
929+
notify you when an instance is in the transition state for the lifecycle hook.
930+
type: string
931+
roleARN:
932+
description: |-
933+
The ARN of the IAM role that allows the Auto Scaling group to publish to the
934+
specified notification target.
935+
type: string
936+
required:
937+
- lifecycleTransition
938+
- name
939+
type: object
940+
type: array
892941
maxSize:
893942
default: 1
894943
description: MaxSize defines the maximum size of the group.

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

+49
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,55 @@ spec:
896896
type: string
897897
description: Labels specifies labels for the Kubernetes node objects
898898
type: object
899+
lifecycleHooks:
900+
description: AWSLifecycleHooks specifies lifecycle hooks for the managed
901+
node group.
902+
items:
903+
description: AWSLifecycleHook describes an AWS lifecycle hook
904+
properties:
905+
defaultResult:
906+
description: The default result for the lifecycle hook. The
907+
possible values are CONTINUE and ABANDON.
908+
enum:
909+
- CONTINUE
910+
- ABANDON
911+
type: string
912+
heartbeatTimeout:
913+
description: |-
914+
The maximum time, in seconds, that an instance can remain in a Pending:Wait or
915+
Terminating:Wait state. The maximum is 172800 seconds (48 hours) or 100 times
916+
HeartbeatTimeout, whichever is smaller.
917+
format: duration
918+
type: string
919+
lifecycleTransition:
920+
description: The state of the EC2 instance to which to attach
921+
the lifecycle hook.
922+
enum:
923+
- autoscaling:EC2_INSTANCE_LAUNCHING
924+
- autoscaling:EC2_INSTANCE_TERMINATING
925+
type: string
926+
name:
927+
description: The name of the lifecycle hook.
928+
type: string
929+
notificationMetadata:
930+
description: Contains additional metadata that will be passed
931+
to the notification target.
932+
type: string
933+
notificationTargetARN:
934+
description: |-
935+
The ARN of the notification target that Amazon EC2 Auto Scaling uses to
936+
notify you when an instance is in the transition state for the lifecycle hook.
937+
type: string
938+
roleARN:
939+
description: |-
940+
The ARN of the IAM role that allows the Auto Scaling group to publish to the
941+
specified notification target.
942+
type: string
943+
required:
944+
- lifecycleTransition
945+
- name
946+
type: object
947+
type: array
899948
providerIDList:
900949
description: |-
901950
ProviderIDList are the provider IDs of instances in the

exp/api/v1beta1/conversion.go

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (src *AWSMachinePool) ConvertTo(dstRaw conversion.Hub) error {
5353
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType
5454
}
5555
dst.Status.InfrastructureMachineKind = restored.Status.InfrastructureMachineKind
56+
if restored.Spec.AWSLifecycleHooks != nil {
57+
dst.Spec.AWSLifecycleHooks = restored.Spec.AWSLifecycleHooks
58+
}
5659

5760
if restored.Spec.AWSLaunchTemplate.PrivateDNSName != nil {
5861
dst.Spec.AWSLaunchTemplate.PrivateDNSName = restored.Spec.AWSLaunchTemplate.PrivateDNSName
@@ -129,6 +132,9 @@ func (src *AWSManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error {
129132
if restored.Spec.AvailabilityZoneSubnetType != nil {
130133
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType
131134
}
135+
if restored.Spec.AWSLifecycleHooks != nil {
136+
dst.Spec.AWSLifecycleHooks = restored.Spec.AWSLifecycleHooks
137+
}
132138

133139
dst.Spec.RolePath = restored.Spec.RolePath
134140
dst.Spec.RolePermissionsBoundary = restored.Spec.RolePermissionsBoundary

exp/api/v1beta1/zz_generated.conversion.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1beta2/awsmachinepool_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ type AWSMachinePoolSpec struct {
100100
// SuspendProcesses defines a list of processes to suspend for the given ASG. This is constantly reconciled.
101101
// If a process is removed from this list it will automatically be resumed.
102102
SuspendProcesses *SuspendProcessesTypes `json:"suspendProcesses,omitempty"`
103+
104+
// AWSLifecycleHooks specifies lifecycle hooks for the autoscaling group.
105+
// +optional
106+
AWSLifecycleHooks []AWSLifecycleHook `json:"lifecycleHooks,omitempty"`
103107
}
104108

105109
// SuspendProcessesTypes contains user friendly auto-completable values for suspended process names.

exp/api/v1beta2/awsmachinepool_webhook.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (r *AWSMachinePool) validateRefreshPreferences() field.ErrorList {
163163
return allErrs
164164
}
165165

166+
func (r *AWSMachinePool) validateLifecycleHooks() field.ErrorList {
167+
return validateLifecycleHooks(r.Spec.AWSLifecycleHooks)
168+
}
169+
166170
// ValidateCreate will do any extra validation when creating a AWSMachinePool.
167171
func (r *AWSMachinePool) ValidateCreate() (admission.Warnings, error) {
168172
log.Info("AWSMachinePool validate create", "machine-pool", klog.KObj(r))
@@ -178,6 +182,7 @@ func (r *AWSMachinePool) ValidateCreate() (admission.Warnings, error) {
178182
allErrs = append(allErrs, r.validateSpotInstances()...)
179183
allErrs = append(allErrs, r.validateRefreshPreferences()...)
180184
allErrs = append(allErrs, r.validateInstanceMarketType()...)
185+
allErrs = append(allErrs, r.validateLifecycleHooks()...)
181186

182187
if len(allErrs) == 0 {
183188
return nil, nil
@@ -205,7 +210,7 @@ func (r *AWSMachinePool) validateInstanceMarketType() field.ErrorList {
205210
switch r.Spec.AWSLaunchTemplate.MarketType {
206211
case "", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock:
207212
default:
208-
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)))
213+
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)))
209214
}
210215
if r.Spec.AWSLaunchTemplate.MarketType == v1beta2.MarketTypeSpot && r.Spec.AWSLaunchTemplate.CapacityReservationID != nil {
211216
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.awsLaunchTemplate.marketType"), "cannot be set to 'Spot' when CapacityReservationID is specified"))
@@ -228,6 +233,7 @@ func (r *AWSMachinePool) ValidateUpdate(_ runtime.Object) (admission.Warnings, e
228233
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
229234
allErrs = append(allErrs, r.validateSpotInstances()...)
230235
allErrs = append(allErrs, r.validateRefreshPreferences()...)
236+
allErrs = append(allErrs, r.validateLifecycleHooks()...)
231237

232238
if len(allErrs) == 0 {
233239
return nil, nil

0 commit comments

Comments
 (0)