mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-25 16:07:24 +00:00
feat: emitting events for Tenant operations
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
corev1 "k8s.io/api/core/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"
|
||||
|
||||
@@ -41,10 +42,13 @@ func (w *webhook) GetPath() string {
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
recorder record.EventRecorder
|
||||
}
|
||||
|
||||
func Handler() capsulewebhook.Handler {
|
||||
return &handler{}
|
||||
func Handler(recorder record.EventRecorder) capsulewebhook.Handler {
|
||||
return &handler{
|
||||
recorder: recorder,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *handler) OnCreate(client client.Client, decoder *admission.Decoder) capsulewebhook.Func {
|
||||
@@ -61,6 +65,8 @@ func (r *handler) OnCreate(client client.Client, decoder *admission.Decoder) cap
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
if tnt.IsFull() {
|
||||
r.recorder.Eventf(tnt, corev1.EventTypeWarning, "Error", "the Namespace quota has been exceeded, Namespace %s cannot been attached", ns.GetName())
|
||||
|
||||
return admission.Denied(NewNamespaceQuotaExceededError().Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
@@ -47,12 +48,14 @@ func (w *webhook) GetPath() string {
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
cfg configuration.Configuration
|
||||
cfg configuration.Configuration
|
||||
recorder record.EventRecorder
|
||||
}
|
||||
|
||||
func Handler(cfg configuration.Configuration) capsulewebhook.Handler {
|
||||
func Handler(cfg configuration.Configuration, recorder record.EventRecorder) capsulewebhook.Handler {
|
||||
return &handler{
|
||||
cfg: cfg,
|
||||
cfg: cfg,
|
||||
recorder: recorder,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,8 +151,13 @@ func (h *handler) patchResponseForOwnerRef(tenant *capsulev1alpha1.Tenant, ns *c
|
||||
|
||||
o, _ := json.Marshal(ns.DeepCopy())
|
||||
if err := controllerutil.SetControllerReference(tenant, ns, scheme); err != nil {
|
||||
h.recorder.Eventf(tenant, corev1.EventTypeWarning, "Error", "namespace %s cannot be assigned to the desired Tenant", ns.GetName())
|
||||
|
||||
return admission.Errored(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
h.recorder.Eventf(tenant, corev1.EventTypeNormal, "NamespaceCreationWebhook", "namespace %s has been assigned to the desired Tenant", ns.GetName())
|
||||
|
||||
c, _ := json.Marshal(ns)
|
||||
return admission.PatchResponseFromRaw(o, c)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
corev1 "k8s.io/api/core/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"
|
||||
|
||||
@@ -45,11 +46,13 @@ func (w *webhook) GetPath() string {
|
||||
|
||||
type handler struct {
|
||||
configuration configuration.Configuration
|
||||
recorder record.EventRecorder
|
||||
}
|
||||
|
||||
func Handler(configuration configuration.Configuration) capsulewebhook.Handler {
|
||||
func Handler(configuration configuration.Configuration, recorder record.EventRecorder) capsulewebhook.Handler {
|
||||
return &handler{
|
||||
configuration: configuration,
|
||||
recorder: recorder,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +79,8 @@ func (r *handler) OnCreate(clt client.Client, decoder *admission.Decoder) capsul
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
if e := fmt.Sprintf("%s-%s", tnt.GetName(), ns.GetName()); !strings.HasPrefix(ns.GetName(), fmt.Sprintf("%s-", tnt.GetName())) {
|
||||
r.recorder.Eventf(tnt, corev1.EventTypeWarning, "TenantPrefix", "Namespace %s does not match the expected prefix", ns.GetName())
|
||||
|
||||
return admission.Denied("The namespace doesn't match the tenant prefix, expected " + e)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user