@@ -3,6 +3,8 @@ package drain
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
7
+ "time"
6
8
7
9
"github.com/sirupsen/logrus"
8
10
"k8s.io/api/core/v1"
@@ -13,6 +15,11 @@ import (
13
15
"k8s.io/client-go/kubernetes"
14
16
)
15
17
18
+ const (
19
+ // Wait up to this amount of time for pods to be evicted from a node.
20
+ drainMaxWait = 300 * time .Second
21
+ )
22
+
16
23
// Config configures a Drainer.
17
24
type Config struct {
18
25
Client kubernetes.Interface
@@ -74,6 +81,7 @@ func (d *drainer) Drain(ctx context.Context, node string) error {
74
81
return err
75
82
}
76
83
84
+ evictedPods := []string {}
77
85
for _ , pod := range pods {
78
86
fields ["pod" ] = pod .GetName ()
79
87
d .log .WithFields (fields ).Info ("drainer: evicting pod" )
@@ -83,6 +91,43 @@ func (d *drainer) Drain(ctx context.Context, node string) error {
83
91
d .log .WithFields (fields ).Errorf ("drainer: error evicting pod: %v" , err )
84
92
return err
85
93
}
94
+ evictedPods = append (evictedPods , pod .GetName ())
95
+ }
96
+
97
+ start := time .Now ()
98
+ for len (evictedPods ) > 0 {
99
+ podsDescription := ""
100
+ if len (evictedPods ) > 5 {
101
+ podsDescription = strings .Join (evictedPods [:5 ], ", " ) + " ..."
102
+ } else {
103
+ podsDescription = strings .Join (evictedPods , ", " )
104
+ }
105
+ d .log .WithFields (fields ).Infof ("drainer: waiting for %d pods to be evicted: %s" , len (evictedPods ), podsDescription )
106
+
107
+ if time .Since (start ) > drainMaxWait {
108
+ d .log .WithFields (fields ).Infof ("drainer: waited maximum amount of time for evictions, continuing" )
109
+ break
110
+ }
111
+
112
+ pods , err := d .getPodsForDeletion (ctx , node )
113
+ if err != nil {
114
+ d .log .WithFields (fields ).Errorf ("drainer: error getting pods: %v" , err )
115
+ return err
116
+ }
117
+ podsByName := make (map [string ]struct {}, len (pods ))
118
+ for _ , pod := range pods {
119
+ podsByName [pod .GetName ()] = struct {}{}
120
+ }
121
+ remainingPods := []string {}
122
+ for _ , pod := range evictedPods {
123
+ if _ , ok := podsByName [pod ]; ok {
124
+ remainingPods = append (remainingPods , pod )
125
+ }
126
+ }
127
+ evictedPods = remainingPods
128
+ if len (evictedPods ) > 0 {
129
+ time .Sleep (1 * time .Second )
130
+ }
86
131
}
87
132
88
133
d .log .WithFields (fields ).Info ("drainer: drained node" )
0 commit comments