Watch
Source: hello-pepr-watch
Pepr Watch() actions are ideal for asynchronous operations that need to respond to resource changes without timeout constraints.
Use Watch when you need to:
Section titled “Use Watch when you need to:”- Monitor existing resources in a cluster
- Perform long-running operations without timeout constraints
- React to resource lifecycle events (creation, updates, deletion)
Code Snippet Examples
Section titled “Code Snippet Examples”This example watches Secret resources and logs different lifecycle events: create, update, delete, and create-or-update.
View full example on Github
Watch Create Events
Section titled “Watch Create Events”When(a.Secret).IsCreated() .InNamespace(name) .WithName("create-me") .Watch((instance) => { Log.info(`Watched ${instance.metadata?.name}: create`); });Example pod log output:
Section titled “Example pod log output:”{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-me: create"}Watch Create or Update Events
Section titled “Watch Create or Update Events”When(a.Secret) .IsCreatedOrUpdated() .InNamespace(name) .WithName("create-or-update-me") .Watch((instance, phase) => { Log.info(`Watched ${instance.metadata?.name}: ${phase}`); });Example pod log output:
Section titled “Example pod log output:”{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: ADDED"}Watch Update Events
Section titled “Watch Update Events”When(a.Secret) .IsUpdated() .InNamespace(name) .WithName("update-me") .Watch((instance) => { Log.info(`Watched ${instance.metadata?.name}: update`); });Example pod log output:
Section titled “Example pod log output:”{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: update"}Watch Delete Events
Section titled “Watch Delete Events”When(a.Secret) .IsDeleted() .InNamespace(name) .WithName("delete-me") .Watch((instance) => { Log.info(`Watched ${instance.metadata?.name}: delete`); });Example pod log output:
Section titled “Example pod log output:”{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Watched create-or-update-me: delete"}