Skip to content

Commit

Permalink
chart: support webhook. #486
Browse files Browse the repository at this point in the history
  • Loading branch information
runkecheng committed May 17, 2022
1 parent 77595c8 commit 9c9a42c
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
16 changes: 16 additions & 0 deletions charts/mysql-operator/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ If release name contains chart name it will be used as a full name.
{{- end }}
{{- end }}

{{- define "validating-webhook-configuration.name" -}}
{{ default "radondb-mysql-validation" }}
{{- end }}

{{- define "certificate.name" -}}
{{ default "radondb-mysql-certificate" }}
{{- end }}

{{- define "issuer.name" -}}
{{ default "radondb-mysql-issuer" }}
{{- end }}

{{- define "webhook.name" -}}
{{ default "radondb-mysql-webhook" }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
Expand Down
38 changes: 38 additions & 0 deletions charts/mysql-operator/templates/ca.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- define "webhook.caBundleCertPEM" -}}
{{- if .Values.webhook.caBundlePEM -}}
{{- trim .Values.webhook.caBundlePEM -}}
{{- else -}}
{{- /* Generate ca with CN "radondb-ca" and 5 years validity duration if not exists in the current scope.*/ -}}
{{- $caKeypair := .selfSignedCAKeypair | default (genCA "radondb-ca" 1825) -}}
{{- $_ := set . "selfSignedCAKeypair" $caKeypair -}}
{{- $caKeypair.Cert -}}
{{- end -}}
{{- end -}}

{{- define "webhook.certPEM" -}}
{{- if .Values.webhook.crtPEM -}}
{{- trim .Values.webhook.crtPEM -}}
{{- else -}}
{{- $webhookDomain := printf "%s.%s.svc" (include "webhook.name" .) .Release.Namespace -}}
{{- $webhookDomainLocal := printf "%s.%s.svc.cluster.local" (include "webhook.name" .) .Release.Namespace -}}
{{- $webhookCA := required "self-signed CA keypair is requried" .selfSignedCAKeypair -}}
{{- /* genSignedCert <CN> <IP> <DNS> <Validity duration> <CA> */ -}}
{{- $webhookServerTLSKeypair := .webhookTLSKeypair | default (genSignedCert "radondb-mysql" nil (list $webhookDomain $webhookDomainLocal) 1825 $webhookCA) -}}
{{- $_ := set . "webhookTLSKeypair" $webhookServerTLSKeypair -}}
{{- $webhookServerTLSKeypair.Cert -}}
{{- end -}}
{{- end -}}

