From 0ee71f0cc2380854e22c7027f13f1bb41c139c41 Mon Sep 17 00:00:00 2001 From: mkimuram Date: Thu, 15 Feb 2018 16:12:48 -0500 Subject: [PATCH] Add binding error message for volumeMode:Block unsupported case This commit adds a binding error message for volumeMode:Block unsupported case. With this message, users can understand why PV is not bound to PVC on both dynamic provisioning and manually creating volume. This patch works as follows: - In syncVolume, before adding the claim to claimQueue, check if there is a volumeMode mismatch, and if there is, record the event for both pv and pvc and skip adding to the queue. fixes: #59942 --- .../volume/persistentvolume/binder_test.go | 40 ++++++++++++++++++- .../volume/persistentvolume/pv_controller.go | 11 +++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pkg/controller/volume/persistentvolume/binder_test.go b/pkg/controller/volume/persistentvolume/binder_test.go index 0f51b47f002..d91156281ab 100644 --- a/pkg/controller/volume/persistentvolume/binder_test.go +++ b/pkg/controller/volume/persistentvolume/binder_test.go @@ -723,7 +723,7 @@ func TestSyncAlphaBlockVolume(t *testing.T) { }, { // failed syncVolume do not bind when pvc is prebound to pv with mismatching volumeModes - "14-8-1 - do not bind when pv is prebound to pvc with mismatching volumeModes", + "14-8-1 - do not bind when pvc is prebound to pv with mismatching volumeModes", withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty)), withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty)), withClaimVolumeMode(&modeFile, newClaimArray("claim14-8-1", "uid14-8-1", "10Gi", "", v1.ClaimPending, nil)), @@ -767,6 +767,44 @@ func TestSyncAlphaBlockVolume(t *testing.T) { withClaimVolumeMode(&modeFile, newClaimArray("claim14-12", "uid14-12", "10Gi", "volume14-12", v1.ClaimBound, nil, annBoundByController, annBindCompleted)), noevents, noerrors, testSyncClaim, }, + { + // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode + "14-13 - output warning when pv is prebound to pvc with different volumeModes", + withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)), + withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)), + []string{"Warning VolumeMismatch"}, + noerrors, testSyncVolume, + }, + { + // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode + "14-13-1 - output warning when pv is prebound to pvc with different volumeModes", + withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)), + withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)), + []string{"Warning VolumeMismatch"}, + noerrors, testSyncVolume, + }, + { + // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode block + "14-14 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes block", + withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)), + withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)), + noevents, noerrors, testSyncVolume, + }, + { + // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode file + "14-14-1 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes file", + withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty, annBoundByController)), + withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)), + withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)), + noevents, noerrors, testSyncVolume, + }, } err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") diff --git a/pkg/controller/volume/persistentvolume/pv_controller.go b/pkg/controller/volume/persistentvolume/pv_controller.go index c245fb15bb9..6fbd0912019 100644 --- a/pkg/controller/volume/persistentvolume/pv_controller.go +++ b/pkg/controller/volume/persistentvolume/pv_controller.go @@ -571,6 +571,17 @@ func (ctrl *PersistentVolumeController) syncVolume(volume *v1.PersistentVolume) } return nil } else if claim.Spec.VolumeName == "" { + if isMisMatch, err := checkVolumeModeMisMatches(&claim.Spec, &volume.Spec); err != nil || isMisMatch { + // Binding for the volume won't be called in syncUnboundClaim, + // because findBestMatchForClaim won't return the volume due to volumeMode mismatch. + volumeMsg := fmt.Sprintf("Cannot bind PersistentVolume to requested PersistentVolumeClaim %q due to incompatible volumeMode.", claim.Name) + ctrl.eventRecorder.Event(volume, v1.EventTypeWarning, events.VolumeMismatch, volumeMsg) + claimMsg := fmt.Sprintf("Cannot bind PersistentVolume %q to requested PersistentVolumeClaim due to incompatible volumeMode.", volume.Name) + ctrl.eventRecorder.Event(claim, v1.EventTypeWarning, events.VolumeMismatch, claimMsg) + // Skipping syncClaim + return nil + } + if metav1.HasAnnotation(volume.ObjectMeta, annBoundByController) { // The binding is not completed; let PVC sync handle it glog.V(4).Infof("synchronizing PersistentVolume[%s]: volume not bound yet, waiting for syncClaim to fix it", volume.Name)