in ray-on-gke/tpu/kuberay-tpu-webhook/main.go [92:125]
func (t *TPUWebhookServer) Mutate(w http.ResponseWriter, r *http.Request) {
t.cacheMutex.Lock()
defer t.cacheMutex.Unlock()
admissionReview := &admissionv1.AdmissionReview{}
if err := json.NewDecoder(r.Body).Decode(admissionReview); err != nil {
http.Error(w, "Error decoding request body", http.StatusBadRequest)
w.WriteHeader(http.StatusBadRequest)
return
}
if admissionReview.Request == nil || admissionReview.Request.Kind.Kind != "Pod" {
http.Error(w, "Invalid Kind", http.StatusBadRequest)
w.WriteHeader(http.StatusBadRequest)
return
}
klog.V(0).InfoS("Mutate", "Received review for Pod creation: %s", admissionReview.Request.Name)
response, err := t.mutatePod(admissionReview)
if err != nil {
klog.Errorf("Failed to mutate Pod: %s", err)
http.Error(w, "Failed to mutate Pod", http.StatusForbidden)
w.WriteHeader(http.StatusForbidden)
return
}
admissionReview.Response = response
responseBytes, err := json.Marshal(admissionReview)
if err != nil {
klog.Errorf("Failed to encode response: %s", err)
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(responseBytes))
}