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.
k3s-v1.14.6
Darren Shepherd 2019-04-07 09:34:40 -07:00 committed by Erik Wilson
parent 94beca160e
commit 4ce15bb26b
2 changed files with 38 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import (
"path" "path"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"context" "context"
@ -60,6 +61,12 @@ const (
csiResyncPeriod = time.Minute 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"} var deprecatedSocketDirVersions = []string{"0.1.0", "0.2.0", "0.3.0", "0.4.0"}
type csiPlugin struct { type csiPlugin struct {
@ -76,11 +83,18 @@ const ephemeralDriverMode driverMode = "ephemeral"
// ProbeVolumePlugins returns implemented plugins // ProbeVolumePlugins returns implemented plugins
func ProbeVolumePlugins() []volume.VolumePlugin { func ProbeVolumePlugins() []volume.VolumePlugin {
p := &csiPlugin{ csiPluginLock.Lock()
defer csiPluginLock.Unlock()
if csiPluginInstance != nil {
return []volume.VolumePlugin{csiPluginInstance}
}
csiPluginInstance = &csiPlugin{
host: nil, host: nil,
blockEnabled: utilfeature.DefaultFeatureGate.Enabled(features.CSIBlockVolume), blockEnabled: utilfeature.DefaultFeatureGate.Enabled(features.CSIBlockVolume),
} }
return []volume.VolumePlugin{p} return []volume.VolumePlugin{csiPluginInstance}
} }
// volume.VolumePlugin methods // volume.VolumePlugin methods
@ -204,6 +218,21 @@ func (h *RegistrationHandler) DeRegisterPlugin(pluginName string) {
} }
func (p *csiPlugin) Init(host volume.VolumeHost) error { 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 p.host = host
if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) { if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) {

View File

@ -18,10 +18,6 @@ package volume
import ( import (
"fmt" "fmt"
"net"
"strings"
"sync"
authenticationv1 "k8s.io/api/authentication/v1" authenticationv1 "k8s.io/api/authentication/v1"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
@ -40,6 +36,9 @@ import (
"k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/volume/util/recyclerclient" "k8s.io/kubernetes/pkg/volume/util/recyclerclient"
"k8s.io/kubernetes/pkg/volume/util/subpath" "k8s.io/kubernetes/pkg/volume/util/subpath"
"net"
"strings"
"sync"
) )
type ProbeOperation uint32 type ProbeOperation uint32
@ -570,6 +569,10 @@ func (pm *VolumePluginMgr) InitPlugins(plugins []VolumePlugin, prober DynamicPlu
pm.mutex.Lock() pm.mutex.Lock()
defer pm.mutex.Unlock() defer pm.mutex.Unlock()
if pm.Host != nil {
return nil
}
pm.Host = host pm.Host = host
if prober == nil { if prober == nil {