optimize the binding logic of bindClaimToVolume

This commit is contained in:
NickrenREN 2017-03-07 14:26:49 +08:00
parent 31db570a00
commit 10779c8bcc

View File

@ -790,9 +790,9 @@ func (ctrl *PersistentVolumeController) bindClaimToVolume(claim *v1.PersistentVo
dirty := false dirty := false
// Check if the claim was already bound (either by controller or by user) // Check if the claim was already bound (either by controller or by user)
shouldSetBoundByController := false shouldBind := false
if volume.Name != claim.Spec.VolumeName { if volume.Name != claim.Spec.VolumeName {
shouldSetBoundByController = true shouldBind = true
} }
// The claim from method args can be pointing to watcher cache. We must not // The claim from method args can be pointing to watcher cache. We must not
@ -806,16 +806,15 @@ func (ctrl *PersistentVolumeController) bindClaimToVolume(claim *v1.PersistentVo
return nil, fmt.Errorf("Unexpected claim cast error : %v", claimClone) return nil, fmt.Errorf("Unexpected claim cast error : %v", claimClone)
} }
// Bind the claim to the volume if it is not bound yet if shouldBind {
if claimClone.Spec.VolumeName != volume.Name { dirty = true
// Bind the claim to the volume
claimClone.Spec.VolumeName = volume.Name claimClone.Spec.VolumeName = volume.Name
dirty = true
}
// Set annBoundByController if it is not set yet // Set annBoundByController if it is not set yet
if shouldSetBoundByController && !metav1.HasAnnotation(claimClone.ObjectMeta, annBoundByController) { if !metav1.HasAnnotation(claimClone.ObjectMeta, annBoundByController) {
metav1.SetMetaDataAnnotation(&claimClone.ObjectMeta, annBoundByController, "yes") metav1.SetMetaDataAnnotation(&claimClone.ObjectMeta, annBoundByController, "yes")
dirty = true }
} }
// Set annBindCompleted if it is not set yet // Set annBindCompleted if it is not set yet