|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package plugins |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + storage "k8s.io/api/storage/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/klog/v2" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + // VSphereDriverName is the name of the CSI driver for vSphere Volume |
| 31 | + VSphereDriverName = "csi.vsphere.vmware.com" |
| 32 | + // VSphereInTreePluginName is the name of the in-tree plugin for vSphere Volume |
| 33 | + VSphereInTreePluginName = "kubernetes.io/vsphere-volume" |
| 34 | + |
| 35 | + // paramStoragePolicyName used to supply SPBM Policy name for Volume provisioning |
| 36 | + paramStoragePolicyName = "storagepolicyname" |
| 37 | + |
| 38 | + // This param is used to tell Driver to return volumePath and not VolumeID |
| 39 | + // in-tree vSphere plugin does not understand volume id, it uses volumePath |
| 40 | + paramcsiMigration = "csimigration" |
| 41 | + |
| 42 | + // This param is used to supply datastore name for Volume provisioning |
| 43 | + paramDatastore = "datastore-migrationparam" |
| 44 | + |
| 45 | + // This param supplies disk foramt (thin, thick, zeoredthick) for Volume provisioning |
| 46 | + paramDiskFormat = "diskformat-migrationparam" |
| 47 | + |
| 48 | + // vSAN Policy Parameters |
| 49 | + paramHostFailuresToTolerate = "hostfailurestotolerate-migrationparam" |
| 50 | + paramForceProvisioning = "forceprovisioning-migrationparam" |
| 51 | + paramCacheReservation = "cachereservation-migrationparam" |
| 52 | + paramDiskstripes = "diskstripes-migrationparam" |
| 53 | + paramObjectspacereservation = "objectspacereservation-migrationparam" |
| 54 | + paramIopslimit = "iopslimit-migrationparam" |
| 55 | + |
| 56 | + // AttributeInitialVolumeFilepath represents the path of volume where volume is created |
| 57 | + AttributeInitialVolumeFilepath = "initialvolumefilepath" |
| 58 | +) |
| 59 | + |
| 60 | +var _ InTreePlugin = &vSphereCSITranslator{} |
| 61 | + |
| 62 | +// vSphereCSITranslator handles translation of PV spec from In-tree vSphere Volume to vSphere CSI |
| 63 | +type vSphereCSITranslator struct{} |
| 64 | + |
| 65 | +// NewvSphereCSITranslator returns a new instance of vSphereCSITranslator |
| 66 | +func NewvSphereCSITranslator() InTreePlugin { |
| 67 | + return &vSphereCSITranslator{} |
| 68 | +} |
| 69 | + |
| 70 | +// TranslateInTreeStorageClassToCSI translates InTree vSphere storage class parameters to CSI storage class |
| 71 | +func (t *vSphereCSITranslator) TranslateInTreeStorageClassToCSI(sc *storage.StorageClass) (*storage.StorageClass, error) { |
| 72 | + if sc == nil { |
| 73 | + return nil, fmt.Errorf("sc is nil") |
| 74 | + } |
| 75 | + var params = map[string]string{} |
| 76 | + for k, v := range sc.Parameters { |
| 77 | + switch strings.ToLower(k) { |
| 78 | + case fsTypeKey: |
| 79 | + params[csiFsTypeKey] = v |
| 80 | + case paramStoragePolicyName: |
| 81 | + params[paramStoragePolicyName] = v |
| 82 | + case "datastore": |
| 83 | + params[paramDatastore] = v |
| 84 | + case "diskformat": |
| 85 | + params[paramDiskFormat] = v |
| 86 | + case "hostfailurestotolerate": |
| 87 | + params[paramHostFailuresToTolerate] = v |
| 88 | + case "forceprovisioning": |
| 89 | + params[paramForceProvisioning] = v |
| 90 | + case "cachereservation": |
| 91 | + params[paramCacheReservation] = v |
| 92 | + case "diskstripes": |
| 93 | + params[paramDiskstripes] = v |
| 94 | + case "objectspacereservation": |
| 95 | + params[paramObjectspacereservation] = v |
| 96 | + case "iopslimit": |
| 97 | + params[paramIopslimit] = v |
| 98 | + default: |
| 99 | + klog.V(2).Infof("StorageClass parameter [name:%q, value:%q] is not supported", k, v) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // This helps vSphere CSI driver to identify in-tree provisioner request vs CSI provisioner request |
| 104 | + // When this is true, Driver returns initialvolumefilepath in the VolumeContext, which is |
| 105 | + // used in TranslateCSIPVToInTree |
| 106 | + params[paramcsiMigration] = "true" |
| 107 | + // Note: sc.AllowedTopologies for Topology based volume provisioning will be supplied as it is. |
| 108 | + sc.Parameters = params |
| 109 | + return sc, nil |
| 110 | +} |
| 111 | + |
| 112 | +// TranslateInTreeInlineVolumeToCSI takes a Volume with VsphereVolume set from in-tree |
| 113 | +// and converts the VsphereVolume source to a CSIPersistentVolumeSource |
| 114 | +func (t *vSphereCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Volume) (*v1.PersistentVolume, error) { |
| 115 | + if volume == nil || volume.VsphereVolume == nil { |
| 116 | + return nil, fmt.Errorf("volume is nil or VsphereVolume not defined on volume") |
| 117 | + } |
| 118 | + pv := &v1.PersistentVolume{ |
| 119 | + ObjectMeta: metav1.ObjectMeta{ |
| 120 | + // Must be unique per disk as it is used as the unique part of the |
| 121 | + // staging path |
| 122 | + Name: fmt.Sprintf("%s-%s", VSphereDriverName, volume.VsphereVolume.VolumePath), |
| 123 | + }, |
| 124 | + Spec: v1.PersistentVolumeSpec{ |
| 125 | + PersistentVolumeSource: v1.PersistentVolumeSource{ |
| 126 | + CSI: &v1.CSIPersistentVolumeSource{ |
| 127 | + Driver: VSphereDriverName, |
| 128 | + VolumeHandle: volume.VsphereVolume.VolumePath, |
| 129 | + FSType: volume.VsphereVolume.FSType, |
| 130 | + VolumeAttributes: make(map[string]string), |
| 131 | + }, |
| 132 | + }, |
| 133 | + AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}, |
| 134 | + }, |
| 135 | + } |
| 136 | + if volume.VsphereVolume.StoragePolicyName != "" { |
| 137 | + pv.Spec.CSI.VolumeAttributes[paramStoragePolicyName] = pv.Spec.VsphereVolume.StoragePolicyName |
| 138 | + } |
| 139 | + return pv, nil |
| 140 | +} |
| 141 | + |
| 142 | +// TranslateInTreePVToCSI takes a PV with VsphereVolume set from in-tree |
| 143 | +// and converts the VsphereVolume source to a CSIPersistentVolumeSource |
| 144 | +func (t *vSphereCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) { |
| 145 | + if pv == nil || pv.Spec.VsphereVolume == nil { |
| 146 | + return nil, fmt.Errorf("pv is nil or VsphereVolume not defined on pv") |
| 147 | + } |
| 148 | + csiSource := &v1.CSIPersistentVolumeSource{ |
| 149 | + Driver: VSphereDriverName, |
| 150 | + VolumeHandle: pv.Spec.VsphereVolume.VolumePath, |
| 151 | + FSType: pv.Spec.VsphereVolume.FSType, |
| 152 | + VolumeAttributes: make(map[string]string), |
| 153 | + } |
| 154 | + if pv.Spec.VsphereVolume.StoragePolicyName != "" { |
| 155 | + csiSource.VolumeAttributes[paramStoragePolicyName] = pv.Spec.VsphereVolume.StoragePolicyName |
| 156 | + } |
| 157 | + pv.Spec.VsphereVolume = nil |
| 158 | + pv.Spec.CSI = csiSource |
| 159 | + return pv, nil |
| 160 | +} |
| 161 | + |
| 162 | +// TranslateCSIPVToInTree takes a PV with CSIPersistentVolumeSource set and |
| 163 | +// translates the vSphere CSI source to a vSphereVolume source. |
| 164 | +func (t *vSphereCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error) { |
| 165 | + if pv == nil || pv.Spec.CSI == nil { |
| 166 | + return nil, fmt.Errorf("pv is nil or CSI source not defined on pv") |
| 167 | + } |
| 168 | + csiSource := pv.Spec.CSI |
| 169 | + vsphereVirtualDiskVolumeSource := &v1.VsphereVirtualDiskVolumeSource{ |
| 170 | + FSType: csiSource.FSType, |
| 171 | + } |
| 172 | + volumeFilePath, ok := csiSource.VolumeAttributes[AttributeInitialVolumeFilepath] |
| 173 | + if ok { |
| 174 | + vsphereVirtualDiskVolumeSource.VolumePath = volumeFilePath |
| 175 | + } |
| 176 | + pv.Spec.CSI = nil |
| 177 | + pv.Spec.VsphereVolume = vsphereVirtualDiskVolumeSource |
| 178 | + return pv, nil |
| 179 | +} |
| 180 | + |
| 181 | +// CanSupport tests whether the plugin supports a given persistent volume |
| 182 | +// specification from the API. |
| 183 | +func (t *vSphereCSITranslator) CanSupport(pv *v1.PersistentVolume) bool { |
| 184 | + return pv != nil && pv.Spec.VsphereVolume != nil |
| 185 | +} |
| 186 | + |
| 187 | +// CanSupportInline tests whether the plugin supports a given inline volume |
| 188 | +// specification from the API. |
| 189 | +func (t *vSphereCSITranslator) CanSupportInline(volume *v1.Volume) bool { |
| 190 | + return volume != nil && volume.VsphereVolume != nil |
| 191 | +} |
| 192 | + |
| 193 | +// GetInTreePluginName returns the name of the in-tree plugin driver |
| 194 | +func (t *vSphereCSITranslator) GetInTreePluginName() string { |
| 195 | + return VSphereInTreePluginName |
| 196 | +} |
| 197 | + |
| 198 | +// GetCSIPluginName returns the name of the CSI plugin |
| 199 | +func (t *vSphereCSITranslator) GetCSIPluginName() string { |
| 200 | + return VSphereDriverName |
| 201 | +} |
| 202 | + |
| 203 | +// RepairVolumeHandle is needed in VerifyVolumesAttached on the external attacher when we need to do strict volume |
| 204 | +// handle matching to check VolumeAttachment attached status. |
| 205 | +// vSphere volume does not need patch to help verify whether that volume is attached. |
| 206 | +func (t *vSphereCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) { |
| 207 | + return volumeHandle, nil |
| 208 | +} |
0 commit comments