Skip to content

Commit 8682e90

Browse files
committed
kubectl/drain: Add context support
This commits allows specifying a context.Context in the Helper type. This context is utilized to cancel waitForDelete.
1 parent 2b9aeab commit 8682e90

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

staging/src/k8s.io/kubectl/pkg/drain/drain.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343

4444
// Helper contains the parameters to control the behaviour of drainer
4545
type Helper struct {
46+
Ctx context.Context
4647
Client kubernetes.Interface
4748
Force bool
4849
GracePeriodSeconds int
@@ -203,7 +204,7 @@ func (d *Helper) evictPods(pods []corev1.Pod, policyGroupVersion string, getPodF
203204
} else {
204205
globalTimeout = d.Timeout
205206
}
206-
ctx, cancel := context.WithTimeout(context.TODO(), globalTimeout)
207+
ctx, cancel := context.WithTimeout(d.getContext(), globalTimeout)
207208
defer cancel()
208209
for _, pod := range pods {
209210
go func(pod corev1.Pod, returnCh chan error) {
@@ -271,7 +272,7 @@ func (d *Helper) deletePods(pods []corev1.Pod, getPodFn func(namespace, name str
271272
return err
272273
}
273274
}
274-
ctx := context.TODO()
275+
ctx := d.getContext()
275276
_, err := waitForDelete(ctx, pods, 1*time.Second, globalTimeout, false, getPodFn, d.OnPodDeletedOrEvicted, globalTimeout)
276277
return err
277278
}
@@ -306,3 +307,13 @@ func waitForDelete(ctx context.Context, pods []corev1.Pod, interval, timeout tim
306307
})
307308
return pods, err
308309
}
310+
311+
// Since Helper does not have a constructor, we can't enforce Helper.Ctx != nil
312+
// Multiple public methods prevent us from initializing the context in a single
313+
// place as well.
314+
func (d *Helper) getContext() context.Context {
315+
if d.Ctx != nil {
316+
return d.Ctx
317+
}
318+
return context.Background()
319+
}

staging/src/k8s.io/kubectl/pkg/drain/drain_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestDeletePods(t *testing.T) {
4646
description string
4747
interval time.Duration
4848
timeout time.Duration
49+
ctxTimeoutEarly bool
4950
expectPendingPods bool
5051
expectError bool
5152
expectedError *error
@@ -91,6 +92,22 @@ func TestDeletePods(t *testing.T) {
9192
return nil, fmt.Errorf("%q: not found", name)
9293
},
9394
},
95+
{
96+
description: "Context Canceled",
97+
interval: 1000 * time.Millisecond,
98+
timeout: 5 * time.Second,
99+
ctxTimeoutEarly: true,
100+
expectPendingPods: true,
101+
expectError: true,
102+
expectedError: &wait.ErrWaitTimeout,
103+
getPodFn: func(namespace, name string) (*corev1.Pod, error) {
104+
oldPodMap, _ := createPods(false)
105+
if oldPod, found := oldPodMap[name]; found {
106+
return &oldPod, nil
107+
}
108+
return nil, fmt.Errorf("%q: not found", name)
109+
},
110+
},
94111
{
95112
description: "Client error could be passed out",
96113
interval: 200 * time.Millisecond,
@@ -107,14 +124,22 @@ func TestDeletePods(t *testing.T) {
107124
for _, test := range tests {
108125
t.Run(test.description, func(t *testing.T) {
109126
_, pods := createPods(false)
110-
ctx := context.TODO()
127+
ctx := context.Background()
128+
if test.ctxTimeoutEarly {
129+
ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)
130+
}
131+
start := time.Now()
111132
pendingPods, err := waitForDelete(ctx, pods, test.interval, test.timeout, false, test.getPodFn, nil, time.Duration(math.MaxInt64))
112-
133+
elapsed := time.Since(start)
113134
if test.expectError {
114135
if err == nil {
115136
t.Fatalf("%s: unexpected non-error", test.description)
116137
} else if test.expectedError != nil {
117-
if *test.expectedError != err {
138+
if test.ctxTimeoutEarly {
139+
if elapsed >= test.timeout {
140+
t.Fatalf("%s: the supplied context did not effectively cancel the waitForDelete", test.description)
141+
}
142+
} else if *test.expectedError != err {
118143
t.Fatalf("%s: the error does not match expected error", test.description)
119144
}
120145
}

0 commit comments

Comments
 (0)