From a195802d3ee778bcdbd0ab8e22cb35cce66b2a62 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Tue, 17 May 2016 14:55:06 +0200 Subject: [PATCH] Make standalone function to check for (pre-)bound volumes. Note the semantic change, we now check for UID="" --- .../persistentvolume_controller.go | 16 ++++++++++++++++ .../persistentvolume/persistentvolume_index.go | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/controller/persistentvolume/persistentvolume_controller.go b/pkg/controller/persistentvolume/persistentvolume_controller.go index 485a92dd4f2..1cda746f289 100644 --- a/pkg/controller/persistentvolume/persistentvolume_controller.go +++ b/pkg/controller/persistentvolume/persistentvolume_controller.go @@ -270,3 +270,19 @@ func (ctrl *PersistentVolumeController) Stop() { func (ctrl *PersistentVolumeController) isFullySynced() bool { return ctrl.volumeController.HasSynced() && ctrl.claimController.HasSynced() } + +// isVolumeBoundToClaim returns true, if given volume is pre-bound or bound +// to specific claim. Both claim.Name and claim.Namespace must be equal. +// If claim.UID is present in volume.Spec.ClaimRef, it must be equal too. +func isVolumeBoundToClaim(volume *api.PersistentVolume, claim *api.PersistentVolumeClaim) bool { + if volume.Spec.ClaimRef == nil { + return false + } + if claim.Name != volume.Spec.ClaimRef.Name || claim.Namespace != volume.Spec.ClaimRef.Namespace { + return false + } + if volume.Spec.ClaimRef.UID != "" && claim.UID != volume.Spec.ClaimRef.UID { + return false + } + return true +} diff --git a/pkg/controller/persistentvolume/persistentvolume_index.go b/pkg/controller/persistentvolume/persistentvolume_index.go index c323ef49744..a86bf7c2b45 100644 --- a/pkg/controller/persistentvolume/persistentvolume_index.go +++ b/pkg/controller/persistentvolume/persistentvolume_index.go @@ -92,8 +92,8 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo continue } - if claim.Name == volume.Spec.ClaimRef.Name && claim.Namespace == volume.Spec.ClaimRef.Namespace && claim.UID == volume.Spec.ClaimRef.UID { - // exact match! No search required. + if isVolumeBoundToClaim(volume, claim) { + // Exact match! No search required. return volume, nil } }