Refactor to use new csi.DriversList & csi.Driver

pull/564/head
Hannes Hörl 2019-01-25 12:00:31 +00:00
parent 96aa4fb5d2
commit 84c46629c5
3 changed files with 22 additions and 53 deletions

View File

@ -139,19 +139,12 @@ func newCsiDriverClient(driverName csiDriverName) (*csiDriverClient, error) {
addr := fmt.Sprintf(csiAddrTemplate, driverName) addr := fmt.Sprintf(csiAddrTemplate, driverName)
requiresV0Client := true requiresV0Client := true
if utilfeature.DefaultFeatureGate.Enabled(features.KubeletPluginsWatcher) { if utilfeature.DefaultFeatureGate.Enabled(features.KubeletPluginsWatcher) {
var existingDriver csiDriver existingDriver, driverExists := csiDrivers.Get(string(driverName))
driverExists := false
func() {
csiDrivers.RLock()
defer csiDrivers.RUnlock()
existingDriver, driverExists = csiDrivers.driversMap[string(driverName)]
}()
if !driverExists { if !driverExists {
return nil, fmt.Errorf("driver name %s not found in the list of registered CSI drivers", driverName) return nil, fmt.Errorf("driver name %s not found in the list of registered CSI drivers", driverName)
} }
addr = existingDriver.driverEndpoint addr = existingDriver.endpoint
requiresV0Client = versionRequiresV0Client(existingDriver.highestSupportedVersion) requiresV0Client = versionRequiresV0Client(existingDriver.highestSupportedVersion)
} }

View File

@ -23,7 +23,6 @@ import (
"path" "path"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"context" "context"
@ -84,17 +83,6 @@ func ProbeVolumePlugins() []volume.VolumePlugin {
// volume.VolumePlugin methods // volume.VolumePlugin methods
var _ volume.VolumePlugin = &csiPlugin{} var _ volume.VolumePlugin = &csiPlugin{}
type csiDriver struct {
driverName string
driverEndpoint string
highestSupportedVersion *utilversion.Version
}
type csiDriversStore struct {
driversMap map[string]csiDriver
sync.RWMutex
}
// RegistrationHandler is the handler which is fed to the pluginwatcher API. // RegistrationHandler is the handler which is fed to the pluginwatcher API.
type RegistrationHandler struct { type RegistrationHandler struct {
} }
@ -102,7 +90,7 @@ type RegistrationHandler struct {
// TODO (verult) consider using a struct instead of global variables // TODO (verult) consider using a struct instead of global variables
// csiDrivers map keep track of all registered CSI drivers on the node and their // csiDrivers map keep track of all registered CSI drivers on the node and their
// corresponding sockets // corresponding sockets
var csiDrivers csiDriversStore var csiDrivers = &DriversStore{}
var nim nodeinfomanager.Interface var nim nodeinfomanager.Interface
@ -141,17 +129,12 @@ func (h *RegistrationHandler) RegisterPlugin(pluginName string, endpoint string,
return err return err
} }
func() { // Storing endpoint of newly registered CSI driver into the map, where CSI driver name will be the key
// Storing endpoint of newly registered CSI driver into the map, where CSI driver name will be the key // all other CSI components will be able to get the actual socket of CSI drivers by its name.
// all other CSI components will be able to get the actual socket of CSI drivers by its name. csiDrivers.Set(pluginName, Driver{
endpoint: endpoint,
// It's not necessary to lock the entire RegistrationCallback() function because only the CSI highestSupportedVersion: highestSupportedVersion,
// client depends on this driver map, and the CSI client does not depend on node information })
// updated in the rest of the function.
csiDrivers.Lock()
defer csiDrivers.Unlock()
csiDrivers.driversMap[pluginName] = csiDriver{driverName: pluginName, driverEndpoint: endpoint, highestSupportedVersion: highestSupportedVersion}
}()
// Get node info from the driver. // Get node info from the driver.
csi, err := newCsiDriverClient(csiDriverName(pluginName)) csi, err := newCsiDriverClient(csiDriverName(pluginName))
@ -201,15 +184,7 @@ func (h *RegistrationHandler) validateVersions(callerName, pluginName string, en
return nil, err return nil, err
} }
// Check for existing drivers with the same name existingDriver, driverExists := csiDrivers.Get(pluginName)
var existingDriver csiDriver
driverExists := false
func() {
csiDrivers.RLock()
defer csiDrivers.RUnlock()
existingDriver, driverExists = csiDrivers.driversMap[pluginName]
}()
if driverExists { if driverExists {
if !existingDriver.highestSupportedVersion.LessThan(newDriverHighestVersion) { if !existingDriver.highestSupportedVersion.LessThan(newDriverHighestVersion) {
err := fmt.Errorf("%s for CSI driver %q failed. Another driver with the same name is already registered with a higher supported version: %q", callerName, pluginName, existingDriver.highestSupportedVersion) err := fmt.Errorf("%s for CSI driver %q failed. Another driver with the same name is already registered with a higher supported version: %q", callerName, pluginName, existingDriver.highestSupportedVersion)
@ -246,8 +221,7 @@ func (p *csiPlugin) Init(host volume.VolumeHost) error {
} }
} }
// Initializing csiDrivers map and label management channels // Initializing the label management channels
csiDrivers = csiDriversStore{driversMap: map[string]csiDriver{}}
nim = nodeinfomanager.NewNodeInfoManager(host.GetNodeName(), host) nim = nodeinfomanager.NewNodeInfoManager(host.GetNodeName(), host)
// TODO(#70514) Init CSINodeInfo object if the CRD exists and create Driver // TODO(#70514) Init CSINodeInfo object if the CRD exists and create Driver
@ -658,11 +632,7 @@ func (p *csiPlugin) getPublishContext(client clientset.Interface, handle, driver
} }
func unregisterDriver(driverName string) error { func unregisterDriver(driverName string) error {
func() { csiDrivers.Delete(driverName)
csiDrivers.Lock()
defer csiDrivers.Unlock()
delete(csiDrivers.driversMap, driverName)
}()
if err := nim.UninstallCSIDriver(driverName); err != nil { if err := nim.UninstallCSIDriver(driverName); err != nil {
klog.Errorf("Error uninstalling CSI driver: %v", err) klog.Errorf("Error uninstalling CSI driver: %v", err)

View File

@ -105,13 +105,16 @@ func makeTestPV(name string, sizeGig int, driverName, volID string) *api.Persist
} }
func registerFakePlugin(pluginName, endpoint string, versions []string, t *testing.T) { func registerFakePlugin(pluginName, endpoint string, versions []string, t *testing.T) {
csiDrivers = csiDriversStore{driversMap: map[string]csiDriver{}}
highestSupportedVersions, err := highestSupportedVersion(versions) highestSupportedVersions, err := highestSupportedVersion(versions)
if err != nil { if err != nil {
t.Fatalf("unexpected error parsing versions (%v) for pluginName % q endpoint %q: %#v", versions, pluginName, endpoint, err) t.Fatalf("unexpected error parsing versions (%v) for pluginName % q endpoint %q: %#v", versions, pluginName, endpoint, err)
} }
csiDrivers.driversMap[pluginName] = csiDriver{driverName: pluginName, driverEndpoint: endpoint, highestSupportedVersion: highestSupportedVersions} csiDrivers.Clear()
csiDrivers.Set(pluginName, Driver{
endpoint: endpoint,
highestSupportedVersion: highestSupportedVersions,
})
} }
func TestPluginGetPluginName(t *testing.T) { func TestPluginGetPluginName(t *testing.T) {
@ -839,13 +842,16 @@ func TestValidatePluginExistingDriver(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
// Arrange & Act // Arrange & Act
csiDrivers = csiDriversStore{driversMap: map[string]csiDriver{}}
highestSupportedVersions1, err := highestSupportedVersion(tc.versions1) highestSupportedVersions1, err := highestSupportedVersion(tc.versions1)
if err != nil { if err != nil {
t.Fatalf("unexpected error parsing version for testcase: %#v", tc) t.Fatalf("unexpected error parsing version for testcase: %#v", tc)
} }
csiDrivers.driversMap[tc.pluginName1] = csiDriver{driverName: tc.pluginName1, driverEndpoint: tc.endpoint1, highestSupportedVersion: highestSupportedVersions1} csiDrivers.Clear()
csiDrivers.Set(tc.pluginName1, Driver{
endpoint: tc.endpoint1,
highestSupportedVersion: highestSupportedVersions1,
})
// Arrange & Act // Arrange & Act
err = PluginHandler.ValidatePlugin(tc.pluginName2, tc.endpoint2, tc.versions2, tc.foundInDeprecatedDir2) err = PluginHandler.ValidatePlugin(tc.pluginName2, tc.endpoint2, tc.versions2, tc.foundInDeprecatedDir2)