Skip to content

Commit

Permalink
Add some migration unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidz627 committed Sep 30, 2019
1 parent f6b8273 commit 3c4e127
Show file tree
Hide file tree
Showing 4 changed files with 450 additions and 124 deletions.
2 changes: 1 addition & 1 deletion cmd/csi-provisioner/csi-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func main() {

// Create the provisioner: it implements the Provisioner interface expected by
// the controller
csiProvisioner := ctrl.NewCSIProvisioner(clientset, *operationTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, grpcClient, snapClient, provisionerName, pluginCapabilities, controllerCapabilities, supportsMigrationFromInTreePluginName, *strictTopology)
csiProvisioner := ctrl.NewCSIProvisioner(clientset, *operationTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, grpcClient, snapClient, provisionerName, pluginCapabilities, controllerCapabilities, supportsMigrationFromInTreePluginName, *strictTopology, &csitranslationlib.CSITranslationLibrary{})
provisionController = controller.NewProvisionController(
clientset,
provisionerName,
Expand Down
27 changes: 21 additions & 6 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/sig-storage-lib-external-provisioner/util"

v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/apimachinery/pkg/util/json"
Expand All @@ -45,7 +46,6 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
csitranslationlib "k8s.io/csi-translation-lib"
"k8s.io/klog"

"google.golang.org/grpc"
Expand Down Expand Up @@ -158,6 +158,18 @@ var (
}
)

// ProvisionerCSITranslator contains the set of CSI Translation functionality
// required by the provisioner
type ProvisionerCSITranslator interface {
TranslateInTreeStorageClassToCSI(inTreePluginName string, sc *storagev1.StorageClass) (*storagev1.StorageClass, error)
TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, error)
IsPVMigratable(pv *v1.PersistentVolume) bool
TranslateInTreePVToCSI(pv *v1.PersistentVolume) (*v1.PersistentVolume, error)

IsMigratedCSIDriverByName(csiPluginName string) bool
GetInTreeNameFromCSIName(pluginName string) (string, error)
}

// requiredCapabilities provides a set of extra capabilities required for special/optional features provided by a plugin
type requiredCapabilities struct {
snapshot bool
Expand All @@ -180,6 +192,7 @@ type csiProvisioner struct {
controllerCapabilities connection.ControllerCapabilitySet
supportsMigrationFromInTreePluginName string
strictTopology bool
csiTranslationLibrary ProvisionerCSITranslator
}

var _ controller.Provisioner = &csiProvisioner{}
Expand Down Expand Up @@ -237,7 +250,8 @@ func NewCSIProvisioner(client kubernetes.Interface,
pluginCapabilities connection.PluginCapabilitySet,
controllerCapabilities connection.ControllerCapabilitySet,
supportsMigrationFromInTreePluginName string,
strictTopology bool) controller.Provisioner {
strictTopology bool,
csiTranslationLibrary ProvisionerCSITranslator) controller.Provisioner {

csiClient := csi.NewControllerClient(grpcClient)
provisioner := &csiProvisioner{
Expand All @@ -254,6 +268,7 @@ func NewCSIProvisioner(client kubernetes.Interface,
controllerCapabilities: controllerCapabilities,
supportsMigrationFromInTreePluginName: supportsMigrationFromInTreePluginName,
strictTopology: strictTopology,
csiTranslationLibrary: csiTranslationLibrary,
}
return provisioner
}
Expand Down Expand Up @@ -377,7 +392,7 @@ func (p *csiProvisioner) ProvisionExt(options controller.ProvisionOptions) (*v1.
// so that external provisioner can correctly pick up the PVC pointing to an in-tree plugin
if options.StorageClass.Provisioner == p.supportsMigrationFromInTreePluginName {
klog.V(2).Infof("translating storage class for in-tree plugin %s to CSI", options.StorageClass.Provisioner)
storageClass, err := csitranslationlib.TranslateInTreeStorageClassToCSI(p.supportsMigrationFromInTreePluginName, options.StorageClass)
storageClass, err := p.csiTranslationLibrary.TranslateInTreeStorageClassToCSI(p.supportsMigrationFromInTreePluginName, options.StorageClass)
if err != nil {
return nil, controller.ProvisioningFinished, fmt.Errorf("failed to translate storage class: %v", err)
}
Expand Down Expand Up @@ -620,7 +635,7 @@ func (p *csiProvisioner) ProvisionExt(options controller.ProvisionOptions) (*v1.
klog.V(2).Infof("successfully created PV %v for PVC %v and csi volume name %v", pv.Name, options.PVC.Name, pv.Spec.CSI.VolumeHandle)

if migratedVolume {
pv, err = csitranslationlib.TranslateCSIPVToInTree(pv)
pv, err = p.csiTranslationLibrary.TranslateCSIPVToInTree(pv)
if err != nil {
klog.Warningf("failed to translate CSI PV to in-tree due to: %v. Deleting provisioned PV", err)
deleteErr := p.Delete(pv)
Expand Down Expand Up @@ -808,12 +823,12 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error {
}

var err error
if csitranslationlib.IsPVMigratable(volume) {
if p.csiTranslationLibrary.IsPVMigratable(volume) {
// we end up here only if CSI migration is enabled in-tree (both overall
// and for the specific plugin that is migratable) causing in-tree PV
// controller to yield deletion of PVs with in-tree source to external provisioner
// based on AnnDynamicallyProvisioned annotation.
volume, err = csitranslationlib.TranslateInTreePVToCSI(volume)
volume, err = p.csiTranslationLibrary.TranslateInTreePVToCSI(volume)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 3c4e127

Please sign in to comment.