Skip to content

Mutate

Source: hello-pepr-mutate

Pepr Mutate() action is used to modify Kubernetes objects during admission. Mutations allow you to enforce defaults, apply standard labels/annotations, or clean up unwanted metadata before the resource is persisted.

Use Mutate() when you need to:

  • Apply default labels or annotations to resources as they are created.
  • Remove unwanted metadata automatically (labels, annotations, fields).
  • Normalize user input so objects follow your standards.
  • Enforce consistent cluster hygiene by modifying objects before they are stored.
  • Transform objects while still allowing them through.

This example mutates 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-yay")
.Mutate(function createYay() {
Log.info("Mutate: create-yay");
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Mutate: create-yay"}
When(a.Secret)
.IsCreatedOrUpdated()
.InNamespace(name)
.WithName("cou-create-yay")
.Mutate(function couCreateYay() {
Log.info("Mutate: cou-create-yay");
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Mutate: cou-create-yay"}
When(a.Secret)
.IsUpdated()
.InNamespace(name)
.WithName("update-yay")
.Mutate(function updateYay() {
Log.info("Mutate: update-yay");
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Mutate: update-yay"}
When(a.Secret)
.IsDeleted()
.InNamespace(name)
.WithName("delete-yay")
.Mutate(function deleteYay() {
Log.info("Mutate: delete-yay");
});
{"level":30,"time":<timestamp>,"pid":<pid>,"hostname":"pepr-<hostname>","msg":"Mutate: delete-yay"}