title: Watch and wait for POD deletion with Golang k8s client
body: <p>I need to watch (and wait) until a POD is deleted. I need to this is because I need to start a second pod (with the same name) immediately after the first one has been deleted.</p>
  <p>This is what I'm trying:</p>
  <pre><code>func (k *k8sClient) waitPodDeleted(ctx context.Context, resName string) error {
      watcher, err := k.createPodWatcher(ctx, resName)
      if err != nil {
          return err
      }
  
      defer watcher.Stop()
  
      for {
          select {
          case event := &lt;-watcher.ResultChan():
  
              if event.Type == watch.Deleted {
                  k.logger.Debugf(&quot;The POD \&quot;%s\&quot; is deleted&quot;, resName)
  
                  return nil
              }
  
          case &lt;-ctx.Done():
              k.logger.Debugf(&quot;Exit from waitPodDeleted for POD \&quot;%s\&quot; because the context is done&quot;, resName)
              return nil
          }
      }
  }
  </code></pre>
  <p>The problem with this approach is that when I get the <code>Deleted</code> event, is when the POD receives the event for deletion, but not when it is actually deleted. Doing some extra tests I ended debugging the process with this code:</p>
  <pre><code>case event := &lt;-watcher.ResultChan():
  
      if event.Type == watch.Deleted {
          pod := event.Object.(*v1.Pod)
          k.logger.Debugf(&quot;EVENT %s PHASE %s MESSAGE %s&quot;, event.Type, pod.Status.Phase, pod.Status.Message)
      }
  </code></pre>
  <p>The log result for this is:</p>
  <pre><code>2022-02-15T08:21:51 DEBUG EVENT DELETED PHASE Running MESSAGE
  2022-02-15T08:22:21 DEBUG EVENT DELETED PHASE Running MESSAGE
  </code></pre>
  <p>I'm getting two Deleted events. The first one right away I send the delete command. The last one when the pod is effectively deleted from the cluster.</p>
  <p>My questions are:</p>
  <ul>
  <li>Why I'm getting two Deleted events? How can I differentiate one from another? I've tried to compare the two events and they seems exactly the same (except the timestamps)</li>
  <li>What is the best approach to watch and wait for a pod deletion, so I can immediately relaunch it? should I poll the API until the pod is not returned?</li>
  </ul>
  <p>The usecase I'm trying to solve:
  In my application there is a feature to replace a tool with another with different options. The feature needs to delete the pod that contains the tool and relaunch it with another set of options. In this scenario I need to wait for the pod deletion so I can start it again.</p>
  <p>Thanks in advance!</p>