Skip to content

Commit

Permalink
Merge pull request #127881 from omerap12/drain-package-PollUntilConte…
Browse files Browse the repository at this point in the history
…xtTimeout

Update waitForDelete to use PollUntilContextTimeout
  • Loading branch information
k8s-ci-robot authored Oct 14, 2024
2 parents 4dc7a48 + bba0550 commit 5b1a4ca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
15 changes: 5 additions & 10 deletions staging/src/k8s.io/kubectl/pkg/drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ func (d *Helper) deletePods(pods []corev1.Pod, getPodFn func(namespace, name str

func waitForDelete(params waitForDeleteParams) ([]corev1.Pod, error) {
pods := params.pods
err := wait.PollImmediate(params.interval, params.timeout, func() (bool, error) {
if params.ctx == nil {
params.ctx = context.Background()
}
err := wait.PollUntilContextTimeout(params.ctx, params.interval, params.timeout, true, func(ctx context.Context) (done bool, err error) {
pendingPods := []corev1.Pod{}
for i, pod := range pods {
p, err := params.getPodFn(pod.Namespace, pod.Name)
Expand All @@ -440,15 +443,7 @@ func waitForDelete(params waitForDeleteParams) ([]corev1.Pod, error) {
}
}
pods = pendingPods
if len(pendingPods) > 0 {
select {
case <-params.ctx.Done():
return false, fmt.Errorf("global timeout reached: %v", params.globalTimeout)
default:
return false, nil
}
}
return true, nil
return len(pods) == 0, nil
})
return pods, err
}
Expand Down
5 changes: 2 additions & 3 deletions staging/src/k8s.io/kubectl/pkg/drain/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"
ktest "k8s.io/client-go/testing"
)
Expand Down Expand Up @@ -101,7 +100,7 @@ func TestDeletePods(t *testing.T) {
timeout: 3 * time.Second,
expectPendingPods: true,
expectError: true,
expectedError: &wait.ErrWaitTimeout,
expectedError: &context.DeadlineExceeded,
getPodFn: func(namespace, name string) (*corev1.Pod, error) {
oldPodMap, _ := createPods(false)
if oldPod, found := oldPodMap[name]; found {
Expand All @@ -117,7 +116,7 @@ func TestDeletePods(t *testing.T) {
ctxTimeoutEarly: true,
expectPendingPods: true,
expectError: true,
expectedError: &wait.ErrWaitTimeout,
expectedError: &context.Canceled,
getPodFn: func(namespace, name string) (*corev1.Pod, error) {
oldPodMap, _ := createPods(false)
if oldPod, found := oldPodMap[name]; found {
Expand Down

0 comments on commit 5b1a4ca

Please sign in to comment.