Your commit message

This commit is contained in:
zeaslity
2024-11-27 10:33:20 +08:00
commit 080c7bb97f
911 changed files with 168439 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "mysql.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "mysql.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "mysql.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "mysql.labels" -}}
helm.sh/chart: {{ include "mysql.chart" . }}
{{ include "mysql.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "mysql.selectorLabels" -}}
app.kubernetes.io/name: {{ include "mysql.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Create the name of the service account to use
*/}}
{{- define "mysql.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "mysql.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
{{/*
Generate secret with configuration
*/}}
{{- define "mysql.createSecureConfig" -}}
{{- if or (.Values.settings.rootPassword) (.Values.userDatabase) }}
true
{{- end }}
{{- end }}

View File

@@ -0,0 +1,11 @@
{{- if .Values.customConfig }}
kind: ConfigMap
apiVersion: v1
metadata:
name: {{ include "mysql.fullname" . }}
labels:
{{- include "mysql.labels" . | nindent 4 }}
data:
custom.cnf: |
{{- .Values.customConfig | nindent 4 }}
{{- end }}

View File

@@ -0,0 +1,13 @@
{{- if .Values.customScripts }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "mysql.fullname" . }}-customscripts
labels:
{{- include "mysql.labels" . | nindent 4 }}
data:
{{- range $name, $value := .Values.customScripts }}
{{- $name | nindent 2 }}: |
{{- $value | nindent 4 }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,27 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "mysql.fullname" . }}-scripts
labels:
{{- include "mysql.labels" . | nindent 4 }}
data:
init.sh: |
#!/bin/sh
echo "Start initialization"
if [ -d /extrascripts ]; then
echo "Copy extra scripts"
cp /extrascripts/* /scripts
fi
if [ -d /customscripts ]; then
echo "Copy custom scripts"
cp /customscripts/* /scripts
fi
if [ -d /extraconfigs ]; then
echo "Copy extra configs"
cp /extraconfigs/* /configs
fi
if [ -d /customconfig ]; then
echo "Copy custom config"
cp /customconfig/* /configs
fi
echo "Initialization done."

View File

@@ -0,0 +1,22 @@
{{- if include "mysql.createSecureConfig" . }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "mysql.fullname" . }}
labels:
{{- include "mysql.labels" . | nindent 4 }}
type: Opaque
data:
{{- with .Values.settings }}
{{- if .rootPassword }}
MYSQL_ROOT_PASSWORD: {{ .rootPassword | b64enc }}
{{- end }}
{{- end }}
{{- with .Values.userDatabase }}
MYSQL_DATABASE: {{ required "Values: userDatabase.name is mandatory if userDatabase is specified." .name | b64enc }}
{{- if .user }}
MYSQL_USER: {{ .user | b64enc }}
MYSQL_PASSWORD: {{ required "Values: userDatabase.password is mandatory if userDatabase.user is specified." .password | b64enc }}
{{- end }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,28 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "mysql.fullname" . }}
labels:
{{- include "mysql.labels" . | nindent 4 }}
{{- with .Values.service.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: sql
protocol: TCP
name: sql
{{- if and ( or (eq .Values.service.type "LoadBalancer") (eq .Values.service.type "NodePort") ) (.Values.service.nodePort) }}
nodePort: {{ .Values.service.nodePort }}
{{- end }}
{{- if and (eq .Values.service.type "LoadBalancer") (.Values.service.loadBalancerIP) }}
loadBalancerIP: {{ .Values.service.loadBalancerIP }}
{{- end }}
{{- if .Values.service.clusterIP }}
clusterIP: {{ .Values.service.clusterIP }}
{{- end }}
selector:
{{- include "mysql.selectorLabels" . | nindent 4 }}

View File

@@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "mysql.serviceAccountName" . }}
labels:
{{- include "mysql.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,275 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ include "mysql.fullname" . }}
labels:
{{- include "mysql.labels" . | nindent 4 }}
spec:
replicas: 1
{{- if .Values.revisionHistoryLimit }}
revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
{{- end }}
serviceName: {{ include "mysql.fullname" . }}
podManagementPolicy: {{ .Values.podManagementPolicy }}
updateStrategy:
type: {{ .Values.updateStrategyType }}
selector:
matchLabels:
{{- include "mysql.selectorLabels" . | nindent 6 }}
template:
metadata:
annotations:
checksum/customconfig: {{ include (print $.Template.BasePath "/customconfig.yaml") . | sha256sum }}
checksum/secureconfig: {{ include (print $.Template.BasePath "/secureconfig.yaml") . | sha256sum }}
checksum/customscripts: {{ include (print $.Template.BasePath "/customscripts.yaml") . | sha256sum }}
checksum/scripts: {{ include (print $.Template.BasePath "/scripts.yaml") . | sha256sum }}
{{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "mysql.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "mysql.serviceAccountName" . }}
{{- with .Values.podSecurityContext }}
securityContext:
{{- toYaml . | nindent 8 }}
{{- end }}
initContainers:
- name: {{ .Chart.Name }}-init
{{- with .Values.securityContext }}
securityContext:
{{- toYaml . | nindent 12 }}
{{- end }}
image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
{{- if .Values.extraScripts }}
- mountPath: /extrascripts
name: extrascripts-volume
{{- end }}
{{- if .Values.customScripts }}
- mountPath: /customscripts
name: customscripts-volume
{{- end }}
{{- if .Values.extraSecretConfigs }}
- mountPath: /extraconfigs
name: extraconfigs-volume
{{- end }}
{{- if .Values.customConfig }}
- mountPath: /customconfig
name: customconfig-volume
{{- end }}
- mountPath: /initscripts
name: initscripts
- mountPath: /scripts
name: scripts
- mountPath: /configs
name: configs
command: [ "/initscripts/init.sh" ]
{{- with .Values.extraInitContainers }}
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
{{- with .Values.securityContext }}
securityContext:
{{- toYaml . | nindent 12 }}
{{- end }}
image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: sql
containerPort: 3306
protocol: TCP
{{- if .Values.customStartupProbe }}
startupProbe:
{{- toYaml .Values.customStartupProbe | nindent 12 }}
{{- else }}
{{- if .Values.startupProbe.enabled }}
startupProbe:
exec:
command:
- bash
- -c
- mysqladmin status -uroot -p$MYSQL_ROOT_PASSWORD
{{- with .Values.startupProbe }}
initialDelaySeconds: {{ .initialDelaySeconds }}
timeoutSeconds: {{ .timeoutSeconds }}
failureThreshold: {{ .failureThreshold }}
successThreshold: {{ .successThreshold }}
periodSeconds: {{ .periodSeconds }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.customLivenessProbe }}
livenessProbe:
{{- toYaml .Values.customLivenessProbe | nindent 12 }}
{{- else }}
{{- if .Values.livenessProbe.enabled }}
livenessProbe:
exec:
command:
- bash
- -c
- mysqladmin status -uroot -p$MYSQL_ROOT_PASSWORD
{{- with .Values.livenessProbe }}
initialDelaySeconds: {{ .initialDelaySeconds }}
timeoutSeconds: {{ .timeoutSeconds }}
failureThreshold: {{ .failureThreshold }}
successThreshold: {{ .successThreshold }}
periodSeconds: {{ .periodSeconds }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.customReadinessProbe }}
readinessProbe:
{{- toYaml .Values.customReadinessProbe | nindent 12 }}
{{- else }}
{{- if .Values.readinessProbe.enabled }}
readinessProbe:
exec:
command:
- bash
- -c
- mysqladmin status -uroot -p$MYSQL_ROOT_PASSWORD
{{- with .Values.readinessProbe }}
initialDelaySeconds: {{ .initialDelaySeconds }}
timeoutSeconds: {{ .timeoutSeconds }}
failureThreshold: {{ .failureThreshold }}
successThreshold: {{ .successThreshold }}
periodSeconds: {{ .periodSeconds }}
{{- end }}
{{- end }}
{{- end }}
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 12 }}
{{- end }}
volumeMounts:
- mountPath: /var/lib/mysql
name: {{ .Values.storage.volumeName }}
- mountPath: /tmp
name: tmp-volume
- mountPath: /var/run/mysqld
name: tmp-volume
- mountPath: /etc/mysql/conf.d
name: configs
- mountPath: /docker-entrypoint-initdb.d
name: scripts
{{- range $secret := .Values.extraSecrets }}
- name: {{ $secret.name }}
mountPath: {{ $secret.mountPath }}
{{- end }}
{{- if .Values.settings.arguments }}
args:
{{- range .Values.settings.arguments }}
- {{ . }}
{{- end }}
{{- end }}
env:
{{- if .Values.settings.skipTZInfo }}
- name: MYSQL_INITDB_SKIP_TZINFO
value: "yes"
{{- end }}
{{- if .Values.settings.allowEmptyRootPassword }}
- name: MYSQL_ALLOW_EMPTY_ROOT_PASSWORD
value: "yes"
{{- end }}
{{- with .Values.env }}
{{- toYaml . | nindent 12 }}
{{- end }}
envFrom:
{{- if include "mysql.createSecureConfig" . }}
- secretRef:
name: {{ include "mysql.fullname" . }}
{{- end }}
{{- range .Values.extraEnvSecrets }}
- secretRef:
name: {{ . }}
{{- end }}
{{- with .Values.extraContainers }}
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
- name: tmp-volume
emptyDir: {}
- name: configs
emptyDir: {}
- name: scripts
emptyDir: {}
- name: initscripts
configMap:
name: {{ include "mysql.fullname" . }}-scripts
defaultMode: 0555
{{- if .Values.customScripts }}
- name: customscripts-volume
configMap:
name: {{ include "mysql.fullname" . }}-customscripts
defaultMode: 0555
{{- end }}
{{- if .Values.extraSecretConfigs }}
- name: extraconfigs-volume
secret:
secretName: {{ .Values.extraSecretConfigs }}
{{- end }}
{{- if .Values.customConfig }}
- name: customconfig-volume
configMap:
name: {{ include "mysql.fullname" . }}
{{- end }}
{{- if .Values.extraScripts }}
- name: extrascripts-volume
configMap:
name: {{ .Values.extraScripts }}
defaultMode: 0555
{{- end }}
{{- range $secret := .Values.extraSecrets }}
- name: {{ $secret.name }}
secret:
secretName: {{ $secret.name }}
defaultMode: 0440
{{- end }}
{{- $fullname := include "mysql.fullname" . }}
{{- with .Values.storage }}
{{- $createPvc := and (empty .persistentVolumeClaimName) .requestedSize }}
{{- if not $createPvc }}
- name: {{ .volumeName }}
{{- if .persistentVolumeClaimName }}
persistentVolumeClaim:
claimName: {{ .persistentVolumeClaimName }}
{{- else }}
emptyDir: {}
{{- end }}
{{- else }}
volumeClaimTemplates:
- metadata:
name: {{ .volumeName }}
spec:
{{- with .accessModes }}
accessModes:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- if .className }}
storageClassName: {{ .className }}
{{- end }}
resources:
requests:
storage: {{ .requestedSize }}
{{- end }}
{{- end }}