2015-04-14 21:14:39 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-04-14 21:14:39 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 03:58:57 +00:00
|
|
|
package persistentvolume
|
2015-04-14 21:14:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-05-19 20:52:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-03 21:40:58 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
2016-05-19 20:52:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-04-14 21:14:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// persistentVolumeOrderedIndex is a cache.Store that keeps persistent volumes indexed by AccessModes and ordered by storage capacity.
|
|
|
|
type persistentVolumeOrderedIndex struct {
|
2016-05-17 12:55:03 +00:00
|
|
|
store cache.Indexer
|
2015-04-14 21:14:39 +00:00
|
|
|
}
|
|
|
|
|
volume controller: Add cache with the latest version of PVs and PVCs
When the controller binds a PV to PVC, it saves both objects to etcd.
However, there is still an old version of these objects in the controller
Informer cache. So, when a new PVC comes, the PV is still seen as available
and may get bound to the new PVC. This will be blocked by etcd, still, it
creates unnecessary traffic that slows everything down.
Also, we save bound PV/PVC as two transactions - we save PV/PVC.Spec first
and then .Status. The controller gets "PV/PVC.Spec updated" event from etcd
and tries to fix the Status, as it seems to the controller it's outdated.
This write again fails - there already is a correct version in etcd.
We can't influence the Informer cache, it is read-only to the controller.
To prevent these useless writes to etcd, this patch introduces second cache
in the controller, which holds latest and greatest version on PVs and PVCs.
It gets updated with events from etcd *and* after etcd confirms successful
save of PV/PVC modified by the controller.
The cache stores only *pointers* to PVs/PVCs, so in ideal case it shares the
actual object data with the informer cache. They will diverge only when
the controller modifies something and the informer cache did not get update
events yet.
2016-05-19 11:31:19 +00:00
|
|
|
func newPersistentVolumeOrderedIndex() persistentVolumeOrderedIndex {
|
|
|
|
return persistentVolumeOrderedIndex{cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{"accessmodes": accessModesIndexFunc})}
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:14:39 +00:00
|
|
|
// accessModesIndexFunc is an indexing function that returns a persistent volume's AccessModes as a string
|
2015-07-28 14:01:05 +00:00
|
|
|
func accessModesIndexFunc(obj interface{}) ([]string, error) {
|
2015-04-14 21:14:39 +00:00
|
|
|
if pv, ok := obj.(*api.PersistentVolume); ok {
|
2015-07-13 19:10:04 +00:00
|
|
|
modes := api.GetAccessModesAsString(pv.Spec.AccessModes)
|
2015-07-28 14:01:05 +00:00
|
|
|
return []string{modes}, nil
|
2015-04-14 21:14:39 +00:00
|
|
|
}
|
2015-07-28 14:01:05 +00:00
|
|
|
return []string{""}, fmt.Errorf("object is not a persistent volume: %v", obj)
|
2015-04-14 21:14:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 12:25:52 +00:00
|
|
|
// listByAccessModes returns all volumes with the given set of AccessModeTypes. The list is unsorted!
|
2016-05-17 12:55:03 +00:00
|
|
|
func (pvIndex *persistentVolumeOrderedIndex) listByAccessModes(modes []api.PersistentVolumeAccessMode) ([]*api.PersistentVolume, error) {
|
2015-04-14 21:14:39 +00:00
|
|
|
pv := &api.PersistentVolume{
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
AccessModes: modes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:55:03 +00:00
|
|
|
objs, err := pvIndex.store.Index("accessmodes", pv)
|
2015-04-14 21:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volumes := make([]*api.PersistentVolume, len(objs))
|
|
|
|
for i, obj := range objs {
|
|
|
|
volumes[i] = obj.(*api.PersistentVolume)
|
|
|
|
}
|
|
|
|
|
|
|
|
return volumes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchPredicate is a function that indicates that a persistent volume matches another
|
|
|
|
type matchPredicate func(compareThis, toThis *api.PersistentVolume) bool
|
|
|
|
|
2015-10-28 15:03:04 +00:00
|
|
|
// find returns the nearest PV from the ordered list or nil if a match is not found
|
2015-10-12 18:27:49 +00:00
|
|
|
func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVolumeClaim, matchPredicate matchPredicate) (*api.PersistentVolume, error) {
|
2015-07-13 19:10:04 +00:00
|
|
|
// PVs are indexed by their access modes to allow easier searching. Each index is the string representation of a set of access modes.
|
|
|
|
// There is a finite number of possible sets and PVs will only be indexed in one of them (whichever index matches the PV's modes).
|
|
|
|
//
|
|
|
|
// A request for resources will always specify its desired access modes. Any matching PV must have at least that number
|
|
|
|
// of access modes, but it can have more. For example, a user asks for ReadWriteOnce but a GCEPD is available, which is ReadWriteOnce+ReadOnlyMany.
|
|
|
|
//
|
|
|
|
// Searches are performed against a set of access modes, so we can attempt not only the exact matching modes but also
|
|
|
|
// potential matches (the GCEPD example above).
|
2015-10-12 18:27:49 +00:00
|
|
|
allPossibleModes := pvIndex.allPossibleMatchingAccessModes(claim.Spec.AccessModes)
|
2015-10-28 15:03:04 +00:00
|
|
|
|
2016-05-19 12:25:52 +00:00
|
|
|
var smallestVolume *api.PersistentVolume
|
|
|
|
var smallestVolumeSize int64
|
|
|
|
requestedQty := claim.Spec.Resources.Requests[api.ResourceName(api.ResourceStorage)]
|
|
|
|
requestedSize := requestedQty.Value()
|
|
|
|
|
2016-05-19 20:52:29 +00:00
|
|
|
var selector labels.Selector
|
|
|
|
if claim.Spec.Selector != nil {
|
|
|
|
internalSelector, err := unversioned.LabelSelectorAsSelector(claim.Spec.Selector)
|
|
|
|
if err != nil {
|
|
|
|
// should be unreachable code due to validation
|
|
|
|
return nil, fmt.Errorf("error creating internal label selector for claim: %v: %v", claimToClaimKey(claim), err)
|
|
|
|
}
|
|
|
|
selector = internalSelector
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:10:04 +00:00
|
|
|
for _, modes := range allPossibleModes {
|
2016-05-17 12:55:03 +00:00
|
|
|
volumes, err := pvIndex.listByAccessModes(modes)
|
2015-07-13 19:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-14 21:14:39 +00:00
|
|
|
|
2016-05-19 12:25:52 +00:00
|
|
|
// Go through all available volumes with two goals:
|
|
|
|
// - find a volume that is either pre-bound by user or dynamically
|
|
|
|
// provisioned for this claim. Because of this we need to loop through
|
|
|
|
// all volumes.
|
|
|
|
// - find the smallest matching one if there is no volume pre-bound to
|
|
|
|
// the claim.
|
2015-09-23 02:38:25 +00:00
|
|
|
for _, volume := range volumes {
|
2016-05-19 12:25:52 +00:00
|
|
|
if isVolumeBoundToClaim(volume, claim) {
|
2016-05-19 20:52:29 +00:00
|
|
|
// this claim and volume are bound; return it,
|
|
|
|
// whether the claim is prebound or for volumes
|
|
|
|
// intended for dynamic provisioning v1
|
2016-05-19 12:25:52 +00:00
|
|
|
return volume, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 20:52:29 +00:00
|
|
|
// filter out:
|
|
|
|
// - volumes bound to another claim
|
|
|
|
// - volumes whose labels don't match the claim's selector, if specified
|
2016-05-19 12:25:52 +00:00
|
|
|
if volume.Spec.ClaimRef != nil {
|
2016-05-19 20:52:29 +00:00
|
|
|
continue
|
|
|
|
} else if selector != nil && !selector.Matches(labels.Set(volume.Labels)) {
|
2015-09-23 02:38:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-19 12:25:52 +00:00
|
|
|
volumeQty := volume.Spec.Capacity[api.ResourceStorage]
|
|
|
|
volumeSize := volumeQty.Value()
|
|
|
|
if volumeSize >= requestedSize {
|
|
|
|
if smallestVolume == nil || smallestVolumeSize > volumeSize {
|
|
|
|
smallestVolume = volume
|
|
|
|
smallestVolumeSize = volumeSize
|
|
|
|
}
|
2015-07-13 19:10:04 +00:00
|
|
|
}
|
2015-05-20 19:25:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 12:55:03 +00:00
|
|
|
// We want to provision volumes if the annotation is set even if there
|
|
|
|
// is matching PV. Therefore, do not look for available PV and let
|
|
|
|
// a new volume to be provisioned.
|
|
|
|
//
|
|
|
|
// When provisioner creates a new PV to this claim, an exact match
|
|
|
|
// pre-bound to the claim will be found by the checks above during
|
|
|
|
// subsequent claim sync.
|
|
|
|
if hasAnnotation(claim.ObjectMeta, annClass) {
|
2015-10-12 18:27:49 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 12:25:52 +00:00
|
|
|
if smallestVolume != nil {
|
|
|
|
// Found a matching volume
|
|
|
|
return smallestVolume, nil
|
2015-07-13 19:10:04 +00:00
|
|
|
}
|
2015-04-14 21:14:39 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:03:04 +00:00
|
|
|
// findBestMatchForClaim is a convenience method that finds a volume by the claim's AccessModes and requests for Storage
|
|
|
|
func (pvIndex *persistentVolumeOrderedIndex) findBestMatchForClaim(claim *api.PersistentVolumeClaim) (*api.PersistentVolume, error) {
|
2015-10-12 18:27:49 +00:00
|
|
|
return pvIndex.findByClaim(claim, matchStorageCapacity)
|
2015-04-14 21:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// matchStorageCapacity is a matchPredicate used to sort and find volumes
|
|
|
|
func matchStorageCapacity(pvA, pvB *api.PersistentVolume) bool {
|
|
|
|
aQty := pvA.Spec.Capacity[api.ResourceStorage]
|
|
|
|
bQty := pvB.Spec.Capacity[api.ResourceStorage]
|
|
|
|
aSize := aQty.Value()
|
|
|
|
bSize := bQty.Value()
|
|
|
|
return aSize <= bSize
|
|
|
|
}
|
2015-07-13 19:10:04 +00:00
|
|
|
|
|
|
|
// allPossibleMatchingAccessModes returns an array of AccessMode arrays that can satisfy a user's requested modes.
|
|
|
|
//
|
|
|
|
// see comments in the Find func above regarding indexing.
|
|
|
|
//
|
|
|
|
// allPossibleMatchingAccessModes gets all stringified accessmodes from the index and returns all those that
|
|
|
|
// contain at least all of the requested mode.
|
|
|
|
//
|
|
|
|
// For example, assume the index contains 2 types of PVs where the stringified accessmodes are:
|
|
|
|
//
|
|
|
|
// "RWO,ROX" -- some number of GCEPDs
|
|
|
|
// "RWO,ROX,RWX" -- some number of NFS volumes
|
|
|
|
//
|
|
|
|
// A request for RWO could be satisfied by both sets of indexed volumes, so allPossibleMatchingAccessModes returns:
|
|
|
|
//
|
|
|
|
// [][]api.PersistentVolumeAccessMode {
|
|
|
|
// []api.PersistentVolumeAccessMode {
|
|
|
|
// api.ReadWriteOnce, api.ReadOnlyMany,
|
|
|
|
// },
|
|
|
|
// []api.PersistentVolumeAccessMode {
|
|
|
|
// api.ReadWriteOnce, api.ReadOnlyMany, api.ReadWriteMany,
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// A request for RWX can be satisfied by only one set of indexed volumes, so the return is:
|
|
|
|
//
|
|
|
|
// [][]api.PersistentVolumeAccessMode {
|
|
|
|
// []api.PersistentVolumeAccessMode {
|
|
|
|
// api.ReadWriteOnce, api.ReadOnlyMany, api.ReadWriteMany,
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This func returns modes with ascending levels of modes to give the user what is closest to what they actually asked for.
|
|
|
|
//
|
|
|
|
func (pvIndex *persistentVolumeOrderedIndex) allPossibleMatchingAccessModes(requestedModes []api.PersistentVolumeAccessMode) [][]api.PersistentVolumeAccessMode {
|
|
|
|
matchedModes := [][]api.PersistentVolumeAccessMode{}
|
2016-05-17 12:55:03 +00:00
|
|
|
keys := pvIndex.store.ListIndexFuncValues("accessmodes")
|
2015-07-13 19:10:04 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
indexedModes := api.GetAccessModesFromString(key)
|
|
|
|
if containedInAll(indexedModes, requestedModes) {
|
|
|
|
matchedModes = append(matchedModes, indexedModes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by the number of modes in each array with the fewest number of modes coming first.
|
|
|
|
// this allows searching for volumes by the minimum number of modes required of the possible matches.
|
|
|
|
sort.Sort(byAccessModes{matchedModes})
|
|
|
|
return matchedModes
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeAccessMode) bool {
|
|
|
|
for _, m := range modes {
|
|
|
|
if m == mode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func containedInAll(indexedModes []api.PersistentVolumeAccessMode, requestedModes []api.PersistentVolumeAccessMode) bool {
|
|
|
|
for _, mode := range requestedModes {
|
|
|
|
if !contains(indexedModes, mode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// byAccessModes is used to order access modes by size, with the fewest modes first
|
|
|
|
type byAccessModes struct {
|
|
|
|
modes [][]api.PersistentVolumeAccessMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c byAccessModes) Less(i, j int) bool {
|
|
|
|
return len(c.modes[i]) < len(c.modes[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c byAccessModes) Swap(i, j int) {
|
|
|
|
c.modes[i], c.modes[j] = c.modes[j], c.modes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c byAccessModes) Len() int {
|
|
|
|
return len(c.modes)
|
|
|
|
}
|
2015-10-12 18:27:49 +00:00
|
|
|
|
|
|
|
func claimToClaimKey(claim *api.PersistentVolumeClaim) string {
|
|
|
|
return fmt.Sprintf("%s/%s", claim.Namespace, claim.Name)
|
|
|
|
}
|
2016-05-17 12:55:11 +00:00
|
|
|
|
|
|
|
func claimrefToClaimKey(claimref *api.ObjectReference) string {
|
|
|
|
return fmt.Sprintf("%s/%s", claimref.Namespace, claimref.Name)
|
|
|
|
}
|