From c62479a6f15df7f00d3da9f41bdabdfada439a48 Mon Sep 17 00:00:00 2001 From: kidddddddddddddddddddddd <1062602710@qq.com> Date: Tue, 5 Jul 2022 20:23:07 +0800 Subject: [PATCH] Pass ctx to BindPodVolumes. --- .../framework/plugins/volumebinding/binder.go | 12 ++++++------ .../framework/plugins/volumebinding/binder_test.go | 4 ++-- .../framework/plugins/volumebinding/fake_binder.go | 8 ++++++-- .../plugins/volumebinding/volume_binding.go | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/scheduler/framework/plugins/volumebinding/binder.go b/pkg/scheduler/framework/plugins/volumebinding/binder.go index f715699e52d..b1a0bead666 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/binder.go +++ b/pkg/scheduler/framework/plugins/volumebinding/binder.go @@ -185,7 +185,7 @@ type SchedulerVolumeBinder interface { // 3. Wait for PVCs to be completely bound by the PV controller // // This function can be called in parallel. - BindPodVolumes(assumedPod *v1.Pod, podVolumes *PodVolumes) error + BindPodVolumes(ctx context.Context, assumedPod *v1.Pod, podVolumes *PodVolumes) error } type volumeBinder struct { @@ -432,7 +432,7 @@ func (b *volumeBinder) RevertAssumedPodVolumes(podVolumes *PodVolumes) { // BindPodVolumes gets the cached bindings and PVCs to provision in pod's volumes information, // makes the API update for those PVs/PVCs, and waits for the PVCs to be completely bound // by the PV controller. -func (b *volumeBinder) BindPodVolumes(assumedPod *v1.Pod, podVolumes *PodVolumes) (err error) { +func (b *volumeBinder) BindPodVolumes(ctx context.Context, assumedPod *v1.Pod, podVolumes *PodVolumes) (err error) { klog.V(4).InfoS("BindPodVolumes", "pod", klog.KObj(assumedPod), "node", klog.KRef("", assumedPod.Spec.NodeName)) defer func() { @@ -445,7 +445,7 @@ func (b *volumeBinder) BindPodVolumes(assumedPod *v1.Pod, podVolumes *PodVolumes claimsToProvision := podVolumes.DynamicProvisions // Start API operations - err = b.bindAPIUpdate(assumedPod, bindings, claimsToProvision) + err = b.bindAPIUpdate(ctx, assumedPod, bindings, claimsToProvision) if err != nil { return err } @@ -469,7 +469,7 @@ func getPVCName(pvc *v1.PersistentVolumeClaim) string { } // bindAPIUpdate makes the API update for those PVs/PVCs. -func (b *volumeBinder) bindAPIUpdate(pod *v1.Pod, bindings []*BindingInfo, claimsToProvision []*v1.PersistentVolumeClaim) error { +func (b *volumeBinder) bindAPIUpdate(ctx context.Context, pod *v1.Pod, bindings []*BindingInfo, claimsToProvision []*v1.PersistentVolumeClaim) error { podName := getPodName(pod) if bindings == nil { return fmt.Errorf("failed to get cached bindings for pod %q", podName) @@ -503,7 +503,7 @@ func (b *volumeBinder) bindAPIUpdate(pod *v1.Pod, bindings []*BindingInfo, claim klog.V(5).InfoS("bindAPIUpdate: binding PV to PVC", "pod", klog.KObj(pod), "PV", klog.KObj(binding.pv), "PVC", klog.KObj(binding.pvc)) // TODO: does it hurt if we make an api call and nothing needs to be updated? klog.V(2).InfoS("Claim bound to volume", "PVC", klog.KObj(binding.pvc), "PV", klog.KObj(binding.pv)) - newPV, err := b.kubeClient.CoreV1().PersistentVolumes().Update(context.TODO(), binding.pv, metav1.UpdateOptions{}) + newPV, err := b.kubeClient.CoreV1().PersistentVolumes().Update(ctx, binding.pv, metav1.UpdateOptions{}) if err != nil { klog.V(4).InfoS("Updating PersistentVolume: binding to claim failed", "PV", klog.KObj(binding.pv), "PVC", klog.KObj(binding.pvc), "err", err) return err @@ -518,7 +518,7 @@ func (b *volumeBinder) bindAPIUpdate(pod *v1.Pod, bindings []*BindingInfo, claim // PV controller is expected to signal back by removing related annotations if actual provisioning fails for i, claim = range claimsToProvision { klog.V(5).InfoS("Updating claims objects to trigger volume provisioning", "pod", klog.KObj(pod), "PVC", klog.KObj(claim)) - newClaim, err := b.kubeClient.CoreV1().PersistentVolumeClaims(claim.Namespace).Update(context.TODO(), claim, metav1.UpdateOptions{}) + newClaim, err := b.kubeClient.CoreV1().PersistentVolumeClaims(claim.Namespace).Update(ctx, claim, metav1.UpdateOptions{}) if err != nil { return err } diff --git a/pkg/scheduler/framework/plugins/volumebinding/binder_test.go b/pkg/scheduler/framework/plugins/volumebinding/binder_test.go index 094144fa6b3..94e93dd6291 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/binder_test.go +++ b/pkg/scheduler/framework/plugins/volumebinding/binder_test.go @@ -1529,7 +1529,7 @@ func TestBindAPIUpdate(t *testing.T) { testEnv.assumeVolumes(t, "node1", pod, scenario.bindings, scenario.provisionedPVCs) // Execute - err := testEnv.internalBinder.bindAPIUpdate(pod, scenario.bindings, scenario.provisionedPVCs) + err := testEnv.internalBinder.bindAPIUpdate(ctx, pod, scenario.bindings, scenario.provisionedPVCs) // Validate if !scenario.shouldFail && err != nil { @@ -2085,7 +2085,7 @@ func TestBindPodVolumes(t *testing.T) { StaticBindings: bindings, DynamicProvisions: claimsToProvision, } - err := testEnv.binder.BindPodVolumes(pod, podVolumes) + err := testEnv.binder.BindPodVolumes(ctx, pod, podVolumes) // Validate if !scenario.shouldFail && err != nil { diff --git a/pkg/scheduler/framework/plugins/volumebinding/fake_binder.go b/pkg/scheduler/framework/plugins/volumebinding/fake_binder.go index 4a07eff9a2f..ef28891f288 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/fake_binder.go +++ b/pkg/scheduler/framework/plugins/volumebinding/fake_binder.go @@ -16,7 +16,11 @@ limitations under the License. package volumebinding -import v1 "k8s.io/api/core/v1" +import ( + "context" + + v1 "k8s.io/api/core/v1" +) // FakeVolumeBinderConfig holds configurations for fake volume binder. type FakeVolumeBinderConfig struct { @@ -62,7 +66,7 @@ func (b *FakeVolumeBinder) AssumePodVolumes(assumedPod *v1.Pod, nodeName string, func (b *FakeVolumeBinder) RevertAssumedPodVolumes(_ *PodVolumes) {} // BindPodVolumes implements SchedulerVolumeBinder.BindPodVolumes. -func (b *FakeVolumeBinder) BindPodVolumes(assumedPod *v1.Pod, podVolumes *PodVolumes) error { +func (b *FakeVolumeBinder) BindPodVolumes(ctx context.Context, assumedPod *v1.Pod, podVolumes *PodVolumes) error { b.BindCalled = true return b.config.BindErr } diff --git a/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go b/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go index 48ee01dea4e..c355c2dc91b 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go +++ b/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go @@ -333,7 +333,7 @@ func (pl *VolumeBinding) PreBind(ctx context.Context, cs *framework.CycleState, return framework.AsStatus(fmt.Errorf("no pod volumes found for node %q", nodeName)) } klog.V(5).InfoS("Trying to bind volumes for pod", "pod", klog.KObj(pod)) - err = pl.Binder.BindPodVolumes(pod, podVolumes) + err = pl.Binder.BindPodVolumes(ctx, pod, podVolumes) if err != nil { klog.V(1).InfoS("Failed to bind volumes for pod", "pod", klog.KObj(pod), "err", err) return framework.AsStatus(err)