mirror of
https://github.com/ysoftdevs/imagepullsecret-injector.git
synced 2026-03-26 19:11:39 +01:00
Refactor serve method
This commit is contained in:
@@ -278,8 +278,8 @@ func (whsvr *WebhookServer) mutateServiceAccount(ar *v1beta1.AdmissionReview) *v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// serve parses the raw incoming request, calls the mutation logic and sends the proper response
|
func parseIncomingRequest(r *http.Request) (v1beta1.AdmissionReview, *errors.StatusError) {
|
||||||
func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
var ar v1beta1.AdmissionReview
|
||||||
var body []byte
|
var body []byte
|
||||||
if r.Body != nil {
|
if r.Body != nil {
|
||||||
if data, err := ioutil.ReadAll(r.Body); err == nil {
|
if data, err := ioutil.ReadAll(r.Body); err == nil {
|
||||||
@@ -287,50 +287,69 @@ func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(body) == 0 {
|
if len(body) == 0 {
|
||||||
glog.Error("empty body")
|
glog.Error("Empty body")
|
||||||
http.Error(w, "empty body", http.StatusBadRequest)
|
return ar, errors.NewBadRequest("Empty body")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify the content type is accurate
|
// verify the content type is accurate
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
if contentType != "application/json" {
|
if contentType != "application/json" {
|
||||||
glog.Errorf("Content-Type=%s, expect application/json", contentType)
|
glog.Errorf("Content-Type=%s, expect application/json", contentType)
|
||||||
http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
|
err := &errors.StatusError{ErrStatus: metav1.Status{
|
||||||
return
|
Status: metav1.StatusFailure,
|
||||||
|
Message: fmt.Sprintf("Content-Type=%s, expect application/json", contentType),
|
||||||
|
Reason: metav1.StatusReasonUnsupportedMediaType,
|
||||||
|
Code: http.StatusUnsupportedMediaType,
|
||||||
|
}}
|
||||||
|
return ar, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var admissionResponse *v1beta1.AdmissionResponse
|
|
||||||
ar := v1beta1.AdmissionReview{}
|
|
||||||
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
|
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
|
||||||
glog.Errorf("Can't decode body: %v", err)
|
glog.Error("Could not parse the request body")
|
||||||
admissionResponse = &v1beta1.AdmissionResponse{
|
return ar, errors.NewBadRequest(fmt.Sprintf("Could not parse the request body: %+v", err))
|
||||||
Result: &metav1.Status{
|
|
||||||
Message: err.Error(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
admissionResponse = whsvr.mutateServiceAccount(&ar)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
admissionReview := v1beta1.AdmissionReview{
|
return ar, nil
|
||||||
TypeMeta: metav1.TypeMeta{Kind: "AdmissionReview", APIVersion: "admission.k8s.io/v1"},
|
}
|
||||||
}
|
|
||||||
if admissionResponse != nil {
|
|
||||||
admissionReview.Response = admissionResponse
|
|
||||||
if ar.Request != nil {
|
|
||||||
admissionReview.Response.UID = ar.Request.UID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func (whsvr *WebhookServer) sendResponse(w http.ResponseWriter, admissionReview v1beta1.AdmissionReview) error {
|
||||||
resp, err := json.Marshal(admissionReview)
|
resp, err := json.Marshal(admissionReview)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Can't encode response: %v", err)
|
glog.Errorf("Can't encode response: %v", err)
|
||||||
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
glog.Infof("Ready to write reponse ...")
|
glog.Infof("Writing response")
|
||||||
if _, err := w.Write(resp); err != nil {
|
if _, err := w.Write(resp); err != nil {
|
||||||
glog.Errorf("Can't write response: %v", err)
|
glog.Errorf("Can't write response: %v", err)
|
||||||
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve parses the raw incoming request, calls the mutation logic and sends the proper response
|
||||||
|
func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
||||||
|
admissionReviewIn, statusErr := parseIncomingRequest(r)
|
||||||
|
if statusErr != nil {
|
||||||
|
http.Error(w, statusErr.ErrStatus.Message, int(statusErr.ErrStatus.Code))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
admissionResponse := whsvr.mutateServiceAccount(&admissionReviewIn)
|
||||||
|
|
||||||
|
admissionReviewOut := v1beta1.AdmissionReview{
|
||||||
|
TypeMeta: metav1.TypeMeta{Kind: "AdmissionReview", APIVersion: "admission.k8s.io/v1"},
|
||||||
|
}
|
||||||
|
if admissionResponse != nil {
|
||||||
|
admissionReviewOut.Response = admissionResponse
|
||||||
|
if admissionReviewIn.Request != nil {
|
||||||
|
admissionReviewOut.Response.UID = admissionReviewIn.Request.UID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := whsvr.sendResponse(w, admissionReviewOut); err != nil {
|
||||||
|
glog.Errorf("Could not send response %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user