Skip to content

Commit

Permalink
[app] init statefulset support (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladStarr authored Jul 25, 2024
1 parent 18eb970 commit 70a158f
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dysnix/app/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: app
description: Generic application Helm chart
home: https://github.com/dysnix/charts/tree/main/dysnix/app
icon: https://dysnix.com/images/logo.svg
version: 0.99.17
version: 0.99.18
sources:
- https://github.com/dysnix/charts
keywords:
Expand Down
58 changes: 30 additions & 28 deletions dysnix/app/README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dysnix/app/templates/_resources.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Usage
{{- include "app.resources.include" (dict "resource" "deployment" | merge .) -}}
{{- end -}}

{{- define "app.statefulset" -}}
{{- include "app.resources.include" (dict "resource" "statefulset" | merge .) -}}
{{- end -}}

{{- define "app.service-account" -}}
{{- include "app.resources.include" (dict "resource" "service-account" | merge .) -}}
{{- end -}}
Expand Down
3 changes: 3 additions & 0 deletions dysnix/app/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ spec:
{{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
{{- end }}
spec:
{{- with .Values.enableServiceLinks }}
enableServiceLinks: {{ . }}
{{- end }}
serviceAccountName: {{ template "app.serviceAccountName" . }}
{{- if kindIs "bool" .Values.automountServiceAccountToken }}
automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
Expand Down
2 changes: 1 addition & 1 deletion dysnix/app/templates/pvc.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{/* vim: set filetype=helm: */}}
{{- define "app.resources.pvc" -}}
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) (not (has .Values.persistence.type (list "sts" "statefulset"))) }}
---
apiVersion: v1
kind: PersistentVolumeClaim
Expand Down
266 changes: 266 additions & 0 deletions dysnix/app/templates/statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{{/* vim: set filetype=helm: */}}

