@@ -101,44 +101,35 @@ func enumerateOnDeleteDaemonSetPods(ctx context.Context, cs *kubernetes.Clientse
101101// dryRunEvictOrDeleteNodePod checks eviction or deletion of Pods on the specified Node can proceed.
102102// It returns an error if a running Pod exists or an eviction of the Pod in protected namespace failed.
103103func 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 )
110105}
111106
112107// evictOrDeleteNodePod evicts or delete Pods on the specified Node.
113108// If a running Job Pod exists, this function returns an error.
114109func 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 )
121111}
122112
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 )
126116}
127117
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.
129119// It first tries eviction.
130120// If the eviction failed and the Pod's namespace is not protected, it deletes the Pod.
131121// 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.
132123// 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 {
134125 var deleteOptions * metav1.DeleteOptions
135126 if dry {
136127 deleteOptions = & metav1.DeleteOptions {
137128 DryRun : []string {"All" },
138129 }
139130 }
140131
141- return func (pod * corev1.Pod ) error {
132+ return enumeratePods ( ctx , cs , node , func (pod * corev1.Pod ) error {
142133 if dry && ! protected [pod .Namespace ] {
143134 // in case of dry-run for Pods in non-protected namespace,
144135 // return immediately because its "eviction or deletion" never fails
@@ -209,7 +200,24 @@ func doEvictOrDeleteNodePod(ctx context.Context, cs *kubernetes.Clientset, node
209200 return fmt .Errorf ("failed to evict pod %s/%s due to PDB: %w" , pod .Namespace , pod .Name , err )
210201 }
211202 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+ })
213221}
214222
215223// checkPodDeletion checks whether the evicted or deleted Pods are eventually deleted.
0 commit comments