2019-04-07 17:07:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package drain
|
|
|
|
|
|
|
|
import (
|
2019-12-12 01:27:03 +00:00
|
|
|
"context"
|
2019-09-27 21:51:53 +00:00
|
|
|
"fmt"
|
2019-04-07 17:07:55 +00:00
|
|
|
"io"
|
2019-09-27 21:51:53 +00:00
|
|
|
"math"
|
2019-04-07 17:07:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
policyv1beta1 "k8s.io/api/policy/v1beta1"
|
2019-09-27 21:51:53 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2019-04-07 17:07:55 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2019-09-27 21:51:53 +00:00
|
|
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2020-03-26 21:07:15 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2020-03-26 21:07:15 +00:00
|
|
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EvictionKind represents the kind of evictions object
|
|
|
|
EvictionKind = "Eviction"
|
|
|
|
// EvictionSubresource represents the kind of evictions object as pod's subresource
|
|
|
|
EvictionSubresource = "pods/eviction"
|
2020-03-26 21:07:15 +00:00
|
|
|
podSkipMsgTemplate = "pod %q has DeletionTimestamp older than %v seconds, skipping\n"
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Helper contains the parameters to control the behaviour of drainer
|
|
|
|
type Helper struct {
|
2021-03-18 22:40:29 +00:00
|
|
|
Ctx context.Context
|
|
|
|
Client kubernetes.Interface
|
|
|
|
Force bool
|
|
|
|
|
|
|
|
// GracePeriodSeconds is how long to wait for a pod to terminate.
|
|
|
|
// IMPORTANT: 0 means "delete immediately"; set to a negative value
|
|
|
|
// to use the pod's terminationGracePeriodSeconds.
|
|
|
|
GracePeriodSeconds int
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
IgnoreAllDaemonSets bool
|
2021-03-18 22:40:29 +00:00
|
|
|
IgnoreErrors bool
|
2019-04-07 17:07:55 +00:00
|
|
|
Timeout time.Duration
|
2020-12-01 01:06:26 +00:00
|
|
|
DeleteEmptyDirData bool
|
2019-04-07 17:07:55 +00:00
|
|
|
Selector string
|
|
|
|
PodSelector string
|
2019-09-27 21:51:53 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// DisableEviction forces drain to use delete rather than evict
|
|
|
|
DisableEviction bool
|
|
|
|
|
|
|
|
// SkipWaitForDeleteTimeoutSeconds ignores pods that have a
|
|
|
|
// DeletionTimeStamp > N seconds. It's up to the user to decide when this
|
|
|
|
// option is appropriate; examples include the Node is unready and the pods
|
|
|
|
// won't drain otherwise
|
|
|
|
SkipWaitForDeleteTimeoutSeconds int
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
// AdditionalFilters are applied sequentially after base drain filters to
|
|
|
|
// exclude pods using custom logic. Any filter that returns PodDeleteStatus
|
|
|
|
// with Delete == false will immediately stop execution of further filters.
|
|
|
|
AdditionalFilters []PodFilter
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
Out io.Writer
|
|
|
|
ErrOut io.Writer
|
|
|
|
|
|
|
|
DryRunStrategy cmdutil.DryRunStrategy
|
|
|
|
DryRunVerifier *resource.DryRunVerifier
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// OnPodDeletedOrEvicted is called when a pod is evicted/deleted; for printing progress output
|
|
|
|
OnPodDeletedOrEvicted func(pod *corev1.Pod, usingEviction bool)
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
type waitForDeleteParams struct {
|
|
|
|
ctx context.Context
|
|
|
|
pods []corev1.Pod
|
|
|
|
interval time.Duration
|
|
|
|
timeout time.Duration
|
|
|
|
usingEviction bool
|
|
|
|
getPodFn func(string, string) (*corev1.Pod, error)
|
|
|
|
onDoneFn func(pod *corev1.Pod, usingEviction bool)
|
|
|
|
globalTimeout time.Duration
|
|
|
|
skipWaitForDeleteTimeoutSeconds int
|
|
|
|
out io.Writer
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// CheckEvictionSupport uses Discovery API to find out if the server support
|
|
|
|
// eviction subresource If support, it will return its groupVersion; Otherwise,
|
|
|
|
// it will return an empty string
|
|
|
|
func CheckEvictionSupport(clientset kubernetes.Interface) (string, error) {
|
|
|
|
discoveryClient := clientset.Discovery()
|
|
|
|
groupList, err := discoveryClient.ServerGroups()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
foundPolicyGroup := false
|
|
|
|
var policyGroupVersion string
|
|
|
|
for _, group := range groupList.Groups {
|
|
|
|
if group.Name == "policy" {
|
|
|
|
foundPolicyGroup = true
|
|
|
|
policyGroupVersion = group.PreferredVersion.GroupVersion
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundPolicyGroup {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
resourceList, err := discoveryClient.ServerResourcesForGroupVersion("v1")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, resource := range resourceList.APIResources {
|
|
|
|
if resource.Name == EvictionSubresource && resource.Kind == EvictionKind {
|
|
|
|
return policyGroupVersion, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
func (d *Helper) makeDeleteOptions() metav1.DeleteOptions {
|
|
|
|
deleteOptions := metav1.DeleteOptions{}
|
2019-04-07 17:07:55 +00:00
|
|
|
if d.GracePeriodSeconds >= 0 {
|
|
|
|
gracePeriodSeconds := int64(d.GracePeriodSeconds)
|
|
|
|
deleteOptions.GracePeriodSeconds = &gracePeriodSeconds
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
if d.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
deleteOptions.DryRun = []string{metav1.DryRunAll}
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
return deleteOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePod will delete the given pod, or return an error if it couldn't
|
|
|
|
func (d *Helper) DeletePod(pod corev1.Pod) error {
|
2020-03-26 21:07:15 +00:00
|
|
|
if d.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
if err := d.DryRunVerifier.HasSupport(pod.GroupVersionKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.Client.CoreV1().Pods(pod.Namespace).Delete(context.TODO(), pod.Name, d.makeDeleteOptions())
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvictPod will evict the give pod, or return an error if it couldn't
|
|
|
|
func (d *Helper) EvictPod(pod corev1.Pod, policyGroupVersion string) error {
|
2020-03-26 21:07:15 +00:00
|
|
|
if d.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
if err := d.DryRunVerifier.HasSupport(pod.GroupVersionKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delOpts := d.makeDeleteOptions()
|
2019-04-07 17:07:55 +00:00
|
|
|
eviction := &policyv1beta1.Eviction{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
APIVersion: policyGroupVersion,
|
|
|
|
Kind: EvictionKind,
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: pod.Name,
|
|
|
|
Namespace: pod.Namespace,
|
|
|
|
},
|
2020-03-26 21:07:15 +00:00
|
|
|
DeleteOptions: &delOpts,
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// Remember to change change the URL manipulation func when Eviction's version change
|
2020-03-26 21:07:15 +00:00
|
|
|
return d.Client.PolicyV1beta1().Evictions(eviction.Namespace).Evict(context.TODO(), eviction)
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPodsForDeletion receives resource info for a node, and returns those pods as PodDeleteList,
|
|
|
|
// or error if it cannot list pods. All pods that are ready to be deleted can be obtained with .Pods(),
|
|
|
|
// and string with all warning can be obtained with .Warnings(), and .Errors() for all errors that
|
|
|
|
// occurred during deletion.
|
2020-12-01 01:06:26 +00:00
|
|
|
func (d *Helper) GetPodsForDeletion(nodeName string) (*PodDeleteList, []error) {
|
2019-04-07 17:07:55 +00:00
|
|
|
labelSelector, err := labels.Parse(d.PodSelector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
podList, err := d.Client.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{
|
2019-04-07 17:07:55 +00:00
|
|
|
LabelSelector: labelSelector.String(),
|
|
|
|
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": nodeName}).String()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
list := filterPods(podList, d.makeFilters())
|
|
|
|
if errs := list.errors(); len(errs) > 0 {
|
|
|
|
return list, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
func filterPods(podList *corev1.PodList, filters []PodFilter) *PodDeleteList {
|
|
|
|
pods := []PodDelete{}
|
2019-04-07 17:07:55 +00:00
|
|
|
for _, pod := range podList.Items {
|
2020-12-01 01:06:26 +00:00
|
|
|
var status PodDeleteStatus
|
|
|
|
for _, filter := range filters {
|
2019-04-07 17:07:55 +00:00
|
|
|
status = filter(pod)
|
2020-12-01 01:06:26 +00:00
|
|
|
if !status.Delete {
|
2019-04-07 17:07:55 +00:00
|
|
|
// short-circuit as soon as pod is filtered out
|
|
|
|
// at that point, there is no reason to run pod
|
|
|
|
// through any additional filters
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
// Add the pod to PodDeleteList no matter what PodDeleteStatus is,
|
|
|
|
// those pods whose PodDeleteStatus is false like DaemonSet will
|
2020-03-26 21:07:15 +00:00
|
|
|
// be catched by list.errors()
|
2020-12-01 01:06:26 +00:00
|
|
|
pods = append(pods, PodDelete{
|
|
|
|
Pod: pod,
|
|
|
|
Status: status,
|
2020-03-26 21:07:15 +00:00
|
|
|
})
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
list := &PodDeleteList{items: pods}
|
|
|
|
return list
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// DeleteOrEvictPods deletes or evicts the pods on the api server
|
|
|
|
func (d *Helper) DeleteOrEvictPods(pods []corev1.Pod) error {
|
|
|
|
if len(pods) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(justinsb): unnecessary?
|
|
|
|
getPodFn := func(namespace, name string) (*corev1.Pod, error) {
|
2020-03-26 21:07:15 +00:00
|
|
|
return d.Client.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
|
|
|
if !d.DisableEviction {
|
|
|
|
policyGroupVersion, err := CheckEvictionSupport(d.Client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policyGroupVersion) > 0 {
|
|
|
|
return d.evictPods(pods, policyGroupVersion, getPodFn)
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return d.deletePods(pods, getPodFn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Helper) evictPods(pods []corev1.Pod, policyGroupVersion string, getPodFn func(namespace, name string) (*corev1.Pod, error)) error {
|
|
|
|
returnCh := make(chan error, 1)
|
2019-12-12 01:27:03 +00:00
|
|
|
// 0 timeout means infinite, we use MaxInt64 to represent it.
|
|
|
|
var globalTimeout time.Duration
|
|
|
|
if d.Timeout == 0 {
|
|
|
|
globalTimeout = time.Duration(math.MaxInt64)
|
|
|
|
} else {
|
|
|
|
globalTimeout = d.Timeout
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(d.getContext(), globalTimeout)
|
2019-12-12 01:27:03 +00:00
|
|
|
defer cancel()
|
2019-09-27 21:51:53 +00:00
|
|
|
for _, pod := range pods {
|
|
|
|
go func(pod corev1.Pod, returnCh chan error) {
|
2020-12-01 01:06:26 +00:00
|
|
|
refreshPod := false
|
2019-09-27 21:51:53 +00:00
|
|
|
for {
|
2020-03-26 21:07:15 +00:00
|
|
|
switch d.DryRunStrategy {
|
|
|
|
case cmdutil.DryRunServer:
|
|
|
|
fmt.Fprintf(d.Out, "evicting pod %s/%s (server dry run)\n", pod.Namespace, pod.Name)
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(d.Out, "evicting pod %s/%s\n", pod.Namespace, pod.Name)
|
|
|
|
}
|
2019-12-12 01:27:03 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// return here or we'll leak a goroutine.
|
2020-12-01 01:06:26 +00:00
|
|
|
returnCh <- fmt.Errorf("error when evicting pods/%q -n %q: global timeout reached: %v", pod.Name, pod.Namespace, globalTimeout)
|
2019-12-12 01:27:03 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
|
|
|
|
// Create a temporary pod so we don't mutate the pod in the loop.
|
|
|
|
activePod := pod
|
|
|
|
if refreshPod {
|
|
|
|
freshPod, err := getPodFn(pod.Namespace, pod.Name)
|
|
|
|
// We ignore errors and let eviction sort it out with
|
|
|
|
// the original pod.
|
|
|
|
if err == nil {
|
|
|
|
activePod = *freshPod
|
|
|
|
}
|
|
|
|
refreshPod = false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.EvictPod(activePod, policyGroupVersion)
|
2019-09-27 21:51:53 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else if apierrors.IsNotFound(err) {
|
|
|
|
returnCh <- nil
|
|
|
|
return
|
|
|
|
} else if apierrors.IsTooManyRequests(err) {
|
2020-12-01 01:06:26 +00:00
|
|
|
fmt.Fprintf(d.ErrOut, "error when evicting pods/%q -n %q (will retry after 5s): %v\n", activePod.Name, activePod.Namespace, err)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
} else if !activePod.ObjectMeta.DeletionTimestamp.IsZero() && apierrors.IsForbidden(err) && apierrors.HasStatusCause(err, corev1.NamespaceTerminatingCause) {
|
|
|
|
// an eviction request in a deleting namespace will throw a forbidden error,
|
|
|
|
// if the pod is already marked deleted, we can ignore this error, an eviction
|
|
|
|
// request will never succeed, but we will waitForDelete for this pod.
|
|
|
|
break
|
|
|
|
} else if apierrors.IsForbidden(err) && apierrors.HasStatusCause(err, corev1.NamespaceTerminatingCause) {
|
|
|
|
// an eviction request in a deleting namespace will throw a forbidden error,
|
|
|
|
// if the pod is not marked deleted, we retry until it is.
|
|
|
|
fmt.Fprintf(d.ErrOut, "error when evicting pod %q (will retry after 5s): %v\n", activePod.Name, err)
|
2019-09-27 21:51:53 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
} else {
|
2020-12-01 01:06:26 +00:00
|
|
|
returnCh <- fmt.Errorf("error when evicting pods/%q -n %q: %v", activePod.Name, activePod.Namespace, err)
|
2019-09-27 21:51:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
if d.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
returnCh <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params := waitForDeleteParams{
|
|
|
|
ctx: ctx,
|
|
|
|
pods: []corev1.Pod{pod},
|
|
|
|
interval: 1 * time.Second,
|
|
|
|
timeout: time.Duration(math.MaxInt64),
|
|
|
|
usingEviction: true,
|
|
|
|
getPodFn: getPodFn,
|
|
|
|
onDoneFn: d.OnPodDeletedOrEvicted,
|
|
|
|
globalTimeout: globalTimeout,
|
|
|
|
skipWaitForDeleteTimeoutSeconds: d.SkipWaitForDeleteTimeoutSeconds,
|
|
|
|
out: d.Out,
|
|
|
|
}
|
|
|
|
_, err := waitForDelete(params)
|
2019-09-27 21:51:53 +00:00
|
|
|
if err == nil {
|
|
|
|
returnCh <- nil
|
|
|
|
} else {
|
|
|
|
returnCh <- fmt.Errorf("error when waiting for pod %q terminating: %v", pod.Name, err)
|
|
|
|
}
|
|
|
|
}(pod, returnCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
doneCount := 0
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
numPods := len(pods)
|
|
|
|
for doneCount < numPods {
|
|
|
|
select {
|
|
|
|
case err := <-returnCh:
|
|
|
|
doneCount++
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 01:27:03 +00:00
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
return utilerrors.NewAggregate(errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Helper) deletePods(pods []corev1.Pod, getPodFn func(namespace, name string) (*corev1.Pod, error)) error {
|
|
|
|
// 0 timeout means infinite, we use MaxInt64 to represent it.
|
|
|
|
var globalTimeout time.Duration
|
|
|
|
if d.Timeout == 0 {
|
|
|
|
globalTimeout = time.Duration(math.MaxInt64)
|
|
|
|
} else {
|
|
|
|
globalTimeout = d.Timeout
|
|
|
|
}
|
|
|
|
for _, pod := range pods {
|
|
|
|
err := d.DeletePod(pod)
|
|
|
|
if err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
ctx := d.getContext()
|
|
|
|
params := waitForDeleteParams{
|
|
|
|
ctx: ctx,
|
|
|
|
pods: pods,
|
|
|
|
interval: 1 * time.Second,
|
|
|
|
timeout: globalTimeout,
|
|
|
|
usingEviction: false,
|
|
|
|
getPodFn: getPodFn,
|
|
|
|
onDoneFn: d.OnPodDeletedOrEvicted,
|
|
|
|
globalTimeout: globalTimeout,
|
|
|
|
skipWaitForDeleteTimeoutSeconds: d.SkipWaitForDeleteTimeoutSeconds,
|
|
|
|
out: d.Out,
|
|
|
|
}
|
|
|
|
_, err := waitForDelete(params)
|
2019-09-27 21:51:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
func waitForDelete(params waitForDeleteParams) ([]corev1.Pod, error) {
|
|
|
|
pods := params.pods
|
|
|
|
err := wait.PollImmediate(params.interval, params.timeout, func() (bool, error) {
|
2019-09-27 21:51:53 +00:00
|
|
|
pendingPods := []corev1.Pod{}
|
|
|
|
for i, pod := range pods {
|
2020-03-26 21:07:15 +00:00
|
|
|
p, err := params.getPodFn(pod.Namespace, pod.Name)
|
2019-09-27 21:51:53 +00:00
|
|
|
if apierrors.IsNotFound(err) || (p != nil && p.ObjectMeta.UID != pod.ObjectMeta.UID) {
|
2020-03-26 21:07:15 +00:00
|
|
|
if params.onDoneFn != nil {
|
|
|
|
params.onDoneFn(&pod, params.usingEviction)
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return false, err
|
|
|
|
} else {
|
2020-03-26 21:07:15 +00:00
|
|
|
if shouldSkipPod(*p, params.skipWaitForDeleteTimeoutSeconds) {
|
|
|
|
fmt.Fprintf(params.out, podSkipMsgTemplate, pod.Name, params.skipWaitForDeleteTimeoutSeconds)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
pendingPods = append(pendingPods, pods[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pods = pendingPods
|
|
|
|
if len(pendingPods) > 0 {
|
2019-12-12 01:27:03 +00:00
|
|
|
select {
|
2020-03-26 21:07:15 +00:00
|
|
|
case <-params.ctx.Done():
|
|
|
|
return false, fmt.Errorf("global timeout reached: %v", params.globalTimeout)
|
2019-12-12 01:27:03 +00:00
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
return pods, err
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|