func serve()

in pkg/webhook/server.go [68:115]


func serve(w http.ResponseWriter, r *http.Request, ac admissionController, executorFunc requestExecutor) {

	// verify the content type is accurate
	contentType := r.Header.Get("Content-Type")
	if contentType != "application/json" {
		errMessage := fmt.Sprintf("Expected 'application/json' contentType but got '%s'", contentType)
		sendAdmissionResponse(w, requestDeniedBad("", errMessage))
		return
	}

	// Read all the request body content
	bodyBytes, err := io.ReadAll(r.Body)
	if err != nil {
		sendAdmissionResponse(w, requestDeniedBad("", err.Error()))
		return
	}
	klog.V(5).Infof("Received body : %s", string(bodyBytes))

	// Decode the request body content into the AdmissionReview struct
	requestedAdmissionReview := admissionv1.AdmissionReview{}
	_, _, err = scheme.Codecs.UniversalDeserializer().Decode(bodyBytes, nil, &requestedAdmissionReview)
	if err != nil {
		sendAdmissionResponse(w, requestDeniedBad("", err.Error()))
		return
	}
	klog.V(5).Infof("Received review request : %s", requestedAdmissionReview.String())

	if requestedAdmissionReview.APIVersion != "admission.k8s.io/v1" {
		// Only v1 is supported
		errMessage := fmt.Sprintf(
			"Webhook can only handle API version 'admission.k8s.io/v1' but got '%s'",
			requestedAdmissionReview.APIVersion)
		sendAdmissionResponse(w, requestDeniedBad("", errMessage))
		return
	}

	request := requestedAdmissionReview.Request
	if request == nil {
		sendAdmissionResponse(w, requestDeniedBad("", "Received an empty(nil) AdmissionRequest"))
		return
	}

	klog.Infof("Serving request with UID '%s'", request.UID)

	// Review the request and reply
	response := executorFunc(request, ac)
	sendAdmissionResponse(w, response)
}