1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/webhooks/handlers/admission.go
shuting 7170cbb0c2
feat:Webhook config per policy (#9483)
* add spec.webhookConfigurations

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* update crd

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* configure webhook

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* register webhook handler

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* skip storing finegrained policies in cache

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* update resource validate handler

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* updates

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* enable mutate resource handler for fine-grained policies

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix: tests

Signed-off-by: ShutingZhao <shuting@nirmata.com>

---------

Signed-off-by: ShutingZhao <shuting@nirmata.com>
2024-01-27 13:00:22 +00:00

70 lines
2.3 KiB
Go

package handlers
import (
"encoding/json"
"errors"
"io"
"net/http"
"time"
"github.com/go-logr/logr"
"github.com/julienschmidt/httprouter"
admissionv1 "k8s.io/api/admission/v1"
)
func (inner AdmissionHandler) WithAdmission(logger logr.Logger) HttpHandler {
return inner.withAdmission(logger).WithMetrics(logger).WithTrace("ADMISSION")
}
func (inner AdmissionHandler) withAdmission(logger logr.Logger) HttpHandler {
return func(writer http.ResponseWriter, request *http.Request) {
startTime := time.Now()
if request.Body == nil {
HttpError(request.Context(), writer, request, logger, errors.New("empty body"), http.StatusBadRequest)
return
}
defer request.Body.Close()
body, err := io.ReadAll(request.Body)
if err != nil {
HttpError(request.Context(), writer, request, logger, err, http.StatusBadRequest)
return
}
contentType := request.Header.Get("Content-Type")
if contentType != "application/json" {
HttpError(request.Context(), writer, request, logger, errors.New("invalid Content-Type"), http.StatusUnsupportedMediaType)
return
}
var admissionReview admissionv1.AdmissionReview
if err := json.Unmarshal(body, &admissionReview); err != nil {
HttpError(request.Context(), writer, request, logger, err, http.StatusExpectationFailed)
return
}
logger := logger.WithValues(
"gvk", admissionReview.Request.Kind,
"gvr", admissionReview.Request.Resource,
"namespace", admissionReview.Request.Namespace,
"name", admissionReview.Request.Name,
"operation", admissionReview.Request.Operation,
"uid", admissionReview.Request.UID,
"user", admissionReview.Request.UserInfo,
)
params := httprouter.ParamsFromContext(request.Context())
admissionRequest := AdmissionRequest{
AdmissionRequest: *admissionReview.Request,
URLParams: params.ByName("policy"),
}
admissionResponse := inner(request.Context(), logger, admissionRequest, startTime)
admissionReview.Response = &admissionResponse
responseJSON, err := json.Marshal(admissionReview)
if err != nil {
HttpError(request.Context(), writer, request, logger, err, http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := writer.Write(responseJSON); err != nil {
HttpError(request.Context(), writer, request, logger, err, http.StatusInternalServerError)
return
}
}
}