k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/framework/plugins/volumebinding/fake_binder.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package volumebinding 18 19 import ( 20 "context" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/util/sets" 24 "k8s.io/klog/v2" 25 ) 26 27 // FakeVolumeBinderConfig holds configurations for fake volume binder. 28 type FakeVolumeBinderConfig struct { 29 AllBound bool 30 FindReasons ConflictReasons 31 FindErr error 32 AssumeErr error 33 BindErr error 34 } 35 36 // NewFakeVolumeBinder sets up all the caches needed for the scheduler to make 37 // topology-aware volume binding decisions. 38 func NewFakeVolumeBinder(config *FakeVolumeBinderConfig) *FakeVolumeBinder { 39 return &FakeVolumeBinder{ 40 config: config, 41 } 42 } 43 44 // FakeVolumeBinder represents a fake volume binder for testing. 45 type FakeVolumeBinder struct { 46 config *FakeVolumeBinderConfig 47 AssumeCalled bool 48 BindCalled bool 49 } 50 51 var _ SchedulerVolumeBinder = &FakeVolumeBinder{} 52 53 // GetPodVolumeClaims implements SchedulerVolumeBinder.GetPodVolumes. 54 func (b *FakeVolumeBinder) GetPodVolumeClaims(_ klog.Logger, pod *v1.Pod) (podVolumeClaims *PodVolumeClaims, err error) { 55 return &PodVolumeClaims{}, nil 56 } 57 58 // GetEligibleNodes implements SchedulerVolumeBinder.GetEligibleNodes. 59 func (b *FakeVolumeBinder) GetEligibleNodes(_ klog.Logger, boundClaims []*v1.PersistentVolumeClaim) (eligibleNodes sets.Set[string]) { 60 return nil 61 } 62 63 // FindPodVolumes implements SchedulerVolumeBinder.FindPodVolumes. 64 func (b *FakeVolumeBinder) FindPodVolumes(_ klog.Logger, pod *v1.Pod, _ *PodVolumeClaims, node *v1.Node) (podVolumes *PodVolumes, reasons ConflictReasons, err error) { 65 return nil, b.config.FindReasons, b.config.FindErr 66 } 67 68 // AssumePodVolumes implements SchedulerVolumeBinder.AssumePodVolumes. 69 func (b *FakeVolumeBinder) AssumePodVolumes(_ klog.Logger, assumedPod *v1.Pod, nodeName string, podVolumes *PodVolumes) (bool, error) { 70 b.AssumeCalled = true 71 return b.config.AllBound, b.config.AssumeErr 72 } 73 74 // RevertAssumedPodVolumes implements SchedulerVolumeBinder.RevertAssumedPodVolumes 75 func (b *FakeVolumeBinder) RevertAssumedPodVolumes(_ *PodVolumes) {} 76 77 // BindPodVolumes implements SchedulerVolumeBinder.BindPodVolumes. 78 func (b *FakeVolumeBinder) BindPodVolumes(ctx context.Context, assumedPod *v1.Pod, podVolumes *PodVolumes) error { 79 b.BindCalled = true 80 return b.config.BindErr 81 }