-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚠️ webhook redesign for generic case #323
Changes from all commits
ab62d27
f0ec881
eee0406
5963ad2
14f4abf
6dbde68
c7969dc
66be336
9a7de90
f46c199
027ce71
1859301
107c8e9
e08eee7
65a93df
1a4bbe1
a866ee9
34a4153
286b0b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,67 +23,51 @@ import ( | |
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types" | ||
) | ||
|
||
// podAnnotator annotates Pods | ||
type podAnnotator struct { | ||
client client.Client | ||
decoder types.Decoder | ||
decoder *admission.Decoder | ||
} | ||
|
||
// Implement admission.Handler so the controller can handle admission request. | ||
var _ admission.Handler = &podAnnotator{} | ||
|
||
// podAnnotator adds an annotation to every incoming pods. | ||
func (a *podAnnotator) Handle(ctx context.Context, req types.Request) types.Response { | ||
func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
pod := &corev1.Pod{} | ||
|
||
err := a.decoder.Decode(req, pod) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusBadRequest, err) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
err = a.mutatePodsFn(ctx, pod) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusInternalServerError, err) | ||
if pod.Annotations == nil { | ||
pod.Annotations = map[string]string{} | ||
} | ||
pod.Annotations["example-mutating-admission-webhook"] = "foo" | ||
|
||
marshaledPod, err := json.Marshal(pod) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusInternalServerError, err) | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaledPod) | ||
} | ||
|
||
// mutatePodsFn add an annotation to the given pod | ||
func (a *podAnnotator) mutatePodsFn(ctx context.Context, pod *corev1.Pod) error { | ||
mengqiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if pod.Annotations == nil { | ||
pod.Annotations = map[string]string{} | ||
} | ||
pod.Annotations["example-mutating-admission-webhook"] = "foo" | ||
return nil | ||
} | ||
|
||
// podAnnotator implements inject.Client. | ||
// A client will be automatically injected. | ||
var _ inject.Client = &podAnnotator{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really add much, and necessitates an extra import. I can add it back in if we really want it, but I think the comment suffices here. |
||
|
||
// InjectClient injects the client. | ||
func (v *podAnnotator) InjectClient(c client.Client) error { | ||
v.client = c | ||
func (a *podAnnotator) InjectClient(c client.Client) error { | ||
a.client = c | ||
return nil | ||
} | ||
|
||
// podAnnotator implements inject.Decoder. | ||
// A decoder will be automatically injected. | ||
var _ inject.Decoder = &podAnnotator{} | ||
|
||
// InjectDecoder injects the decoder. | ||
func (v *podAnnotator) InjectDecoder(d types.Decoder) error { | ||
v.decoder = d | ||
func (a *podAnnotator) InjectDecoder(d *admission.Decoder) error { | ||
a.decoder = d | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on readability
admission.{Request, Response}
ashttp.{Request, Response}