...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/logging_handler.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package webhook
    16  
    17  import (
    18  	"context"
    19  
    20  	"k8s.io/apimachinery/pkg/types"
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  	"sigs.k8s.io/controller-runtime/pkg/log"
    23  	"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
    24  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    25  )
    26  
    27  var logger = log.Log
    28  
    29  type RequestLoggingHandler struct {
    30  	handler     admission.Handler
    31  	handlerName string
    32  }
    33  
    34  var _ inject.Client = &RequestLoggingHandler{}
    35  
    36  func NewRequestLoggingHandler(handler admission.Handler, handlerName string) *RequestLoggingHandler {
    37  	return &RequestLoggingHandler{
    38  		handler:     handler,
    39  		handlerName: handlerName,
    40  	}
    41  }
    42  
    43  func (a *RequestLoggingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
    44  	nn := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
    45  	logger.Info("processing request",
    46  		"operation", req.Operation,
    47  		"handler", a.handlerName,
    48  		"kind", req.Kind.Kind,
    49  		"resource", nn)
    50  	response := a.handler.Handle(ctx, req)
    51  	if response.Result == nil {
    52  		logger.Info("done processing request",
    53  			"operation", req.Operation,
    54  			"handler", a.handlerName,
    55  			"kind", req.Kind.Kind,
    56  			"resource", nn)
    57  	} else {
    58  		logger.Info("done processing request",
    59  			"operation", req.Operation,
    60  			"handler", a.handlerName,
    61  			"kind", req.Kind.Kind,
    62  			"resource", nn,
    63  			"result-code", response.Result.Code,
    64  			"result-reason", response.Result.Reason)
    65  	}
    66  	return response
    67  }
    68  
    69  // InjectClient is called by controller-runtime to inject a client into the handler
    70  func (a *RequestLoggingHandler) InjectClient(c client.Client) error {
    71  	injectClient, ok := a.handler.(inject.Client)
    72  	if !ok {
    73  		return nil
    74  	}
    75  	return injectClient.InjectClient(c)
    76  }
    77  

View as plain text