{{- define "app.resources.statefulset" -}}
{{- if and .Values.app.workload.enabled (has .Values.app.workload.type (list "statefulset" "sts")) }}
---
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
name: {{ template "common.names.fullname" . }}
namespace: {{ include "common.names.namespace" . | quote }}
labels:
{{- include "common.labels.standard" . | nindent 4 }}
{{- include "app.labels.component" . | nindent 4 }}
{{- if .Values.commonLabels }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
{{- end }}
{{- if .Values.commonAnnotations }}
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
spec:
serviceName: {{ template "common.names.fullname" . }}
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
{{- if .Values.updateStrategy }}
updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
{{- end }}
minReadySeconds: {{ .Values.minReadySeconds }}
podManagementPolicy: {{ .Values.podManagementPolicy }}
{{- with .Values.persistentVolumeClaimRetentionPolicy }}
persistentVolumeClaimRetentionPolicy: {{- toYaml . | nindent 4 }}
{{- end }}
selector:
matchLabels:
{{- include "common.labels.matchLabels" . | nindent 6 }}
{{- include "app.labels.component" . | nindent 6 }}
template:
metadata:
annotations:
{{- include "app.template.checksums" $ | indent 8 }}
{{- if .Values.podAnnotations }}
{{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
{{- end }}
labels:
{{- include "common.labels.standard" . | nindent 8 }}
{{- include "app.labels.component" . | nindent 8 }}
{{- if .Values.commonLabels }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
{{- end }}
{{- if .Values.podLabels }}
{{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
{{- end }}
spec:
{{- with .Values.enableServiceLinks }}
enableServiceLinks: {{ . }}
{{- end }}
serviceAccountName: {{ template "app.serviceAccountName" . }}
{{- if kindIs "bool" .Values.automountServiceAccountToken }}
automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
{{- end }}
{{- include "app.imagePullSecrets" . | nindent 6 }}
{{- if .Values.hostAliases }}
hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
{{- end }}
{{- if kindIs "bool" .Values.hostNetwork }}
hostNetwork: {{ .Values.hostNetwork }}
{{- end }}
{{- if .Values.dnsPolicy }}
dnsPolicy: {{ .Values.dnsPolicy }}
{{- end }}
{{- if .Values.affinity }}
affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
{{- else }}
affinity:
podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" ._include.component "context" $) | nindent 10 }}
podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" ._include.component "context" $) | nindent 10 }}
nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
{{- end }}
{{- if .Values.nodeSelector }}
nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.tolerations }}
tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.priorityClassName }}
priorityClassName: {{ .Values.priorityClassName | quote }}
{{- end }}
{{- if .Values.schedulerName }}
schedulerName: {{ .Values.schedulerName | quote }}
{{- end }}
{{- if .Values.topologySpreadConstraints }}
topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.podSecurityContext.enabled }}
securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
{{- end }}
{{- if .Values.terminationGracePeriodSeconds }}
terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
{{- end }}
initContainers:
{{- if and .Values.volumePermissions .Values.persistence .Values.volumePermissions.command .Values.persistence.enabled }}
- name: volume-permissions
image: {{ include "app.volumePermissions.image" .Values }}
imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.volumePermissions.command "context" $) | nindent 12 -}}
securityContext: {{- include "common.tplvalues.render" . | nindent 12 }}
{{- if .Values.volumePermissions.resources }}
resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
{{- end }}
volumeMounts:
{{- if .Values.persistence.enabled }}
- name: {{ .Values.persistence.mountName }}
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.initContainers -}}
{{/* Use extendeded .Values.initContainers (use, values map) */}}
{{- include "app.containers" (dict "initContainers" true "values" .Values.initContainers "top" $) | nindent 8 }}
{{- end }}
{{- if .Values.extraInitContainers }}
{{- include "common.tplvalues.render" (dict "value" .Values.extraInitContainers "context" $) | nindent 8 }}
{{- end }}
containers:
- name: {{ .Values.app.name }}
image: {{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- 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:
{{- with .Values.env }}
{{- include "app.tplvalues.named-list" (dict "valueKey" "value" "value" . "toString" true "context" $) | nindent 12 -}}
{{- end }}
{{- if .Values.extraEnvVars }}
{{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
{{- end }}
envFrom:
{{- with .Values.envFrom }}
{{- include "common.tplvalues.render" (dict "value" . "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.extraEnvVarsCM }}
- configMapRef:
name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
{{- end }}
{{- if .Values.extraEnvVarsSecret }}
- secretRef:
name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
{{- end }}
{{- if .Values.resources }}
resources: {{- toYaml .Values.resources | nindent 12 }}
{{- end }}
{{- with include "app.tplvalues.named-list" ( dict "value" .Values.containerPorts "valueKey" "containerPort" "context" $) }}
ports: {{ . | nindent 12 }}
{{- end }}
{{- if not .Values.diagnosticMode.enabled }}
{{- if .Values.customLivenessProbe }}
livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
{{- else if .Values.livenessProbe.enabled }}
livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.customReadinessProbe }}
readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
{{- else if .Values.readinessProbe.enabled }}
readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.customStartupProbe }}
startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
{{- else if .Values.startupProbe.enabled }}
startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
{{- end }}
{{- end }}
{{- if .Values.lifecycleHooks }}
lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
{{- end }}
volumeMounts:
{{- if .Values.persistence.enabled }}
- name: {{ .Values.persistence.mountName }}
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
{{- end }}
{{- with .Values.volumeMounts }}
{{- include "app.tplvalues.named-list" (dict "value" . "context" $) | nindent 12 -}}
{{- end }}
{{- if .Values.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.containers }}
{{/* Use extendeded .Values.app.containers (use, values map) */}}
{{- include "app.containers" (dict "values" .Values.containers "top" $) | nindent 8 }}
{{- end }}
{{- if .Values.sidecars }}
{{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
{{- end }}
volumes:
{{- if and .Values.persistence.enabled (or (eq .Values.persistence.type "pvc") .Values.persistence.emptyDir) }}
- name: {{ .Values.persistence.mountName }}
{{- if .Values.persistence.emptyDir }}
emptyDir: {}
{{- else }}
persistentVolumeClaim:
claimName: {{ default (include "common.names.fullname" $) .Values.persistence.existingClaim }}
{{- end }}
{{- end }}
{{- with .Values.volumes }}
{{- include "app.tplvalues.named-list" (dict "value" . "context" $) | nindent 8 -}}
{{- end }}
{{- if .Values.extraVolumes }}
{{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
{{- end }}
{{- if and .Values.persistence.enabled (has .Values.persistence.type (list "sts" "statefulset")) }}
volumeClaimTemplates:
- metadata:
name: {{ .Values.persistence.mountName }}
labels:
{{- include "common.labels.matchLabels" . | nindent 8 }}
{{- include "app.labels.component" . | nindent 8 }}
{{- if or .Values.persistence.annotations .Values.commonAnnotations }}
annotations:
{{- if .Values.persistence.annotations }}
{{- include "common.tplvalues.render" ( dict "value" .Values.persistence.annotations "context" $ ) | nindent 8 }}
{{- end }}
{{- if .Values.commonAnnotations }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
{{- end }}
{{- end }}
spec:
accessModes:
{{- range .Values.persistence.accessModes }}
- {{ . | quote }}
{{- end }}
resources:
requests:
storage: {{ .Values.persistence.size | quote }}
{{- if .Values.persistence.selector }}
selector: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.selector "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.persistence.dataSource }}
dataSource: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.dataSource "context" $) | nindent 8 }}
{{- end }}
{{- include "common.storage.class" (dict "persistence" .Values.persistence "global" .Values.global) | nindent 6 }}
{{- end }}
{{- end }}
{{- end -}}

{{/* Include the resource */}}
{{- if eq "direct" (include "app.chart.mode" .) -}}
{{- range $_, $component := concat (list "") $.Values.app.components -}}
{{- $values := ternary $.Values (get $.Values "component") (eq $component "") | default dict -}}
{{- include "app.statefulset" (dict "component" $component "values" $values "top" $) -}}
{{- end -}}
{{- end -}}
20 changes: 19 additions & 1 deletion dysnix/app/tests/persistence_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ suite: persistence
templates:
- pvc.yaml
- deployment.yaml
- statefulset.yaml
tests:
-
it: persistence enabled
Expand Down Expand Up @@ -88,4 +89,21 @@ tests:
isSubset:
path: metadata.labels
content:
common: label
common: label
-
it: persistence enabled with app.workload.type "sts" and .persistence.type "sts"
values:
- ./values/persistence.yaml
- ./values/app.yaml
set:
app.workload.type: sts
persistence.type: sts
asserts:
-
template: pvc.yaml
hasDocuments:
count: 0
-
template: statefulset.yaml
isNotEmpty:
path: spec.volumeClaimTemplates[0]
Loading

0 comments on commit 70a158f

Please sign in to comment.