@@ -34,14 +34,18 @@ import (
34
34
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
35
35
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors"
36
36
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/converters"
37
- "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services"
38
37
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/wait"
39
38
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record"
40
39
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
41
40
"sigs.k8s.io/cluster-api/util/annotations"
42
41
"sigs.k8s.io/cluster-api/util/conditions"
43
42
)
44
43
44
+ var IgnoreLifecycleHooks = map [string ]bool {
45
+ "Launch-LC-Hook" : true ,
46
+ "Terminate-LC-Hook" : true ,
47
+ }
48
+
45
49
func (s * NodegroupService ) describeNodegroup () (* eks.Nodegroup , error ) {
46
50
eksClusterName := s .scope .KubernetesClusterName ()
47
51
nodegroupName := s .scope .NodegroupName ()
@@ -152,7 +156,7 @@ func (s *NodegroupService) remoteAccess() (*eks.RemoteAccessConfig, error) {
152
156
// SourceSecurityGroups is validated to be empty if PublicAccess is true
153
157
// but just in case we use an empty list to take advantage of the documented
154
158
// API behavior
155
- var sSGs = []string {}
159
+ sSGs : = []string {}
156
160
157
161
if ! pool .RemoteAccess .Public {
158
162
sSGs = pool .RemoteAccess .SourceSecurityGroups
@@ -573,7 +577,7 @@ func (s *NodegroupService) reconcileNodegroup(ctx context.Context) error {
573
577
return errors .Wrapf (err , "failed to reconcile asg tags" )
574
578
}
575
579
576
- if err := s .reconcileLifecycleHooks (s . ASGService ); err != nil {
580
+ if err := s .reconcileLifecycleHooks (ng ); err != nil {
577
581
return errors .Wrapf (err , "failed to reconcile lifecyle hooks" )
578
582
}
579
583
@@ -653,21 +657,29 @@ func (s *NodegroupService) waitForNodegroupActive() (*eks.Nodegroup, error) {
653
657
}
654
658
655
659
// 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
+
657
666
lifecyleHooks := s .scope .GetLifecycleHooks ()
658
667
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 {
660
669
return err
661
670
}
662
671
}
663
672
664
673
// 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 )
666
675
if err != nil {
667
676
return err
668
677
}
669
678
for _ , hook := range hooks {
670
679
found := false
680
+ if IgnoreLifecycleHooks [hook .Name ] {
681
+ continue
682
+ }
671
683
for _ , definedHook := range lifecyleHooks {
672
684
if hook .Name == definedHook .Name {
673
685
found = true
@@ -676,7 +688,7 @@ func (s *NodegroupService) reconcileLifecycleHooks(asgsvc services.ASGInterface)
676
688
}
677
689
if ! found {
678
690
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 {
680
692
conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookDeletionFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
681
693
return err
682
694
}
@@ -686,29 +698,34 @@ func (s *NodegroupService) reconcileLifecycleHooks(asgsvc services.ASGInterface)
686
698
return nil
687
699
}
688
700
689
- func (s * NodegroupService ) reconcileLifecycleHook (hook * expinfrav1.AWSLifecycleHook , asgsvc services. ASGInterface ) error {
701
+ func (s * NodegroupService ) reconcileLifecycleHook (asgName string , hook * expinfrav1.AWSLifecycleHook ) error {
690
702
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 )
692
709
if err != nil {
693
710
conditions .MarkUnknown (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookNotFoundReason , err .Error ())
694
711
return err
695
712
}
696
713
697
714
if existingHook == nil {
698
715
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 {
700
717
conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookCreationFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
701
718
return err
702
719
}
703
720
return nil
704
721
}
705
722
706
723
// 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 )
708
725
709
726
if needsUpdate {
710
727
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 {
712
729
conditions .MarkFalse (s .scope .GetMachinePool (), expinfrav1 .LifecycleHookReadyCondition , expinfrav1 .LifecycleHookUpdateFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
713
730
return err
714
731
}
0 commit comments