Merge pull request #97086 from xing-yang/check_datasource

Only CSI plugin can have a DataSource
This commit is contained in:
Kubernetes Prow Robot 2021-03-01 06:53:26 -08:00 committed by GitHub
commit f6152d1521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

View File

@ -443,6 +443,15 @@ func claimWithAnnotation(name, value string, claims []*v1.PersistentVolumeClaim)
return claims
}
func claimWithDataSource(name, kind, apiGroup string, claims []*v1.PersistentVolumeClaim) []*v1.PersistentVolumeClaim {
claims[0].Spec.DataSource = &v1.TypedLocalObjectReference{
Name: name,
Kind: kind,
APIGroup: &apiGroup,
}
return claims
}
func annotateClaim(claim *v1.PersistentVolumeClaim, ann map[string]string) *v1.PersistentVolumeClaim {
if claim.Annotations == nil {
claim.Annotations = map[string]string{}
@ -514,6 +523,7 @@ var (
classUnsupportedMountOptions string = "unsupported-mountoptions"
classLarge string = "large"
classWait string = "wait"
classCSI string = "csi"
modeWait = storage.VolumeBindingWaitForFirstConsumer
)

View File

@ -127,6 +127,20 @@ var storageClasses = []*storage.StorageClass{
MountOptions: []string{"foo"},
VolumeBindingMode: &modeImmediate,
},
{
TypeMeta: metav1.TypeMeta{
Kind: "StorageClass",
},
ObjectMeta: metav1.ObjectMeta{
Name: "csi",
},
Provisioner: "mydriver.csi.k8s.io",
Parameters: class1Parameters,
ReclaimPolicy: &deleteReclaimPolicy,
VolumeBindingMode: &modeImmediate,
},
}
// call to storageClass 1, returning an error
@ -506,6 +520,30 @@ func TestProvisionSync(t *testing.T) {
[]string{"Normal ExternalProvisioning"},
noerrors, testSyncClaim,
},
{
// Provision a volume with a data source will fail
// for in-tree plugins
"11-25 - failed in-tree provision with data source",
novolumes,
novolumes,
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)),
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)),
[]string{"Warning ProvisioningFailed"}, noerrors,
testSyncClaim,
},
{
// Provision a volume with a data source will proceed
// for CSI plugins
"11-26 - csi with data source",
novolumes,
novolumes,
claimWithAnnotation(pvutil.AnnStorageProvisioner, "mydriver.csi.k8s.io",
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-26", "uid11-26", "1Gi", "", v1.ClaimPending, &classCSI))),
claimWithAnnotation(pvutil.AnnStorageProvisioner, "mydriver.csi.k8s.io",
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-26", "uid11-26", "1Gi", "", v1.ClaimPending, &classCSI))),
[]string{"Normal ExternalProvisioning"},
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
},
}
runSyncTests(t, tests, storageClasses, []*v1.Pod{})
}

View File

@ -1474,7 +1474,17 @@ func (ctrl *PersistentVolumeController) provisionClaimOperation(
// called from provisionClaim(), in this case, plugin MUST NOT be nil
// NOTE: checks on plugin/storageClass has been saved
pluginName := plugin.GetPluginName()
if pluginName != "kubernetes.io/csi" && claim.Spec.DataSource != nil {
// Only CSI plugin can have a DataSource. Fail the operation
// if Datasource in Claim is not nil and it is not a CSI plugin,
strerr := fmt.Sprintf("plugin %q is not a CSI plugin. Only CSI plugin can provision a claim with a datasource", pluginName)
klog.V(2).Infof(strerr)
ctrl.eventRecorder.Event(claim, v1.EventTypeWarning, events.ProvisioningFailed, strerr)
return pluginName, fmt.Errorf(strerr)
}
provisionerName := storageClass.Provisioner
klog.V(4).Infof("provisionClaimOperation [%s]: plugin name: %s, provisioner name: %s", claimToClaimKey(claim), pluginName, provisionerName)
// Add provisioner annotation to be consistent with external provisioner workflow
newClaim, err := ctrl.setClaimProvisioner(claim, provisionerName)