mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Refactor PV selection into a common call for scheduler and PV controller
This commit is contained in:
parent
754017bef4
commit
3211b8b0c4
@ -90,6 +90,25 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
// example above).
|
// example above).
|
||||||
allPossibleModes := pvIndex.allPossibleMatchingAccessModes(claim.Spec.AccessModes)
|
allPossibleModes := pvIndex.allPossibleMatchingAccessModes(claim.Spec.AccessModes)
|
||||||
|
|
||||||
|
for _, modes := range allPossibleModes {
|
||||||
|
volumes, err := pvIndex.listByAccessModes(modes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bestVol, err := findMatchingVolume(claim, volumes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if bestVol != nil {
|
||||||
|
return bestVol, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMatchingVolume(claim *v1.PersistentVolumeClaim, volumes []*v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
||||||
var smallestVolume *v1.PersistentVolume
|
var smallestVolume *v1.PersistentVolume
|
||||||
var smallestVolumeQty resource.Quantity
|
var smallestVolumeQty resource.Quantity
|
||||||
requestedQty := claim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
|
requestedQty := claim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
|
||||||
@ -105,12 +124,6 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
selector = internalSelector
|
selector = internalSelector
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, modes := range allPossibleModes {
|
|
||||||
volumes, err := pvIndex.listByAccessModes(modes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go through all available volumes with two goals:
|
// Go through all available volumes with two goals:
|
||||||
// - find a volume that is either pre-bound by user or dynamically
|
// - find a volume that is either pre-bound by user or dynamically
|
||||||
// provisioned for this claim. Because of this we need to loop through
|
// provisioned for this claim. Because of this we need to loop through
|
||||||
@ -118,6 +131,8 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
// - find the smallest matching one if there is no volume pre-bound to
|
// - find the smallest matching one if there is no volume pre-bound to
|
||||||
// the claim.
|
// the claim.
|
||||||
for _, volume := range volumes {
|
for _, volume := range volumes {
|
||||||
|
volumeQty := volume.Spec.Capacity[v1.ResourceStorage]
|
||||||
|
|
||||||
// check if volumeModes do not match (Alpha and feature gate protected)
|
// check if volumeModes do not match (Alpha and feature gate protected)
|
||||||
isMisMatch, err := checkVolumeModeMisMatches(&claim.Spec, &volume.Spec)
|
isMisMatch, err := checkVolumeModeMisMatches(&claim.Spec, &volume.Spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -132,7 +147,6 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
// this claim and volume are pre-bound; return
|
// this claim and volume are pre-bound; return
|
||||||
// the volume if the size request is satisfied,
|
// the volume if the size request is satisfied,
|
||||||
// otherwise continue searching for a match
|
// otherwise continue searching for a match
|
||||||
volumeQty := volume.Spec.Capacity[v1.ResourceStorage]
|
|
||||||
if volumeQty.Cmp(requestedQty) < 0 {
|
if volumeQty.Cmp(requestedQty) < 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -152,7 +166,6 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
volumeQty := volume.Spec.Capacity[v1.ResourceStorage]
|
|
||||||
if volumeQty.Cmp(requestedQty) >= 0 {
|
if volumeQty.Cmp(requestedQty) >= 0 {
|
||||||
if smallestVolume == nil || smallestVolumeQty.Cmp(volumeQty) > 0 {
|
if smallestVolume == nil || smallestVolumeQty.Cmp(volumeQty) > 0 {
|
||||||
smallestVolume = volume
|
smallestVolume = volume
|
||||||
@ -165,7 +178,7 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *v1.PersistentVol
|
|||||||
// Found a matching volume
|
// Found a matching volume
|
||||||
return smallestVolume, nil
|
return smallestVolume, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user