Skip to content
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

chore: remove decoder injector interface from webhook #1563

Merged
merged 6 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 2 additions & 8 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 @@ -126,12 +126,6 @@ func (a *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
// PodMutatingWebhook implements admission.DecoderInjector.
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
// 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