Skip to content

Commit 0ac8288

Browse files
committed
fix(operator): support running with webhooks disabled
1 parent b0ed100 commit 0ac8288

9 files changed

Lines changed: 69 additions & 24 deletions

File tree

charts/imp/templates/operator/deployment.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- $webhooksEnabled := and .Values.webhook.enabled .Values.webhook.certManager.enabled }}
12
apiVersion: apps/v1
23
kind: Deployment
34
metadata:
@@ -44,10 +45,13 @@ spec:
4445
args:
4546
- --leader-elect
4647
- --health-probe-bind-address=:8081
48+
- --enable-webhooks={{ $webhooksEnabled }}
4749
ports:
50+
{{- if $webhooksEnabled }}
4851
- name: webhook
4952
containerPort: 9443
5053
protocol: TCP
54+
{{- end }}
5155
- name: metrics
5256
containerPort: 8080
5357
protocol: TCP
@@ -73,13 +77,13 @@ spec:
7377
periodSeconds: 10
7478
resources:
7579
{{- toYaml .Values.operator.resources | nindent 10 }}
76-
{{- if .Values.webhook.certManager.enabled }}
80+
{{- if $webhooksEnabled }}
7781
volumeMounts:
7882
- name: webhook-tls
7983
mountPath: /tmp/k8s-webhook-server/serving-certs
8084
readOnly: true
8185
{{- end }}
82-
{{- if .Values.webhook.certManager.enabled }}
86+
{{- if $webhooksEnabled }}
8387
volumes:
8488
- name: webhook-tls
8589
secret:

charts/imp/templates/operator/service.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- $webhooksEnabled := and .Values.webhook.enabled .Values.webhook.certManager.enabled }}
12
apiVersion: v1
23
kind: Service
34
metadata:
@@ -11,9 +12,11 @@ spec:
1112
{{- include "imp.selectorLabels" . | nindent 4 }}
1213
app.kubernetes.io/component: operator
1314
ports:
15+
{{- if $webhooksEnabled }}
1416
- name: webhook
1517
port: 9443
1618
targetPort: 9443
19+
{{- end }}
1720
- name: metrics
1821
port: 8080
1922
targetPort: 8080

charts/imp/templates/webhook/certificate.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.webhook.certManager.enabled }}
1+
{{- if and .Values.webhook.enabled .Values.webhook.certManager.enabled }}
22
apiVersion: cert-manager.io/v1
33
kind: Certificate
44
metadata:

charts/imp/templates/webhook/issuer.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if and .Values.webhook.certManager.enabled (not .Values.webhook.certManager.issuerRef.name) }}
1+
{{- if and .Values.webhook.enabled .Values.webhook.certManager.enabled (not .Values.webhook.certManager.issuerRef.name) }}
22
apiVersion: cert-manager.io/v1
33
kind: Issuer
44
metadata:

charts/imp/templates/webhook/mutatingwebhook.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.webhook.certManager.enabled }}
1+
{{- if and .Values.webhook.enabled .Values.webhook.certManager.enabled }}
22
apiVersion: admissionregistration.k8s.io/v1
33
kind: MutatingWebhookConfiguration
44
metadata:

charts/imp/templates/webhook/validatingwebhook.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.webhook.certManager.enabled }}
1+
{{- if and .Values.webhook.enabled .Values.webhook.certManager.enabled }}
22
apiVersion: admissionregistration.k8s.io/v1
33
kind: ValidatingWebhookConfiguration
44
metadata:

charts/imp/tests/operator-deployment_test.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,33 @@ tests:
5656
name: metrics
5757
port: 8080
5858
targetPort: 8080
59+
60+
- it: disables webhook mount and flag when certManager is disabled
61+
template: templates/operator/deployment.yaml
62+
set:
63+
agent.env.kernelPath: /usr/local/bin/firecracker
64+
webhook.certManager.enabled: false
65+
asserts:
66+
- notContains:
67+
path: spec.template.spec.containers[0].volumeMounts
68+
content:
69+
name: webhook-tls
70+
- notContains:
71+
path: spec.template.spec.volumes
72+
content:
73+
name: webhook-tls
74+
- contains:
75+
path: spec.template.spec.containers[0].args
76+
content: --enable-webhooks=false
77+
78+
- it: omits webhook service port when certManager is disabled
79+
template: templates/operator/service.yaml
80+
set:
81+
webhook.certManager.enabled: false
82+
asserts:
83+
- notContains:
84+
path: spec.ports
85+
content:
86+
name: webhook
87+
port: 9443
88+
targetPort: 9443

charts/imp/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ agent:
5151
path: /var/lib/imp/images
5252

5353
webhook:
54+
# When false, operator admission webhooks are disabled.
55+
enabled: true
5456
certManager:
5557
enabled: true
5658
issuerRef: {}

cmd/operator/main.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ func (r *cniDetectRunnable) Start(ctx context.Context) error {
118118
func main() {
119119
var metricsAddr string
120120
var enableLeaderElection bool
121+
var enableWebhooks bool
121122
var probeAddr string
122123

123124
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metrics endpoint binds to.")
124125
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
125126
flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager.")
127+
flag.BoolVar(&enableWebhooks, "enable-webhooks", true, "Enable admission webhooks.")
126128

127129
opts := zap.Options{Development: true}
128130
opts.BindFlags(flag.CommandLine)
@@ -218,26 +220,30 @@ func main() {
218220
os.Exit(1)
219221
}
220222

221-
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVM{}).
222-
WithDefaulter(&webhookv1alpha1.ImpVMWebhook{}).
223-
WithValidator(&webhookv1alpha1.ImpVMWebhook{}).
224-
Complete(); err != nil {
225-
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVM")
226-
os.Exit(1)
227-
}
223+
if enableWebhooks {
224+
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVM{}).
225+
WithDefaulter(&webhookv1alpha1.ImpVMWebhook{}).
226+
WithValidator(&webhookv1alpha1.ImpVMWebhook{}).
227+
Complete(); err != nil {
228+
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVM")
229+
os.Exit(1)
230+
}
228231

229-
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVMClass{}).
230-
WithValidator(&webhookv1alpha1.ImpVMClassWebhook{}).
231-
Complete(); err != nil {
232-
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVMClass")
233-
os.Exit(1)
234-
}
232+
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVMClass{}).
233+
WithValidator(&webhookv1alpha1.ImpVMClassWebhook{}).
234+
Complete(); err != nil {
235+
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVMClass")
236+
os.Exit(1)
237+
}
235238

236-
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVMTemplate{}).
237-
WithValidator(&webhookv1alpha1.ImpVMTemplateWebhook{}).
238-
Complete(); err != nil {
239-
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVMTemplate")
240-
os.Exit(1)
239+
if err = builder.WebhookManagedBy(mgr, &impv1alpha1.ImpVMTemplate{}).
240+
WithValidator(&webhookv1alpha1.ImpVMTemplateWebhook{}).
241+
Complete(); err != nil {
242+
setupLog.Error(err, "unable to register webhook", "webhook", "ImpVMTemplate")
243+
os.Exit(1)
244+
}
245+
} else {
246+
setupLog.Info("admission webhooks disabled")
241247
}
242248

243249
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

0 commit comments

Comments
 (0)