Skip to content

Commit 9c3f648

Browse files
authored
Merge pull request kubernetes#91705 from mrkm4ntr/revert-assumed-in-unreserve
Revert assumed PVs and PVCs in unreserve extension point
2 parents 3d78928 + 79ab958 commit 9c3f648

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

pkg/controller/volume/scheduling/scheduler_binder.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ type SchedulerVolumeBinder interface {
124124
// This function is called serially.
125125
AssumePodVolumes(assumedPod *v1.Pod, nodeName string) (allFullyBound bool, err error)
126126

127+
// RevertAssumedPodVolumes will revert assumed PV and PVC cache.
128+
RevertAssumedPodVolumes(assumedPod *v1.Pod, nodeName string)
129+
127130
// BindPodVolumes will:
128131
// 1. Initiate the volume binding by making the API call to prebind the PV
129132
// to its matching PVC.
@@ -381,6 +384,12 @@ func (b *volumeBinder) AssumePodVolumes(assumedPod *v1.Pod, nodeName string) (al
381384
return
382385
}
383386

387+
// RevertAssumedPodVolumes will revert assumed PV and PVC cache.
388+
func (b *volumeBinder) RevertAssumedPodVolumes(assumedPod *v1.Pod, nodeName string) {
389+
b.revertAssumedPVs(b.podBindingCache.GetBindings(assumedPod, nodeName))
390+
b.revertAssumedPVCs(b.podBindingCache.GetProvisionedPVCs(assumedPod, nodeName))
391+
}
392+
384393
// BindPodVolumes gets the cached bindings and PVCs to provision in podBindingCache,
385394
// makes the API update for those PVs/PVCs, and waits for the PVCs to be completely bound
386395
// by the PV controller.

pkg/controller/volume/scheduling/scheduler_binder_fake.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func (b *FakeVolumeBinder) AssumePodVolumes(assumedPod *v1.Pod, nodeName string)
5858
return b.config.AllBound, b.config.AssumeErr
5959
}
6060

61+
// RevertAssumedPodVolumes implements SchedulerVolumeBinder.RevertAssumedPodVolumes
62+
func (b *FakeVolumeBinder) RevertAssumedPodVolumes(assumedPod *v1.Pod, nodeName string) {}
63+
6164
// BindPodVolumes implements SchedulerVolumeBinder.BindPodVolumes.
6265
func (b *FakeVolumeBinder) BindPodVolumes(assumedPod *v1.Pod) error {
6366
b.BindCalled = true

pkg/controller/volume/scheduling/scheduler_binder_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,14 @@ func (env *testEnv) validateAssume(t *testing.T, pod *v1.Pod, bindings []*bindin
465465
}
466466
}
467467

468-
func (env *testEnv) validateFailedAssume(t *testing.T, pod *v1.Pod, bindings []*bindingInfo, provisionings []*v1.PersistentVolumeClaim) {
468+
func (env *testEnv) validateCacheRestored(t *testing.T, pod *v1.Pod, bindings []*bindingInfo, provisionings []*v1.PersistentVolumeClaim) {
469469
// All PVs have been unmodified in cache
470470
pvCache := env.internalBinder.pvCache
471471
for _, b := range bindings {
472472
pv, _ := pvCache.GetPV(b.pv.Name)
473+
apiPV, _ := pvCache.GetAPIPV(b.pv.Name)
473474
// PV could be nil if it's missing from cache
474-
if pv != nil && pv != b.pv {
475+
if pv != nil && pv != apiPV {
475476
t.Errorf("PV %q was modified in cache", b.pv.Name)
476477
}
477478
}
@@ -1237,7 +1238,7 @@ func TestAssumePodVolumes(t *testing.T) {
12371238
scenario.expectedProvisionings = scenario.provisionedPVCs
12381239
}
12391240
if scenario.shouldFail {
1240-
testEnv.validateFailedAssume(t, pod, scenario.expectedBindings, scenario.expectedProvisionings)
1241+
testEnv.validateCacheRestored(t, pod, scenario.bindings, scenario.provisionedPVCs)
12411242
} else {
12421243
testEnv.validateAssume(t, pod, scenario.expectedBindings, scenario.expectedProvisionings)
12431244
}
@@ -1249,6 +1250,34 @@ func TestAssumePodVolumes(t *testing.T) {
12491250
}
12501251
}
12511252

1253+
func TestRevertAssumedPodVolumes(t *testing.T) {
1254+
ctx, cancel := context.WithCancel(context.Background())
1255+
defer cancel()
1256+
1257+
podPVCs := []*v1.PersistentVolumeClaim{unboundPVC, provisionedPVC}
1258+
bindings := []*bindingInfo{makeBinding(unboundPVC, pvNode1a)}
1259+
pvs := []*v1.PersistentVolume{pvNode1a}
1260+
provisionedPVCs := []*v1.PersistentVolumeClaim{provisionedPVC}
1261+
expectedBindings := []*bindingInfo{makeBinding(unboundPVC, pvNode1aBound)}
1262+
expectedProvisionings := []*v1.PersistentVolumeClaim{selectedNodePVC}
1263+
1264+
// Setup
1265+
testEnv := newTestBinder(t, ctx.Done())
1266+
testEnv.initClaims(podPVCs, podPVCs)
1267+
pod := makePod(podPVCs)
1268+
testEnv.initPodCache(pod, "node1", bindings, provisionedPVCs)
1269+
testEnv.initVolumes(pvs, pvs)
1270+
1271+
allbound, err := testEnv.binder.AssumePodVolumes(pod, "node1")
1272+
if allbound || err != nil {
1273+
t.Errorf("No volumes are assumed")
1274+
}
1275+
testEnv.validateAssume(t, pod, expectedBindings, expectedProvisionings)
1276+
1277+
testEnv.binder.RevertAssumedPodVolumes(pod, "node1")
1278+
testEnv.validateCacheRestored(t, pod, bindings, provisionedPVCs)
1279+
}
1280+
12521281
func TestBindAPIUpdate(t *testing.T) {
12531282
type scenarioType struct {
12541283
// Inputs

pkg/scheduler/framework/plugins/volumebinding/volume_binding.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ func (pl *VolumeBinding) PreBind(ctx context.Context, cs *framework.CycleState,
205205
return nil
206206
}
207207

208-
// Unreserve clears pod binding state.
209-
// TODO(#90962) Revert assumed PV/PVC cache
208+
// Unreserve clears assumed PV and PVC cache and pod binding state.
209+
// It's idempotent, and does nothing if no cache or binding state found for the given pod.
210210
func (pl *VolumeBinding) Unreserve(ctx context.Context, cs *framework.CycleState, pod *v1.Pod, nodeName string) {
211+
pl.Binder.RevertAssumedPodVolumes(pod, nodeName)
211212
pl.Binder.DeletePodBindings(pod)
212213
return
213214
}

0 commit comments

Comments
 (0)