Skip to content

Commit f0e8021

Browse files
committed
fix: grant more permissions to cloud node lifecycle controller
1 parent 73e4369 commit f0e8021

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Diff for: internal/node/utils.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
8+
"k8s.io/apimachinery/pkg/api/errors"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
clientset "k8s.io/client-go/kubernetes"
911
cloudprovider "k8s.io/cloud-provider"
@@ -29,16 +31,22 @@ func StartCloudNodeLifecycleControllerWrapper(initContext app.ControllerInitCont
2931

3032
//nolint:gocritic // need to follow upstream function signature
3133
func startCloudNodeLifecycleController(ctx context.Context,
32-
initContext app.ControllerInitContext,
34+
_ app.ControllerInitContext,
3335
controlexContext controllermanagerapp.ControllerContext,
3436
completedConfig *config.CompletedConfig,
3537
cloud cloudprovider.Interface,
3638
) (controller.Interface, bool, error) {
39+
// Use CCM's kubeconfig to create a clientset for the custom node lifecycle controller because we need permissions
40+
// to list and delete VolumeAttachments
41+
ccmClientSet, err := clientset.NewForConfig(completedConfig.Kubeconfig)
42+
if err != nil {
43+
return nil, false, fmt.Errorf("failed to create clientset from ccm kubeconfig: %w", err)
44+
}
45+
3746
// Start the cloudNodeLifecycleController
3847
cloudNodeLifecycleController, err := NewCloudNodeLifecycleController(
3948
completedConfig.SharedInformers.Core().V1().Nodes(),
40-
// cloud node lifecycle controller uses existing cluster role from node-controller
41-
completedConfig.ClientBuilder.ClientOrDie(initContext.ClientName),
49+
ccmClientSet,
4250
cloud,
4351
completedConfig.ComponentConfig.KubeCloudShared.NodeMonitorPeriod.Duration,
4452
)
@@ -54,19 +62,24 @@ func startCloudNodeLifecycleController(ctx context.Context,
5462
}
5563

5664
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-
65+
volumeAttachments, listErr := kubeClient.StorageV1().VolumeAttachments().List(ctx, metav1.ListOptions{})
6066
if listErr != nil {
61-
return fmt.Errorf("failed to list volume attachments for node %s: %w", nodeName, listErr)
67+
return fmt.Errorf("failed to list all volume attachments: %w", listErr)
6268
}
6369

6470
for index := range len(volumeAttachments.Items) {
6571
volumeAttachment := volumeAttachments.Items[index]
72+
if volumeAttachment.Spec.NodeName != nodeName {
73+
continue
74+
}
6675
deleteErr := kubeClient.StorageV1().VolumeAttachments().Delete(ctx, volumeAttachment.Name, metav1.DeleteOptions{})
6776
if deleteErr != nil {
68-
klog.Errorf("failed to delete volume attachment %s for node %s: %v",
69-
volumeAttachment.Name, nodeName, deleteErr)
77+
if errors.IsNotFound(deleteErr) {
78+
klog.Infof("volume attachment %s for node %s already deleted, skipping delete", volumeAttachment.Name, nodeName)
79+
} else {
80+
klog.Errorf("failed to delete volume attachment %s for node %s: %v",
81+
volumeAttachment.Name, nodeName, deleteErr)
82+
}
7083
}
7184
}
7285

0 commit comments

Comments
 (0)