Skip to content

Commit 5f5a8ca

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

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

Diff for: internal/node/utils.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package node
33
import (
44
"context"
55
"fmt"
6-
6+
"k8s.io/apimachinery/pkg/api/errors"
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
clientset "k8s.io/client-go/kubernetes"
99
cloudprovider "k8s.io/cloud-provider"
@@ -29,16 +29,22 @@ func StartCloudNodeLifecycleControllerWrapper(initContext app.ControllerInitCont
2929

3030
//nolint:gocritic // need to follow upstream function signature
3131
func startCloudNodeLifecycleController(ctx context.Context,
32-
initContext app.ControllerInitContext,
32+
_ app.ControllerInitContext,
3333
controlexContext controllermanagerapp.ControllerContext,
3434
completedConfig *config.CompletedConfig,
3535
cloud cloudprovider.Interface,
3636
) (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+
3744
// Start the cloudNodeLifecycleController
3845
cloudNodeLifecycleController, err := NewCloudNodeLifecycleController(
3946
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,
4248
cloud,
4349
completedConfig.ComponentConfig.KubeCloudShared.NodeMonitorPeriod.Duration,
4450
)
@@ -54,19 +60,24 @@ func startCloudNodeLifecycleController(ctx context.Context,
5460
}
5561

5662
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{})
6064
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)
6266
}
6367

6468
for index := range len(volumeAttachments.Items) {
6569
volumeAttachment := volumeAttachments.Items[index]
70+
if volumeAttachment.Spec.NodeName != nodeName {
71+
continue
72+
}
6673
deleteErr := kubeClient.StorageV1().VolumeAttachments().Delete(ctx, volumeAttachment.Name, metav1.DeleteOptions{})
6774
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+
}
7081
}
7182
}
7283

0 commit comments

Comments
 (0)