mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
rebase: update K8s packages to v0.32.1
Update K8s packages in go.mod to v0.32.1 Signed-off-by: Praveen M <m.praveen@ibm.com>
This commit is contained in:
305
vendor/k8s.io/csi-translation-lib/plugins/aws_ebs.go
generated
vendored
Normal file
305
vendor/k8s.io/csi-translation-lib/plugins/aws_ebs.go
generated
vendored
Normal file
@ -0,0 +1,305 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// AWSEBSDriverName is the name of the CSI driver for EBS
|
||||
AWSEBSDriverName = "ebs.csi.aws.com"
|
||||
// AWSEBSInTreePluginName is the name of the intree plugin for EBS
|
||||
AWSEBSInTreePluginName = "kubernetes.io/aws-ebs"
|
||||
// AWSEBSTopologyKey is the zonal topology key for AWS EBS CSI driver
|
||||
AWSEBSTopologyKey = "topology." + AWSEBSDriverName + "/zone"
|
||||
// iopsPerGBKey is StorageClass parameter name that specifies IOPS
|
||||
// Per GB.
|
||||
iopsPerGBKey = "iopspergb"
|
||||
// allowIncreaseIOPSKey is parameter name that allows the CSI driver
|
||||
// to increase IOPS to the minimum value supported by AWS when IOPS
|
||||
// Per GB is too low for a given volume size. This preserves current
|
||||
// in-tree volume plugin behavior.
|
||||
allowIncreaseIOPSKey = "allowautoiopspergbincrease"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &awsElasticBlockStoreCSITranslator{}
|
||||
|
||||
// awsElasticBlockStoreTranslator handles translation of PV spec from In-tree EBS to CSI EBS and vice versa
|
||||
type awsElasticBlockStoreCSITranslator struct{}
|
||||
|
||||
// NewAWSElasticBlockStoreCSITranslator returns a new instance of awsElasticBlockStoreTranslator
|
||||
func NewAWSElasticBlockStoreCSITranslator() InTreePlugin {
|
||||
return &awsElasticBlockStoreCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree EBS storage class parameters to CSI storage class
|
||||
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
var (
|
||||
generatedTopologies []v1.TopologySelectorTerm
|
||||
params = map[string]string{}
|
||||
)
|
||||
for k, v := range sc.Parameters {
|
||||
switch strings.ToLower(k) {
|
||||
case fsTypeKey:
|
||||
params[csiFsTypeKey] = v
|
||||
case zoneKey:
|
||||
generatedTopologies = generateToplogySelectors(AWSEBSTopologyKey, []string{v})
|
||||
case zonesKey:
|
||||
generatedTopologies = generateToplogySelectors(AWSEBSTopologyKey, strings.Split(v, ","))
|
||||
case iopsPerGBKey:
|
||||
// Keep iopsPerGBKey
|
||||
params[k] = v
|
||||
// Preserve current in-tree volume plugin behavior and allow the CSI
|
||||
// driver to bump volume IOPS when volume size * iopsPerGB is too low.
|
||||
params[allowIncreaseIOPSKey] = "true"
|
||||
default:
|
||||
params[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if len(generatedTopologies) > 0 && len(sc.AllowedTopologies) > 0 {
|
||||
return nil, fmt.Errorf("cannot simultaneously set allowed topologies and zone/zones parameters")
|
||||
} else if len(generatedTopologies) > 0 {
|
||||
sc.AllowedTopologies = generatedTopologies
|
||||
} else if len(sc.AllowedTopologies) > 0 {
|
||||
newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, AWSEBSTopologyKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed translating allowed topologies: %v", err)
|
||||
}
|
||||
sc.AllowedTopologies = newTopologies
|
||||
}
|
||||
|
||||
sc.Parameters = params
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with AWSElasticBlockStore set from in-tree
|
||||
// and converts the AWSElasticBlockStore source to a CSIPersistentVolumeSource
|
||||
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.AWSElasticBlockStore == nil {
|
||||
return nil, fmt.Errorf("volume is nil or AWS EBS not defined on volume")
|
||||
}
|
||||
ebsSource := volume.AWSElasticBlockStore
|
||||
volumeHandle, err := KubernetesVolumeIDToEBSVolumeID(ebsSource.VolumeID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to translate Kubernetes ID to EBS Volume ID %v", err)
|
||||
}
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique per disk as it is used as the unique part of the
|
||||
// staging path
|
||||
Name: fmt.Sprintf("%s-%s", AWSEBSDriverName, volumeHandle),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: AWSEBSDriverName,
|
||||
VolumeHandle: volumeHandle,
|
||||
ReadOnly: ebsSource.ReadOnly,
|
||||
FSType: ebsSource.FSType,
|
||||
VolumeAttributes: map[string]string{
|
||||
"partition": strconv.FormatInt(int64(ebsSource.Partition), 10),
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
||||
},
|
||||
}
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with AWSElasticBlockStore set from in-tree
|
||||
// and converts the AWSElasticBlockStore source to a CSIPersistentVolumeSource
|
||||
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.AWSElasticBlockStore == nil {
|
||||
return nil, fmt.Errorf("pv is nil or AWS EBS not defined on pv")
|
||||
}
|
||||
|
||||
ebsSource := pv.Spec.AWSElasticBlockStore
|
||||
|
||||
volumeHandle, err := KubernetesVolumeIDToEBSVolumeID(ebsSource.VolumeID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to translate Kubernetes ID to EBS Volume ID %v", err)
|
||||
}
|
||||
|
||||
csiSource := &v1.CSIPersistentVolumeSource{
|
||||
Driver: AWSEBSDriverName,
|
||||
VolumeHandle: volumeHandle,
|
||||
ReadOnly: ebsSource.ReadOnly,
|
||||
FSType: ebsSource.FSType,
|
||||
VolumeAttributes: map[string]string{
|
||||
"partition": strconv.FormatInt(int64(ebsSource.Partition), 10),
|
||||
},
|
||||
}
|
||||
|
||||
if err := translateTopologyFromInTreeToCSI(pv, AWSEBSTopologyKey); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology: %v", err)
|
||||
}
|
||||
|
||||
pv.Spec.AWSElasticBlockStore = nil
|
||||
pv.Spec.CSI = csiSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the EBS CSI source to a AWSElasticBlockStore source.
|
||||
func (t *awsElasticBlockStoreCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
ebsSource := &v1.AWSElasticBlockStoreVolumeSource{
|
||||
VolumeID: csiSource.VolumeHandle,
|
||||
FSType: csiSource.FSType,
|
||||
ReadOnly: csiSource.ReadOnly,
|
||||
}
|
||||
|
||||
if partition, ok := csiSource.VolumeAttributes["partition"]; ok {
|
||||
partValue, err := strconv.Atoi(partition)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert partition %v to integer: %v", partition, err)
|
||||
}
|
||||
ebsSource.Partition = int32(partValue)
|
||||
}
|
||||
|
||||
// translate CSI topology to In-tree topology for rollback compatibility
|
||||
if err := translateTopologyFromCSIToInTree(pv, AWSEBSTopologyKey, getAwsRegionFromZones); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology. PV:%+v. Error:%v", *pv, err)
|
||||
}
|
||||
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.AWSElasticBlockStore = ebsSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *awsElasticBlockStoreCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.AWSElasticBlockStore != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *awsElasticBlockStoreCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.AWSElasticBlockStore != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the intree plugin driver
|
||||
func (t *awsElasticBlockStoreCSITranslator) GetInTreePluginName() string {
|
||||
return AWSEBSInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (t *awsElasticBlockStoreCSITranslator) GetCSIPluginName() string {
|
||||
return AWSEBSDriverName
|
||||
}
|
||||
|
||||
func (t *awsElasticBlockStoreCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
||||
|
||||
// awsVolumeRegMatch represents Regex Match for AWS volume.
|
||||
var awsVolumeRegMatch = regexp.MustCompile("^vol-[^/]*$")
|
||||
|
||||
// KubernetesVolumeIDToEBSVolumeID translates Kubernetes volume ID to EBS volume ID
|
||||
// KubernetesVolumeID forms:
|
||||
// - aws://<zone>/<awsVolumeId>
|
||||
// - aws:///<awsVolumeId>
|
||||
// - <awsVolumeId>
|
||||
//
|
||||
// EBS Volume ID form:
|
||||
// - vol-<alphanumberic>
|
||||
//
|
||||
// This translation shouldn't be needed and should be fixed in long run
|
||||
// See https://github.com/kubernetes/kubernetes/issues/73730
|
||||
func KubernetesVolumeIDToEBSVolumeID(kubernetesID string) (string, error) {
|
||||
// name looks like aws://availability-zone/awsVolumeId
|
||||
|
||||
// The original idea of the URL-style name was to put the AZ into the
|
||||
// host, so we could find the AZ immediately from the name without
|
||||
// querying the API. But it turns out we don't actually need it for
|
||||
// multi-AZ clusters, as we put the AZ into the labels on the PV instead.
|
||||
// However, if in future we want to support multi-AZ cluster
|
||||
// volume-awareness without using PersistentVolumes, we likely will
|
||||
// want the AZ in the host.
|
||||
if !strings.HasPrefix(kubernetesID, "aws://") {
|
||||
// Assume a bare aws volume id (vol-1234...)
|
||||
return kubernetesID, nil
|
||||
}
|
||||
url, err := url.Parse(kubernetesID)
|
||||
if err != nil {
|
||||
// TODO: Maybe we should pass a URL into the Volume functions
|
||||
return "", fmt.Errorf("Invalid disk name (%s): %v", kubernetesID, err)
|
||||
}
|
||||
if url.Scheme != "aws" {
|
||||
return "", fmt.Errorf("Invalid scheme for AWS volume (%s)", kubernetesID)
|
||||
}
|
||||
|
||||
awsID := url.Path
|
||||
awsID = strings.Trim(awsID, "/")
|
||||
|
||||
// We sanity check the resulting volume; the two known formats are
|
||||
// vol-12345678 and vol-12345678abcdef01
|
||||
if !awsVolumeRegMatch.MatchString(awsID) {
|
||||
return "", fmt.Errorf("Invalid format for AWS volume (%s)", kubernetesID)
|
||||
}
|
||||
|
||||
return awsID, nil
|
||||
}
|
||||
|
||||
func getAwsRegionFromZones(zones []string) (string, error) {
|
||||
regions := sets.String{}
|
||||
if len(zones) < 1 {
|
||||
return "", fmt.Errorf("no zones specified")
|
||||
}
|
||||
|
||||
// AWS zones can be in four forms:
|
||||
// us-west-2a, us-gov-east-1a, us-west-2-lax-1a (local zone) and us-east-1-wl1-bos-wlz-1 (wavelength).
|
||||
for _, zone := range zones {
|
||||
splitZone := strings.Split(zone, "-")
|
||||
if (len(splitZone) == 3 || len(splitZone) == 4) && len(splitZone[len(splitZone)-1]) == 2 {
|
||||
// this would break if we ever have a location with more than 9 regions, ie us-west-10.
|
||||
splitZone[len(splitZone)-1] = splitZone[len(splitZone)-1][:1]
|
||||
regions.Insert(strings.Join(splitZone, "-"))
|
||||
} else if len(splitZone) == 5 || len(splitZone) == 7 {
|
||||
// local zone or wavelength
|
||||
regions.Insert(strings.Join(splitZone[:3], "-"))
|
||||
} else {
|
||||
return "", fmt.Errorf("Unexpected zone format: %v is not a valid AWS zone", zone)
|
||||
}
|
||||
}
|
||||
if regions.Len() != 1 {
|
||||
return "", fmt.Errorf("multiple or no regions gotten from zones, got: %v", regions)
|
||||
}
|
||||
return regions.UnsortedList()[0], nil
|
||||
}
|
309
vendor/k8s.io/csi-translation-lib/plugins/azure_disk.go
generated
vendored
Normal file
309
vendor/k8s.io/csi-translation-lib/plugins/azure_disk.go
generated
vendored
Normal file
@ -0,0 +1,309 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// AzureDiskDriverName is the name of the CSI driver for Azure Disk
|
||||
AzureDiskDriverName = "disk.csi.azure.com"
|
||||
// AzureDiskTopologyKey is the topology key of Azure Disk CSI driver
|
||||
AzureDiskTopologyKey = "topology.disk.csi.azure.com/zone"
|
||||
// AzureDiskInTreePluginName is the name of the intree plugin for Azure Disk
|
||||
AzureDiskInTreePluginName = "kubernetes.io/azure-disk"
|
||||
|
||||
// Parameter names defined in azure disk CSI driver, refer to
|
||||
// https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md
|
||||
azureDiskKind = "kind"
|
||||
azureDiskCachingMode = "cachingmode"
|
||||
azureDiskFSType = "fstype"
|
||||
)
|
||||
|
||||
var (
|
||||
managedDiskPathRE = regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/disks/(.+)`)
|
||||
unmanagedDiskPathRE = regexp.MustCompile(`http(?:.*)://(?:.*)/vhds/(.+)`)
|
||||
managed = string(v1.AzureManagedDisk)
|
||||
unzonedCSIRegionRE = regexp.MustCompile(`^[0-9]+$`)
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &azureDiskCSITranslator{}
|
||||
|
||||
// azureDiskCSITranslator handles translation of PV spec from In-tree
|
||||
// Azure Disk to CSI Azure Disk and vice versa
|
||||
type azureDiskCSITranslator struct{}
|
||||
|
||||
// NewAzureDiskCSITranslator returns a new instance of azureDiskTranslator
|
||||
func NewAzureDiskCSITranslator() InTreePlugin {
|
||||
return &azureDiskCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree Azure Disk storage class parameters to CSI storage class
|
||||
func (t *azureDiskCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
var (
|
||||
generatedTopologies []v1.TopologySelectorTerm
|
||||
params = map[string]string{}
|
||||
)
|
||||
for k, v := range sc.Parameters {
|
||||
switch strings.ToLower(k) {
|
||||
case zoneKey:
|
||||
generatedTopologies = generateToplogySelectors(AzureDiskTopologyKey, []string{v})
|
||||
case zonesKey:
|
||||
generatedTopologies = generateToplogySelectors(AzureDiskTopologyKey, strings.Split(v, ","))
|
||||
default:
|
||||
params[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if len(generatedTopologies) > 0 && len(sc.AllowedTopologies) > 0 {
|
||||
return nil, fmt.Errorf("cannot simultaneously set allowed topologies and zone/zones parameters")
|
||||
} else if len(generatedTopologies) > 0 {
|
||||
sc.AllowedTopologies = generatedTopologies
|
||||
} else if len(sc.AllowedTopologies) > 0 {
|
||||
newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, AzureDiskTopologyKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed translating allowed topologies: %v", err)
|
||||
}
|
||||
sc.AllowedTopologies = newTopologies
|
||||
}
|
||||
sc.AllowedTopologies = t.replaceFailureDomainsToCSI(sc.AllowedTopologies)
|
||||
|
||||
sc.Parameters = params
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with AzureDisk set from in-tree
|
||||
// and converts the AzureDisk source to a CSIPersistentVolumeSource
|
||||
func (t *azureDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.AzureDisk == nil {
|
||||
return nil, fmt.Errorf("volume is nil or Azure Disk not defined on volume")
|
||||
}
|
||||
|
||||
azureSource := volume.AzureDisk
|
||||
if azureSource.Kind != nil && !strings.EqualFold(string(*azureSource.Kind), managed) {
|
||||
return nil, fmt.Errorf("kind(%v) is not supported in csi migration", *azureSource.Kind)
|
||||
}
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique per disk as it is used as the unique part of the
|
||||
// staging path
|
||||
Name: azureSource.DataDiskURI,
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: AzureDiskDriverName,
|
||||
VolumeHandle: azureSource.DataDiskURI,
|
||||
VolumeAttributes: map[string]string{azureDiskKind: managed},
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
||||
},
|
||||
}
|
||||
if azureSource.ReadOnly != nil {
|
||||
pv.Spec.PersistentVolumeSource.CSI.ReadOnly = *azureSource.ReadOnly
|
||||
}
|
||||
|
||||
if azureSource.CachingMode != nil && *azureSource.CachingMode != "" {
|
||||
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskCachingMode] = string(*azureSource.CachingMode)
|
||||
}
|
||||
if azureSource.FSType != nil {
|
||||
pv.Spec.PersistentVolumeSource.CSI.FSType = *azureSource.FSType
|
||||
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
||||
}
|
||||
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskKind] = managed
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with AzureDisk set from in-tree
|
||||
// and converts the AzureDisk source to a CSIPersistentVolumeSource
|
||||
func (t *azureDiskCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.AzureDisk == nil {
|
||||
return nil, fmt.Errorf("pv is nil or Azure Disk source not defined on pv")
|
||||
}
|
||||
|
||||
var (
|
||||
azureSource = pv.Spec.PersistentVolumeSource.AzureDisk
|
||||
|
||||
// refer to https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md
|
||||
csiSource = &v1.CSIPersistentVolumeSource{
|
||||
Driver: AzureDiskDriverName,
|
||||
VolumeAttributes: map[string]string{azureDiskKind: managed},
|
||||
VolumeHandle: azureSource.DataDiskURI,
|
||||
}
|
||||
)
|
||||
|
||||
if azureSource.Kind != nil && !strings.EqualFold(string(*azureSource.Kind), managed) {
|
||||
return nil, fmt.Errorf("kind(%v) is not supported in csi migration", *azureSource.Kind)
|
||||
}
|
||||
|
||||
if azureSource.CachingMode != nil {
|
||||
csiSource.VolumeAttributes[azureDiskCachingMode] = string(*azureSource.CachingMode)
|
||||
}
|
||||
|
||||
if azureSource.FSType != nil {
|
||||
csiSource.FSType = *azureSource.FSType
|
||||
csiSource.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
||||
}
|
||||
csiSource.VolumeAttributes[azureDiskKind] = managed
|
||||
|
||||
if azureSource.ReadOnly != nil {
|
||||
csiSource.ReadOnly = *azureSource.ReadOnly
|
||||
}
|
||||
|
||||
pv.Spec.PersistentVolumeSource.AzureDisk = nil
|
||||
pv.Spec.PersistentVolumeSource.CSI = csiSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the Azure Disk CSI source to a AzureDisk source.
|
||||
func (t *azureDiskCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
diskURI := csiSource.VolumeHandle
|
||||
diskName, err := getDiskName(diskURI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// refer to https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md
|
||||
managed := v1.AzureManagedDisk
|
||||
azureSource := &v1.AzureDiskVolumeSource{
|
||||
DiskName: diskName,
|
||||
DataDiskURI: diskURI,
|
||||
FSType: &csiSource.FSType,
|
||||
ReadOnly: &csiSource.ReadOnly,
|
||||
Kind: &managed,
|
||||
}
|
||||
|
||||
if csiSource.VolumeAttributes != nil {
|
||||
for k, v := range csiSource.VolumeAttributes {
|
||||
switch strings.ToLower(k) {
|
||||
case azureDiskCachingMode:
|
||||
if v != "" {
|
||||
mode := v1.AzureDataDiskCachingMode(v)
|
||||
azureSource.CachingMode = &mode
|
||||
}
|
||||
case azureDiskFSType:
|
||||
if v != "" {
|
||||
fsType := v
|
||||
azureSource.FSType = &fsType
|
||||
}
|
||||
}
|
||||
}
|
||||
azureSource.Kind = &managed
|
||||
}
|
||||
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.AzureDisk = azureSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *azureDiskCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.AzureDisk != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *azureDiskCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.AzureDisk != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the intree plugin driver
|
||||
func (t *azureDiskCSITranslator) GetInTreePluginName() string {
|
||||
return AzureDiskInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (t *azureDiskCSITranslator) GetCSIPluginName() string {
|
||||
return AzureDiskDriverName
|
||||
}
|
||||
|
||||
func (t *azureDiskCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
||||
|
||||
func isManagedDisk(diskURI string) bool {
|
||||
if len(diskURI) > 4 && strings.ToLower(diskURI[:4]) == "http" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func getDiskName(diskURI string) (string, error) {
|
||||
diskPathRE := managedDiskPathRE
|
||||
if !isManagedDisk(diskURI) {
|
||||
diskPathRE = unmanagedDiskPathRE
|
||||
}
|
||||
|
||||
matches := diskPathRE.FindStringSubmatch(diskURI)
|
||||
if len(matches) != 2 {
|
||||
return "", fmt.Errorf("could not get disk name from %s, correct format: %s", diskURI, diskPathRE)
|
||||
}
|
||||
return matches[1], nil
|
||||
}
|
||||
|
||||
// Replace topology values for failure domains ("<number>") to "",
|
||||
// as it's the value that the CSI driver expects.
|
||||
func (t *azureDiskCSITranslator) replaceFailureDomainsToCSI(terms []v1.TopologySelectorTerm) []v1.TopologySelectorTerm {
|
||||
if terms == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
newTopologies := []v1.TopologySelectorTerm{}
|
||||
for _, term := range terms {
|
||||
newTerm := term.DeepCopy()
|
||||
for i := range newTerm.MatchLabelExpressions {
|
||||
exp := &newTerm.MatchLabelExpressions[i]
|
||||
if exp.Key == AzureDiskTopologyKey {
|
||||
for j := range exp.Values {
|
||||
if unzonedCSIRegionRE.Match([]byte(exp.Values[j])) {
|
||||
// Topologies "0", "1" etc are used when in-tree topology is translated to CSI in Azure
|
||||
// regions that don't have availability zones. E.g.:
|
||||
// topology.kubernetes.io/region: westus
|
||||
// topology.kubernetes.io/zone: "0"
|
||||
// The CSI driver uses zone "" instead of "0" in this case.
|
||||
// topology.disk.csi.azure.com/zone": ""
|
||||
exp.Values[j] = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
newTopologies = append(newTopologies, *newTerm)
|
||||
}
|
||||
return newTopologies
|
||||
}
|
282
vendor/k8s.io/csi-translation-lib/plugins/azure_file.go
generated
vendored
Normal file
282
vendor/k8s.io/csi-translation-lib/plugins/azure_file.go
generated
vendored
Normal file
@ -0,0 +1,282 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// AzureFileDriverName is the name of the CSI driver for Azure File
|
||||
AzureFileDriverName = "file.csi.azure.com"
|
||||
// AzureFileInTreePluginName is the name of the intree plugin for Azure file
|
||||
AzureFileInTreePluginName = "kubernetes.io/azure-file"
|
||||
|
||||
separator = "#"
|
||||
volumeIDTemplate = "%s#%s#%s#%s#%s"
|
||||
// Parameter names defined in azure file CSI driver, refer to
|
||||
// https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
|
||||
shareNameField = "sharename"
|
||||
secretNameField = "secretname"
|
||||
secretNamespaceField = "secretnamespace"
|
||||
secretNameTemplate = "azure-storage-account-%s-secret"
|
||||
defaultSecretNamespace = "default"
|
||||
resourceGroupAnnotation = "kubernetes.io/azure-file-resource-group"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &azureFileCSITranslator{}
|
||||
|
||||
var secretNameFormatRE = regexp.MustCompile(`azure-storage-account-(.+)-secret`)
|
||||
|
||||
// azureFileCSITranslator handles translation of PV spec from In-tree
|
||||
// Azure File to CSI Azure File and vice versa
|
||||
type azureFileCSITranslator struct{}
|
||||
|
||||
// NewAzureFileCSITranslator returns a new instance of azureFileTranslator
|
||||
func NewAzureFileCSITranslator() InTreePlugin {
|
||||
return &azureFileCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree Azure File storage class parameters to CSI storage class
|
||||
func (t *azureFileCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with AzureFile set from in-tree
|
||||
// and converts the AzureFile source to a CSIPersistentVolumeSource
|
||||
func (t *azureFileCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.AzureFile == nil {
|
||||
return nil, fmt.Errorf("volume is nil or Azure File not defined on volume")
|
||||
}
|
||||
|
||||
azureSource := volume.AzureFile
|
||||
accountName, err := getStorageAccountName(azureSource.SecretName)
|
||||
if err != nil {
|
||||
logger.V(5).Info("getStorageAccountName returned with error", "secretName", azureSource.SecretName, "err", err)
|
||||
accountName = azureSource.SecretName
|
||||
}
|
||||
|
||||
secretNamespace := defaultSecretNamespace
|
||||
if podNamespace != "" {
|
||||
secretNamespace = podNamespace
|
||||
}
|
||||
volumeID := fmt.Sprintf(volumeIDTemplate, "", accountName, azureSource.ShareName, volume.Name, secretNamespace)
|
||||
|
||||
var (
|
||||
pv = &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique as it is used as the unique part of the staging path
|
||||
Name: volumeID,
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: AzureFileDriverName,
|
||||
VolumeHandle: volumeID,
|
||||
ReadOnly: azureSource.ReadOnly,
|
||||
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
|
||||
NodeStageSecretRef: &v1.SecretReference{
|
||||
Name: azureSource.SecretName,
|
||||
Namespace: secretNamespace,
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteMany},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with AzureFile set from in-tree
|
||||
// and converts the AzureFile source to a CSIPersistentVolumeSource
|
||||
func (t *azureFileCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.AzureFile == nil {
|
||||
return nil, fmt.Errorf("pv is nil or Azure File source not defined on pv")
|
||||
}
|
||||
|
||||
azureSource := pv.Spec.PersistentVolumeSource.AzureFile
|
||||
accountName, err := getStorageAccountName(azureSource.SecretName)
|
||||
if err != nil {
|
||||
logger.V(5).Info("getStorageAccountName returned with error", "secretName", azureSource.SecretName, "err", err)
|
||||
accountName = azureSource.SecretName
|
||||
}
|
||||
resourceGroup := ""
|
||||
if pv.ObjectMeta.Annotations != nil {
|
||||
if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok {
|
||||
resourceGroup = v
|
||||
}
|
||||
}
|
||||
|
||||
// Secret is required when mounting a volume but pod presence cannot be assumed - we should not try to read pod now.
|
||||
namespace := ""
|
||||
// Try to read SecretNamespace from source pv.
|
||||
if azureSource.SecretNamespace != nil {
|
||||
namespace = *azureSource.SecretNamespace
|
||||
} else {
|
||||
// Try to read namespace from ClaimRef which should be always present.
|
||||
if pv.Spec.ClaimRef != nil {
|
||||
namespace = pv.Spec.ClaimRef.Namespace
|
||||
}
|
||||
}
|
||||
|
||||
if len(namespace) == 0 {
|
||||
return nil, fmt.Errorf("could not find a secret namespace in PersistentVolumeSource or ClaimRef")
|
||||
}
|
||||
|
||||
volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, pv.ObjectMeta.Name, namespace)
|
||||
|
||||
var (
|
||||
// refer to https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
|
||||
csiSource = &v1.CSIPersistentVolumeSource{
|
||||
Driver: AzureFileDriverName,
|
||||
NodeStageSecretRef: &v1.SecretReference{
|
||||
Name: azureSource.SecretName,
|
||||
Namespace: namespace,
|
||||
},
|
||||
ReadOnly: azureSource.ReadOnly,
|
||||
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
|
||||
VolumeHandle: volumeID,
|
||||
}
|
||||
)
|
||||
|
||||
pv.Spec.PersistentVolumeSource.AzureFile = nil
|
||||
pv.Spec.PersistentVolumeSource.CSI = csiSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the Azure File CSI source to a AzureFile source.
|
||||
func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
// refer to https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
|
||||
azureSource := &v1.AzureFilePersistentVolumeSource{
|
||||
ReadOnly: csiSource.ReadOnly,
|
||||
}
|
||||
|
||||
for k, v := range csiSource.VolumeAttributes {
|
||||
switch strings.ToLower(k) {
|
||||
case shareNameField:
|
||||
azureSource.ShareName = v
|
||||
case secretNameField:
|
||||
azureSource.SecretName = v
|
||||
case secretNamespaceField:
|
||||
ns := v
|
||||
azureSource.SecretNamespace = &ns
|
||||
}
|
||||
}
|
||||
|
||||
resourceGroup := ""
|
||||
if csiSource.NodeStageSecretRef != nil && csiSource.NodeStageSecretRef.Name != "" {
|
||||
azureSource.SecretName = csiSource.NodeStageSecretRef.Name
|
||||
azureSource.SecretNamespace = &csiSource.NodeStageSecretRef.Namespace
|
||||
}
|
||||
if azureSource.ShareName == "" || azureSource.SecretName == "" {
|
||||
rg, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if azureSource.ShareName == "" {
|
||||
azureSource.ShareName = fileShareName
|
||||
}
|
||||
if azureSource.SecretName == "" {
|
||||
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
|
||||
}
|
||||
resourceGroup = rg
|
||||
}
|
||||
|
||||
if azureSource.SecretNamespace == nil {
|
||||
ns := defaultSecretNamespace
|
||||
azureSource.SecretNamespace = &ns
|
||||
}
|
||||
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.AzureFile = azureSource
|
||||
if pv.ObjectMeta.Annotations == nil {
|
||||
pv.ObjectMeta.Annotations = map[string]string{}
|
||||
}
|
||||
if resourceGroup != "" {
|
||||
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
|
||||
}
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *azureFileCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.AzureFile != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *azureFileCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.AzureFile != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the intree plugin driver
|
||||
func (t *azureFileCSITranslator) GetInTreePluginName() string {
|
||||
return AzureFileInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (t *azureFileCSITranslator) GetCSIPluginName() string {
|
||||
return AzureFileDriverName
|
||||
}
|
||||
|
||||
func (t *azureFileCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
||||
|
||||
// get file share info according to volume id, e.g.
|
||||
// input: "rg#f5713de20cde511e8ba4900#pvc-file-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41#diskname.vhd"
|
||||
// output: rg, f5713de20cde511e8ba4900, pvc-file-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41, diskname.vhd
|
||||
func getFileShareInfo(id string) (string, string, string, string, error) {
|
||||
segments := strings.Split(id, separator)
|
||||
if len(segments) < 3 {
|
||||
return "", "", "", "", fmt.Errorf("error parsing volume id: %q, should at least contain two #", id)
|
||||
}
|
||||
var diskName string
|
||||
if len(segments) > 3 {
|
||||
diskName = segments[3]
|
||||
}
|
||||
return segments[0], segments[1], segments[2], diskName, nil
|
||||
}
|
||||
|
||||
// get storage account name from secret name
|
||||
func getStorageAccountName(secretName string) (string, error) {
|
||||
matches := secretNameFormatRE.FindStringSubmatch(secretName)
|
||||
if len(matches) != 2 {
|
||||
return "", fmt.Errorf("could not get account name from %s, correct format: %s", secretName, secretNameFormatRE)
|
||||
}
|
||||
return matches[1], nil
|
||||
}
|
21
vendor/k8s.io/csi-translation-lib/plugins/const.go
generated
vendored
Normal file
21
vendor/k8s.io/csi-translation-lib/plugins/const.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
// Matches the delimiter LabelMultiZoneDelimiter used by k8s.io/cloud-provider/volume and is mirrored here to avoid a large dependency
|
||||
// labelMultiZoneDelimiter separates zones for volumes
|
||||
const labelMultiZoneDelimiter = "__"
|
400
vendor/k8s.io/csi-translation-lib/plugins/gce_pd.go
generated
vendored
Normal file
400
vendor/k8s.io/csi-translation-lib/plugins/gce_pd.go
generated
vendored
Normal file
@ -0,0 +1,400 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// GCEPDDriverName is the name of the CSI driver for GCE PD
|
||||
GCEPDDriverName = "pd.csi.storage.gke.io"
|
||||
// GCEPDInTreePluginName is the name of the intree plugin for GCE PD
|
||||
GCEPDInTreePluginName = "kubernetes.io/gce-pd"
|
||||
|
||||
// GCEPDTopologyKey is the zonal topology key for GCE PD CSI Driver
|
||||
GCEPDTopologyKey = "topology.gke.io/zone"
|
||||
|
||||
// Volume ID Expected Format
|
||||
// "projects/{projectName}/zones/{zoneName}/disks/{diskName}"
|
||||
volIDZonalFmt = "projects/%s/zones/%s/disks/%s"
|
||||
// "projects/{projectName}/regions/{regionName}/disks/{diskName}"
|
||||
volIDRegionalFmt = "projects/%s/regions/%s/disks/%s"
|
||||
volIDProjectValue = 1
|
||||
volIDRegionalityValue = 2
|
||||
volIDZoneValue = 3
|
||||
volIDDiskNameValue = 5
|
||||
volIDTotalElements = 6
|
||||
|
||||
nodeIDFmt = "projects/%s/zones/%s/instances/%s"
|
||||
|
||||
// UnspecifiedValue is used for an unknown zone string
|
||||
UnspecifiedValue = "UNSPECIFIED"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &gcePersistentDiskCSITranslator{}
|
||||
|
||||
// gcePersistentDiskCSITranslator handles translation of PV spec from In-tree
|
||||
// GCE PD to CSI GCE PD and vice versa
|
||||
type gcePersistentDiskCSITranslator struct{}
|
||||
|
||||
// NewGCEPersistentDiskCSITranslator returns a new instance of gcePersistentDiskTranslator
|
||||
func NewGCEPersistentDiskCSITranslator() InTreePlugin {
|
||||
return &gcePersistentDiskCSITranslator{}
|
||||
}
|
||||
|
||||
func generateToplogySelectors(key string, values []string) []v1.TopologySelectorTerm {
|
||||
return []v1.TopologySelectorTerm{
|
||||
{
|
||||
MatchLabelExpressions: []v1.TopologySelectorLabelRequirement{
|
||||
{
|
||||
Key: key,
|
||||
Values: values,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree GCE storage class parameters to CSI storage class
|
||||
func (g *gcePersistentDiskCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
var generatedTopologies []v1.TopologySelectorTerm
|
||||
|
||||
np := map[string]string{}
|
||||
for k, v := range sc.Parameters {
|
||||
switch strings.ToLower(k) {
|
||||
case fsTypeKey:
|
||||
// prefixed fstype parameter is stripped out by external provisioner
|
||||
np[csiFsTypeKey] = v
|
||||
// Strip out zone and zones parameters and translate them into topologies instead
|
||||
case zoneKey:
|
||||
generatedTopologies = generateToplogySelectors(GCEPDTopologyKey, []string{v})
|
||||
case zonesKey:
|
||||
generatedTopologies = generateToplogySelectors(GCEPDTopologyKey, strings.Split(v, ","))
|
||||
default:
|
||||
np[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if len(generatedTopologies) > 0 && len(sc.AllowedTopologies) > 0 {
|
||||
return nil, fmt.Errorf("cannot simultaneously set allowed topologies and zone/zones parameters")
|
||||
} else if len(generatedTopologies) > 0 {
|
||||
sc.AllowedTopologies = generatedTopologies
|
||||
} else if len(sc.AllowedTopologies) > 0 {
|
||||
newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, GCEPDTopologyKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed translating allowed topologies: %v", err)
|
||||
}
|
||||
sc.AllowedTopologies = newTopologies
|
||||
}
|
||||
|
||||
sc.Parameters = np
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// backwardCompatibleAccessModes translates all instances of ReadWriteMany
|
||||
// access mode from the in-tree plugin to ReadWriteOnce. This is because in-tree
|
||||
// plugin never supported ReadWriteMany but also did not validate or enforce
|
||||
// this access mode for pre-provisioned volumes. The GCE PD CSI Driver validates
|
||||
// and enforces (fails) ReadWriteMany. Therefore we treat all in-tree
|
||||
// ReadWriteMany as ReadWriteOnce volumes to not break legacy volumes. It also
|
||||
// takes [ReadWriteOnce, ReadOnlyMany] and makes it ReadWriteOnce. This is
|
||||
// because the in-tree plugin does not enforce access modes and just attaches
|
||||
// the disk in ReadWriteOnce mode; however, the CSI external-attacher will fail
|
||||
// this combination because technically [ReadWriteOnce, ReadOnlyMany] is not
|
||||
// supportable on an attached volume
|
||||
// See: https://github.com/kubernetes-csi/external-attacher/issues/153
|
||||
func backwardCompatibleAccessModes(ams []v1.PersistentVolumeAccessMode) []v1.PersistentVolumeAccessMode {
|
||||
if ams == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
s := map[v1.PersistentVolumeAccessMode]bool{}
|
||||
var newAM []v1.PersistentVolumeAccessMode
|
||||
|
||||
for _, am := range ams {
|
||||
if am == v1.ReadWriteMany {
|
||||
// ReadWriteMany is unsupported in CSI, but in-tree did no
|
||||
// validation and treated it as ReadWriteOnce
|
||||
s[v1.ReadWriteOnce] = true
|
||||
} else {
|
||||
s[am] = true
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case s[v1.ReadOnlyMany] && s[v1.ReadWriteOnce]:
|
||||
// ROX,RWO is unsupported in CSI, but in-tree did not validation and
|
||||
// treated it as ReadWriteOnce
|
||||
newAM = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
|
||||
case s[v1.ReadWriteOnce]:
|
||||
newAM = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
|
||||
case s[v1.ReadOnlyMany]:
|
||||
newAM = []v1.PersistentVolumeAccessMode{v1.ReadOnlyMany}
|
||||
default:
|
||||
newAM = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
|
||||
}
|
||||
|
||||
return newAM
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with GCEPersistentDisk set from in-tree
|
||||
// and converts the GCEPersistentDisk source to a CSIPersistentVolumeSource
|
||||
func (g *gcePersistentDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.GCEPersistentDisk == nil {
|
||||
return nil, fmt.Errorf("volume is nil or GCE PD not defined on volume")
|
||||
}
|
||||
|
||||
pdSource := volume.GCEPersistentDisk
|
||||
|
||||
partition := ""
|
||||
if pdSource.Partition != 0 {
|
||||
partition = strconv.Itoa(int(pdSource.Partition))
|
||||
}
|
||||
|
||||
var am v1.PersistentVolumeAccessMode
|
||||
if pdSource.ReadOnly {
|
||||
am = v1.ReadOnlyMany
|
||||
} else {
|
||||
am = v1.ReadWriteOnce
|
||||
}
|
||||
|
||||
fsMode := v1.PersistentVolumeFilesystem
|
||||
return &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique per disk as it is used as the unique part of the
|
||||
// staging path
|
||||
Name: fmt.Sprintf("%s-%s", GCEPDDriverName, pdSource.PDName),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: GCEPDDriverName,
|
||||
VolumeHandle: fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, UnspecifiedValue, pdSource.PDName),
|
||||
ReadOnly: pdSource.ReadOnly,
|
||||
FSType: pdSource.FSType,
|
||||
VolumeAttributes: map[string]string{
|
||||
"partition": partition,
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{am},
|
||||
VolumeMode: &fsMode,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with GCEPersistentDisk set from in-tree
|
||||
// and converts the GCEPersistentDisk source to a CSIPersistentVolumeSource
|
||||
func (g *gcePersistentDiskCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
var volID string
|
||||
|
||||
if pv == nil || pv.Spec.GCEPersistentDisk == nil {
|
||||
return nil, fmt.Errorf("pv is nil or GCE Persistent Disk source not defined on pv")
|
||||
}
|
||||
|
||||
// depend on which version it migrates from, the label could be failuredomain beta or topology GA version
|
||||
zonesLabel := pv.Labels[v1.LabelFailureDomainBetaZone]
|
||||
if zonesLabel == "" {
|
||||
zonesLabel = pv.Labels[v1.LabelTopologyZone]
|
||||
}
|
||||
|
||||
zones := strings.Split(zonesLabel, labelMultiZoneDelimiter)
|
||||
if len(zones) == 1 && len(zones[0]) != 0 {
|
||||
// Zonal
|
||||
volID = fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, zones[0], pv.Spec.GCEPersistentDisk.PDName)
|
||||
} else if len(zones) > 1 {
|
||||
// Regional
|
||||
region, err := gceGetRegionFromZones(zones)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get region from zones: %v", err)
|
||||
}
|
||||
volID = fmt.Sprintf(volIDRegionalFmt, UnspecifiedValue, region, pv.Spec.GCEPersistentDisk.PDName)
|
||||
} else {
|
||||
// Unspecified
|
||||
volID = fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, UnspecifiedValue, pv.Spec.GCEPersistentDisk.PDName)
|
||||
}
|
||||
|
||||
gceSource := pv.Spec.PersistentVolumeSource.GCEPersistentDisk
|
||||
|
||||
partition := ""
|
||||
if gceSource.Partition != 0 {
|
||||
partition = strconv.Itoa(int(gceSource.Partition))
|
||||
}
|
||||
|
||||
csiSource := &v1.CSIPersistentVolumeSource{
|
||||
Driver: GCEPDDriverName,
|
||||
VolumeHandle: volID,
|
||||
ReadOnly: gceSource.ReadOnly,
|
||||
FSType: gceSource.FSType,
|
||||
VolumeAttributes: map[string]string{
|
||||
"partition": partition,
|
||||
},
|
||||
}
|
||||
|
||||
if err := translateTopologyFromInTreeToCSI(pv, GCEPDTopologyKey); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology: %v", err)
|
||||
}
|
||||
|
||||
pv.Spec.PersistentVolumeSource.GCEPersistentDisk = nil
|
||||
pv.Spec.PersistentVolumeSource.CSI = csiSource
|
||||
pv.Spec.AccessModes = backwardCompatibleAccessModes(pv.Spec.AccessModes)
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the GCE PD CSI source to a GCEPersistentDisk source.
|
||||
func (g *gcePersistentDiskCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
pdName, err := pdNameFromVolumeID(csiSource.VolumeHandle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gceSource := &v1.GCEPersistentDiskVolumeSource{
|
||||
PDName: pdName,
|
||||
FSType: csiSource.FSType,
|
||||
ReadOnly: csiSource.ReadOnly,
|
||||
}
|
||||
if partition, ok := csiSource.VolumeAttributes["partition"]; ok && partition != "" {
|
||||
partInt, err := strconv.Atoi(partition)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to convert partition %v to integer: %v", partition, err)
|
||||
}
|
||||
gceSource.Partition = int32(partInt)
|
||||
}
|
||||
|
||||
// translate CSI topology to In-tree topology for rollback compatibility
|
||||
if err := translateTopologyFromCSIToInTree(pv, GCEPDTopologyKey, gceGetRegionFromZones); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology. PV:%+v. Error:%v", *pv, err)
|
||||
}
|
||||
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.GCEPersistentDisk = gceSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (g *gcePersistentDiskCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.GCEPersistentDisk != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (g *gcePersistentDiskCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.GCEPersistentDisk != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the intree plugin driver
|
||||
func (g *gcePersistentDiskCSITranslator) GetInTreePluginName() string {
|
||||
return GCEPDInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (g *gcePersistentDiskCSITranslator) GetCSIPluginName() string {
|
||||
return GCEPDDriverName
|
||||
}
|
||||
|
||||
// RepairVolumeHandle returns a fully specified volume handle by inferring
|
||||
// project, zone/region from the node ID if the volume handle has UNSPECIFIED
|
||||
// sections
|
||||
func (g *gcePersistentDiskCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
var err error
|
||||
tok := strings.Split(volumeHandle, "/")
|
||||
if len(tok) < volIDTotalElements {
|
||||
return "", fmt.Errorf("volume handle has wrong number of elements; got %v, wanted %v or more", len(tok), volIDTotalElements)
|
||||
}
|
||||
if tok[volIDProjectValue] != UnspecifiedValue {
|
||||
return volumeHandle, nil
|
||||
}
|
||||
|
||||
nodeTok := strings.Split(nodeID, "/")
|
||||
if len(nodeTok) < volIDTotalElements {
|
||||
return "", fmt.Errorf("node handle has wrong number of elements; got %v, wanted %v or more", len(nodeTok), volIDTotalElements)
|
||||
}
|
||||
|
||||
switch tok[volIDRegionalityValue] {
|
||||
case "zones":
|
||||
zone := ""
|
||||
if tok[volIDZoneValue] == UnspecifiedValue {
|
||||
zone = nodeTok[volIDZoneValue]
|
||||
} else {
|
||||
zone = tok[volIDZoneValue]
|
||||
}
|
||||
return fmt.Sprintf(volIDZonalFmt, nodeTok[volIDProjectValue], zone, tok[volIDDiskNameValue]), nil
|
||||
case "regions":
|
||||
region := ""
|
||||
if tok[volIDZoneValue] == UnspecifiedValue {
|
||||
region, err = gceGetRegionFromZones([]string{nodeTok[volIDZoneValue]})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get region from zone %s: %v", nodeTok[volIDZoneValue], err)
|
||||
}
|
||||
} else {
|
||||
region = tok[volIDZoneValue]
|
||||
}
|
||||
return fmt.Sprintf(volIDRegionalFmt, nodeTok[volIDProjectValue], region, tok[volIDDiskNameValue]), nil
|
||||
default:
|
||||
return "", fmt.Errorf("expected volume handle to have zones or regions regionality value, got: %s", tok[volIDRegionalityValue])
|
||||
}
|
||||
}
|
||||
|
||||
func pdNameFromVolumeID(id string) (string, error) {
|
||||
splitID := strings.Split(id, "/")
|
||||
if len(splitID) < volIDTotalElements {
|
||||
return "", fmt.Errorf("failed to get id components.Got: %v, wanted %v components or more. ", len(splitID), volIDTotalElements)
|
||||
}
|
||||
return splitID[volIDDiskNameValue], nil
|
||||
}
|
||||
|
||||
// TODO: Replace this with the imported one from GCE PD CSI Driver when
|
||||
// the driver removes all k8s/k8s dependencies
|
||||
func gceGetRegionFromZones(zones []string) (string, error) {
|
||||
regions := sets.String{}
|
||||
if len(zones) < 1 {
|
||||
return "", fmt.Errorf("no zones specified")
|
||||
}
|
||||
for _, zone := range zones {
|
||||
// Zone expected format {locale}-{region}-{zone}
|
||||
splitZone := strings.Split(zone, "-")
|
||||
if len(splitZone) != 3 {
|
||||
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone}, got: %v", zone)
|
||||
}
|
||||
regions.Insert(strings.Join(splitZone[0:2], "-"))
|
||||
}
|
||||
if regions.Len() != 1 {
|
||||
return "", fmt.Errorf("multiple or no regions gotten from zones, got: %v", regions)
|
||||
}
|
||||
return regions.UnsortedList()[0], nil
|
||||
}
|
398
vendor/k8s.io/csi-translation-lib/plugins/in_tree_volume.go
generated
vendored
Normal file
398
vendor/k8s.io/csi-translation-lib/plugins/in_tree_volume.go
generated
vendored
Normal file
@ -0,0 +1,398 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// InTreePlugin handles translations between CSI and in-tree sources in a PV
|
||||
type InTreePlugin interface {
|
||||
|
||||
// TranslateInTreeStorageClassToCSI takes in-tree volume options
|
||||
// and translates them to a volume options consumable by CSI plugin
|
||||
TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error)
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a inline volume and will translate
|
||||
// the in-tree inline volume source to a CSIPersistentVolumeSource
|
||||
// A PV object containing the CSIPersistentVolumeSource in it's spec is returned
|
||||
// podNamespace is only needed for azurefile to fetch secret namespace, no need to be set for other plugins.
|
||||
TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error)
|
||||
|
||||
// TranslateInTreePVToCSI takes a persistent volume and will translate
|
||||
// the in-tree pv source to a CSI Source. The input persistent volume can be modified
|
||||
TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error)
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with a CSI PersistentVolume Source and will translate
|
||||
// it to a in-tree Persistent Volume Source for the in-tree volume
|
||||
// by the `Driver` field in the CSI Source. The input PV object can be modified
|
||||
TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error)
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API.
|
||||
CanSupport(pv *v1.PersistentVolume) bool
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API.
|
||||
CanSupportInline(vol *v1.Volume) bool
|
||||
|
||||
// GetInTreePluginName returns the in-tree plugin name this migrates
|
||||
GetInTreePluginName() string
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin that supersedes the in-tree plugin
|
||||
GetCSIPluginName() string
|
||||
|
||||
// RepairVolumeHandle generates a correct volume handle based on node ID information.
|
||||
RepairVolumeHandle(volumeHandle, nodeID string) (string, error)
|
||||
}
|
||||
|
||||
const (
|
||||
// fsTypeKey is the deprecated storage class parameter key for fstype
|
||||
fsTypeKey = "fstype"
|
||||
// csiFsTypeKey is the storage class parameter key for CSI fstype
|
||||
csiFsTypeKey = "csi.storage.k8s.io/fstype"
|
||||
// zoneKey is the deprecated storage class parameter key for zone
|
||||
zoneKey = "zone"
|
||||
// zonesKey is the deprecated storage class parameter key for zones
|
||||
zonesKey = "zones"
|
||||
)
|
||||
|
||||
// replaceTopology overwrites an existing key in NodeAffinity by a new one.
|
||||
// If there are any newKey already exist in an expression of a term, we will
|
||||
// not combine the replaced key Values with the existing ones.
|
||||
// So there might be duplication if there is any newKey expression
|
||||
// already in the terms.
|
||||
func replaceTopology(pv *v1.PersistentVolume, oldKey, newKey string) error {
|
||||
// Make sure the necessary fields exist
|
||||
if pv == nil || pv.Spec.NodeAffinity == nil || pv.Spec.NodeAffinity.Required == nil ||
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms == nil || len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) == 0 {
|
||||
return nil
|
||||
}
|
||||
for i := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
for j, r := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms[i].MatchExpressions {
|
||||
if r.Key == oldKey {
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms[i].MatchExpressions[j].Key = newKey
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getTopologyValues returns all unique topology values with the given key found in
|
||||
// the PV NodeAffinity. Sort by alphabetical order.
|
||||
// This function collapses multiple zones into a list that is ORed. This assumes that
|
||||
// the plugin does not support a constraint like key in "zone1" AND "zone2"
|
||||
func getTopologyValues(pv *v1.PersistentVolume, key string) []string {
|
||||
if pv.Spec.NodeAffinity == nil ||
|
||||
pv.Spec.NodeAffinity.Required == nil ||
|
||||
len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := make(map[string]bool)
|
||||
for i := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
for _, r := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms[i].MatchExpressions {
|
||||
if r.Key == key {
|
||||
for _, v := range r.Values {
|
||||
values[v] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// remove duplication and sort them in order for better usage
|
||||
var re []string
|
||||
for k := range values {
|
||||
re = append(re, k)
|
||||
}
|
||||
sort.Strings(re)
|
||||
return re
|
||||
}
|
||||
|
||||
// addTopology appends the topology to the given PV to all Terms.
|
||||
func addTopology(pv *v1.PersistentVolume, topologyKey string, zones []string) error {
|
||||
// Make sure there are no duplicate or empty strings
|
||||
filteredZones := sets.String{}
|
||||
for i := range zones {
|
||||
zone := strings.TrimSpace(zones[i])
|
||||
if len(zone) > 0 {
|
||||
filteredZones.Insert(zone)
|
||||
}
|
||||
}
|
||||
|
||||
zones = filteredZones.List()
|
||||
if len(zones) < 1 {
|
||||
return errors.New("there are no valid zones to add to pv")
|
||||
}
|
||||
|
||||
// Make sure the necessary fields exist
|
||||
if pv.Spec.NodeAffinity == nil {
|
||||
pv.Spec.NodeAffinity = new(v1.VolumeNodeAffinity)
|
||||
}
|
||||
|
||||
if pv.Spec.NodeAffinity.Required == nil {
|
||||
pv.Spec.NodeAffinity.Required = new(v1.NodeSelector)
|
||||
}
|
||||
|
||||
if len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) == 0 {
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms = make([]v1.NodeSelectorTerm, 1)
|
||||
}
|
||||
|
||||
topology := v1.NodeSelectorRequirement{
|
||||
Key: topologyKey,
|
||||
Operator: v1.NodeSelectorOpIn,
|
||||
Values: zones,
|
||||
}
|
||||
|
||||
// add the CSI topology to each term
|
||||
for i := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms[i].MatchExpressions = append(
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms[i].MatchExpressions,
|
||||
topology,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// translateTopologyFromInTreeToCSI converts existing zone labels or in-tree topology to CSI topology.
|
||||
// In-tree topology has precedence over zone labels. When both in-tree topology and zone labels exist
|
||||
// for a particular CSI topology, in-tree topology will be used.
|
||||
// This function will remove the Beta version Kubernetes topology label in case the node upgrade to a
|
||||
// newer version where it does not have any Beta topology label anymore
|
||||
func translateTopologyFromInTreeToCSI(pv *v1.PersistentVolume, csiTopologyKey string) error {
|
||||
|
||||
zoneLabel, regionLabel := getTopologyLabel(pv)
|
||||
|
||||
// If Zone kubernetes topology exist, replace it to use csiTopologyKey
|
||||
zones := getTopologyValues(pv, zoneLabel)
|
||||
if len(zones) > 0 {
|
||||
replaceTopology(pv, zoneLabel, csiTopologyKey)
|
||||
} else {
|
||||
// if nothing is in the NodeAffinity, try to fetch the topology from PV labels
|
||||
if label, ok := pv.Labels[zoneLabel]; ok {
|
||||
zones = strings.Split(label, labelMultiZoneDelimiter)
|
||||
if len(zones) > 0 {
|
||||
addTopology(pv, csiTopologyKey, zones)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if the in-tree PV has beta region label, replace it with GA label to ensure
|
||||
// the scheduler is able to schedule it on new nodes with only GA kubernetes label
|
||||
// No need to check it for zone label because it has already been replaced if exist
|
||||
if regionLabel == v1.LabelFailureDomainBetaRegion {
|
||||
regions := getTopologyValues(pv, regionLabel)
|
||||
if len(regions) > 0 {
|
||||
replaceTopology(pv, regionLabel, v1.LabelTopologyRegion)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getTopologyLabel checks if the kubernetes topology label used in this
|
||||
// PV is GA and return the zone/region label used.
|
||||
// The version checking follows the following orders
|
||||
// 1. Check NodeAffinity
|
||||
// 1.1 Check if zoneGA exists, if yes return GA labels
|
||||
// 1.2 Check if zoneBeta exists, if yes return Beta labels
|
||||
// 2. Check PV labels
|
||||
// 2.1 Check if zoneGA exists, if yes return GA labels
|
||||
// 2.2 Check if zoneBeta exists, if yes return Beta labels
|
||||
func getTopologyLabel(pv *v1.PersistentVolume) (zoneLabel string, regionLabel string) {
|
||||
|
||||
if zoneGA := TopologyKeyExist(v1.LabelTopologyZone, pv.Spec.NodeAffinity); zoneGA {
|
||||
return v1.LabelTopologyZone, v1.LabelTopologyRegion
|
||||
}
|
||||
if zoneBeta := TopologyKeyExist(v1.LabelFailureDomainBetaZone, pv.Spec.NodeAffinity); zoneBeta {
|
||||
return v1.LabelFailureDomainBetaZone, v1.LabelFailureDomainBetaRegion
|
||||
}
|
||||
if _, zoneGA := pv.Labels[v1.LabelTopologyZone]; zoneGA {
|
||||
return v1.LabelTopologyZone, v1.LabelTopologyRegion
|
||||
}
|
||||
if _, zoneBeta := pv.Labels[v1.LabelFailureDomainBetaZone]; zoneBeta {
|
||||
return v1.LabelFailureDomainBetaZone, v1.LabelFailureDomainBetaRegion
|
||||
}
|
||||
// No labels or NodeAffinity exist, default to GA version
|
||||
return v1.LabelTopologyZone, v1.LabelTopologyRegion
|
||||
}
|
||||
|
||||
// TopologyKeyExist checks if a certain key exists in a VolumeNodeAffinity
|
||||
func TopologyKeyExist(key string, vna *v1.VolumeNodeAffinity) bool {
|
||||
if vna == nil || vna.Required == nil || vna.Required.NodeSelectorTerms == nil || len(vna.Required.NodeSelectorTerms) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, nodeSelectorTerms := range vna.Required.NodeSelectorTerms {
|
||||
nsrequirements := nodeSelectorTerms.MatchExpressions
|
||||
for _, nodeSelectorRequirement := range nsrequirements {
|
||||
if nodeSelectorRequirement.Key == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type regionParserFn func([]string) (string, error)
|
||||
|
||||
// translateTopologyFromCSIToInTree translate a CSI topology to
|
||||
// Kubernetes topology and add topology labels to it. Note that this function
|
||||
// will only work for plugin with a single topologyKey that translates to
|
||||
// Kubernetes zone(and region if regionParser is passed in).
|
||||
// If a plugin has more than one topologyKey, it will need to be processed
|
||||
// separately by the plugin.
|
||||
// If regionParser is nil, no region NodeAffinity will be added. If not nil,
|
||||
// it'll be passed to regionTopologyHandler, which will add region topology NodeAffinity
|
||||
// and labels for the given PV. It assumes the Zone NodeAffinity already exists.
|
||||
// In short this function will,
|
||||
// 1. Replace all CSI topology to Kubernetes Zone topology label
|
||||
// 2. Process and generate region topology if a regionParser is passed
|
||||
// 3. Add Kubernetes Topology labels(zone) if they do not exist
|
||||
func translateTopologyFromCSIToInTree(pv *v1.PersistentVolume, csiTopologyKey string, regionParser regionParserFn) error {
|
||||
|
||||
zoneLabel, _ := getTopologyLabel(pv)
|
||||
|
||||
// 1. Replace all CSI topology to Kubernetes Zone label
|
||||
err := replaceTopology(pv, csiTopologyKey, zoneLabel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to replace CSI topology to Kubernetes topology, error: %v", err)
|
||||
}
|
||||
|
||||
// 2. Take care of region topology if a regionParser is passed
|
||||
if regionParser != nil {
|
||||
// let's make less strict on this one. Even if there is an error in the region processing, just ignore it
|
||||
err = regionTopologyHandler(pv, regionParser)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to handle region topology. error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Add labels about Kubernetes Topology
|
||||
zoneVals := getTopologyValues(pv, zoneLabel)
|
||||
if len(zoneVals) > 0 {
|
||||
if pv.Labels == nil {
|
||||
pv.Labels = make(map[string]string)
|
||||
}
|
||||
_, zoneOK := pv.Labels[zoneLabel]
|
||||
if !zoneOK {
|
||||
zoneValStr := strings.Join(zoneVals, labelMultiZoneDelimiter)
|
||||
pv.Labels[zoneLabel] = zoneValStr
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// translateAllowedTopologies translates allowed topologies within storage class or PV
|
||||
// from legacy failure domain to given CSI topology key
|
||||
func translateAllowedTopologies(terms []v1.TopologySelectorTerm, key string) ([]v1.TopologySelectorTerm, error) {
|
||||
if terms == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
newTopologies := []v1.TopologySelectorTerm{}
|
||||
for _, term := range terms {
|
||||
newTerm := v1.TopologySelectorTerm{}
|
||||
for _, exp := range term.MatchLabelExpressions {
|
||||
var newExp v1.TopologySelectorLabelRequirement
|
||||
if exp.Key == v1.LabelFailureDomainBetaZone || exp.Key == v1.LabelTopologyZone {
|
||||
newExp = v1.TopologySelectorLabelRequirement{
|
||||
Key: key,
|
||||
Values: exp.Values,
|
||||
}
|
||||
} else {
|
||||
// Other topologies are passed through unchanged.
|
||||
newExp = exp
|
||||
}
|
||||
newTerm.MatchLabelExpressions = append(newTerm.MatchLabelExpressions, newExp)
|
||||
}
|
||||
newTopologies = append(newTopologies, newTerm)
|
||||
}
|
||||
return newTopologies, nil
|
||||
}
|
||||
|
||||
// regionTopologyHandler will process the PV and add region
|
||||
// kubernetes topology label to its NodeAffinity and labels
|
||||
// It assumes the Zone NodeAffinity already exists
|
||||
// Each provider is responsible for providing their own regionParser
|
||||
func regionTopologyHandler(pv *v1.PersistentVolume, regionParser regionParserFn) error {
|
||||
|
||||
// Make sure the necessary fields exist
|
||||
if pv == nil || pv.Spec.NodeAffinity == nil || pv.Spec.NodeAffinity.Required == nil ||
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms == nil || len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
zoneLabel, regionLabel := getTopologyLabel(pv)
|
||||
|
||||
// process each term
|
||||
for index, nodeSelectorTerm := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
// In the first loop, see if regionLabel already exist
|
||||
regionExist := false
|
||||
var zoneVals []string
|
||||
for _, nsRequirement := range nodeSelectorTerm.MatchExpressions {
|
||||
if nsRequirement.Key == regionLabel {
|
||||
regionExist = true
|
||||
break
|
||||
} else if nsRequirement.Key == zoneLabel {
|
||||
zoneVals = append(zoneVals, nsRequirement.Values...)
|
||||
}
|
||||
}
|
||||
if regionExist {
|
||||
// Regionlabel already exist in this term, skip it
|
||||
continue
|
||||
}
|
||||
// If no regionLabel found, generate region label from the zoneLabel we collect from this term
|
||||
regionVal, err := regionParser(zoneVals)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Add the regionVal to this term
|
||||
pv.Spec.NodeAffinity.Required.NodeSelectorTerms[index].MatchExpressions =
|
||||
append(pv.Spec.NodeAffinity.Required.NodeSelectorTerms[index].MatchExpressions, v1.NodeSelectorRequirement{
|
||||
Key: regionLabel,
|
||||
Operator: v1.NodeSelectorOpIn,
|
||||
Values: []string{regionVal},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// Add region label
|
||||
regionVals := getTopologyValues(pv, regionLabel)
|
||||
if len(regionVals) == 1 {
|
||||
// We should only have exactly 1 region value
|
||||
if pv.Labels == nil {
|
||||
pv.Labels = make(map[string]string)
|
||||
}
|
||||
_, regionOK := pv.Labels[regionLabel]
|
||||
if !regionOK {
|
||||
pv.Labels[regionLabel] = regionVals[0]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
185
vendor/k8s.io/csi-translation-lib/plugins/openstack_cinder.go
generated
vendored
Normal file
185
vendor/k8s.io/csi-translation-lib/plugins/openstack_cinder.go
generated
vendored
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// CinderDriverName is the name of the CSI driver for Cinder
|
||||
CinderDriverName = "cinder.csi.openstack.org"
|
||||
// CinderTopologyKey is the zonal topology key for Cinder CSI Driver
|
||||
CinderTopologyKey = "topology.cinder.csi.openstack.org/zone"
|
||||
// CinderInTreePluginName is the name of the intree plugin for Cinder
|
||||
CinderInTreePluginName = "kubernetes.io/cinder"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = (*osCinderCSITranslator)(nil)
|
||||
|
||||
// osCinderCSITranslator handles translation of PV spec from In-tree Cinder to CSI Cinder and vice versa
|
||||
type osCinderCSITranslator struct{}
|
||||
|
||||
// NewOpenStackCinderCSITranslator returns a new instance of osCinderCSITranslator
|
||||
func NewOpenStackCinderCSITranslator() InTreePlugin {
|
||||
return &osCinderCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree Cinder storage class parameters to CSI storage class
|
||||
func (t *osCinderCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
var (
|
||||
params = map[string]string{}
|
||||
)
|
||||
for k, v := range sc.Parameters {
|
||||
switch strings.ToLower(k) {
|
||||
case fsTypeKey:
|
||||
params[csiFsTypeKey] = v
|
||||
default:
|
||||
// All other parameters are supported by the CSI driver.
|
||||
// This includes also "availability", therefore do not translate it to sc.AllowedTopologies
|
||||
params[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if len(sc.AllowedTopologies) > 0 {
|
||||
newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, CinderTopologyKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed translating allowed topologies: %v", err)
|
||||
}
|
||||
sc.AllowedTopologies = newTopologies
|
||||
}
|
||||
|
||||
sc.Parameters = params
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with Cinder set from in-tree
|
||||
// and converts the Cinder source to a CSIPersistentVolumeSource
|
||||
func (t *osCinderCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.Cinder == nil {
|
||||
return nil, fmt.Errorf("volume is nil or Cinder not defined on volume")
|
||||
}
|
||||
|
||||
cinderSource := volume.Cinder
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique per disk as it is used as the unique part of the
|
||||
// staging path
|
||||
Name: fmt.Sprintf("%s-%s", CinderDriverName, cinderSource.VolumeID),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: CinderDriverName,
|
||||
VolumeHandle: cinderSource.VolumeID,
|
||||
ReadOnly: cinderSource.ReadOnly,
|
||||
FSType: cinderSource.FSType,
|
||||
VolumeAttributes: map[string]string{},
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
||||
},
|
||||
}
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with Cinder set from in-tree
|
||||
// and converts the Cinder source to a CSIPersistentVolumeSource
|
||||
func (t *osCinderCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.Cinder == nil {
|
||||
return nil, fmt.Errorf("pv is nil or Cinder not defined on pv")
|
||||
}
|
||||
|
||||
cinderSource := pv.Spec.Cinder
|
||||
|
||||
csiSource := &v1.CSIPersistentVolumeSource{
|
||||
Driver: CinderDriverName,
|
||||
VolumeHandle: cinderSource.VolumeID,
|
||||
ReadOnly: cinderSource.ReadOnly,
|
||||
FSType: cinderSource.FSType,
|
||||
VolumeAttributes: map[string]string{},
|
||||
}
|
||||
|
||||
if err := translateTopologyFromInTreeToCSI(pv, CinderTopologyKey); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology: %v", err)
|
||||
}
|
||||
|
||||
pv.Spec.Cinder = nil
|
||||
pv.Spec.CSI = csiSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the Cinder CSI source to a Cinder In-tree source.
|
||||
func (t *osCinderCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
cinderSource := &v1.CinderPersistentVolumeSource{
|
||||
VolumeID: csiSource.VolumeHandle,
|
||||
FSType: csiSource.FSType,
|
||||
ReadOnly: csiSource.ReadOnly,
|
||||
}
|
||||
|
||||
// translate CSI topology to In-tree topology for rollback compatibility.
|
||||
// It is not possible to guess Cinder Region from the Zone, therefore leave it empty.
|
||||
if err := translateTopologyFromCSIToInTree(pv, CinderTopologyKey, nil); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology. PV:%+v. Error:%v", *pv, err)
|
||||
}
|
||||
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.Cinder = cinderSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *osCinderCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.Cinder != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API. The spec pointer should be considered
|
||||
// const.
|
||||
func (t *osCinderCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.Cinder != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the intree plugin driver
|
||||
func (t *osCinderCSITranslator) GetInTreePluginName() string {
|
||||
return CinderInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (t *osCinderCSITranslator) GetCSIPluginName() string {
|
||||
return CinderDriverName
|
||||
}
|
||||
|
||||
func (t *osCinderCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
146
vendor/k8s.io/csi-translation-lib/plugins/portworx.go
generated
vendored
Normal file
146
vendor/k8s.io/csi-translation-lib/plugins/portworx.go
generated
vendored
Normal file
@ -0,0 +1,146 @@
|
||||
/*
|
||||
Copyright 2021 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storagev1 "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
PortworxVolumePluginName = "kubernetes.io/portworx-volume"
|
||||
PortworxDriverName = "pxd.portworx.com"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &portworxCSITranslator{}
|
||||
|
||||
type portworxCSITranslator struct{}
|
||||
|
||||
func NewPortworxCSITranslator() InTreePlugin {
|
||||
return &portworxCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI takes in-tree storage class used by in-tree plugin
|
||||
// and translates them to a storageclass consumable by CSI plugin
|
||||
func (p portworxCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storagev1.StorageClass) (*storagev1.StorageClass, error) {
|
||||
if sc == nil {
|
||||
return nil, fmt.Errorf("sc is nil")
|
||||
}
|
||||
sc.Provisioner = PortworxDriverName
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a inline volume and will translate
|
||||
// the in-tree inline volume source to a CSIPersistentVolumeSource
|
||||
func (p portworxCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.PortworxVolume == nil {
|
||||
return nil, fmt.Errorf("volume is nil or PortworxVolume not defined on volume")
|
||||
}
|
||||
|
||||
var am v1.PersistentVolumeAccessMode
|
||||
if volume.PortworxVolume.ReadOnly {
|
||||
am = v1.ReadOnlyMany
|
||||
} else {
|
||||
am = v1.ReadWriteOnce
|
||||
}
|
||||
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: fmt.Sprintf("%s-%s", PortworxDriverName, volume.PortworxVolume.VolumeID),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: PortworxDriverName,
|
||||
VolumeHandle: volume.PortworxVolume.VolumeID,
|
||||
FSType: volume.PortworxVolume.FSType,
|
||||
VolumeAttributes: make(map[string]string),
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{am},
|
||||
},
|
||||
}
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a Portworx persistent volume and will translate
|
||||
// the in-tree pv source to a CSI Source
|
||||
func (p portworxCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.PortworxVolume == nil {
|
||||
return nil, fmt.Errorf("pv is nil or PortworxVolume not defined on pv")
|
||||
}
|
||||
csiSource := &v1.CSIPersistentVolumeSource{
|
||||
Driver: PortworxDriverName,
|
||||
VolumeHandle: pv.Spec.PortworxVolume.VolumeID,
|
||||
FSType: pv.Spec.PortworxVolume.FSType,
|
||||
VolumeAttributes: make(map[string]string), // copy access mode
|
||||
}
|
||||
pv.Spec.PortworxVolume = nil
|
||||
pv.Spec.CSI = csiSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with a CSI PersistentVolume Source and will translate
|
||||
// it to a in-tree Persistent Volume Source for the in-tree volume
|
||||
func (p portworxCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
csiSource := pv.Spec.CSI
|
||||
|
||||
portworxSource := &v1.PortworxVolumeSource{
|
||||
VolumeID: csiSource.VolumeHandle,
|
||||
FSType: csiSource.FSType,
|
||||
ReadOnly: csiSource.ReadOnly,
|
||||
}
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.PortworxVolume = portworxSource
|
||||
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API.
|
||||
func (p portworxCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.PortworxVolume != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API.
|
||||
func (p portworxCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.PortworxVolume != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the in-tree plugin name this migrates
|
||||
func (p portworxCSITranslator) GetInTreePluginName() string {
|
||||
return PortworxVolumePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin that supersedes the in-tree plugin
|
||||
func (p portworxCSITranslator) GetCSIPluginName() string {
|
||||
return PortworxDriverName
|
||||
}
|
||||
|
||||
// RepairVolumeHandle generates a correct volume handle based on node ID information.
|
||||
func (p portworxCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
302
vendor/k8s.io/csi-translation-lib/plugins/vsphere_volume.go
generated
vendored
Normal file
302
vendor/k8s.io/csi-translation-lib/plugins/vsphere_volume.go
generated
vendored
Normal file
@ -0,0 +1,302 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
storage "k8s.io/api/storage/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// VSphereDriverName is the name of the CSI driver for vSphere Volume
|
||||
VSphereDriverName = "csi.vsphere.vmware.com"
|
||||
// VSphereInTreePluginName is the name of the in-tree plugin for vSphere Volume
|
||||
VSphereInTreePluginName = "kubernetes.io/vsphere-volume"
|
||||
|
||||
// vSphereCSITopologyZoneKey is the zonal topology key for vSphere CSI Driver
|
||||
vSphereCSITopologyZoneKey = "topology.csi.vmware.com/zone"
|
||||
|
||||
// vSphereCSITopologyRegionKey is the region topology key for vSphere CSI Driver
|
||||
vSphereCSITopologyRegionKey = "topology.csi.vmware.com/region"
|
||||
|
||||
// paramStoragePolicyName used to supply SPBM Policy name for Volume provisioning
|
||||
paramStoragePolicyName = "storagepolicyname"
|
||||
|
||||
// This param is used to tell Driver to return volumePath and not VolumeID
|
||||
// in-tree vSphere plugin does not understand volume id, it uses volumePath
|
||||
paramcsiMigration = "csimigration"
|
||||
|
||||
// This param is used to supply datastore name for Volume provisioning
|
||||
paramDatastore = "datastore-migrationparam"
|
||||
|
||||
// This param supplies disk foramt (thin, thick, zeoredthick) for Volume provisioning
|
||||
paramDiskFormat = "diskformat-migrationparam"
|
||||
|
||||
// vSAN Policy Parameters
|
||||
paramHostFailuresToTolerate = "hostfailurestotolerate-migrationparam"
|
||||
paramForceProvisioning = "forceprovisioning-migrationparam"
|
||||
paramCacheReservation = "cachereservation-migrationparam"
|
||||
paramDiskstripes = "diskstripes-migrationparam"
|
||||
paramObjectspacereservation = "objectspacereservation-migrationparam"
|
||||
paramIopslimit = "iopslimit-migrationparam"
|
||||
|
||||
// AttributeInitialVolumeFilepath represents the path of volume where volume is created
|
||||
AttributeInitialVolumeFilepath = "initialvolumefilepath"
|
||||
)
|
||||
|
||||
var _ InTreePlugin = &vSphereCSITranslator{}
|
||||
|
||||
// vSphereCSITranslator handles translation of PV spec from In-tree vSphere Volume to vSphere CSI
|
||||
type vSphereCSITranslator struct{}
|
||||
|
||||
// NewvSphereCSITranslator returns a new instance of vSphereCSITranslator
|
||||
func NewvSphereCSITranslator() InTreePlugin {
|
||||
return &vSphereCSITranslator{}
|
||||
}
|
||||
|
||||
// TranslateInTreeStorageClassToCSI translates InTree vSphere storage class parameters to CSI storage class
|
||||
func (t *vSphereCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logger, sc *storage.StorageClass) (*storage.StorageClass, error) {
|
||||
if sc == nil {
|
||||
return nil, fmt.Errorf("sc is nil")
|
||||
}
|
||||
var params = map[string]string{}
|
||||
for k, v := range sc.Parameters {
|
||||
switch strings.ToLower(k) {
|
||||
case fsTypeKey:
|
||||
params[csiFsTypeKey] = v
|
||||
case paramStoragePolicyName:
|
||||
params[paramStoragePolicyName] = v
|
||||
case "datastore":
|
||||
params[paramDatastore] = v
|
||||
case "diskformat":
|
||||
params[paramDiskFormat] = v
|
||||
case "hostfailurestotolerate":
|
||||
params[paramHostFailuresToTolerate] = v
|
||||
case "forceprovisioning":
|
||||
params[paramForceProvisioning] = v
|
||||
case "cachereservation":
|
||||
params[paramCacheReservation] = v
|
||||
case "diskstripes":
|
||||
params[paramDiskstripes] = v
|
||||
case "objectspacereservation":
|
||||
params[paramObjectspacereservation] = v
|
||||
case "iopslimit":
|
||||
params[paramIopslimit] = v
|
||||
default:
|
||||
logger.V(2).Info("StorageClass parameter is not supported", "name", k, "value", v)
|
||||
}
|
||||
}
|
||||
|
||||
// This helps vSphere CSI driver to identify in-tree provisioner request vs CSI provisioner request
|
||||
// When this is true, Driver returns initialvolumefilepath in the VolumeContext, which is
|
||||
// used in TranslateCSIPVToInTree
|
||||
params[paramcsiMigration] = "true"
|
||||
// translate AllowedTopologies to vSphere CSI Driver topology
|
||||
if len(sc.AllowedTopologies) > 0 {
|
||||
newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, vSphereCSITopologyZoneKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed translating allowed topologies: %v", err)
|
||||
}
|
||||
sc.AllowedTopologies = newTopologies
|
||||
}
|
||||
sc.Parameters = params
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// TranslateInTreeInlineVolumeToCSI takes a Volume with VsphereVolume set from in-tree
|
||||
// and converts the VsphereVolume source to a CSIPersistentVolumeSource
|
||||
func (t *vSphereCSITranslator) TranslateInTreeInlineVolumeToCSI(logger klog.Logger, volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
||||
if volume == nil || volume.VsphereVolume == nil {
|
||||
return nil, fmt.Errorf("volume is nil or VsphereVolume not defined on volume")
|
||||
}
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
// Must be unique per disk as it is used as the unique part of the
|
||||
// staging path
|
||||
Name: fmt.Sprintf("%s-%s", VSphereDriverName, volume.VsphereVolume.VolumePath),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
CSI: &v1.CSIPersistentVolumeSource{
|
||||
Driver: VSphereDriverName,
|
||||
VolumeHandle: volume.VsphereVolume.VolumePath,
|
||||
FSType: volume.VsphereVolume.FSType,
|
||||
VolumeAttributes: make(map[string]string),
|
||||
},
|
||||
},
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
||||
},
|
||||
}
|
||||
if volume.VsphereVolume.StoragePolicyName != "" {
|
||||
pv.Spec.CSI.VolumeAttributes[paramStoragePolicyName] = pv.Spec.VsphereVolume.StoragePolicyName
|
||||
}
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateInTreePVToCSI takes a PV with VsphereVolume set from in-tree
|
||||
// and converts the VsphereVolume source to a CSIPersistentVolumeSource
|
||||
func (t *vSphereCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.VsphereVolume == nil {
|
||||
return nil, fmt.Errorf("pv is nil or VsphereVolume not defined on pv")
|
||||
}
|
||||
csiSource := &v1.CSIPersistentVolumeSource{
|
||||
Driver: VSphereDriverName,
|
||||
VolumeHandle: pv.Spec.VsphereVolume.VolumePath,
|
||||
FSType: pv.Spec.VsphereVolume.FSType,
|
||||
VolumeAttributes: make(map[string]string),
|
||||
}
|
||||
if pv.Spec.VsphereVolume.StoragePolicyName != "" {
|
||||
csiSource.VolumeAttributes[paramStoragePolicyName] = pv.Spec.VsphereVolume.StoragePolicyName
|
||||
}
|
||||
// translate in-tree topology to CSI topology for migration
|
||||
if err := translateTopologyFromInTreevSphereToCSI(pv, vSphereCSITopologyZoneKey, vSphereCSITopologyRegionKey); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology: %v", err)
|
||||
}
|
||||
pv.Spec.VsphereVolume = nil
|
||||
pv.Spec.CSI = csiSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and
|
||||
// translates the vSphere CSI source to a vSphereVolume source.
|
||||
func (t *vSphereCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||
if pv == nil || pv.Spec.CSI == nil {
|
||||
return nil, fmt.Errorf("pv is nil or CSI source not defined on pv")
|
||||
}
|
||||
csiSource := pv.Spec.CSI
|
||||
vsphereVirtualDiskVolumeSource := &v1.VsphereVirtualDiskVolumeSource{
|
||||
FSType: csiSource.FSType,
|
||||
}
|
||||
volumeFilePath, ok := csiSource.VolumeAttributes[AttributeInitialVolumeFilepath]
|
||||
if ok {
|
||||
vsphereVirtualDiskVolumeSource.VolumePath = volumeFilePath
|
||||
}
|
||||
// translate CSI topology to In-tree topology for rollback compatibility.
|
||||
if err := translateTopologyFromCSIToInTreevSphere(pv, vSphereCSITopologyZoneKey, vSphereCSITopologyRegionKey); err != nil {
|
||||
return nil, fmt.Errorf("failed to translate topology. PV:%+v. Error:%v", *pv, err)
|
||||
}
|
||||
pv.Spec.CSI = nil
|
||||
pv.Spec.VsphereVolume = vsphereVirtualDiskVolumeSource
|
||||
return pv, nil
|
||||
}
|
||||
|
||||
// CanSupport tests whether the plugin supports a given persistent volume
|
||||
// specification from the API.
|
||||
func (t *vSphereCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
|
||||
return pv != nil && pv.Spec.VsphereVolume != nil
|
||||
}
|
||||
|
||||
// CanSupportInline tests whether the plugin supports a given inline volume
|
||||
// specification from the API.
|
||||
func (t *vSphereCSITranslator) CanSupportInline(volume *v1.Volume) bool {
|
||||
return volume != nil && volume.VsphereVolume != nil
|
||||
}
|
||||
|
||||
// GetInTreePluginName returns the name of the in-tree plugin driver
|
||||
func (t *vSphereCSITranslator) GetInTreePluginName() string {
|
||||
return VSphereInTreePluginName
|
||||
}
|
||||
|
||||
// GetCSIPluginName returns the name of the CSI plugin
|
||||
func (t *vSphereCSITranslator) GetCSIPluginName() string {
|
||||
return VSphereDriverName
|
||||
}
|
||||
|
||||
// RepairVolumeHandle is needed in VerifyVolumesAttached on the external attacher when we need to do strict volume
|
||||
// handle matching to check VolumeAttachment attached status.
|
||||
// vSphere volume does not need patch to help verify whether that volume is attached.
|
||||
func (t *vSphereCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
||||
return volumeHandle, nil
|
||||
}
|
||||
|
||||
// translateTopologyFromInTreevSphereToCSI converts existing zone labels or in-tree vsphere topology to
|
||||
// vSphere CSI topology.
|
||||
func translateTopologyFromInTreevSphereToCSI(pv *v1.PersistentVolume, csiTopologyKeyZone string, csiTopologyKeyRegion string) error {
|
||||
zoneLabel, regionLabel := getTopologyLabel(pv)
|
||||
|
||||
// If Zone kubernetes topology exist, replace it to use csiTopologyKeyZone
|
||||
zones := getTopologyValues(pv, zoneLabel)
|
||||
if len(zones) > 0 {
|
||||
replaceTopology(pv, zoneLabel, csiTopologyKeyZone)
|
||||
} else {
|
||||
// if nothing is in the NodeAffinity, try to fetch the topology from PV labels
|
||||
if label, ok := pv.Labels[zoneLabel]; ok {
|
||||
if len(label) > 0 {
|
||||
addTopology(pv, csiTopologyKeyZone, []string{label})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If region kubernetes topology exist, replace it to use csiTopologyKeyRegion
|
||||
regions := getTopologyValues(pv, regionLabel)
|
||||
if len(regions) > 0 {
|
||||
replaceTopology(pv, regionLabel, csiTopologyKeyRegion)
|
||||
} else {
|
||||
// if nothing is in the NodeAffinity, try to fetch the topology from PV labels
|
||||
if label, ok := pv.Labels[regionLabel]; ok {
|
||||
if len(label) > 0 {
|
||||
addTopology(pv, csiTopologyKeyRegion, []string{label})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// translateTopologyFromCSIToInTreevSphere converts CSI zone/region affinity rules to in-tree vSphere zone/region labels
|
||||
func translateTopologyFromCSIToInTreevSphere(pv *v1.PersistentVolume,
|
||||
csiTopologyKeyZone string, csiTopologyKeyRegion string) error {
|
||||
zoneLabel, regionLabel := getTopologyLabel(pv)
|
||||
|
||||
// Replace all CSI topology to Kubernetes Zone label
|
||||
err := replaceTopology(pv, csiTopologyKeyZone, zoneLabel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace CSI topology to Kubernetes topology, error: %v", err)
|
||||
}
|
||||
|
||||
// Replace all CSI topology to Kubernetes Region label
|
||||
err = replaceTopology(pv, csiTopologyKeyRegion, regionLabel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace CSI topology to Kubernetes topology, error: %v", err)
|
||||
}
|
||||
|
||||
zoneVals := getTopologyValues(pv, zoneLabel)
|
||||
if len(zoneVals) > 0 {
|
||||
if pv.Labels == nil {
|
||||
pv.Labels = make(map[string]string)
|
||||
}
|
||||
_, zoneOK := pv.Labels[zoneLabel]
|
||||
if !zoneOK {
|
||||
pv.Labels[zoneLabel] = zoneVals[0]
|
||||
}
|
||||
}
|
||||
regionVals := getTopologyValues(pv, regionLabel)
|
||||
if len(regionVals) > 0 {
|
||||
if pv.Labels == nil {
|
||||
pv.Labels = make(map[string]string)
|
||||
}
|
||||
_, regionOK := pv.Labels[regionLabel]
|
||||
if !regionOK {
|
||||
pv.Labels[regionLabel] = regionVals[0]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user