Skip to content

Commit 30ddb21

Browse files
committed
Proper separation of the two features in the same webhook
1 parent 5590df6 commit 30ddb21

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,15 @@ func main() {
569569
}
570570
// +kubebuilder:scaffold:builder
571571

572-
if enableFsGroupChangePolicyWebhook {
572+
if enableFsGroupChangePolicyWebhook || enablePodTopologySpreadConstraintWebhook {
573573
svcClusterMgr.GetWebhookServer().Register(
574574
"/mutate-v1-pod",
575575
&webhook.Admission{
576-
Handler: &webhooks.FsGroupChangePolicySetter{
576+
Handler: &webhooks.SpiloPodMutator{
577577
SvcClient: svcClusterMgr.GetClient(),
578578
Decoder: admission.NewDecoder(svcClusterMgr.GetScheme()),
579-
Log: ctrl.Log.WithName("webhooks").WithName("FsGroupChangePolicySetter"),
579+
Log: ctrl.Log.WithName("webhooks").WithName("SpiloPodMutator"),
580+
EnableFsGroupChangePolicyWebhook: enableFsGroupChangePolicyWebhook,
580581
EnablePodTopologySpreadConstraintWebhook: enablePodTopologySpreadConstraintWebhook,
581582
PodTopologySpreadConstraintTopologyKey: podTopologySpreadConstraintTopologyKey,
582583
PodTopologySpreadConstraintMaxSkew: podTopologySpreadConstraintMaxSkew,

pkg/webhooks/fsGroupChangePolicySetter.go renamed to pkg/webhooks/spiloPodMutator.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ import (
1717

1818
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=ignore,groups=core,resources=pods,verbs=create;update,versions=v1,name=fsgroupchangepolicy.postgres.fits.cloud
1919

20-
// FsGroupChangePolicySetter Adds securityContext.fsGroupChangePolicy=OnRootMismatch when the securityContext.fsGroup field is set
21-
type FsGroupChangePolicySetter struct {
20+
// SpiloPodMutator Adds securityContext.fsGroupChangePolicy=OnRootMismatch when the securityContext.fsGroup field is set
21+
type SpiloPodMutator struct {
2222
SvcClient client.Client
2323
Decoder admission.Decoder
2424
Log logr.Logger
25+
EnableFsGroupChangePolicyWebhook bool
2526
EnablePodTopologySpreadConstraintWebhook bool
2627
PodTopologySpreadConstraintTopologyKey string
2728
PodTopologySpreadConstraintMaxSkew int32
2829
PodTopologySpreadConstraintMinDomains int32
2930
}
3031

31-
func (a *FsGroupChangePolicySetter) Handle(ctx context.Context, req admission.Request) admission.Response {
32+
func (a *SpiloPodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
3233
log := a.Log.WithValues("name", req.Name, "ns", req.Namespace)
3334
log.V(1).Info("handling admission request")
3435

@@ -39,11 +40,16 @@ func (a *FsGroupChangePolicySetter) Handle(ctx context.Context, req admission.Re
3940
return admission.Errored(http.StatusBadRequest, err)
4041
}
4142

42-
// when the fsGroup field is set, also set the fsGroupChangePolicy to OnRootMismatch
43-
if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.FSGroup != nil {
44-
p := v1.FSGroupChangeOnRootMismatch
45-
pod.Spec.SecurityContext.FSGroupChangePolicy = &p
46-
log.V(1).Info("Mutating Pod securityContext", "pod", pod)
43+
//
44+
// FSGroupChangePolicy
45+
//
46+
if a.EnableFsGroupChangePolicyWebhook {
47+
// when the fsGroup field is set, also set the fsGroupChangePolicy to OnRootMismatch
48+
if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.FSGroup != nil {
49+
p := v1.FSGroupChangeOnRootMismatch
50+
pod.Spec.SecurityContext.FSGroupChangePolicy = &p
51+
log.V(1).Info("Mutating Pod securityContext", "pod", pod)
52+
}
4753
}
4854

4955
//

0 commit comments

Comments
 (0)