Skip to content

Commit 0876e8e

Browse files
committed
Update README.md
1 parent e15d222 commit 0876e8e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,44 @@ if err = (&pod.Webhook{}).SetupWebhookWithManager(mgr); err != nil {
5555
setupLog.Error(err, "unable to create webhook", "webhook", "Pod")
5656
os.Exit(1)
5757
}
58+
```
59+
60+
## Examples
61+
### Mutating admission webhook using `MutateObjectByFunc`
62+
```go
63+
package pod
64+
65+
import (
66+
"context"
67+
68+
corev1 "k8s.io/api/core/v1"
69+
"k8s.io/apimachinery/pkg/runtime"
70+
"sigs.k8s.io/controller-runtime/pkg/log"
71+
"sigs.k8s.io/controller-runtime/pkg/manager"
72+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
73+
74+
"github.com/snorwin/k8s-generic-webhook/pkg/webhook"
75+
)
76+
77+
type Webhook struct {
78+
webhook.MutatingWebhook
79+
}
80+
81+
func (w *Webhook) SetupWebhookWithManager(mgr manager.Manager) error {
82+
return webhook.NewGenericWebhookManagedBy(mgr).
83+
For(&corev1.Pod{}).
84+
Complete(w)
85+
}
86+
87+
func (w *Webhook) Mutate(ctx context.Context, req admission.Request) admission.Response {
88+
return webhook.MutateObjectByFunc(ctx, req, func(ctx context.Context, request admission.Request, object runtime.Object) error {
89+
_ = log.FromContext(ctx)
90+
91+
pod := object.(*corev1.Pod)
92+
// TODO add your programmatic mutation logic here
93+
object = pod
94+
95+
return nil
96+
})
97+
}
5898
```

0 commit comments

Comments
 (0)