From 4ce15bb26bd0e1db7e6b3095878d180413de6d70 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Sun, 7 Apr 2019 09:34:40 -0700 Subject: [PATCH] Fix CSI when running kubelet and controller-manager in the same process Both kubelet and controller-manager init the volume plugins. We added a global flag that can be set that will force the init to wait for a valid hostname. --- pkg/volume/csi/csi_plugin.go | 33 +++++++++++++++++++++++++++++++-- pkg/volume/plugins.go | 11 +++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/pkg/volume/csi/csi_plugin.go b/pkg/volume/csi/csi_plugin.go index 310aee5e79..3c69686252 100644 --- a/pkg/volume/csi/csi_plugin.go +++ b/pkg/volume/csi/csi_plugin.go @@ -23,6 +23,7 @@ import ( "path" "sort" "strings" + "sync" "time" "context" @@ -60,6 +61,12 @@ const ( csiResyncPeriod = time.Minute ) +var ( + WaitForValidHostName bool + csiPluginInstance *csiPlugin + csiPluginLock sync.Mutex +) + var deprecatedSocketDirVersions = []string{"0.1.0", "0.2.0", "0.3.0", "0.4.0"} type csiPlugin struct { @@ -76,11 +83,18 @@ const ephemeralDriverMode driverMode = "ephemeral" // ProbeVolumePlugins returns implemented plugins func ProbeVolumePlugins() []volume.VolumePlugin { - p := &csiPlugin{ + csiPluginLock.Lock() + defer csiPluginLock.Unlock() + + if csiPluginInstance != nil { + return []volume.VolumePlugin{csiPluginInstance} + } + + csiPluginInstance = &csiPlugin{ host: nil, blockEnabled: utilfeature.DefaultFeatureGate.Enabled(features.CSIBlockVolume), } - return []volume.VolumePlugin{p} + return []volume.VolumePlugin{csiPluginInstance} } // volume.VolumePlugin methods @@ -204,6 +218,21 @@ func (h *RegistrationHandler) DeRegisterPlugin(pluginName string) { } func (p *csiPlugin) Init(host volume.VolumeHost) error { + csiPluginLock.Lock() + defer csiPluginLock.Unlock() + + if WaitForValidHostName && host.GetHostName() == "" { + for { + if p.host != nil { + return nil + } + csiPluginLock.Unlock() + time.Sleep(time.Second) + klog.Infof("Waiting for CSI volume hostname") + csiPluginLock.Lock() + } + } + p.host = host if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) { diff --git a/pkg/volume/plugins.go b/pkg/volume/plugins.go index 2098fc33b3..1ab3748ed2 100644 --- a/pkg/volume/plugins.go +++ b/pkg/volume/plugins.go @@ -18,10 +18,6 @@ package volume import ( "fmt" - "net" - "strings" - "sync" - authenticationv1 "k8s.io/api/authentication/v1" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -40,6 +36,9 @@ import ( "k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/volume/util/recyclerclient" "k8s.io/kubernetes/pkg/volume/util/subpath" + "net" + "strings" + "sync" ) type ProbeOperation uint32 @@ -570,6 +569,10 @@ func (pm *VolumePluginMgr) InitPlugins(plugins []VolumePlugin, prober DynamicPlu pm.mutex.Lock() defer pm.mutex.Unlock() + if pm.Host != nil { + return nil + } + pm.Host = host if prober == nil {