@@ -101,44 +101,35 @@ func enumerateOnDeleteDaemonSetPods(ctx context.Context, cs *kubernetes.Clientse
101
101
// dryRunEvictOrDeleteNodePod checks eviction or deletion of Pods on the specified Node can proceed.
102
102
// It returns an error if a running Pod exists or an eviction of the Pod in protected namespace failed.
103
103
func dryRunEvictOrDeleteNodePod (ctx context.Context , cs * kubernetes.Clientset , node string , protected map [string ]bool ) error {
104
- return enumeratePods (ctx , cs , node ,
105
- doEvictOrDeleteNodePod (ctx , cs , node , protected , 0 , time .Duration (0 ), true ),
106
- func (pod * corev1.Pod ) error {
107
- return fmt .Errorf ("job-managed pod exists: %s/%s, phase=%s" , pod .Namespace , pod .Name , pod .Status .Phase )
108
- },
109
- )
104
+ return doEvictOrDeleteNodePod (ctx , cs , node , protected , 0 , 0 , true )
110
105
}
111
106
112
107
// evictOrDeleteNodePod evicts or delete Pods on the specified Node.
113
108
// If a running Job Pod exists, this function returns an error.
114
109
func evictOrDeleteNodePod (ctx context.Context , cs * kubernetes.Clientset , node string , protected map [string ]bool , attempts int , interval time.Duration ) error {
115
- return enumeratePods (ctx , cs , node ,
116
- doEvictOrDeleteNodePod (ctx , cs , node , protected , attempts , interval , false ),
117
- func (pod * corev1.Pod ) error {
118
- return fmt .Errorf ("job-managed pod exists: %s/%s, phase=%s" , pod .Namespace , pod .Name , pod .Status .Phase )
119
- },
120
- )
110
+ return doEvictOrDeleteNodePod (ctx , cs , node , protected , attempts , interval , false )
121
111
}
122
112
123
- // evictOrDeleteOnDeleteDaemonSetPod evicts or delete Pods on the specified Node that are owned by "updateStrategy:OnDelete" DaemonSets.
124
- func evictOrDeleteOnDeleteDaemonSetPod (ctx context.Context , cs * kubernetes.Clientset , node string , protected map [ string ] bool , attempts int , interval time. Duration ) error {
125
- return enumerateOnDeleteDaemonSetPods (ctx , cs , node , doEvictOrDeleteNodePod ( ctx , cs , node , protected , attempts , interval , false ) )
113
+ // deleteOnDeleteDaemonSetPod evicts or delete Pods on the specified Node that are owned by "updateStrategy:OnDelete" DaemonSets.
114
+ func deleteOnDeleteDaemonSetPod (ctx context.Context , cs * kubernetes.Clientset , node string ) error {
115
+ return doDeleteOnDeleteDaemonSetPod (ctx , cs , node )
126
116
}
127
117
128
- // doEvictOrDeleteNodePod returns a pod handler that evicts or delete Pods on the specified Node.
118
+ // doEvictOrDeleteNodePod evicts or delete Pods on the specified Node.
129
119
// It first tries eviction.
130
120
// If the eviction failed and the Pod's namespace is not protected, it deletes the Pod.
131
121
// If the eviction failed and the Pod's namespace is protected, it retries after `interval` interval at most `attempts` times.
122
+ // If a running Job Pod exists, this function returns an error.
132
123
// If `dry` is true, it performs dry run and `attempts` and `interval` are ignored.
133
- func doEvictOrDeleteNodePod (ctx context.Context , cs * kubernetes.Clientset , node string , protected map [string ]bool , attempts int , interval time.Duration , dry bool ) func ( pod * corev1. Pod ) error {
124
+ func doEvictOrDeleteNodePod (ctx context.Context , cs * kubernetes.Clientset , node string , protected map [string ]bool , attempts int , interval time.Duration , dry bool ) error {
134
125
var deleteOptions * metav1.DeleteOptions
135
126
if dry {
136
127
deleteOptions = & metav1.DeleteOptions {
137
128
DryRun : []string {"All" },
138
129
}
139
130
}
140
131
141
- return func (pod * corev1.Pod ) error {
132
+ return enumeratePods ( ctx , cs , node , func (pod * corev1.Pod ) error {
142
133
if dry && ! protected [pod .Namespace ] {
143
134
// in case of dry-run for Pods in non-protected namespace,
144
135
// return immediately because its "eviction or deletion" never fails
@@ -209,7 +200,24 @@ func doEvictOrDeleteNodePod(ctx context.Context, cs *kubernetes.Clientset, node
209
200
return fmt .Errorf ("failed to evict pod %s/%s due to PDB: %w" , pod .Namespace , pod .Name , err )
210
201
}
211
202
return nil
212
- }
203
+ }, func (pod * corev1.Pod ) error {
204
+ return fmt .Errorf ("job-managed pod exists: %s/%s, phase=%s" , pod .Namespace , pod .Name , pod .Status .Phase )
205
+ })
206
+ }
207
+
208
+ // doDeleteOnDeleteDaemonSetPod deletes 'OnDelete' DaemonSet pods on the specified Node.
209
+ func doDeleteOnDeleteDaemonSetPod (ctx context.Context , cs * kubernetes.Clientset , node string ) error {
210
+ return enumerateOnDeleteDaemonSetPods (ctx , cs , node , func (pod * corev1.Pod ) error {
211
+ err := cs .CoreV1 ().Pods (pod .Namespace ).Delete (ctx , pod .Name , metav1.DeleteOptions {})
212
+ if err != nil && ! apierrors .IsNotFound (err ) {
213
+ return err
214
+ }
215
+ log .Info ("deleted daemonset pod" , map [string ]interface {}{
216
+ "namespace" : pod .Namespace ,
217
+ "name" : pod .Name ,
218
+ })
219
+ return nil
220
+ })
213
221
}
214
222
215
223
// checkPodDeletion checks whether the evicted or deleted Pods are eventually deleted.
0 commit comments