2 Copyright 2018 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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"
38 var admissionv1beta1scheme = runtime.NewScheme()
39 var admissionv1beta1schemecodecs = serializer.NewCodecFactory(admissionv1beta1scheme)
42 addToScheme(admissionv1beta1scheme)
45 func addToScheme(scheme *runtime.Scheme) {
46 utilruntime.Must(admissionv1beta1.AddToScheme(scheme))
49 var _ http.Handler = &Webhook{}
51 func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
53 defer metrics.RequestLatency.WithLabelValues(wh.Name).Observe(time.Now().Sub(startTS).Seconds())
58 var reviewResponse types.Response
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)
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)
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)
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)
92 // TODO: add panic-recovery for Handle
93 reviewResponse = wh.Handle(context.Background(), types.Request{AdmissionRequest: ar.Request})
94 wh.writeResponse(w, reviewResponse)
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()
102 metrics.TotalRequests.WithLabelValues(wh.Name, "false").Inc()
106 encoder := json.NewEncoder(w)
107 responseAdmissionReview := v1beta1.AdmissionReview{
108 Response: response.Response,
110 err := encoder.Encode(responseAdmissionReview)
112 log.Error(err, "unable to encode the response")
113 wh.writeResponse(w, ErrorResponse(http.StatusInternalServerError, err))