kubectl/drain: Add context support
This commits allows specifying a context.Context in the Helper type. This context is utilized to cancel waitForDelete. Kubernetes-commit: 8682e902f5487e04b893da7230125db0d7ae66b4
This commit is contained in:
parent
23432852b0
commit
fe592e7f80
|
@ -43,6 +43,7 @@ const (
|
||||||
|
|
||||||
// Helper contains the parameters to control the behaviour of drainer
|
// Helper contains the parameters to control the behaviour of drainer
|
||||||
type Helper struct {
|
type Helper struct {
|
||||||
|
Ctx context.Context
|
||||||
Client kubernetes.Interface
|
Client kubernetes.Interface
|
||||||
Force bool
|
Force bool
|
||||||
GracePeriodSeconds int
|
GracePeriodSeconds int
|
||||||
|
@ -203,7 +204,7 @@ func (d *Helper) evictPods(pods []corev1.Pod, policyGroupVersion string, getPodF
|
||||||
} else {
|
} else {
|
||||||
globalTimeout = d.Timeout
|
globalTimeout = d.Timeout
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), globalTimeout)
|
ctx, cancel := context.WithTimeout(d.getContext(), globalTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
for _, pod := range pods {
|
for _, pod := range pods {
|
||||||
go func(pod corev1.Pod, returnCh chan error) {
|
go func(pod corev1.Pod, returnCh chan error) {
|
||||||
|
@ -271,7 +272,7 @@ func (d *Helper) deletePods(pods []corev1.Pod, getPodFn func(namespace, name str
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx := context.TODO()
|
ctx := d.getContext()
|
||||||
_, err := waitForDelete(ctx, pods, 1*time.Second, globalTimeout, false, getPodFn, d.OnPodDeletedOrEvicted, globalTimeout)
|
_, err := waitForDelete(ctx, pods, 1*time.Second, globalTimeout, false, getPodFn, d.OnPodDeletedOrEvicted, globalTimeout)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -306,3 +307,13 @@ func waitForDelete(ctx context.Context, pods []corev1.Pod, interval, timeout tim
|
||||||
})
|
})
|
||||||
return pods, err
|
return pods, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since Helper does not have a constructor, we can't enforce Helper.Ctx != nil
|
||||||
|
// Multiple public methods prevent us from initializing the context in a single
|
||||||
|
// place as well.
|
||||||
|
func (d *Helper) getContext() context.Context {
|
||||||
|
if d.Ctx != nil {
|
||||||
|
return d.Ctx
|
||||||
|
}
|
||||||
|
return context.Background()
|
||||||
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ func TestDeletePods(t *testing.T) {
|
||||||
description string
|
description string
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
ctxTimeoutEarly bool
|
||||||
expectPendingPods bool
|
expectPendingPods bool
|
||||||
expectError bool
|
expectError bool
|
||||||
expectedError *error
|
expectedError *error
|
||||||
|
@ -91,6 +92,22 @@ func TestDeletePods(t *testing.T) {
|
||||||
return nil, fmt.Errorf("%q: not found", name)
|
return nil, fmt.Errorf("%q: not found", name)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Context Canceled",
|
||||||
|
interval: 1000 * time.Millisecond,
|
||||||
|
timeout: 5 * time.Second,
|
||||||
|
ctxTimeoutEarly: true,
|
||||||
|
expectPendingPods: true,
|
||||||
|
expectError: true,
|
||||||
|
expectedError: &wait.ErrWaitTimeout,
|
||||||
|
getPodFn: func(namespace, name string) (*corev1.Pod, error) {
|
||||||
|
oldPodMap, _ := createPods(false)
|
||||||
|
if oldPod, found := oldPodMap[name]; found {
|
||||||
|
return &oldPod, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("%q: not found", name)
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Client error could be passed out",
|
description: "Client error could be passed out",
|
||||||
interval: 200 * time.Millisecond,
|
interval: 200 * time.Millisecond,
|
||||||
|
@ -107,14 +124,22 @@ func TestDeletePods(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.description, func(t *testing.T) {
|
t.Run(test.description, func(t *testing.T) {
|
||||||
_, pods := createPods(false)
|
_, pods := createPods(false)
|
||||||
ctx := context.TODO()
|
ctx := context.Background()
|
||||||
|
if test.ctxTimeoutEarly {
|
||||||
|
ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)
|
||||||
|
}
|
||||||
|
start := time.Now()
|
||||||
pendingPods, err := waitForDelete(ctx, pods, test.interval, test.timeout, false, test.getPodFn, nil, time.Duration(math.MaxInt64))
|
pendingPods, err := waitForDelete(ctx, pods, test.interval, test.timeout, false, test.getPodFn, nil, time.Duration(math.MaxInt64))
|
||||||
|
elapsed := time.Since(start)
|
||||||
if test.expectError {
|
if test.expectError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("%s: unexpected non-error", test.description)
|
t.Fatalf("%s: unexpected non-error", test.description)
|
||||||
} else if test.expectedError != nil {
|
} else if test.expectedError != nil {
|
||||||
if *test.expectedError != err {
|
if test.ctxTimeoutEarly {
|
||||||
|
if elapsed >= test.timeout {
|
||||||
|
t.Fatalf("%s: the supplied context did not effectively cancel the waitForDelete", test.description)
|
||||||
|
}
|
||||||
|
} else if *test.expectedError != err {
|
||||||
t.Fatalf("%s: the error does not match expected error", test.description)
|
t.Fatalf("%s: the error does not match expected error", test.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue