Skip to content

Commit e37c04b

Browse files
authored
Merge pull request kubernetes#92684 from cofyc/volume-scheduling-cleanup
cleanup in volume scheduling
2 parents eee27e8 + 2cdc63a commit e37c04b

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

pkg/controller/volume/scheduling/scheduler_assume_cache.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"k8s.io/klog/v2"
2525

26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/api/meta"
2828
"k8s.io/client-go/tools/cache"
2929
)
@@ -134,7 +134,11 @@ func NewAssumeCache(informer cache.SharedIndexInformer, description, indexName s
134134
indexFunc: indexFunc,
135135
indexName: indexName,
136136
}
137-
c.store = cache.NewIndexer(objInfoKeyFunc, cache.Indexers{indexName: c.objInfoIndexFunc})
137+
indexers := cache.Indexers{}
138+
if indexName != "" && indexFunc != nil {
139+
indexers[indexName] = c.objInfoIndexFunc
140+
}
141+
c.store = cache.NewIndexer(objInfoKeyFunc, indexers)
138142

139143
// Unit tests don't use informers
140144
if informer != nil {
@@ -422,7 +426,7 @@ type pvcAssumeCache struct {
422426

423427
// NewPVCAssumeCache creates a PVC assume cache.
424428
func NewPVCAssumeCache(informer cache.SharedIndexInformer) PVCAssumeCache {
425-
return &pvcAssumeCache{NewAssumeCache(informer, "v1.PersistentVolumeClaim", "namespace", cache.MetaNamespaceIndexFunc)}
429+
return &pvcAssumeCache{NewAssumeCache(informer, "v1.PersistentVolumeClaim", "", nil)}
426430
}
427431

428432
func (c *pvcAssumeCache) GetPVC(pvcKey string) (*v1.PersistentVolumeClaim, error) {

pkg/controller/volume/scheduling/scheduler_binder.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ type InTreeToCSITranslator interface {
115115
// PV controller to fully bind and provision the PVCs. If binding fails, the Pod is sent
116116
// back through the scheduler.
117117
// ii. After BindPodVolumes() is complete, then the scheduler does the final Pod->Node binding.
118-
// 2. Once all the assume operations are done in d), the scheduler processes the next Pod in the scheduler queue
118+
// 2. Once all the assume operations are done in e), the scheduler processes the next Pod in the scheduler queue
119119
// while the actual binding operation occurs in the background.
120120
type SchedulerVolumeBinder interface {
121121
// GetPodVolumes returns a pod's PVCs separated into bound, unbound with delayed binding (including provisioning)
@@ -160,14 +160,15 @@ type SchedulerVolumeBinder interface {
160160
}
161161

162162
type volumeBinder struct {
163-
kubeClient clientset.Interface
164-
classLister storagelisters.StorageClassLister
163+
kubeClient clientset.Interface
165164

166-
podLister corelisters.PodLister
167-
nodeInformer coreinformers.NodeInformer
168-
csiNodeInformer storageinformers.CSINodeInformer
169-
pvcCache PVCAssumeCache
170-
pvCache PVAssumeCache
165+
classLister storagelisters.StorageClassLister
166+
podLister corelisters.PodLister
167+
nodeLister corelisters.NodeLister
168+
csiNodeLister storagelisters.CSINodeLister
169+
170+
pvcCache PVCAssumeCache
171+
pvCache PVAssumeCache
171172

172173
// Amount of time to wait for the bind operation to succeed
173174
bindTimeout time.Duration
@@ -186,15 +187,15 @@ func NewVolumeBinder(
186187
storageClassInformer storageinformers.StorageClassInformer,
187188
bindTimeout time.Duration) SchedulerVolumeBinder {
188189
return &volumeBinder{
189-
kubeClient: kubeClient,
190-
podLister: podInformer.Lister(),
191-
classLister: storageClassInformer.Lister(),
192-
nodeInformer: nodeInformer,
193-
csiNodeInformer: csiNodeInformer,
194-
pvcCache: NewPVCAssumeCache(pvcInformer.Informer()),
195-
pvCache: NewPVAssumeCache(pvInformer.Informer()),
196-
bindTimeout: bindTimeout,
197-
translator: csitrans.New(),
190+
kubeClient: kubeClient,
191+
podLister: podInformer.Lister(),
192+
classLister: storageClassInformer.Lister(),
193+
nodeLister: nodeInformer.Lister(),
194+
csiNodeLister: csiNodeInformer.Lister(),
195+
pvcCache: NewPVCAssumeCache(pvcInformer.Informer()),
196+
pvCache: NewPVAssumeCache(pvInformer.Informer()),
197+
bindTimeout: bindTimeout,
198+
translator: csitrans.New(),
198199
}
199200
}
200201

@@ -234,20 +235,20 @@ func (b *volumeBinder) FindPodVolumes(pod *v1.Pod, boundClaims, claimsToBind []*
234235
}()
235236

236237
var (
237-
matchedBindings []*BindingInfo
238-
provisionedClaims []*v1.PersistentVolumeClaim
238+
staticBindings []*BindingInfo
239+
dynamicProvisions []*v1.PersistentVolumeClaim
239240
)
240241
defer func() {
241242
// Although we do not distinguish nil from empty in this function, for
242243
// easier testing, we normalize empty to nil.
243-
if len(matchedBindings) == 0 {
244-
matchedBindings = nil
244+
if len(staticBindings) == 0 {
245+
staticBindings = nil
245246
}
246-
if len(provisionedClaims) == 0 {
247-
provisionedClaims = nil
247+
if len(dynamicProvisions) == 0 {
248+
dynamicProvisions = nil
248249
}
249-
podVolumes.StaticBindings = matchedBindings
250-
podVolumes.DynamicProvisions = provisionedClaims
250+
podVolumes.StaticBindings = staticBindings
251+
podVolumes.DynamicProvisions = dynamicProvisions
251252
}()
252253

253254
// Check PV node affinity on bound volumes
@@ -282,7 +283,7 @@ func (b *volumeBinder) FindPodVolumes(pod *v1.Pod, boundClaims, claimsToBind []*
282283
// Find matching volumes
283284
if len(claimsToFindMatching) > 0 {
284285
var unboundClaims []*v1.PersistentVolumeClaim
285-
unboundVolumesSatisfied, matchedBindings, unboundClaims, err = b.findMatchingVolumes(pod, claimsToFindMatching, node)
286+
unboundVolumesSatisfied, staticBindings, unboundClaims, err = b.findMatchingVolumes(pod, claimsToFindMatching, node)
286287
if err != nil {
287288
return
288289
}
@@ -291,7 +292,7 @@ func (b *volumeBinder) FindPodVolumes(pod *v1.Pod, boundClaims, claimsToBind []*
291292

292293
// Check for claims to provision
293294
if len(claimsToProvision) > 0 {
294-
unboundVolumesSatisfied, provisionedClaims, err = b.checkVolumeProvisions(pod, claimsToProvision, node)
295+
unboundVolumesSatisfied, dynamicProvisions, err = b.checkVolumeProvisions(pod, claimsToProvision, node)
295296
if err != nil {
296297
return
297298
}
@@ -452,7 +453,7 @@ func (b *volumeBinder) bindAPIUpdate(podName string, bindings []*BindingInfo, cl
452453
for _, binding = range bindings {
453454
klog.V(5).Infof("bindAPIUpdate: Pod %q, binding PV %q to PVC %q", podName, binding.pv.Name, binding.pvc.Name)
454455
// TODO: does it hurt if we make an api call and nothing needs to be updated?
455-
claimKey := claimToClaimKey(binding.pvc)
456+
claimKey := getPVCName(binding.pvc)
456457
klog.V(2).Infof("claim %q bound to volume %q", claimKey, binding.pv.Name)
457458
newPV, err := b.kubeClient.CoreV1().PersistentVolumes().Update(context.TODO(), binding.pv, metav1.UpdateOptions{})
458459
if err != nil {
@@ -504,12 +505,12 @@ func (b *volumeBinder) checkBindings(pod *v1.Pod, bindings []*BindingInfo, claim
504505
return false, fmt.Errorf("failed to get cached claims to provision for pod %q", podName)
505506
}
506507

507-
node, err := b.nodeInformer.Lister().Get(pod.Spec.NodeName)
508+
node, err := b.nodeLister.Get(pod.Spec.NodeName)
508509
if err != nil {
509510
return false, fmt.Errorf("failed to get node %q: %v", pod.Spec.NodeName, err)
510511
}
511512

512-
csiNode, err := b.csiNodeInformer.Lister().Get(node.Name)
513+
csiNode, err := b.csiNodeLister.Get(node.Name)
513514
if err != nil {
514515
// TODO: return the error once CSINode is created by default
515516
klog.V(4).Infof("Could not get a CSINode object for the node %q: %v", node.Name, err)
@@ -711,7 +712,7 @@ func (b *volumeBinder) GetPodVolumes(pod *v1.Pod) (boundClaims []*v1.PersistentV
711712
}
712713

713714
func (b *volumeBinder) checkBoundClaims(claims []*v1.PersistentVolumeClaim, node *v1.Node, podName string) (bool, error) {
714-
csiNode, err := b.csiNodeInformer.Lister().Get(node.Name)
715+
csiNode, err := b.csiNodeLister.Get(node.Name)
715716
if err != nil {
716717
// TODO: return the error once CSINode is created by default
717718
klog.V(4).Infof("Could not get a CSINode object for the node %q: %v", node.Name, err)
@@ -786,9 +787,9 @@ func (b *volumeBinder) findMatchingVolumes(pod *v1.Pod, claimsToBind []*v1.Persi
786787
// checkVolumeProvisions checks given unbound claims (the claims have gone through func
787788
// findMatchingVolumes, and do not have matching volumes for binding), and return true
788789
// if all of the claims are eligible for dynamic provision.
789-
func (b *volumeBinder) checkVolumeProvisions(pod *v1.Pod, claimsToProvision []*v1.PersistentVolumeClaim, node *v1.Node) (provisionSatisfied bool, provisionedClaims []*v1.PersistentVolumeClaim, err error) {
790+
func (b *volumeBinder) checkVolumeProvisions(pod *v1.Pod, claimsToProvision []*v1.PersistentVolumeClaim, node *v1.Node) (provisionSatisfied bool, dynamicProvisions []*v1.PersistentVolumeClaim, err error) {
790791
podName := getPodName(pod)
791-
provisionedClaims = []*v1.PersistentVolumeClaim{}
792+
dynamicProvisions = []*v1.PersistentVolumeClaim{}
792793

793794
for _, claim := range claimsToProvision {
794795
pvcName := getPVCName(claim)
@@ -816,12 +817,12 @@ func (b *volumeBinder) checkVolumeProvisions(pod *v1.Pod, claimsToProvision []*v
816817
// TODO: Check if capacity of the node domain in the storage class
817818
// can satisfy resource requirement of given claim
818819

819-
provisionedClaims = append(provisionedClaims, claim)
820+
dynamicProvisions = append(dynamicProvisions, claim)
820821

821822
}
822823
klog.V(4).Infof("Provisioning for %d claims of pod %q that has no matching volumes on node %q ...", len(claimsToProvision), podName, node.Name)
823824

824-
return true, provisionedClaims, nil
825+
return true, dynamicProvisions, nil
825826
}
826827

827828
func (b *volumeBinder) revertAssumedPVs(bindings []*BindingInfo) {
@@ -853,10 +854,6 @@ func (a byPVCSize) Less(i, j int) bool {
853854
return iSize.Cmp(jSize) == -1
854855
}
855856

856-
func claimToClaimKey(claim *v1.PersistentVolumeClaim) string {
857-
return fmt.Sprintf("%s/%s", claim.Namespace, claim.Name)
858-
}
859-
860857
// isCSIMigrationOnForPlugin checks if CSI migrartion is enabled for a given plugin.
861858
func isCSIMigrationOnForPlugin(pluginName string) bool {
862859
switch pluginName {

0 commit comments

Comments
 (0)