@@ -15,8 +15,6 @@ package crds
15
15
16
16
import (
17
17
"context"
18
- "fmt"
19
- "strings"
20
18
"time"
21
19
22
20
"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
133
131
134
132
if cniNode .GetDeletionTimestamp ().IsZero () {
135
133
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
138
136
139
137
if shouldPatch {
140
138
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
147
145
}
148
146
}
149
147
return ctrl.Result {}, err
150
-
151
148
} else { // CNINode is marked for deletion
152
149
if ! nodeFound {
153
150
// node is also deleted, proceed with running the cleanup routine and remove the finalizer
154
-
155
151
// run cleanup for Linux nodes only
156
152
if val , ok := cniNode .ObjectMeta .Labels [config .NodeLabelOS ]; ok && val == config .OSLinux {
157
153
r .log .Info ("running the finalizer routine on cniNode" , "cniNode" , cniNode .Name )
158
154
// run cleanup when node id is present
159
155
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 )
164
164
}
165
165
}
166
166
167
- if err := r .removeFinalizers (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
167
+ if err := r .removeFinalizer (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
168
168
r .log .Error (err , "failed to remove finalizer on CNINode, will retry" , "cniNode" , cniNode .Name , "finalizer" , config .NodeTerminationFinalizer )
169
169
if apierrors .IsConflict (err ) {
170
170
return ctrl.Result {Requeue : true }, nil
@@ -188,7 +188,7 @@ func (r *CNINodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
188
188
Spec : cniNode .Spec ,
189
189
}
190
190
191
- if err := r .removeFinalizers (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
191
+ if err := r .removeFinalizer (ctx , cniNode , config .NodeTerminationFinalizer ); err != nil {
192
192
r .log .Error (err , "failed to remove finalizer on CNINode, will retry" )
193
193
return ctrl.Result {}, err
194
194
}
@@ -248,17 +248,7 @@ func (r *CNINodeReconciler) createCNINodeFromObj(ctx context.Context, newCNINode
248
248
})
249
249
}
250
250
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 ) {
262
252
shouldPatch := false
263
253
var err error
264
254
if cniNode .Spec .Tags == nil {
@@ -269,9 +259,9 @@ func (r *CNINodeReconciler) ensureTagsAndLabels(cniNode *v1alpha1.CNINode, node
269
259
cniNode .Spec .Tags [config .VPCCNIClusterNameKey ] = r .clusterName
270
260
shouldPatch = true
271
261
}
272
- if nodeFound {
262
+ if node != nil {
273
263
var nodeID string
274
- nodeID , err = r .GetNodeID (node )
264
+ nodeID , err = utils .GetNodeID (node )
275
265
276
266
if cniNode .Spec .Tags [config .NetworkInterfaceNodeIDKey ] != nodeID {
277
267
cniNode .Spec .Tags [config .NetworkInterfaceNodeIDKey ] = nodeID
@@ -290,28 +280,12 @@ func (r *CNINodeReconciler) ensureTagsAndLabels(cniNode *v1alpha1.CNINode, node
290
280
return shouldPatch , err
291
281
}
292
282
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 {
304
284
cniNodeCopy := cniNode .DeepCopy ()
305
- needsUpdate := false
306
285
307
- if controllerutil .ContainsFinalizer (cniNodeCopy , finalizer ) {
286
+ if controllerutil .RemoveFinalizer (cniNodeCopy , finalizer ) {
308
287
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 {}))
315
289
}
316
- return r . Client . Patch ( ctx , cniNodeCopy , client . MergeFromWithOptions ( cniNode , client. MergeFromWithOptimisticLock {}))
290
+ return nil
317
291
}
0 commit comments