Skip to content

Commit

Permalink
chore: remove decoder injector interface from webhook (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey1330 authored Jun 15, 2023
1 parent 8b62f33 commit 7850766
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
9 changes: 9 additions & 0 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
ctrlWebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var (
Expand Down Expand Up @@ -341,12 +342,20 @@ func main() {
)

setupLog.Info("starting webhook and manager")

decoder, err := admission.NewDecoder(mgr.GetScheme())
if err != nil {
setupLog.Error(err, "unable to initialize decoder")
os.Exit(1)
}

if err := webhookBuilder.Run(mgr, map[string]*ctrlWebhook.Admission{
"/mutate-v1-pod": {
Handler: &pod_mutator.PodMutatingWebhook{
Client: mgr.GetClient(),
Tracer: otel.Tracer("keptn/webhook"),
Recorder: mgr.GetEventRecorderFor("keptn/webhook"),
Decoder: decoder,
Log: ctrl.Log.WithName("Mutating Webhook"),
},
},
Expand Down
13 changes: 2 additions & 11 deletions operator/webhooks/pod_mutator/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
type PodMutatingWebhook struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand All @@ -65,7 +65,7 @@ func (a *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)

pod := &corev1.Pod{}

err := a.decoder.Decode(req, pod)
err := a.Decoder.Decode(req, pod)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down Expand Up @@ -123,15 +123,6 @@ func (a *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
}

// PodMutatingWebhook implements admission.DecoderInjector.
// A decoder will be automatically injected.

// InjectDecoder injects the decoder.
func (a *PodMutatingWebhook) InjectDecoder(d *admission.Decoder) error {
a.decoder = d
return nil
}

func (a *PodMutatingWebhook) isPodAnnotated(pod *corev1.Pod) bool {
_, gotWorkloadAnnotation := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
_, gotVersionAnnotation := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
Expand Down
36 changes: 18 additions & 18 deletions operator/webhooks/pod_mutator/pod_mutating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPodMutatingWebhook_getOwnerReference(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestPodMutatingWebhook_getOwnerReference(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand All @@ -101,7 +101,7 @@ func TestPodMutatingWebhook_getAppName(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestPodMutatingWebhook_getAppName(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand All @@ -177,7 +177,7 @@ func TestPodMutatingWebhook_getWorkloadName(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestPodMutatingWebhook_getWorkloadName(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func TestPodMutatingWebhook_isPodAnnotated(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func TestPodMutatingWebhook_isPodAnnotated(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestPodMutatingWebhook_copyAnnotationsIfParentAnnotated(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -692,7 +692,7 @@ func TestPodMutatingWebhook_copyAnnotationsIfParentAnnotated(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand All @@ -708,7 +708,7 @@ func TestPodMutatingWebhook_copyResourceLabelsIfPresent(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -851,7 +851,7 @@ func TestPodMutatingWebhook_copyResourceLabelsIfPresent(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand All @@ -870,7 +870,7 @@ func TestPodMutatingWebhook_isAppAnnotationPresent(t *testing.T) {
type fields struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
Expand Down Expand Up @@ -931,7 +931,7 @@ func TestPodMutatingWebhook_isAppAnnotationPresent(t *testing.T) {
a := &PodMutatingWebhook{
Client: tt.fields.Client,
Tracer: tt.fields.Tracer,
decoder: tt.fields.decoder,
Decoder: tt.fields.Decoder,
Recorder: tt.fields.Recorder,
Log: tt.fields.Log,
}
Expand Down Expand Up @@ -965,7 +965,7 @@ func TestPodMutatingWebhook_Handle_DisabledNamespace(t *testing.T) {
wh := &PodMutatingWebhook{
Client: fakeClient,
Tracer: tr,
decoder: decoder,
Decoder: decoder,
Recorder: recorder,
Log: testr.New(t),
}
Expand Down Expand Up @@ -1030,7 +1030,7 @@ func TestPodMutatingWebhook_Handle_SingleService(t *testing.T) {
wh := &PodMutatingWebhook{
Client: fakeClient,
Tracer: tr,
decoder: decoder,
Decoder: decoder,
Recorder: recorder,
Log: testr.New(t),
}
Expand Down Expand Up @@ -1153,7 +1153,7 @@ func TestPodMutatingWebhook_Handle_SingleService_AppCreationRequestAlreadyPresen
wh := &PodMutatingWebhook{
Client: fakeClient,
Tracer: tr,
decoder: decoder,
Decoder: decoder,
Recorder: recorder,
Log: testr.New(t),
}
Expand Down Expand Up @@ -1263,7 +1263,7 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) {
wh := &PodMutatingWebhook{
Client: fakeClient,
Tracer: tr,
decoder: decoder,
Decoder: decoder,
Recorder: recorder,
Log: testr.New(t),
}
Expand Down

0 comments on commit 7850766

Please sign in to comment.