@@ -34,14 +34,18 @@ import (
3434	expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2" 
3535	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors" 
3636	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/converters" 
37- 	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services" 
3837	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/wait" 
3938	"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record" 
4039	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 
4140	"sigs.k8s.io/cluster-api/util/annotations" 
4241	"sigs.k8s.io/cluster-api/util/conditions" 
4342)
4443
44+ var  IgnoreLifecycleHooks  =  map [string ]bool {
45+ 	"Launch-LC-Hook" :    true ,
46+ 	"Terminate-LC-Hook" : true ,
47+ }
48+ 
4549func  (s  * NodegroupService ) describeNodegroup () (* eks.Nodegroup , error ) {
4650	eksClusterName  :=  s .scope .KubernetesClusterName ()
4751	nodegroupName  :=  s .scope .NodegroupName ()
@@ -152,7 +156,7 @@ func (s *NodegroupService) remoteAccess() (*eks.RemoteAccessConfig, error) {
152156	// SourceSecurityGroups is validated to be empty if PublicAccess is true 
153157	// but just in case we use an empty list to take advantage of the documented 
154158	// API behavior 
155- 	var   sSGs  =  []string {}
159+ 	sSGs  : =  []string {}
156160
157161	if  ! pool .RemoteAccess .Public  {
158162		sSGs  =  pool .RemoteAccess .SourceSecurityGroups 
@@ -573,7 +577,7 @@ func (s *NodegroupService) reconcileNodegroup(ctx context.Context) error {
573577		return  errors .Wrapf (err , "failed to reconcile asg tags" )
574578	}
575579
576- 	if  err  :=  s .reconcileLifecycleHooks (s . ASGService ); err  !=  nil  {
580+ 	if  err  :=  s .reconcileLifecycleHooks (ng ); err  !=  nil  {
577581		return  errors .Wrapf (err , "failed to reconcile lifecyle hooks" )
578582	}
579583
@@ -653,21 +657,29 @@ func (s *NodegroupService) waitForNodegroupActive() (*eks.Nodegroup, error) {
653657}
654658
655659// ReconcileLifecycleHooks periodically reconciles a lifecycle hook for the ASG. 
656- func  (s  * NodegroupService ) reconcileLifecycleHooks (asgsvc  services.ASGInterface ) error  {
660+ func  (s  * NodegroupService ) reconcileLifecycleHooks (ng  * eks.Nodegroup ) error  {
661+ 	asg , err  :=  s .describeASGs (ng )
662+ 	if  err  !=  nil  {
663+ 		return  err 
664+ 	}
665+ 
657666	lifecyleHooks  :=  s .scope .GetLifecycleHooks ()
658667	for  i  :=  range  lifecyleHooks  {
659- 		if  err  :=  s .reconcileLifecycleHook (& lifecyleHooks [i ],  asgsvc ); err  !=  nil  {
668+ 		if  err  :=  s .reconcileLifecycleHook (* asg . AutoScalingGroupName ,  & lifecyleHooks [i ]); err  !=  nil  {
660669			return  err 
661670		}
662671	}
663672
664673	// Get a list of lifecycle hooks that are registered with the ASG but not defined in the MachinePool and delete them. 
665- 	hooks , err  :=  asgsvc . DescribeLifecycleHooks ( s . scope . NodegroupName () )
674+ 	hooks , err  :=  s . ASGService . DescribeLifecycleHooks ( * asg . AutoScalingGroupName )
666675	if  err  !=  nil  {
667676		return  err 
668677	}
669678	for  _ , hook  :=  range  hooks  {
670679		found  :=  false 
680+ 		if  IgnoreLifecycleHooks [hook .Name ] {
681+ 			continue 
682+ 		}
671683		for  _ , definedHook  :=  range  lifecyleHooks  {
672684			if  hook .Name  ==  definedHook .Name  {
673685				found  =  true 
@@ -676,7 +688,7 @@ func (s *NodegroupService) reconcileLifecycleHooks(asgsvc services.ASGInterface)
676688		}
677689		if  ! found  {
678690			s .scope .Info ("Deleting extraneous lifecycle hook" , "hook" , hook .Name )
679- 			if  err  :=  asgsvc . DeleteLifecycleHook ( s . scope . NodegroupName () , hook ); err  !=  nil  {
691+ 			if  err  :=  s . ASGService . DeleteLifecycleHook ( * asg . AutoScalingGroupName , hook ); err  !=  nil  {
680692				conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookDeletionFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
681693				return  err 
682694			}
@@ -686,29 +698,34 @@ func (s *NodegroupService) reconcileLifecycleHooks(asgsvc services.ASGInterface)
686698	return  nil 
687699}
688700
689- func  (s  * NodegroupService ) reconcileLifecycleHook (hook  * expinfrav1.AWSLifecycleHook ,  asgsvc  services. ASGInterface ) error  {
701+ func  (s  * NodegroupService ) reconcileLifecycleHook (asgName   string ,  hook  * expinfrav1.AWSLifecycleHook ) error  {
690702	s .scope .Info ("Checking for existing lifecycle hook" )
691- 	existingHook , err  :=  asgsvc .DescribeLifecycleHook (s .scope .NodegroupName (), hook )
703+ 	// Ignore hooks that are not managed by the controller 
704+ 	if  ignore , ok  :=  IgnoreLifecycleHooks [hook .Name ]; ok  &&  ignore  {
705+ 		return  nil 
706+ 	}
707+ 
708+ 	existingHook , err  :=  s .ASGService .DescribeLifecycleHook (asgName , hook )
692709	if  err  !=  nil  {
693710		conditions .MarkUnknown (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookNotFoundReason , err .Error ())
694711		return  err 
695712	}
696713
697714	if  existingHook  ==  nil  {
698715		s .scope .Info ("Creating lifecycle hook" )
699- 		if  err  :=  asgsvc . CreateLifecycleHook ( s . scope . NodegroupName () , hook ); err  !=  nil  {
716+ 		if  err  :=  s . ASGService . CreateLifecycleHook ( asgName , hook ); err  !=  nil  {
700717			conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookCreationFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
701718			return  err 
702719		}
703720		return  nil 
704721	}
705722
706723	// If the lifecycle hook exists, we need to check if it's up to date 
707- 	needsUpdate  :=  asgsvc .LifecycleHookNeedsUpdate (existingHook , hook )
724+ 	needsUpdate  :=  s . ASGService .LifecycleHookNeedsUpdate (existingHook , hook )
708725
709726	if  needsUpdate  {
710727		s .scope .Info ("Updating lifecycle hook" )
711- 		if  err  :=  asgsvc . UpdateLifecycleHook ( s . scope . NodegroupName () , hook ); err  !=  nil  {
728+ 		if  err  :=  s . ASGService . UpdateLifecycleHook ( asgName , hook ); err  !=  nil  {
712729			conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookUpdateFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
713730			return  err 
714731		}
0 commit comments