Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / webhook / admission / http.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package admission
18
19 import (
20         "context"
21         "encoding/json"
22         "errors"
23         "fmt"
24         "io"
25         "io/ioutil"
26         "net/http"
27         "time"
28
29         "k8s.io/api/admission/v1beta1"
30         admissionv1beta1 "k8s.io/api/admission/v1beta1"
31         "k8s.io/apimachinery/pkg/runtime"
32         "k8s.io/apimachinery/pkg/runtime/serializer"
33         utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34         "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
35         "sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
36 )
37
38 var admissionv1beta1scheme = runtime.NewScheme()
39 var admissionv1beta1schemecodecs = serializer.NewCodecFactory(admissionv1beta1scheme)
40
41 func init() {
42         addToScheme(admissionv1beta1scheme)
43 }
44
45 func addToScheme(scheme *runtime.Scheme) {
46         utilruntime.Must(admissionv1beta1.AddToScheme(scheme))
47 }
48
49 var _ http.Handler = &Webhook{}
50
51 func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
52         startTS := time.Now()
53         defer metrics.RequestLatency.WithLabelValues(wh.Name).Observe(time.Now().Sub(startTS).Seconds())
54
55         var body []byte
56         var err error
57
58         var reviewResponse types.Response
59         if r.Body != nil {
60                 if body, err = ioutil.ReadAll(r.Body); err != nil {
61                         log.Error(err, "unable to read the body from the incoming request")
62                         reviewResponse = ErrorResponse(http.StatusBadRequest, err)
63                         wh.writeResponse(w, reviewResponse)
64                         return
65                 }
66         } else {
67                 err = errors.New("request body is empty")
68                 log.Error(err, "bad request")
69                 reviewResponse = ErrorResponse(http.StatusBadRequest, err)
70                 wh.writeResponse(w, reviewResponse)
71                 return
72         }
73
74         // verify the content type is accurate
75         contentType := r.Header.Get("Content-Type")
76         if contentType != "application/json" {
77                 err = fmt.Errorf("contentType=%s, expect application/json", contentType)
78                 log.Error(err, "unable to process a request with an unknown content type", "content type", contentType)
79                 reviewResponse = ErrorResponse(http.StatusBadRequest, err)
80                 wh.writeResponse(w, reviewResponse)
81                 return
82         }
83
84         ar := v1beta1.AdmissionReview{}
85         if _, _, err := admissionv1beta1schemecodecs.UniversalDeserializer().Decode(body, nil, &ar); err != nil {
86                 log.Error(err, "unable to decode the request")
87                 reviewResponse = ErrorResponse(http.StatusBadRequest, err)
88                 wh.writeResponse(w, reviewResponse)
89                 return
90         }
91
92         // TODO: add panic-recovery for Handle
93         reviewResponse = wh.Handle(context.Background(), types.Request{AdmissionRequest: ar.Request})
94         wh.writeResponse(w, reviewResponse)
95 }
96
97 func (wh *Webhook) writeResponse(w io.Writer, response types.Response) {
98         if response.Response.Result.Code != 0 {
99                 if response.Response.Result.Code == http.StatusOK {
100                         metrics.TotalRequests.WithLabelValues(wh.Name, "true").Inc()
101                 } else {
102                         metrics.TotalRequests.WithLabelValues(wh.Name, "false").Inc()
103                 }
104         }
105
106         encoder := json.NewEncoder(w)
107         responseAdmissionReview := v1beta1.AdmissionReview{
108                 Response: response.Response,
109         }
110         err := encoder.Encode(responseAdmissionReview)
111         if err != nil {
112                 log.Error(err, "unable to encode the response")
113                 wh.writeResponse(w, ErrorResponse(http.StatusInternalServerError, err))
114         }
115 }