{{- define "webhook.keyPEM" -}}
{{- if .Values.webhook.keyPEM -}}
{{ trim .Values.webhook.keyPEM }}
{{- else -}}
{{- $webhookDomain := printf "%s.%s.svc" (include "webhook.name" .) .Release.Namespace -}}
{{- $webhookDomainLocal := printf "%s.%s.svc.cluster.local" (include "webhook.name" .) .Release.Namespace -}}
{{- $webhookCA := required "self-signed CA keypair is requried" .selfSignedCAKeypair -}}
{{- /* genSignedCert <CN> <IP> <DNS> <Validity duration> <CA> */ -}}
{{- $webhookServerTLSKeypair := .webhookTLSKeypair | default (genSignedCert "radondb-mysql" nil (list $webhookDomain $webhookDomainLocal) 1825 $webhookCA) -}}
{{- $_ := set . "webhookTLSKeypair" $webhookServerTLSKeypair -}}
{{- $webhookServerTLSKeypair.Key -}}
{{- end -}}
{{- end -}}
23 changes: 23 additions & 0 deletions charts/mysql-operator/templates/cert_manager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{- if .Values.webhook.certManager.enabled }}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: {{ template "issuer.name" . }}
namespace: {{ .Release.Namespace }}
spec:
selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: {{ template "certificate.name" . }}
namespace: {{ .Release.Namespace }}
spec:
dnsNames:
- {{ printf "%s.%s.svc" (include "webhook.name" .) .Release.Namespace }}
- {{ printf "%s.%s.svc.cluster.local" (include "webhook.name" .) .Release.Namespace }}
issuerRef:
kind: Issuer
name: {{ template "issuer.name" . }}
secretName: "{{ template "webhook.name" . }}-certs"
{{- end }}
15 changes: 15 additions & 0 deletions charts/mysql-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ spec:
spec:
securityContext:
runAsNonRoot: true
volumes:
- name: cert
secret:
defaultMode: 420
secretName: "{{ template "webhook.name" . }}-certs"
containers:
{{- if .Values.rbacProxy.create }}
- name: kube-rbac-proxy
Expand All @@ -39,6 +44,14 @@ spec:
name: https
{{- end }}
- name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
volumeMounts:
- name: cert
mountPath: /tmp/k8s-webhook-server/serving-certs/
readOnly: true
command:
- /manager
args:
Expand All @@ -54,6 +67,8 @@ spec:
env:
- name: IMAGE_PREFIX
value: {{ .Values.imagePrefix }}
- name: ENABLED_WEBHOOKS
value: {{ .Values.manager.enabledWebhooks | quote }}
securityContext:
allowPrivilegeEscalation: false
livenessProbe:
Expand Down
71 changes: 71 additions & 0 deletions charts/mysql-operator/templates/webhook_configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{{- $certManagerEnabled := .Values.webhook.certManager.enabled -}}
{{- $caCert := include "webhook.caBundleCertPEM" . -}}
{{- $crtPEM := include "webhook.certPEM" . -}}
{{- $keyPEM := include "webhook.keyPEM" . -}}

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
creationTimestamp: null
name: {{ template "validating-webhook-configuration.name" . }}
{{- if $certManagerEnabled }}
annotations:
cert-manager.io/inject-ca-from: "{{ .Release.Namespace }}/{{ template "certificate.name" . }}"
{{- end }}
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
{{- if $certManagerEnabled }}
caBundle: Cg==
{{- else }}
caBundle: {{ ternary (b64enc $caCert) (b64enc (trim $crtPEM)) (empty $crtPEM) }}
{{- end }}
service:
name: {{ template "webhook.name" .}}
namespace: {{ .Release.Namespace }}
path: /validate-mysql-radondb-com-v1alpha1-mysqlcluster
failurePolicy: Fail
name: vmysqlcluster.kb.io
rules:
- apiGroups:
- mysql.radondb.com
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- mysqlclusters
sideEffects: None
---

apiVersion: v1
kind: Service
metadata:
name: {{ template "webhook.name" .}}
namespace: {{ .Release.Namespace }}
spec:
ports:
- port: 443
protocol: TCP
targetPort: 9443
selector:
app: {{ template "mysql-operator.name" . }}

---
{{- if not $certManagerEnabled }}
kind: Secret
apiVersion: v1
metadata:
name: {{ template "webhook.name" . }}-certs
namespace: {{ .Release.Namespace | quote }}
labels:
{{- include "mysql-operator.labels" . | nindent 4 }}
app.kubernetes.io/component: webhook-secret
type: Opaque
data:
ca.crt: {{ b64enc (include "webhook.caBundleCertPEM" .) }}
tls.crt: {{ b64enc (include "webhook.certPEM" .) }}
tls.key: {{ b64enc (include "webhook.keyPEM" .) }}
{{- end }}
12 changes: 12 additions & 0 deletions charts/mysql-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tolerationSeconds: 30
manager:
image: radondb/mysql-operator
tag: v2.2.0
enabledWebhooks: true
resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
Expand Down Expand Up @@ -86,3 +87,14 @@ nfsBackup:
localPVCapacity: 50G
hostName: ""
hostPath: "/mnt/radondb-nfs-backup"

webhook:
certManager:
# If true, make sure that cert-manager has been installed.
enabled: false
# If empty and disable certManager, Helm will auto-generate these fields.
caBundlePEM: |
crtPEM: |
keyPEM: |

0 comments on commit 9c9a42c

Please sign in to comment.