
In case some ingress have a syntax error in the snippet configuration, the freshly generated configuration will not be reloaded to prevent tearing down existing rules. Although, once inserted, this configuration is preventing from any other valid configuration to be inserted as it remains in the ingresses of the cluster. To solve this problem, implement an optional validation webhook that simulates the addition of the ingress to be added together with the rest of ingresses. In case the generated configuration is not validated by nginx, deny the insertion of the ingress. In case certificates are mounted using kubernetes secrets, when those changes, keys are automatically updated in the container volume, and the controller reloads it using the filewatcher. Related changes: - Update vendors - Extract useful functions to check configuration with an additional ingress - Update documentation for validating webhook - Add validating webhook examples - Add a metric for each syntax check success and errors - Add more certificate generation examples
93 lines
3.4 KiB
Go
93 lines
3.4 KiB
Go
/*
|
|
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 (
|
|
"github.com/google/uuid"
|
|
"k8s.io/api/admission/v1beta1"
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
"k8s.io/klog"
|
|
)
|
|
|
|
// Checker must return an error if the ingress provided as argument
|
|
// contains invalid instructions
|
|
type Checker interface {
|
|
CheckIngress(ing *extensions.Ingress) error
|
|
}
|
|
|
|
// IngressAdmission implements the AdmissionController interface
|
|
// to handle Admission Reviews and deny requests that are not validated
|
|
type IngressAdmission struct {
|
|
Checker Checker
|
|
}
|
|
|
|
// 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
|
|
func (ia *IngressAdmission) HandleAdmission(ar *v1beta1.AdmissionReview) error {
|
|
if ar.Request == nil {
|
|
klog.Infof("rejecting nil request")
|
|
ar.Response = &v1beta1.AdmissionResponse{
|
|
UID: types.UID(uuid.New().String()),
|
|
Allowed: false,
|
|
}
|
|
return nil
|
|
}
|
|
klog.V(3).Infof("handling ingress admission webhook request for {%s} %s in namespace %s", ar.Request.Resource.String(), ar.Request.Name, ar.Request.Namespace)
|
|
|
|
ingressResource := v1.GroupVersionResource{Group: extensions.SchemeGroupVersion.Group, Version: extensions.SchemeGroupVersion.Version, Resource: "ingresses"}
|
|
|
|
if ar.Request.Resource == ingressResource {
|
|
ar.Response = &v1beta1.AdmissionResponse{
|
|
UID: types.UID(uuid.New().String()),
|
|
Allowed: false,
|
|
}
|
|
ingress := extensions.Ingress{}
|
|
deserializer := codecs.UniversalDeserializer()
|
|
if _, _, err := deserializer.Decode(ar.Request.Object.Raw, nil, &ingress); err != nil {
|
|
ar.Response.Result = &v1.Status{Message: err.Error()}
|
|
ar.Response.AuditAnnotations = map[string]string{
|
|
parser.GetAnnotationWithPrefix("error"): err.Error(),
|
|
}
|
|
klog.Errorf("failed to decode ingress %s in namespace %s: %s, refusing it", ar.Request.Name, ar.Request.Namespace, err.Error())
|
|
return err
|
|
}
|
|
|
|
err := ia.Checker.CheckIngress(&ingress)
|
|
if err != nil {
|
|
ar.Response.Result = &v1.Status{Message: err.Error()}
|
|
ar.Response.AuditAnnotations = map[string]string{
|
|
parser.GetAnnotationWithPrefix("error"): err.Error(),
|
|
}
|
|
klog.Errorf("failed to generate configuration for ingress %s in namespace %s: %s, refusing it", ar.Request.Name, ar.Request.Namespace, err.Error())
|
|
return err
|
|
}
|
|
ar.Response.Allowed = true
|
|
klog.Infof("successfully validated configuration, accepting ingress %s in namespace %s", ar.Request.Name, ar.Request.Namespace)
|
|
return nil
|
|
}
|
|
|
|
klog.Infof("accepting non ingress %s in namespace %s %s", ar.Request.Name, ar.Request.Namespace, ar.Request.Resource.String())
|
|
ar.Response = &v1beta1.AdmissionResponse{
|
|
UID: types.UID(uuid.New().String()),
|
|
Allowed: true,
|
|
}
|
|
return nil
|
|
}
|