Skip to content

Commit

Permalink
Add Migration unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidz627 committed Oct 4, 2019
1 parent d24b8ad commit e6ea6b5
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 59 deletions.
10 changes: 6 additions & 4 deletions cmd/csi-provisioner/csi-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (

utilfeature "k8s.io/apiserver/pkg/util/feature"
utilflag "k8s.io/component-base/cli/flag"
csitranslationlib "k8s.io/csi-translation-lib"
csitrans "k8s.io/csi-translation-lib"
)

var (
Expand Down Expand Up @@ -169,9 +169,11 @@ func main() {
controller.CreateProvisionedPVLimiter(workqueue.DefaultControllerRateLimiter()),
}

csiTranslator := csitrans.New()

supportsMigrationFromInTreePluginName := ""
if csitranslationlib.IsMigratedCSIDriverByName(provisionerName) {
supportsMigrationFromInTreePluginName, err = csitranslationlib.GetInTreeNameFromCSIName(provisionerName)
if csiTranslator.IsMigratedCSIDriverByName(provisionerName) {
supportsMigrationFromInTreePluginName, err = csiTranslator.GetInTreeNameFromCSIName(provisionerName)
if err != nil {
klog.Fatalf("Failed to get InTree plugin name for migrated CSI plugin %s: %v", provisionerName, err)
}
Expand All @@ -181,7 +183,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, csiTranslator)
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 @@ -160,6 +160,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 @@ -182,6 +194,7 @@ type csiProvisioner struct {
controllerCapabilities connection.ControllerCapabilitySet
supportsMigrationFromInTreePluginName string
strictTopology bool
csiTranslationLibrary ProvisionerCSITranslator
}

var _ controller.Provisioner = &csiProvisioner{}
Expand Down Expand Up @@ -239,7 +252,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 @@ -256,6 +270,7 @@ func NewCSIProvisioner(client kubernetes.Interface,
controllerCapabilities: controllerCapabilities,
supportsMigrationFromInTreePluginName: supportsMigrationFromInTreePluginName,
strictTopology: strictTopology,
csiTranslationLibrary: csiTranslationLibrary,
}
return provisioner
}
Expand Down Expand Up @@ -387,7 +402,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 @@ -630,7 +645,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 @@ -818,12 +833,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 e6ea6b5

Please sign in to comment.