Skip to content

Watch

Source: hello-pepr-watch

Pepr Watch() actions are ideal for asynchronous operations that need to respond to resource changes without timeout constraints.

  • Monitor existing resources in a cluster
  • Perform long-running operations without timeout constraints
  • React to resource lifecycle events (creation, updates, deletion)

This example watches Secret resources and logs different lifecycle events: create, update, delete, and create-or-update.

View full example on Github

When(a.Secret)
.IsCreated()
.InNamespace(name)
.WithName("create-me")
.Watch((instance) => {
Log.info(`Watched ${instance.metadata?.name}: create`);
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-me: create"}
When(a.Secret)
.IsCreatedOrUpdated()
.InNamespace(name)
.WithName("create-or-update-me")
.Watch((instance, phase) => {
Log.info(`Watched ${instance.metadata?.name}: ${phase}`);
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: ADDED"}
When(a.Secret)
.IsUpdated()
.InNamespace(name)
.WithName("update-me")
.Watch((instance) => {
Log.info(`Watched ${instance.metadata?.name}: update`);
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: update"}
When(a.Secret)
.IsDeleted()
.InNamespace(name)
.WithName("delete-me")
.Watch((instance) => {
Log.info(`Watched ${instance.metadata?.name}: delete`);
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: delete"}