@@ -3,7 +3,7 @@ package node
3
3
import (
4
4
"context"
5
5
"fmt"
6
-
6
+ "k8s.io/apimachinery/pkg/api/errors"
7
7
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
8
clientset "k8s.io/client-go/kubernetes"
9
9
cloudprovider "k8s.io/cloud-provider"
@@ -29,16 +29,22 @@ func StartCloudNodeLifecycleControllerWrapper(initContext app.ControllerInitCont
29
29
30
30
//nolint:gocritic // need to follow upstream function signature
31
31
func startCloudNodeLifecycleController (ctx context.Context ,
32
- initContext app.ControllerInitContext ,
32
+ _ app.ControllerInitContext ,
33
33
controlexContext controllermanagerapp.ControllerContext ,
34
34
completedConfig * config.CompletedConfig ,
35
35
cloud cloudprovider.Interface ,
36
36
) (controller.Interface , bool , error ) {
37
+ // Use CCM's kubeconfig to create a clientset for the custom node lifecycle controller because we need permissions
38
+ // to list and delete VolumeAttachments
39
+ ccmClientSet , err := clientset .NewForConfig (completedConfig .Kubeconfig )
40
+ if err != nil {
41
+ return nil , false , fmt .Errorf ("failed to create clientset from ccm kubeconfig: %w" , err )
42
+ }
43
+
37
44
// Start the cloudNodeLifecycleController
38
45
cloudNodeLifecycleController , err := NewCloudNodeLifecycleController (
39
46
completedConfig .SharedInformers .Core ().V1 ().Nodes (),
40
- // cloud node lifecycle controller uses existing cluster role from node-controller
41
- completedConfig .ClientBuilder .ClientOrDie (initContext .ClientName ),
47
+ ccmClientSet ,
42
48
cloud ,
43
49
completedConfig .ComponentConfig .KubeCloudShared .NodeMonitorPeriod .Duration ,
44
50
)
@@ -54,19 +60,24 @@ func startCloudNodeLifecycleController(ctx context.Context,
54
60
}
55
61
56
62
func CleanUpVolumeAttachmentsForNode (ctx context.Context , kubeClient clientset.Interface , nodeName string ) error {
57
- volumeAttachments , listErr := kubeClient .StorageV1 ().VolumeAttachments ().List (ctx ,
58
- metav1.ListOptions {FieldSelector : fmt .Sprintf ("spec.nodeName=%s" , nodeName )})
59
-
63
+ volumeAttachments , listErr := kubeClient .StorageV1 ().VolumeAttachments ().List (ctx , metav1.ListOptions {})
60
64
if listErr != nil {
61
- return fmt .Errorf ("failed to list volume attachments for node %s : %w" , nodeName , listErr )
65
+ return fmt .Errorf ("failed to list all volume attachments: %w" , listErr )
62
66
}
63
67
64
68
for index := range len (volumeAttachments .Items ) {
65
69
volumeAttachment := volumeAttachments .Items [index ]
70
+ if volumeAttachment .Spec .NodeName != nodeName {
71
+ continue
72
+ }
66
73
deleteErr := kubeClient .StorageV1 ().VolumeAttachments ().Delete (ctx , volumeAttachment .Name , metav1.DeleteOptions {})
67
74
if deleteErr != nil {
68
- klog .Errorf ("failed to delete volume attachment %s for node %s: %v" ,
69
- volumeAttachment .Name , nodeName , deleteErr )
75
+ if errors .IsNotFound (deleteErr ) {
76
+ klog .Infof ("volume attachment %s for node %s already deleted, skipping delete" , volumeAttachment .Name , nodeName )
77
+ } else {
78
+ klog .Errorf ("failed to delete volume attachment %s for node %s: %v" ,
79
+ volumeAttachment .Name , nodeName , deleteErr )
80
+ }
70
81
}
71
82
}
72
83
0 commit comments