refactored templates and helper

This commit is contained in:
Sebastian Wendel 2022-06-22 17:44:49 +02:00
parent 737d0aa326
commit 5c0db3bdcd
No known key found for this signature in database
GPG key ID: 1422B7DF78144640
7 changed files with 308 additions and 29 deletions

View file

@ -1,7 +1,7 @@
{{/* {{/*
Expand the name of the chart. Expand the name of the chart.
*/}} */}}
{{- define "fcos-core.name" -}} {{- define "fcoscore.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }} {{- end }}
@ -10,7 +10,7 @@ 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). 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. If release name contains chart name it will be used as a full name.
*/}} */}}
{{- define "fcos-core.fullname" -}} {{- define "fcoscore.fullname" -}}
{{- if .Values.fullnameOverride }} {{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }} {{- else }}
@ -26,16 +26,16 @@ If release name contains chart name it will be used as a full name.
{{/* {{/*
Create chart name and version as used by the chart label. Create chart name and version as used by the chart label.
*/}} */}}
{{- define "fcos-core.chart" -}} {{- define "fcoscore.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }} {{- end }}
{{/* {{/*
Common labels Common labels
*/}} */}}
{{- define "fcos-core.labels" -}} {{- define "fcoscore.labels" -}}
helm.sh/chart: {{ include "fcos-core.chart" . }} helm.sh/chart: {{ include "fcoscore.chart" . }}
{{ include "fcos-core.selectorLabels" . }} {{ include "fcoscore.selectorLabels" . }}
{{- if .Chart.AppVersion }} {{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }} {{- end }}
@ -45,18 +45,202 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
{{/* {{/*
Selector labels Selector labels
*/}} */}}
{{- define "fcos-core.selectorLabels" -}} {{- define "fcoscore.selectorLabels" -}}
app.kubernetes.io/name: {{ include "fcos-core.name" . }} app.kubernetes.io/name: {{ include "fcoscore.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }} {{- end }}
{{/* {{/*
Create the name of the service account to use Create the name of the service account to use
*/}} */}}
{{- define "fcos-core.serviceAccountName" -}} {{- define "fcoscore.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }} {{- if .Values.serviceAccount.create }}
{{- default (include "fcos-core.fullname" .) .Values.serviceAccount.name }} {{- default (include "fcoscore.fullname" .) .Values.serviceAccount.name }}
{{- else }} {{- else }}
{{- default "default" .Values.serviceAccount.name }} {{- default "default" .Values.serviceAccount.name }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{/*
Get the user defined LoadBalancerIP for this release
Note, returns 127.0.0.1 if using ClusterIP.
*/}}
{{- define "fcoscore.serviceIP" -}}
{{- if eq .Values.service.type "ClusterIP" -}}
127.0.0.1
{{- else -}}
{{- .Values.service.loadBalancerIP | default "" -}}
{{- end -}}
{{- end -}}
{{/*
Gets the host to be used for this application.
If not using ClusterIP, or if a host or LoadBalancerIP is not defined, the value will be empty
*/}}
{{- define "fcoscore.host" -}}
{{- if .Values.ingress.enabled }}
{{- $host := .Values.ingress.hostname | default "" -}}
{{- default (include "fcoscore.serviceIP" .) $host -}}
{{- else -}}
{{- $host := .Values.host | default "" -}}
{{- default (include "fcoscore.serviceIP" .) $host -}}
{{- end -}}
{{- end -}}
{{/*
Return true if a secret object for FabCityOS should be created
*/}}
{{- define "fcoscore.createSecret" -}}
{{- if or (and .Values.smtp.enabled .Values.smtp.password (not .Values.smtp.existingSecret)) }}
{{- true -}}
{{- end -}}
{{- end -}}
{{/*
Return the FabCityOS secret name
*/}}
{{- define "fcoscore.secretName" -}}
{{- default (include "common.names.fullname" .) -}}
{{- end -}}
{{/*
Return the Postgresql hostname
*/}}
{{- define "fcoscore.database.host" -}}
{{- ternary (include "fcoscore.postgresql.fullname" .) .Values.externalDatabase.host .Values.postgresql.enabled | quote -}}
{{- end -}}
{{/*
Return the Postgresql port
*/}}
{{- define "fcoscore.database.port" -}}
{{- ternary "5432" .Values.externalDatabase.port .Values.postgresql.enabled | quote -}}
{{- end -}}
{{/*
Return the Postgresql database name
*/}}
{{- define "fcoscore.database.name" -}}
{{- if .Values.postgresql.enabled }}
{{- if .Values.global.postgresql }}
{{- if .Values.global.postgresql.auth }}
{{- coalesce .Values.global.postgresql.auth.database .Values.postgresql.auth.database | quote -}}
{{- else -}}
{{- .Values.postgresql.auth.database | quote -}}
{{- end -}}
{{- else -}}
{{- .Values.postgresql.auth.database | quote -}}
{{- end -}}
{{- else -}}
{{- .Values.externalDatabase.database | quote -}}
{{- end -}}
{{- end -}}
{{/*
Return the Postgresql user
*/}}
{{- define "fcoscore.database.user" -}}
{{- if .Values.postgresql.enabled }}
{{- if .Values.global.postgresql }}
{{- if .Values.global.postgresql.auth }}
{{- coalesce .Values.global.postgresql.auth.username .Values.postgresql.auth.username | quote -}}
{{- else -}}
{{- .Values.postgresql.auth.username | quote -}}
{{- end -}}
{{- else -}}
{{- .Values.postgresql.auth.username | quote -}}
{{- end -}}
{{- else -}}
{{- .Values.externalDatabase.user | quote -}}
{{- end -}}
{{- end -}}
{{/*
Return the Postgresql secret name
*/}}
{{- define "fcoscore.database.secretName" -}}
{{- if .Values.postgresql.enabled }}
{{- if .Values.global.postgresql }}
{{- if .Values.global.postgresql.auth }}
{{- if .Values.global.postgresql.auth.existingSecret }}
{{- tpl .Values.global.postgresql.auth.existingSecret $ -}}
{{- else -}}
{{- default (include "fcoscore.postgresql.fullname" .) (tpl .Values.postgresql.auth.existingSecret $) -}}
{{- end -}}
{{- else -}}
{{- default (include "fcoscore.postgresql.fullname" .) (tpl .Values.postgresql.auth.existingSecret $) -}}
{{- end -}}
{{- else -}}
{{- default (include "fcoscore.postgresql.fullname" .) (tpl .Values.postgresql.auth.existingSecret $) -}}
{{- end -}}
{{- else -}}
{{- default (printf "%s-database" .Release.Name) (tpl .Values.externalDatabase.existingSecret $) -}}
{{- end -}}
{{- end -}}
{{/*
Return the Postgresql password secret key
*/}}
{{- define "fcoscore.database.secretPasswordKey" -}}
{{- if .Values.postgresql.enabled -}}
{{- print "password" -}}
{{- else -}}
{{- if .Values.externalDatabase.existingSecret -}}
{{- default "password" .Values.externalDatabase.existingSecretPasswordKey }}
{{- else -}}
{{- print "password" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/*
Return the Postgresql postgres password secret key
*/}}
{{- define "fcoscore.database.secretPostgresPasswordKey" -}}
{{- if .Values.postgresql.enabled -}}
{{- print "postgres-password" -}}
{{- else -}}
{{- if .Values.externalDatabase.existingSecret -}}
{{- default "postgres-password" .Values.externalDatabase.existingSecretPostgresPasswordKey }}
{{- else -}}
{{- print "postgres-password" -}}
{{- end -}}
{{- end -}}
{{- 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).
*/}}
{{- define "fcoscore.postgresql.fullname" -}}
{{- include "common.names.dependency.fullname" (dict "chartName" "postgresql" "chartValues" .Values.postgresql "context" $) -}}
{{- end -}}
{{- define "fcoscore.meilisearch.host" -}}
{{- include "common.names.dependency.fullname" (dict "chartName" "meilisearch" "chartValues" .Values.meilisearch "context" $) -}}
{{- end }}
{{- define "fcoscore.meilisearch.port" -}}
{{- .Values.meilisearch.service.port -}}
{{- end -}}
{{- define "fcoscore.meilisearch.url" -}}
{{- printf "http://%s:%s" (include "fcoscore.meilisearch.host" . ) (include "fcoscore.meilisearch.port" . ) -}}
{{- end -}}
{{/*
Return the FabCityOS SMTP secret name
*/}}
{{- define "smtp.secretName" -}}
{{- default (printf "%s" (include "common.names.fullname" .)) (tpl .Values.smtp.existingSecret $) -}}
{{- end -}}
{{/*
Return true if FabCityOS SMTP uses password authentication
*/}}
{{- define "smtp.password.enabled" -}}
{{- if and .Values.smtp.enabled (or .Values.smtp.password .Values.smtp.existingSecret) }}
{{- true -}}
{{- end -}}
{{- end -}}

View file

@ -1,16 +1,16 @@
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: {{ include "fcos-core.fullname" . }} name: {{ include "fcoscore.fullname" . }}
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
spec: spec:
{{- if not .Values.autoscaling.enabled }} {{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }} replicas: {{ .Values.replicaCount }}
{{- end }} {{- end }}
selector: selector:
matchLabels: matchLabels:
{{- include "fcos-core.selectorLabels" . | nindent 6 }} {{- include "fcoscore.selectorLabels" . | nindent 6 }}
template: template:
metadata: metadata:
{{- with .Values.podAnnotations }} {{- with .Values.podAnnotations }}
@ -18,13 +18,13 @@ spec:
{{- toYaml . | nindent 8 }} {{- toYaml . | nindent 8 }}
{{- end }} {{- end }}
labels: labels:
{{- include "fcos-core.selectorLabels" . | nindent 8 }} {{- include "fcoscore.selectorLabels" . | nindent 8 }}
spec: spec:
{{- with .Values.imagePullSecrets }} {{- with .Values.imagePullSecrets }}
imagePullSecrets: imagePullSecrets:
{{- toYaml . | nindent 8 }} {{- toYaml . | nindent 8 }}
{{- end }} {{- end }}
serviceAccountName: {{ include "fcos-core.serviceAccountName" . }} serviceAccountName: {{ include "fcoscore.serviceAccountName" . }}
securityContext: securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }} {{- toYaml .Values.podSecurityContext | nindent 8 }}
containers: containers:
@ -33,9 +33,104 @@ spec:
{{- toYaml .Values.securityContext | nindent 12 }} {{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }} imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.lifecycleHooks }}
lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
{{- end }}
{{- if .Values.diagnosticMode.enabled }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
{{- else if .Values.command }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.diagnosticMode.enabled }}
args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
{{- else if .Values.args }}
args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
{{- end }}
env:
{{- if or .Values.postgresql.enabled .Values.externalDatabase.create }}
- name: POSTGRESQL_CLIENT_POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.database.secretName" . }}
key: {{ include "fcoscore.database.secretPostgresPasswordKey" . }}
- name: POSTGRESQL_CLIENT_CREATE_DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.database.secretName" . }}
key: {{ include "fcoscore.database.secretPasswordKey" . }}
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.database.secretName" . }}
key: {{ include "fcoscore.database.secretPasswordKey" . }}
{{- end }}
- name: SECRET_KEY_BASE
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: secret_key_base
- name: SIGNING_SALT
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: signing_salt
- name: ENCRYPTION_SALT
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: encryption_salt
- name: SEEDS_PW
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: seeds_pw
{{- if (include "smtp.password.enabled" .) }}
- name: MAIL_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "smtp.secretName" . }}
key: smtp_password
- name: MAIL_KEY
valueFrom:
secretKeyRef:
name: {{ include "smtp.api_key" . }}
key: smtp_api_key
{{- end }}
- name: MEILI_MASTER_KEY
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: meili_master_key
- name: WEB_PUSH_PRIVATE_KEY
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: web_push_private_key
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: {{ include "fcoscore.secretName" . }}
key: github_token
{{- if .Values.fcoscore.extraEnvVars }}
{{- include "common.tplvalues.render" (dict "value" .Values.fcoscore.extraEnvVars "context" $) | nindent 12 }}
{{- end }}
envFrom:
- configMapRef:
name: {{ include "common.names.fullname" . }}
{{- if .Values.fcoscore.extraEnvVarsCM }}
- configMapRef:
name: {{ .Values.fcoscore.extraEnvVarsCM }}
{{- end }}
{{- if .Values.fcoscore.extraEnvVarsSecret }}
- secretRef:
name: {{ .Values.fcoscore.extraEnvVarsSecret }}
{{- end }}
ports: ports:
- name: http - name: http
containerPort: 80 containerPort: {{ .Values.service.port }}
protocol: TCP protocol: TCP
livenessProbe: livenessProbe:
httpGet: httpGet:

