mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-22 14:07:10 +00:00
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
// Copyright 2020-2021 Clastix Labs
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package networkpolicies
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
networkingv1 "k8s.io/api/networking/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/client-go/tools/record"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
|
|
"github.com/clastix/capsule/api/v1alpha1"
|
|
capsulewebhook "github.com/clastix/capsule/pkg/webhook"
|
|
)
|
|
|
|
// +kubebuilder:webhook:path=/validating-v1-network-policy,mutating=false,sideEffects=None,admissionReviewVersions=v1,failurePolicy=fail,groups=networking.k8s.io,resources=networkpolicies,verbs=create;update;delete,versions=v1,name=validating.network-policy.capsule.clastix.io
|
|
|
|
type webhook struct {
|
|
handler capsulewebhook.Handler
|
|
}
|
|
|
|
func Webhook(handler capsulewebhook.Handler) capsulewebhook.Webhook {
|
|
return &webhook{handler: handler}
|
|
}
|
|
|
|
func (w *webhook) GetHandler() capsulewebhook.Handler {
|
|
return w.handler
|
|
}
|
|
|
|
func (w *webhook) GetName() string {
|
|
return "NetworkPolicy"
|
|
}
|
|
|
|
func (w *webhook) GetPath() string {
|
|
return "/validating-v1-network-policy"
|
|
}
|
|
|
|
type handler struct {
|
|
}
|
|
|
|
func Handler() capsulewebhook.Handler {
|
|
return &handler{}
|
|
}
|
|
|
|
func (r *handler) OnCreate(client client.Client, decoder *admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
|
|
return func(ctx context.Context, req admission.Request) admission.Response {
|
|
return admission.Allowed("")
|
|
}
|
|
}
|
|
|
|
func (r *handler) generic(ctx context.Context, req admission.Request, client client.Client, _ *admission.Decoder) (*v1alpha1.Tenant, error) {
|
|
var err error
|
|
np := &networkingv1.NetworkPolicy{}
|
|
err = client.Get(ctx, types.NamespacedName{Namespace: req.AdmissionRequest.Namespace, Name: req.AdmissionRequest.Name}, np)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tnt := &v1alpha1.Tenant{}
|
|
|
|
l, _ := v1alpha1.GetTypeLabel(&v1alpha1.Tenant{})
|
|
if v, ok := np.GetLabels()[l]; ok {
|
|
if err = client.Get(ctx, types.NamespacedName{Name: v}, tnt); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tnt, nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// nolint:dupl
|
|
func (r *handler) OnDelete(client client.Client, decoder *admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
|
|
return func(ctx context.Context, req admission.Request) admission.Response {
|
|
tnt, err := r.generic(ctx, req, client, decoder)
|
|
if err != nil {
|
|
return admission.Errored(http.StatusInternalServerError, err)
|
|
}
|
|
if tnt != nil {
|
|
recorder.Eventf(tnt, corev1.EventTypeWarning, "NetworkPolicyDeletion", "NetworkPolicy %s/%s cannot be deleted", req.Namespace, req.Name)
|
|
|
|
return admission.Denied("Capsule Network Policies cannot be deleted: please, reach out to the system administrators")
|
|
}
|
|
|
|
return admission.Allowed("")
|
|
}
|
|
}
|
|
|
|
// nolint:dupl
|
|
func (r *handler) OnUpdate(client client.Client, decoder *admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
|
|
return func(ctx context.Context, req admission.Request) admission.Response {
|
|
tnt, err := r.generic(ctx, req, client, decoder)
|
|
if err != nil {
|
|
return admission.Errored(http.StatusInternalServerError, err)
|
|
}
|
|
if tnt != nil {
|
|
recorder.Eventf(tnt, corev1.EventTypeWarning, "NetworkPolicyUpdate", "NetworkPolicy %s/%s cannot be updated", req.Namespace, req.Name)
|
|
|
|
return admission.Denied("Capsule Network Policies cannot be updated: please, reach out to the system administrators")
|
|
}
|
|
|
|
return admission.Allowed("")
|
|
}
|
|
}
|