func()

in controller/webhook.go [76:134]


func (whsvr *WebhookServer) Handler(writer http.ResponseWriter, request *http.Request) {
	if request.Body == nil {
		fmt.Errorf("Error: empty request body")
		http.Error(writer, "Empty request body", http.StatusBadRequest)
		return
	}

	if request.Header.Get("Content-Type") != "application/json" {
		fmt.Errorf("Invalid Content-Type %s, expected application/json", request.Header.Get("Content-Type"))
		http.Error(writer, "Invalid Content-Type, expected application/json", http.StatusUnsupportedMediaType)
		return
	}

	body, err := ioutil.ReadAll(request.Body)

	if err != nil {
		fmt.Errorf("Error reading body: %v", err)
		http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	admissionReview := v1beta1.AdmissionReview{}

	err = json.Unmarshal(body, &admissionReview)

	if err != nil {
		fmt.Errorf("Error unmarshaling body: %v", err)
		http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	var admissionResponse *v1beta1.AdmissionResponse

	admissionResponse, err = whsvr.mutate(request.Context(), &admissionReview)

	if err != nil {
		fmt.Errorf("Error mutating AdmissionReview: %v", err)
		http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	if admissionResponse != nil {
		admissionReview.Response = admissionResponse
	}

	response, err := json.Marshal(admissionReview)

	if err != nil {
		fmt.Errorf("Error encoding response: %v", err)
		http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	if _, err := writer.Write(response); err != nil {
		fmt.Errorf("Error writing response: %v", err)
		http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
		return
	}
}