in pkg/admission/admission_controller.go [630:697]
func (c *AdmissionController) Serve(w http.ResponseWriter, r *http.Request) {
log.Log(log.Admission).Debug("request", zap.Any("httpRequest", r))
var body []byte
if r.Body != nil {
var err error
body, err = io.ReadAll(r.Body)
if err != nil || len(body) == 0 {
log.Log(log.Admission).Debug("illegal request received: body invalid", zap.Error(err))
http.Error(w, "empty or invalid body", http.StatusBadRequest)
return
}
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
log.Log(log.Admission).Debug("illegal request received: invalid content type", zap.String("requested content type", contentType))
http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
return
}
urlPath := r.URL.Path
if urlPath != mutateURL && urlPath != validateConfURL {
log.Log(log.Admission).Debug("unsupported request received", zap.String("urlPath", urlPath))
http.Error(w, "request is neither mutation nor validation", http.StatusNotFound)
return
}
ar := admissionv1.AdmissionReview{TypeMeta: metav1.TypeMeta{
APIVersion: admissionReviewAPIVersion,
Kind: admissionReviewKind,
}}
var admissionResponse *admissionv1.AdmissionResponse
_, _, err := deserializer.Decode(body, nil, &ar)
if err != nil || ar.Request == nil {
log.Log(log.Admission).Error("request body decode failed or request empty", zap.Error(err))
admissionResponse = admissionResponseBuilder("yunikorn-invalid-body", false, "body decode failed", nil)
} else {
req := ar.Request
switch urlPath {
case mutateURL:
admissionResponse = c.mutate(req)
case validateConfURL:
admissionResponse = c.validateConf(req)
}
}
admissionReview := admissionv1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
APIVersion: admissionReviewAPIVersion,
Kind: admissionReviewKind,
},
Response: admissionResponse,
}
var resp []byte
resp, err = json.Marshal(admissionReview)
if err != nil {
errMessage := fmt.Sprintf("could not encode response: %v", err)
log.Log(log.Admission).Error(errMessage)
http.Error(w, errMessage, http.StatusInternalServerError)
}
if _, err = w.Write(resp); err != nil {
errMessage := fmt.Sprintf("could not write response: %v", err)
log.Log(log.Admission).Error(errMessage)
http.Error(w, errMessage, http.StatusInternalServerError)
}
}