|
| 1 | +package webhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/runtime/inject" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 12 | +) |
| 13 | + |
| 14 | +// ObjectMutator specifies the interface for an object mutating webhook. |
| 15 | +type ObjectMutator interface { |
| 16 | + // MutateObject mutates the runtime.Object of a admission.Request |
| 17 | + MutateObject(context.Context, admission.Request, runtime.Object) error |
| 18 | +} |
| 19 | + |
| 20 | +// wrapAsMutator wrap the given ObjectMutator as generic Mutator |
| 21 | +func wrapAsMutator(mutator ObjectMutator) Mutator { |
| 22 | + return &objectMutatorAdapter{mutator} |
| 23 | +} |
| 24 | + |
| 25 | +type objectMutatorAdapter struct { |
| 26 | + ObjectMutator |
| 27 | +} |
| 28 | + |
| 29 | +// Mutate implements the Mutator interface. |
| 30 | +func (a *objectMutatorAdapter) Mutate(ctx context.Context, req admission.Request) admission.Response { |
| 31 | + return MutateObjectByFunc(ctx, req, a.ObjectMutator.MutateObject) |
| 32 | +} |
| 33 | + |
| 34 | +// InjectDecoder implements the admission.DecoderInjector interface. |
| 35 | +func (a *objectMutatorAdapter) InjectDecoder(decoder *admission.Decoder) error { |
| 36 | + // pass decoder to the underlying handler |
| 37 | + if injector, ok := a.ObjectMutator.(admission.DecoderInjector); ok { |
| 38 | + return injector.InjectDecoder(decoder) |
| 39 | + } |
| 40 | + |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// InjectClient implements the inject.Client interface. |
| 45 | +func (a *objectMutatorAdapter) InjectClient(client client.Client) error { |
| 46 | + // pass client to the underlying handler |
| 47 | + if injector, ok := a.ObjectMutator.(inject.Client); ok { |
| 48 | + return injector.InjectClient(client) |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// ensure MutatingObjectWebhook implements ObjectMutator |
| 55 | +var _ ObjectMutator = &MutatingObjectWebhook{} |
| 56 | + |
| 57 | +// MutatingObjectWebhook is a simplified mutating admission webhook. |
| 58 | +type MutatingObjectWebhook struct { |
| 59 | + InjectedClient |
| 60 | + InjectedDecoder |
| 61 | +} |
| 62 | + |
| 63 | +// MutateObject implements the ObjectMutator interface. |
| 64 | +func (w *MutatingObjectWebhook) MutateObject(_ context.Context, _ admission.Request, _ runtime.Object) error { |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// MutateObjectFunc is a functional interface for an object mutating admission webhook. |
| 69 | +type MutateObjectFunc struct { |
| 70 | + MutatingWebhook |
| 71 | + |
| 72 | + Func func(context.Context, admission.Request, runtime.Object) error |
| 73 | +} |
| 74 | + |
| 75 | +// Mutate implements the Mutator interface by calling the Func using the request's runtime.Object. |
| 76 | +func (m *MutateObjectFunc) Mutate(ctx context.Context, req admission.Request) admission.Response { |
| 77 | + if m.Func != nil { |
| 78 | + return MutateObjectByFunc(ctx, req, m.Func) |
| 79 | + } |
| 80 | + |
| 81 | + return m.MutatingWebhook.Mutate(ctx, req) |
| 82 | +} |
| 83 | + |
| 84 | +// MutateObjectByFunc yields and admission.Response for a runtime.Object mutated bu the specified function. |
| 85 | +func MutateObjectByFunc(ctx context.Context, req admission.Request, f func(context.Context, admission.Request, runtime.Object) error) admission.Response { |
| 86 | + obj := req.Object.Object |
| 87 | + err := f(ctx, req, obj) |
| 88 | + if err != nil { |
| 89 | + return admission.Errored(http.StatusInternalServerError, err) |
| 90 | + } |
| 91 | + |
| 92 | + marshalled, err := json.Marshal(obj) |
| 93 | + if err != nil { |
| 94 | + return admission.Errored(http.StatusInternalServerError, err) |
| 95 | + } |
| 96 | + |
| 97 | + return admission.PatchResponseFromRaw(req.Object.Raw, marshalled) |
| 98 | +} |
0 commit comments