Unverified Commit 693b636a authored by Kubernetes Prow Robot's avatar Kubernetes Prow Robot Committed by GitHub

Merge pull request #75366 from cofyc/automated-cherry-pick-of-#74652-upstream-release-1.13

Automated cherry pick of #74652: Delay CSI client initialization
parents 6fbd00e6 5eeb19bb
...@@ -37,8 +37,8 @@ import ( ...@@ -37,8 +37,8 @@ import (
) )
type csiBlockMapper struct { type csiBlockMapper struct {
csiClientGetter
k8s kubernetes.Interface k8s kubernetes.Interface
csiClient csiClient
plugin *csiPlugin plugin *csiPlugin
driverName csiDriverName driverName csiDriverName
specName string specName string
...@@ -247,14 +247,20 @@ func (m *csiBlockMapper) SetUpDevice() (string, error) { ...@@ -247,14 +247,20 @@ func (m *csiBlockMapper) SetUpDevice() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
defer cancel() defer cancel()
csiClient, err := m.csiClientGetter.Get()
if err != nil {
klog.Error(log("blockMapper.SetUpDevice failed to get CSI client: %v", err))
return "", err
}
// Call NodeStageVolume // Call NodeStageVolume
stagingPath, err := m.stageVolumeForBlock(ctx, m.csiClient, accessMode, csiSource, attachment) stagingPath, err := m.stageVolumeForBlock(ctx, csiClient, accessMode, csiSource, attachment)
if err != nil { if err != nil {
return "", err return "", err
} }
// Call NodePublishVolume // Call NodePublishVolume
publishPath, err := m.publishVolumeForBlock(ctx, m.csiClient, accessMode, csiSource, attachment, stagingPath) publishPath, err := m.publishVolumeForBlock(ctx, csiClient, accessMode, csiSource, attachment, stagingPath)
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -326,6 +332,12 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error ...@@ -326,6 +332,12 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
defer cancel() defer cancel()
csiClient, err := m.csiClientGetter.Get()
if err != nil {
klog.Error(log("blockMapper.TearDownDevice failed to get CSI client: %v", err))
return err
}
// Call NodeUnpublishVolume // Call NodeUnpublishVolume
publishPath := m.getPublishPath() publishPath := m.getPublishPath()
if _, err := os.Stat(publishPath); err != nil { if _, err := os.Stat(publishPath); err != nil {
...@@ -335,7 +347,7 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error ...@@ -335,7 +347,7 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
return err return err
} }
} else { } else {
err := m.unpublishVolumeForBlock(ctx, m.csiClient, publishPath) err := m.unpublishVolumeForBlock(ctx, csiClient, publishPath)
if err != nil { if err != nil {
return err return err
} }
...@@ -350,7 +362,7 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error ...@@ -350,7 +362,7 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
return err return err
} }
} else { } else {
err := m.unstageVolumeForBlock(ctx, m.csiClient, stagingPath) err := m.unstageVolumeForBlock(ctx, csiClient, stagingPath)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"sync"
"time" "time"
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi" csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
...@@ -720,3 +721,36 @@ func versionRequiresV0Client(version *utilversion.Version) bool { ...@@ -720,3 +721,36 @@ func versionRequiresV0Client(version *utilversion.Version) bool {
return false return false
} }
// CSI client getter with cache.
// This provides a method to initialize CSI client with driver name and caches
// it for later use. When CSI clients have not been discovered yet (e.g.
// on kubelet restart), client initialization will fail. Users of CSI client (e.g.
// mounter manager and block mapper) can use this to delay CSI client
// initialization until needed.
type csiClientGetter struct {
sync.RWMutex
csiClient csiClient
driverName csiDriverName
}
func (c *csiClientGetter) Get() (csiClient, error) {
c.RLock()
if c.csiClient != nil {
c.RUnlock()
return c.csiClient, nil
}
c.RUnlock()
c.Lock()
defer c.Unlock()
// Double-checking locking criterion.
if c.csiClient != nil {
return c.csiClient, nil
}
csi, err := newCsiDriverClient(c.driverName)
if err != nil {
return nil, err
}
c.csiClient = csi
return c.csiClient, nil
}
...@@ -55,7 +55,7 @@ var ( ...@@ -55,7 +55,7 @@ var (
) )
type csiMountMgr struct { type csiMountMgr struct {
csiClient csiClient csiClientGetter
k8s kubernetes.Interface k8s kubernetes.Interface
plugin *csiPlugin plugin *csiPlugin
driverName csiDriverName driverName csiDriverName
...@@ -115,7 +115,11 @@ func (c *csiMountMgr) SetUpAt(dir string, fsGroup *int64) error { ...@@ -115,7 +115,11 @@ func (c *csiMountMgr) SetUpAt(dir string, fsGroup *int64) error {
return err return err
} }
csi := c.csiClient csi, err := c.csiClientGetter.Get()
if err != nil {
klog.Error(log("mounter.SetUpAt failed to get CSI client: %v", err))
return err
}
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
defer cancel() defer cancel()
...@@ -304,7 +308,11 @@ func (c *csiMountMgr) TearDownAt(dir string) error { ...@@ -304,7 +308,11 @@ func (c *csiMountMgr) TearDownAt(dir string) error {
} }
volID := c.volumeID volID := c.volumeID
csi := c.csiClient csi, err := c.csiClientGetter.Get()
if err != nil {
klog.Error(log("mounter.SetUpAt failed to get CSI client: %v", err))
return err
}
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
defer cancel() defer cancel()
......
...@@ -302,11 +302,6 @@ func (p *csiPlugin) NewMounter( ...@@ -302,11 +302,6 @@ func (p *csiPlugin) NewMounter(
return nil, errors.New("failed to get a Kubernetes client") return nil, errors.New("failed to get a Kubernetes client")
} }
csi, err := newCsiDriverClient(csiDriverName(pvSource.Driver))
if err != nil {
return nil, err
}
mounter := &csiMountMgr{ mounter := &csiMountMgr{
plugin: p, plugin: p,
k8s: k8s, k8s: k8s,
...@@ -316,9 +311,9 @@ func (p *csiPlugin) NewMounter( ...@@ -316,9 +311,9 @@ func (p *csiPlugin) NewMounter(
driverName: csiDriverName(pvSource.Driver), driverName: csiDriverName(pvSource.Driver),
volumeID: pvSource.VolumeHandle, volumeID: pvSource.VolumeHandle,
specVolumeID: spec.Name(), specVolumeID: spec.Name(),
csiClient: csi,
readOnly: readOnly, readOnly: readOnly,
} }
mounter.csiClientGetter.driverName = csiDriverName(pvSource.Driver)
// Save volume info in pod dir // Save volume info in pod dir
dir := mounter.GetPath() dir := mounter.GetPath()
...@@ -374,10 +369,7 @@ func (p *csiPlugin) NewUnmounter(specName string, podUID types.UID) (volume.Unmo ...@@ -374,10 +369,7 @@ func (p *csiPlugin) NewUnmounter(specName string, podUID types.UID) (volume.Unmo
} }
unmounter.driverName = csiDriverName(data[volDataKey.driverName]) unmounter.driverName = csiDriverName(data[volDataKey.driverName])
unmounter.volumeID = data[volDataKey.volHandle] unmounter.volumeID = data[volDataKey.volHandle]
unmounter.csiClient, err = newCsiDriverClient(unmounter.driverName) unmounter.csiClientGetter.driverName = unmounter.driverName
if err != nil {
return nil, err
}
return unmounter, nil return unmounter, nil
} }
...@@ -489,10 +481,6 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt ...@@ -489,10 +481,6 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt
} }
klog.V(4).Info(log("setting up block mapper for [volume=%v,driver=%v]", pvSource.VolumeHandle, pvSource.Driver)) klog.V(4).Info(log("setting up block mapper for [volume=%v,driver=%v]", pvSource.VolumeHandle, pvSource.Driver))
client, err := newCsiDriverClient(csiDriverName(pvSource.Driver))
if err != nil {
return nil, err
}
k8s := p.host.GetKubeClient() k8s := p.host.GetKubeClient()
if k8s == nil { if k8s == nil {
...@@ -501,7 +489,6 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt ...@@ -501,7 +489,6 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt
} }
mapper := &csiBlockMapper{ mapper := &csiBlockMapper{
csiClient: client,
k8s: k8s, k8s: k8s,
plugin: p, plugin: p,
volumeID: pvSource.VolumeHandle, volumeID: pvSource.VolumeHandle,
...@@ -511,6 +498,7 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt ...@@ -511,6 +498,7 @@ func (p *csiPlugin) NewBlockVolumeMapper(spec *volume.Spec, podRef *api.Pod, opt
specName: spec.Name(), specName: spec.Name(),
podUID: podRef.UID, podUID: podRef.UID,
} }
mapper.csiClientGetter.driverName = csiDriverName(pvSource.Driver)
// Save volume info in pod dir // Save volume info in pod dir
dataDir := getVolumeDeviceDataDir(spec.Name(), p.host) dataDir := getVolumeDeviceDataDir(spec.Name(), p.host)
...@@ -565,7 +553,7 @@ func (p *csiPlugin) NewBlockVolumeUnmapper(volName string, podUID types.UID) (vo ...@@ -565,7 +553,7 @@ func (p *csiPlugin) NewBlockVolumeUnmapper(volName string, podUID types.UID) (vo
} }
unmapper.driverName = csiDriverName(data[volDataKey.driverName]) unmapper.driverName = csiDriverName(data[volDataKey.driverName])
unmapper.volumeID = data[volDataKey.volHandle] unmapper.volumeID = data[volDataKey.volHandle]
unmapper.csiClient, err = newCsiDriverClient(unmapper.driverName) unmapper.csiClientGetter.driverName = unmapper.driverName
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -268,7 +268,8 @@ func TestPluginNewMounter(t *testing.T) { ...@@ -268,7 +268,8 @@ func TestPluginNewMounter(t *testing.T) {
if csiMounter.podUID == types.UID("") { if csiMounter.podUID == types.UID("") {
t.Error("mounter podUID not set") t.Error("mounter podUID not set")
} }
if csiMounter.csiClient == nil { csiClient, err := csiMounter.csiClientGetter.Get()
if csiClient == nil {
t.Error("mounter csiClient is nil") t.Error("mounter csiClient is nil")
} }
...@@ -327,8 +328,9 @@ func TestPluginNewUnmounter(t *testing.T) { ...@@ -327,8 +328,9 @@ func TestPluginNewUnmounter(t *testing.T) {
t.Error("podUID not set") t.Error("podUID not set")
} }
if csiUnmounter.csiClient == nil { csiClient, err := csiUnmounter.csiClientGetter.Get()
t.Error("unmounter csiClient is nil") if csiClient == nil {
t.Error("mounter csiClient is nil")
} }
} }
...@@ -405,7 +407,8 @@ func TestPluginNewBlockMapper(t *testing.T) { ...@@ -405,7 +407,8 @@ func TestPluginNewBlockMapper(t *testing.T) {
if csiMapper.podUID == types.UID("") { if csiMapper.podUID == types.UID("") {
t.Error("CSI block mapper missing pod.UID") t.Error("CSI block mapper missing pod.UID")
} }
if csiMapper.csiClient == nil { csiClient, err := csiMapper.csiClientGetter.Get()
if csiClient == nil {
t.Error("mapper csiClient is nil") t.Error("mapper csiClient is nil")
} }
...@@ -467,7 +470,8 @@ func TestPluginNewUnmapper(t *testing.T) { ...@@ -467,7 +470,8 @@ func TestPluginNewUnmapper(t *testing.T) {
t.Error("specName not set") t.Error("specName not set")
} }
if csiUnmapper.csiClient == nil { csiClient, err := csiUnmapper.csiClientGetter.Get()
if csiClient == nil {
t.Error("unmapper csiClient is nil") t.Error("unmapper csiClient is nil")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment