-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathserviceaccount.go
More file actions
72 lines (60 loc) · 1.89 KB
/
Copy pathserviceaccount.go
File metadata and controls
72 lines (60 loc) · 1.89 KB
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
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package cfg
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
ad "github.com/projectcapsule/capsule/pkg/runtime/admission"
"github.com/projectcapsule/capsule/pkg/runtime/events"
"github.com/projectcapsule/capsule/pkg/runtime/handlers"
)
type serviceAccountHandler struct{}
func ServiceAccountHandler() handlers.TypedHandler[*capsulev1beta2.CapsuleConfiguration] {
return &serviceAccountHandler{}
}
func (h *serviceAccountHandler) OnCreate(
_ client.Client,
_ client.Reader,
cfg *capsulev1beta2.CapsuleConfiguration,
_ admission.Decoder,
_ events.EventRecorder,
) handlers.Func {
return func(context.Context, admission.Request) *admission.Response {
return h.handle(cfg)
}
}
func (h *serviceAccountHandler) OnDelete(
client.Client,
client.Reader,
*capsulev1beta2.CapsuleConfiguration,
admission.Decoder,
events.EventRecorder,
) handlers.Func {
return func(context.Context, admission.Request) *admission.Response {
return nil
}
}
func (h *serviceAccountHandler) OnUpdate(
_ client.Client,
_ client.Reader,
cfg *capsulev1beta2.CapsuleConfiguration,
old *capsulev1beta2.CapsuleConfiguration,
_ admission.Decoder,
_ events.EventRecorder,
) handlers.Func {
return func(context.Context, admission.Request) *admission.Response {
return h.handle(cfg)
}
}
func (h *serviceAccountHandler) handle(config *capsulev1beta2.CapsuleConfiguration) *admission.Response {
nameSet := config.Spec.Impersonation.GlobalDefaultServiceAccount != ""
nsSet := config.Spec.Impersonation.GlobalDefaultServiceAccountNamespace != ""
if nameSet != nsSet {
return ad.Deny(
"both globalDefaultServiceAccount and globalDefaultServiceAccountNamespace must be set together",
)
}
return nil
}