2019-04-07 17:07:55 +00:00
|
|
|
/*
|
|
|
|
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"
|
2021-02-22 20:08:19 +00:00
|
|
|
"strings"
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-08-30 18:33:25 +00:00
|
|
|
storage "k8s.io/api/storage/v1"
|
2019-09-27 21:51:53 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CinderDriverName is the name of the CSI driver for Cinder
|
|
|
|
CinderDriverName = "cinder.csi.openstack.org"
|
2019-12-12 01:27:03 +00:00
|
|
|
// CinderTopologyKey is the zonal topology key for Cinder CSI Driver
|
|
|
|
CinderTopologyKey = "topology.cinder.csi.openstack.org/zone"
|
2019-04-07 17:07:55 +00:00
|
|
|
// 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{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TranslateInTreeStorageClassParametersToCSI translates InTree Cinder storage class parameters to CSI storage class
|
2019-08-30 18:33:25 +00:00
|
|
|
func (t *osCinderCSITranslator) TranslateInTreeStorageClassToCSI(sc *storage.StorageClass) (*storage.StorageClass, error) {
|
2021-02-22 20:08:19 +00:00
|
|
|
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
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
return sc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TranslateInTreeInlineVolumeToCSI takes a Volume with Cinder set from in-tree
|
|
|
|
// and converts the Cinder source to a CSIPersistentVolumeSource
|
2021-05-14 17:12:55 +00:00
|
|
|
func (t *osCinderCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Volume, podNamespace string) (*v1.PersistentVolume, error) {
|
2019-08-30 18:33:25 +00:00
|
|
|
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{
|
2019-09-27 21:51:53 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-12-12 01:27:03 +00:00
|
|
|
// 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),
|
2019-09-27 21:51:53 +00:00
|
|
|
},
|
2019-08-30 18:33:25 +00:00
|
|
|
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
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TranslateInTreePVToCSI takes a PV with Cinder set from in-tree
|
|
|
|
// and converts the Cinder source to a CSIPersistentVolumeSource
|
|
|
|
func (t *osCinderCSITranslator) TranslateInTreePVToCSI(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{},
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
if err := translateTopologyFromInTreeToCSI(pv, CinderTopologyKey); err != nil {
|
2019-12-12 01:27:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to translate topology: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
pv.Spec.CSI = nil
|
|
|
|
pv.Spec.Cinder = cinderSource
|
|
|
|
return pv, nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// CanSupport tests whether the plugin supports a given persistent volume
|
2019-04-07 17:07:55 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-12-12 01:27:03 +00:00
|
|
|
|
|
|
|
func (t *osCinderCSITranslator) RepairVolumeHandle(volumeHandle, nodeID string) (string, error) {
|
|
|
|
return volumeHandle, nil
|
|
|
|
}
|