Skip to content

Commit

Permalink
Add Request usage example in the webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Sep 4, 2023
1 parent 6240c17 commit db9989c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions kubernetes/controller/webhook/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ func SetupWebhookWithManager(mgr ctrl.Manager) error {
type podAnnotator struct{}

// Default ...
func (a *podAnnotator) Default(_ context.Context, obj runtime.Object) error {
func (a *podAnnotator) Default(ctx context.Context, obj runtime.Object) error {
pod, ok := obj.(*corev1.Pod)
if !ok {
return fmt.Errorf("expected a Pod but got a %T", obj)
}
req, err := admission.RequestFromContext(ctx)
if err != nil {
return err
}

if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pod.Annotations["example-mutating-admission-webhook"] = "foo"
pod.Annotations["example-mutating-admission-webhook"] = req.UserInfo.Username
return nil
}

Expand Down

0 comments on commit db9989c

Please sign in to comment.