-
Notifications
You must be signed in to change notification settings - Fork 148
/
in_capsule_groups.go
75 lines (60 loc) · 2 KB
/
in_capsule_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2020-2023 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"context"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/projectcapsule/capsule/pkg/configuration"
"github.com/projectcapsule/capsule/pkg/webhook"
)
func InCapsuleGroups(configuration configuration.Configuration, handlers ...webhook.Handler) webhook.Handler {
return &handler{
configuration: configuration,
handlers: handlers,
}
}
type handler struct {
configuration configuration.Configuration
handlers []webhook.Handler
}
func (h *handler) OnCreate(client client.Client, decoder admission.Decoder, recorder record.EventRecorder) webhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
if !IsCapsuleUser(ctx, req, client, h.configuration.UserGroups()) {
return nil
}
for _, hndl := range h.handlers {
if response := hndl.OnCreate(client, decoder, recorder)(ctx, req); response != nil {
return response
}
}
return nil
}
}
func (h *handler) OnDelete(client client.Client, decoder admission.Decoder, recorder record.EventRecorder) webhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
if !IsCapsuleUser(ctx, req, client, h.configuration.UserGroups()) {
return nil
}
for _, hndl := range h.handlers {
if response := hndl.OnDelete(client, decoder, recorder)(ctx, req); response != nil {
return response
}
}
return nil
}
}
func (h *handler) OnUpdate(client client.Client, decoder admission.Decoder, recorder record.EventRecorder) webhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
if !IsCapsuleUser(ctx, req, client, h.configuration.UserGroups()) {
return nil
}
for _, hndl := range h.handlers {
if response := hndl.OnUpdate(client, decoder, recorder)(ctx, req); response != nil {
return response
}
}
return nil
}
}