Skip to content

Commit cc8a3f3

Browse files
authored
Merge pull request #175 from xing-yang/release-1.0
Cherry-pick to 1.0: Verify PV/PVC binding and driver
2 parents f718dea + f855f15 commit cc8a3f3

File tree

4 files changed

+144
-35
lines changed

4 files changed

+144
-35
lines changed

pkg/controller/framework_test.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,15 @@ func newClaimArray(name, claimUID, capacity, boundToVolume string, phase v1.Pers
888888
}
889889

890890
// newVolume returns a new volume with given attributes
891-
func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, annotations ...string) *v1.PersistentVolume {
891+
func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, driver string, namespace string, annotations ...string) *v1.PersistentVolume {
892+
inDriverName := mockDriverName
893+
if driver != "" {
894+
inDriverName = driver
895+
}
896+
inNamespace := testNamespace
897+
if namespace != "" {
898+
inNamespace = namespace
899+
}
892900
volume := v1.PersistentVolume{
893901
ObjectMeta: metav1.ObjectMeta{
894902
Name: name,
@@ -902,7 +910,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
902910
},
903911
PersistentVolumeSource: v1.PersistentVolumeSource{
904912
CSI: &v1.CSIPersistentVolumeSource{
905-
Driver: mockDriverName,
913+
Driver: inDriverName,
906914
VolumeHandle: volumeHandle,
907915
},
908916
},
@@ -920,7 +928,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
920928
Kind: "PersistentVolumeClaim",
921929
APIVersion: "v1",
922930
UID: types.UID(boundToClaimUID),
923-
Namespace: testNamespace,
931+
Namespace: inNamespace,
924932
Name: boundToClaimName,
925933
}
926934
}
@@ -930,9 +938,9 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
930938

931939
// newVolumeArray returns array with a single volume that would be returned by
932940
// newVolume() with the same parameters.
933-
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string) []*v1.PersistentVolume {
941+
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, driver string, namespace string) []*v1.PersistentVolume {
934942
return []*v1.PersistentVolume{
935-
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class),
943+
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class, driver, namespace),
936944
}
937945
}
938946

pkg/controller/snapshot_controller.go

+29
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,40 @@ func (ctrl *csiSnapshotController) getVolumeFromVolumeSnapshot(snapshot *crdv1.V
844844
return nil, fmt.Errorf("failed to retrieve PV %s from the API server: %q", pvName, err)
845845
}
846846

847+
// Verify binding between PV/PVC is still valid
848+
bound := ctrl.IsVolumeBoundToClaim(pv, pvc)
849+
if bound == false {
850+
glog.Warningf("binding between PV %s and PVC %s is broken", pvName, pvc.Name)
851+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
852+
}
853+
854+
// Verify driver for PVC is the same as driver for VolumeSnapshot
855+
if pv.Spec.PersistentVolumeSource.CSI == nil || pv.Spec.PersistentVolumeSource.CSI.Driver != ctrl.snapshotterName {
856+
glog.Warningf("driver for PV %s is different from driver %s for snapshot %s", pvName, ctrl.snapshotterName, snapshot.Name)
857+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
858+
}
859+
847860
glog.V(5).Infof("getVolumeFromVolumeSnapshot: snapshot [%s] PV name [%s]", snapshot.Name, pvName)
848861

849862
return pv, nil
850863
}
851864

865+
// IsVolumeBoundToClaim returns true, if given volume is pre-bound or bound
866+
// to specific claim. Both claim.Name and claim.Namespace must be equal.
867+
// If claim.UID is present in volume.Spec.ClaimRef, it must be equal too.
868+
func (ctrl *csiSnapshotController) IsVolumeBoundToClaim(volume *v1.PersistentVolume, claim *v1.PersistentVolumeClaim) bool {
869+
if volume.Spec.ClaimRef == nil {
870+
return false
871+
}
872+
if claim.Name != volume.Spec.ClaimRef.Name || claim.Namespace != volume.Spec.ClaimRef.Namespace {
873+
return false
874+
}
875+
if volume.Spec.ClaimRef.UID != "" && claim.UID != volume.Spec.ClaimRef.UID {
876+
return false
877+
}
878+
return true
879+
}
880+
852881
func (ctrl *csiSnapshotController) getStorageClassFromVolumeSnapshot(snapshot *crdv1.VolumeSnapshot) (*storagev1.StorageClass, error) {
853882
// Get storage class from PVC or PV
854883
pvc, err := ctrl.getClaimFromVolumeSnapshot(snapshot)

0 commit comments

Comments
 (0)