<p>We are interested in running certain commands as pods and services, as they start or stop. Using the life-cycle hooks in the yml files does not work for us, since these commands are not optional. We have considered running a watcher pod that uses the watch api to run these commands. But we can't figure out how to use the watch api so that it does not keep sending the same events again and again. Is there a way to tell the watch api to only send new events since connection was opened? If expecting a stateful watch api is unreasonable, will it be possible to pass it a timestamp or a monotonically increasing id to avoid getting already seen events?</p>

<p>Basically what we are doing now we are running a pod with a daemon process that communicates with the api. we can find the events as stream. But we are interested to run some task when a pod created or deleted.</p>

<p>I have found the answer. In case anyone else is watching.</p>

<p>There is a much better system to watch resource and handle events with custom tasks with <code>pkg/controller/framework</code> <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/framework/controller.go" rel="noreferrer">package</a></p>

<p>I found the step like this,</p>

<pre><code>1. initiate a framework.NewInFormer
2. Run the controller
3. the NewInFormer loads with your custom event handlers that will call when the events occured.
</code></pre>