View file

@ -2,14 +2,14 @@
apiVersion: autoscaling/v2beta1 apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler kind: HorizontalPodAutoscaler
metadata: metadata:
name: {{ include "fcos-core.fullname" . }} name: {{ include "fcoscore.fullname" . }}
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
spec: spec:
scaleTargetRef: scaleTargetRef:
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
name: {{ include "fcos-core.fullname" . }} name: {{ include "fcoscore.fullname" . }}
minReplicas: {{ .Values.autoscaling.minReplicas }} minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }} maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics: metrics:

View file

@ -1,5 +1,5 @@
{{- if .Values.ingress.enabled -}} {{- if .Values.ingress.enabled -}}
{{- $fullName := include "fcos-core.fullname" . -}} {{- $fullName := include "fcoscore.fullname" . -}}
{{- $svcPort := .Values.service.port -}} {{- $svcPort := .Values.service.port -}}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} {{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
@ -17,7 +17,7 @@ kind: Ingress
metadata: metadata:
name: {{ $fullName }} name: {{ $fullName }}
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }} {{- with .Values.ingress.annotations }}
annotations: annotations:
{{- toYaml . | nindent 4 }} {{- toYaml . | nindent 4 }}

View file

@ -1,9 +1,9 @@
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: {{ include "fcos-core.fullname" . }} name: {{ include "fcoscore.fullname" . }}
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
spec: spec:
type: {{ .Values.service.type }} type: {{ .Values.service.type }}
ports: ports:
@ -12,4 +12,4 @@ spec:
protocol: TCP protocol: TCP
name: http name: http
selector: selector:
{{- include "fcos-core.selectorLabels" . | nindent 4 }} {{- include "fcoscore.selectorLabels" . | nindent 4 }}

View file

@ -2,9 +2,9 @@
apiVersion: v1 apiVersion: v1
kind: ServiceAccount kind: ServiceAccount
metadata: metadata:
name: {{ include "fcos-core.serviceAccountName" . }} name: {{ include "fcoscore.serviceAccountName" . }}
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }} {{- with .Values.serviceAccount.annotations }}
annotations: annotations:
{{- toYaml . | nindent 4 }} {{- toYaml . | nindent 4 }}

View file

@ -1,9 +1,9 @@
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod
metadata: metadata:
name: "{{ include "fcos-core.fullname" . }}-test-connection" name: "{{ include "fcoscore.fullname" . }}-test-connection"
labels: labels:
{{- include "fcos-core.labels" . | nindent 4 }} {{- include "fcoscore.labels" . | nindent 4 }}
annotations: annotations:
"helm.sh/hook": test "helm.sh/hook": test
spec: spec:
@ -11,5 +11,5 @@ spec:
- name: wget - name: wget
image: busybox image: busybox
command: ['wget'] command: ['wget']
args: ['{{ include "fcos-core.fullname" . }}:{{ .Values.service.port }}'] args: ['{{ include "fcoscore.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never restartPolicy: Never