2019-02-21 19:45:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2020-04-28 15:14:27 +00:00
|
|
|
"fmt"
|
2020-10-02 15:04:08 +00:00
|
|
|
"net/http"
|
2020-04-28 15:14:27 +00:00
|
|
|
|
2020-09-25 21:45:13 +00:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2020-10-02 15:04:08 +00:00
|
|
|
admissionv1beta1 "k8s.io/api/admission/v1beta1"
|
2019-06-09 22:49:59 +00:00
|
|
|
networking "k8s.io/api/networking/v1beta1"
|
2020-04-28 15:14:27 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-10-02 15:04:08 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
2020-08-08 23:31:02 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-02-21 19:45:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Checker must return an error if the ingress provided as argument
|
|
|
|
// contains invalid instructions
|
|
|
|
type Checker interface {
|
2019-06-09 22:49:59 +00:00
|
|
|
CheckIngress(ing *networking.Ingress) error
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IngressAdmission implements the AdmissionController interface
|
|
|
|
// to handle Admission Reviews and deny requests that are not validated
|
|
|
|
type IngressAdmission struct {
|
|
|
|
Checker Checker
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:14:27 +00:00
|
|
|
var (
|
2020-09-17 09:48:05 +00:00
|
|
|
networkingV1Beta1Resource = metav1.GroupVersionResource{
|
2020-09-02 13:08:53 +00:00
|
|
|
Group: networking.GroupName,
|
2020-04-28 15:14:27 +00:00
|
|
|
Version: "v1beta1",
|
|
|
|
Resource: "ingresses",
|
|
|
|
}
|
2020-09-17 09:48:05 +00:00
|
|
|
|
|
|
|
networkingV1Resource = metav1.GroupVersionResource{
|
|
|
|
Group: networking.GroupName,
|
|
|
|
Version: "v1",
|
|
|
|
Resource: "ingresses",
|
|
|
|
}
|
2020-04-28 15:14:27 +00:00
|
|
|
)
|
|
|
|
|
2019-02-21 19:45:21 +00:00
|
|
|
// HandleAdmission populates the admission Response
|
|
|
|
// with Allowed=false if the Object is an ingress that would prevent nginx to reload the configuration
|
|
|
|
// with Allowed=true otherwise
|
2020-10-02 15:04:08 +00:00
|
|
|
func (ia *IngressAdmission) HandleAdmission(obj runtime.Object) (runtime.Object, error) {
|
|
|
|
outputVersion := admissionv1.SchemeGroupVersion
|
2020-04-28 15:14:27 +00:00
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
review, isV1 := obj.(*admissionv1.AdmissionReview)
|
2019-02-21 19:45:21 +00:00
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
if !isV1 {
|
|
|
|
outputVersion = admissionv1beta1.SchemeGroupVersion
|
|
|
|
reviewv1beta1, isv1beta1 := obj.(*admissionv1beta1.AdmissionReview)
|
|
|
|
if !isv1beta1 {
|
|
|
|
return nil, fmt.Errorf("request is not of type apiextensions v1 or v1beta1")
|
2020-04-28 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
review = &admissionv1.AdmissionReview{}
|
|
|
|
convertV1beta1AdmissionReviewToAdmissionAdmissionReview(reviewv1beta1, review)
|
|
|
|
}
|
|
|
|
|
|
|
|
if review.Request.Resource != networkingV1Beta1Resource && review.Request.Resource != networkingV1Resource {
|
2020-10-06 05:47:36 +00:00
|
|
|
return nil, fmt.Errorf("rejecting admission review because the request does not contain an Ingress resource but %s with name %s in namespace %s",
|
2020-10-02 15:04:08 +00:00
|
|
|
review.Request.Resource.String(), review.Request.Name, review.Request.Namespace)
|
2020-04-28 15:14:27 +00:00
|
|
|
}
|
2019-02-21 19:45:21 +00:00
|
|
|
|
2020-10-06 05:47:36 +00:00
|
|
|
status := &admissionv1.AdmissionResponse{}
|
|
|
|
status.UID = review.Request.UID
|
|
|
|
|
2020-04-28 15:14:27 +00:00
|
|
|
ingress := networking.Ingress{}
|
2020-10-02 15:04:08 +00:00
|
|
|
|
|
|
|
codec := json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, json.SerializerOptions{
|
|
|
|
Pretty: true,
|
|
|
|
})
|
|
|
|
codec.Decode(review.Request.Object.Raw, nil, nil)
|
|
|
|
_, _, err := codec.Decode(review.Request.Object.Raw, nil, &ingress)
|
|
|
|
if err != nil {
|
|
|
|
klog.ErrorS(err, "failed to decode ingress")
|
|
|
|
status.Allowed = false
|
|
|
|
status.Result = &metav1.Status{
|
|
|
|
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
|
|
|
|
Message: err.Error(),
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
review.Response = status
|
|
|
|
return convertResponse(review, outputVersion), nil
|
2020-04-28 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ia.Checker.CheckIngress(&ingress); err != nil {
|
2020-10-02 15:04:08 +00:00
|
|
|
klog.ErrorS(err, "invalid ingress configuration", "ingress", review.Request.Name, "namespace", review.Request.Namespace)
|
|
|
|
status.Allowed = false
|
|
|
|
status.Result = &metav1.Status{
|
|
|
|
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
|
|
|
|
Message: err.Error(),
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|
2020-04-28 15:14:27 +00:00
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
review.Response = status
|
|
|
|
return convertResponse(review, outputVersion), nil
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:04:08 +00:00
|
|
|
klog.InfoS("successfully validated configuration, accepting", "ingress", review.Request.Name, "namespace", review.Request.Namespace)
|
|
|
|
status.Allowed = true
|
|
|
|
review.Response = status
|
|
|
|
|
|
|
|
return convertResponse(review, outputVersion), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertResponse(review *admissionv1.AdmissionReview, outputVersion schema.GroupVersion) runtime.Object {
|
|
|
|
// reply v1
|
|
|
|
if outputVersion.Version == admissionv1.SchemeGroupVersion.Version {
|
|
|
|
return review
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|
2020-10-02 15:04:08 +00:00
|
|
|
|
|
|
|
// reply v1beta1
|
|
|
|
reviewv1beta1 := &admissionv1beta1.AdmissionReview{}
|
|
|
|
convertAdmissionAdmissionReviewToV1beta1AdmissionReview(review, reviewv1beta1)
|
|
|
|
return review
|
2019-02-21 19:45:21 +00:00
|
|
|
}
|