@@ -15,8 +15,6 @@ package crds
1515
1616import (
1717 "context"
18- "fmt"
19- "strings"
2018 "time"
2119
2220 "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1alpha1"
@@ -133,8 +131,8 @@ func (r *CNINodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
133131
134132 if cniNode .GetDeletionTimestamp ().IsZero () {
135133 cniNodeCopy := cniNode .DeepCopy ()
136- shouldPatch , err := r .ensureTagsAndLabels (cniNodeCopy , node , nodeFound )
137- shouldPatch = r . ensureFinalizer (cniNodeCopy ) || shouldPatch
134+ shouldPatch , err := r .ensureTagsAndLabels (cniNodeCopy , node )
135+ shouldPatch = controllerutil . AddFinalizer (cniNodeCopy , config . NodeTerminationFinalizer ) || shouldPatch
138136
139137 if shouldPatch {
140138 r .log .Info ("patching CNINode to add fields Tags, Labels and finalizer" , "cninode" , cniNode .Name )
@@ -147,24 +145,26 @@ func (r *CNINodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
147145 }
148146 }
149147 return ctrl.Result {}, err
150-
151148 } else { // CNINode is marked for deletion
152149 if ! nodeFound {
153150 // node is also deleted, proceed with running the cleanup routine and remove the finalizer
154-
155151 // run cleanup for Linux nodes only
156152 if val , ok := cniNode .ObjectMeta .Labels [config .NodeLabelOS ]; ok && val == config .OSLinux {
157153 r .log .Info ("running the finalizer routine on cniNode" , "cniNode" , cniNode .Name )
158154 // run cleanup when node id is present
159155 if nodeID , ok := cniNode .Spec .Tags [config .NetworkInterfaceNodeIDKey ]; ok && nodeID != "" {
160- if err := r .newResourceCleaner (nodeID , r .eC2Wrapper , r .vpcId , r .log ).DeleteLeakedResources (); err != nil {
161- r .log .Error (err , "failed to cleanup resources during node termination" )
162- ec2API .NodeTerminationENICleanupFailure .Inc ()
163- }
156+ go func (nodeID string ) {
157+ childCtx , cancel := context .WithTimeout (ctx , config .NodeTerminationTimeout )
158+ defer cancel ()
159+ if err := r .newResourceCleaner (nodeID , r .eC2Wrapper , r .vpcId , r .log ).DeleteLeakedResources (childCtx ); err != nil {
160+ r .log .Error (err , "failed to cleanup resources during node termination" )
161+ ec2API .NodeTerminationENICleanupFailure .Inc ()
162+ }
163+ }(nodeID )
164164 }
165165 }
166166
167- if err := r .removeFinalizers (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
167+ if err := r .removeFinalizer (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
168168 r .log .Error (err , "failed to remove finalizer on CNINode, will retry" , "cniNode" , cniNode .Name , "finalizer" , config .NodeTerminationFinalizer )
169169 if apierrors .IsConflict (err ) {
170170 return ctrl.Result {Requeue : true }, nil
@@ -188,7 +188,7 @@ func (r *CNINodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
188188 Spec : cniNode .Spec ,
189189 }
190190
191- if err := r .removeFinalizers (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
191+ if err := r .removeFinalizer (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
192192 r .log .Error (err , "failed to remove finalizer on CNINode, will retry" )
193193 return ctrl.Result {}, err
194194 }
@@ -248,17 +248,7 @@ func (r *CNINodeReconciler) createCNINodeFromObj(ctx context.Context, newCNINode
248248 })
249249}
250250
251- func (r * CNINodeReconciler ) GetNodeID (node * v1.Node ) (string , error ) {
252- if node .Spec .ProviderID == "" {
253- return "" , fmt .Errorf ("provider ID is not set for node %s" , node .Name )
254- }
255- if idx := strings .LastIndex (node .Spec .ProviderID , "/" ); idx != - 1 && idx < len (node .Spec .ProviderID )- 1 {
256- return node .Spec .ProviderID [idx + 1 :], nil
257- }
258- return "" , fmt .Errorf ("invalid provider ID format for node %s, with providerId" , node .Spec .ProviderID )
259- }
260-
261- func (r * CNINodeReconciler ) ensureTagsAndLabels (cniNode * v1alpha1.CNINode , node * v1.Node , nodeFound bool ) (bool , error ) {
251+ func (r * CNINodeReconciler ) ensureTagsAndLabels (cniNode * v1alpha1.CNINode , node * v1.Node ) (bool , error ) {
262252 shouldPatch := false
263253 var err error
264254 if cniNode .Spec .Tags == nil {
@@ -269,9 +259,9 @@ func (r *CNINodeReconciler) ensureTagsAndLabels(cniNode *v1alpha1.CNINode, node
269259 cniNode .Spec .Tags [config .VPCCNIClusterNameKey ] = r .clusterName
270260 shouldPatch = true
271261 }
272- if nodeFound {
262+ if node != nil {
273263 var nodeID string
274- nodeID , err = r .GetNodeID (node )
264+ nodeID , err = utils .GetNodeID (node )
275265
276266 if cniNode .Spec .Tags [config .NetworkInterfaceNodeIDKey ] != nodeID {
277267 cniNode .Spec .Tags [config .NetworkInterfaceNodeIDKey ] = nodeID
@@ -290,28 +280,12 @@ func (r *CNINodeReconciler) ensureTagsAndLabels(cniNode *v1alpha1.CNINode, node
290280 return shouldPatch , err
291281}
292282
293- func (r * CNINodeReconciler ) ensureFinalizer (cniNode * v1alpha1.CNINode ) bool {
294- shouldPatch := false
295- if ! controllerutil .ContainsFinalizer (cniNode , config .NodeTerminationFinalizer ) {
296- r .log .Info ("adding finalizer" , "object" , cniNode .GetObjectKind ().GroupVersionKind ().Kind , "name" , cniNode .GetName (), "finalizer" , config .NodeTerminationFinalizer )
297- controllerutil .AddFinalizer (cniNode , config .NodeTerminationFinalizer )
298- shouldPatch = true
299- }
300- return shouldPatch
301- }
302-
303- func (r * CNINodeReconciler ) removeFinalizers (ctx context.Context , cniNode * v1alpha1.CNINode , finalizer string ) error {
283+ func (r * CNINodeReconciler ) removeFinalizer (ctx context.Context , cniNode * v1alpha1.CNINode , finalizer string ) error {
304284 cniNodeCopy := cniNode .DeepCopy ()
305- needsUpdate := false
306285
307- if controllerutil .ContainsFinalizer (cniNodeCopy , finalizer ) {
286+ if controllerutil .RemoveFinalizer (cniNodeCopy , finalizer ) {
308287 r .log .Info ("removing finalizer for cninode" , "name" , cniNode .GetName (), "finalizer" , finalizer )
309- controllerutil .RemoveFinalizer (cniNodeCopy , finalizer )
310- needsUpdate = true
311- }
312-
313- if ! needsUpdate {
314- return nil
288+ return r .Client .Patch (ctx , cniNodeCopy , client .MergeFromWithOptions (cniNode , client.MergeFromWithOptimisticLock {}))
315289 }
316- return r . Client . Patch ( ctx , cniNodeCopy , client . MergeFromWithOptions ( cniNode , client. MergeFromWithOptimisticLock {}))
290+ return nil
317291}
0 commit comments