From 45aa59946aff65edb51fef5863d436bd836462b6 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Fri, 5 May 2023 12:36:34 +0200 Subject: [PATCH] Refactor FindAttachablePluginBySpec out of CSI code path reconstructVolume() is called when kubelet may not have connection to the API server yet, therefore it cannot get CSIDriver instances to figure out if a CSI volume is attachable or not. Refactor reconstructVolume(), so it does not need FindAttachablePluginBySpec for CSI volumes, because all of them are deviceMountable (i.e. FindDeviceMountablePluginBySpec always returns the CSI volume plugin). --- .../reconciler/reconstruct_common.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/kubelet/volumemanager/reconciler/reconstruct_common.go b/pkg/kubelet/volumemanager/reconciler/reconstruct_common.go index ca44dda1839..57e534d9a0e 100644 --- a/pkg/kubelet/volumemanager/reconciler/reconstruct_common.go +++ b/pkg/kubelet/volumemanager/reconciler/reconstruct_common.go @@ -236,17 +236,28 @@ func (rc *reconciler) reconstructVolume(volume podVolume) (rvolume *reconstructe // Searching by spec checks whether the volume is actually attachable // (i.e. has a PV) whereas searching by plugin name can only tell whether // the plugin supports attachable volumes. - attachablePlugin, err := rc.volumePluginMgr.FindAttachablePluginBySpec(volumeSpec) - if err != nil { - return nil, err - } deviceMountablePlugin, err := rc.volumePluginMgr.FindDeviceMountablePluginBySpec(volumeSpec) if err != nil { return nil, err } + // The unique volume name used depends on whether the volume is attachable/device-mountable + // (needsNameFromSpec = true) or not. + needsNameFromSpec := deviceMountablePlugin != nil + if !needsNameFromSpec { + // Check attach-ability of a volume only as a fallback to avoid calling + // FindAttachablePluginBySpec for CSI volumes - it needs a connection to the API server, + // but it may not be available at this stage of kubelet startup. + // All CSI volumes are device-mountable, so they won't reach this code. + attachablePlugin, err := rc.volumePluginMgr.FindAttachablePluginBySpec(volumeSpec) + if err != nil { + return nil, err + } + needsNameFromSpec = attachablePlugin != nil + } + var uniqueVolumeName v1.UniqueVolumeName - if attachablePlugin != nil || deviceMountablePlugin != nil { + if needsNameFromSpec { uniqueVolumeName, err = util.GetUniqueVolumeNameFromSpec(plugin, volumeSpec) if err != nil { return nil, err