ScaleIO - API source code update

This commit tracks all human-generated code for API source updates.
This commit is contained in:
Vladimir Vivien
2017-10-13 22:57:37 -04:00
parent 7b588817ca
commit 3fb3cc7122
14 changed files with 455 additions and 154 deletions

View File

@@ -31,11 +31,17 @@ import (
volutil "k8s.io/kubernetes/pkg/volume/util"
)
type volSourceAttribs struct {
volName,
fsType string
readOnly bool
}
var (
confKey = struct {
gateway,
sslEnabled,
secretRef,
secretName,
system,
protectionDomain,
storagePool,
@@ -47,12 +53,13 @@ var (
readOnly,
username,
password,
namespace,
secretNamespace,
sdcGuid string
}{
gateway: "gateway",
sslEnabled: "sslEnabled",
secretRef: "secretRef",
secretName: "secretRef",
secretNamespace: "secretNamespace",
system: "system",
protectionDomain: "protectionDomain",
storagePool: "storagePool",
@@ -64,7 +71,6 @@ var (
readOnly: "readOnly",
username: "username",
password: "password",
namespace: "namespace",
sdcGuid: "sdcGuid",
}
sdcGuidLabelName = "scaleio.sdcGuid"
@@ -79,23 +85,32 @@ var (
protectionDomainNotProvidedErr = errors.New("ScaleIO protection domain not provided")
)
// mapScaleIOVolumeSource maps attributes from a ScaleIOVolumeSource to config
func mapVolumeSource(config map[string]string, source *api.ScaleIOVolumeSource) {
config[confKey.gateway] = source.Gateway
config[confKey.secretRef] = func() string {
if source.SecretRef != nil {
return string(source.SecretRef.Name)
}
return ""
}()
config[confKey.system] = source.System
config[confKey.volumeName] = source.VolumeName
config[confKey.sslEnabled] = strconv.FormatBool(source.SSLEnabled)
config[confKey.protectionDomain] = source.ProtectionDomain
config[confKey.storagePool] = source.StoragePool
config[confKey.storageMode] = source.StorageMode
config[confKey.fsType] = source.FSType
config[confKey.readOnly] = strconv.FormatBool(source.ReadOnly)
// mapVolumeSpec maps attributes from either ScaleIOVolumeSource or ScaleIOPersistentVolumeSource to config
func mapVolumeSpec(config map[string]string, spec *volume.Spec) {
if source, err := getScaleIOPersistentVolumeSourceFromSpec(spec); err == nil {
config[confKey.gateway] = source.Gateway
config[confKey.system] = source.System
config[confKey.volumeName] = source.VolumeName
config[confKey.sslEnabled] = strconv.FormatBool(source.SSLEnabled)
config[confKey.protectionDomain] = source.ProtectionDomain
config[confKey.storagePool] = source.StoragePool
config[confKey.storageMode] = source.StorageMode
config[confKey.fsType] = source.FSType
config[confKey.readOnly] = strconv.FormatBool(source.ReadOnly)
}
if source, err := getScaleIOVolumeSourceFromSpec(spec); err == nil {
config[confKey.gateway] = source.Gateway
config[confKey.system] = source.System
config[confKey.volumeName] = source.VolumeName
config[confKey.sslEnabled] = strconv.FormatBool(source.SSLEnabled)
config[confKey.protectionDomain] = source.ProtectionDomain
config[confKey.storagePool] = source.StoragePool
config[confKey.storageMode] = source.StorageMode
config[confKey.fsType] = source.FSType
config[confKey.readOnly] = strconv.FormatBool(source.ReadOnly)
}
//optionals
applyConfigDefaults(config)
@@ -105,7 +120,7 @@ func validateConfigs(config map[string]string) error {
if config[confKey.gateway] == "" {
return gatewayNotProvidedErr
}
if config[confKey.secretRef] == "" {
if config[confKey.secretName] == "" {
return secretRefNotProvidedErr
}
if config[confKey.system] == "" {
@@ -202,7 +217,7 @@ func saveConfig(configName string, data map[string]string) error {
// attachSecret loads secret object and attaches to configData
func attachSecret(plug *sioPlugin, namespace string, configData map[string]string) error {
// load secret
secretRefName := configData[confKey.secretRef]
secretRefName := configData[confKey.secretName]
kubeClient := plug.host.GetKubeClient()
secretMap, err := volutil.GetSecretForPV(namespace, secretRefName, sioPluginName, kubeClient)
if err != nil {
@@ -244,8 +259,8 @@ func getSdcGuidLabel(plug *sioPlugin) (string, error) {
return label, nil
}
// getVolumeSourceFromSpec safely extracts ScaleIOVolumeSource from spec
func getVolumeSourceFromSpec(spec *volume.Spec) (*api.ScaleIOVolumeSource, error) {
// getVolumeSourceFromSpec safely extracts ScaleIOVolumeSource or ScaleIOPersistentVolumeSource from spec
func getVolumeSourceFromSpec(spec *volume.Spec) (interface{}, error) {
if spec.Volume != nil && spec.Volume.ScaleIO != nil {
return spec.Volume.ScaleIO, nil
}
@@ -257,6 +272,66 @@ func getVolumeSourceFromSpec(spec *volume.Spec) (*api.ScaleIOVolumeSource, error
return nil, fmt.Errorf("ScaleIO not defined in spec")
}
func getVolumeSourceAttribs(spec *volume.Spec) (*volSourceAttribs, error) {
attribs := new(volSourceAttribs)
if pvSource, err := getScaleIOPersistentVolumeSourceFromSpec(spec); err == nil {
attribs.volName = pvSource.VolumeName
attribs.fsType = pvSource.FSType
attribs.readOnly = pvSource.ReadOnly
} else if pSource, err := getScaleIOVolumeSourceFromSpec(spec); err == nil {
attribs.volName = pSource.VolumeName
attribs.fsType = pSource.FSType
attribs.readOnly = pSource.ReadOnly
} else {
msg := log("failed to get ScaleIOVolumeSource or ScaleIOPersistentVolumeSource from spec")
glog.Error(msg)
return nil, errors.New(msg)
}
return attribs, nil
}
func getScaleIOPersistentVolumeSourceFromSpec(spec *volume.Spec) (*api.ScaleIOPersistentVolumeSource, error) {
source, err := getVolumeSourceFromSpec(spec)
if err != nil {
return nil, err
}
if val, ok := source.(*api.ScaleIOPersistentVolumeSource); ok {
return val, nil
}
return nil, fmt.Errorf("spec is not a valid ScaleIOPersistentVolume type")
}
func getScaleIOVolumeSourceFromSpec(spec *volume.Spec) (*api.ScaleIOVolumeSource, error) {
source, err := getVolumeSourceFromSpec(spec)
if err != nil {
return nil, err
}
if val, ok := source.(*api.ScaleIOVolumeSource); ok {
return val, nil
}
return nil, fmt.Errorf("spec is not a valid ScaleIOVolume type")
}
func getSecretAndNamespaceFromSpec(spec *volume.Spec, pod *api.Pod) (secretName string, secretNS string, err error) {
if source, err := getScaleIOVolumeSourceFromSpec(spec); err == nil {
secretName = source.SecretRef.Name
if pod != nil {
secretNS = pod.Namespace
}
} else if source, err := getScaleIOPersistentVolumeSourceFromSpec(spec); err == nil {
if source.SecretRef != nil {
secretName = source.SecretRef.Name
secretNS = source.SecretRef.Namespace
if secretNS == "" && pod != nil {
secretNS = pod.Namespace
}
}
} else {
return "", "", errors.New("failed to get ScaleIOVolumeSource or ScaleIOPersistentVolumeSource")
}
return secretName, secretNS, nil
}
func log(msg string, parts ...interface{}) string {
return fmt.Sprintf(fmt.Sprintf("scaleio: %s", msg), parts...)
}