Skip to content

Commit

Permalink
perf:add error info
Browse files Browse the repository at this point in the history
  • Loading branch information
qinguoyi committed Jan 29, 2024
1 parent 39352d6 commit 8b58cf6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions injector/injector_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
} else {
err = fmt.Errorf("fail to read request body: %v", err)
klog.Error(err)
return
}
}

// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
klog.Errorf("contentType=%s, expect application/json", contentType)
err := fmt.Errorf("contentType=%s, expect application/json", contentType)
klog.Error(err)
return
}

Expand All @@ -50,6 +55,7 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {

deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(body, nil, &requestedAdmissionReview); err != nil {
err = fmt.Errorf("fail to decode admission request: %v", err)
klog.Error(err)
responseAdmissionReview.Response = toAdmissionResponse(err)
} else {
Expand All @@ -64,9 +70,11 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {

respBytes, err := json.Marshal(responseAdmissionReview)
if err != nil {
err = fmt.Errorf("fail to return json encoding of admission response: %v", contentType)
klog.Error(err)
}
if _, err := w.Write(respBytes); err != nil {
if _, err = w.Write(respBytes); err != nil {
err = fmt.Errorf("fail to write the data to the connection: %v", err)
klog.Error(err)
}
}
Expand Down
1 change: 1 addition & 0 deletions injector/logsidecar_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func MutateLogsidecarPods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse
pod := corev1.Pod{}
deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
err = fmt.Errorf("fail to decode admission request: %v", err)
klog.Error(err)
return toAdmissionResponse(err)
}
Expand Down

0 comments on commit 8b58cf6

Please sign in to comment.