From 10633873fbd43bc1e26e84ab05fadb72663d426a Mon Sep 17 00:00:00 2001
From: Denys SAVCHENKO <savchenko@apc.in2p3.fr>
Date: Tue, 25 Mar 2025 17:54:15 +0100
Subject: [PATCH] better chart init

---
 docs/kubernetes/06_my_first_helm.md           | 19 +++---
 .../jupyter-chart/templates/_helpers.tpl      | 62 +++++++++++++++++++
 .../jupyter-chart/templates/_helpers.tpl      | 62 +++++++++++++++++++
 .../jupyter-chart/templates/_helpers.tpl      | 62 +++++++++++++++++++
 .../jupyter-chart/templates/_helpers.tpl      | 62 +++++++++++++++++++
 5 files changed, 259 insertions(+), 8 deletions(-)
 create mode 100644 files/charts/step0/jupyter-chart/templates/_helpers.tpl
 create mode 100644 files/charts/step1/jupyter-chart/templates/_helpers.tpl
 create mode 100644 files/charts/step2/jupyter-chart/templates/_helpers.tpl
 create mode 100644 files/charts/step3/jupyter-chart/templates/_helpers.tpl

diff --git a/docs/kubernetes/06_my_first_helm.md b/docs/kubernetes/06_my_first_helm.md
index be3b01f..0e1e070 100644
--- a/docs/kubernetes/06_my_first_helm.md
+++ b/docs/kubernetes/06_my_first_helm.md
@@ -91,12 +91,12 @@ Maintenant, nous allons créer le chart Helm à partir des manifests que nous av
 Créer un projet de chart avec :
 
 ```bash
-helm create example-chart
-tree example-chart
+helm create jupyter-chart
+tree jupyter-chart
 ```
 
 ```{.console .no-copy}
-example-chart
+jupyter-chart
 ├── charts
 ├── Chart.yaml
 ├── templates
@@ -124,18 +124,19 @@ example-chart
 
 ## Creation du chart Jupyter
 
-Nous allons suivre la structure de l'exemple pour créer notre propre charte d'application jupyter.
+Nous allons remplaces les manifests de base pour créer notre propre charte d'application Jupyter.
 
 ```bash
-mkdir -p jupyter-chart/templates
-cp example-chart/Chart.yaml jupyter-chart/Chart.yaml
-touch jupyter-chart/values.yaml
 cp configmap.yaml jupyter-chart/templates/.
 cp ingress.yaml jupyter-chart/templates/.
 cp jupyter-claim0-persistentvolumeclaim.yaml jupyter-chart/templates/pvc.yaml
 cp jupyter-deployment.yaml jupyter-chart/templates/deployment.yaml
 cp jupyter-service.yaml jupyter-chart/templates/service.yaml
 cp secret.yaml jupyter-chart/templates/.
+rm jupyter-chart/templates/serviceaccount.yaml
+rm jupyter-chart/templates/hpa.yaml
+rm -r jupyter-chart/tests
+echo '' > jupyter-chart/templates/NOTES.txt
 tree jupyter-chart
 ```
 
@@ -144,6 +145,7 @@ tree jupyter-chart/
 jupyter-chart/
 ├── Chart.yaml
 ├── templates
+│   ├── _helpers.tpl
 │   ├── configmap.yaml
 │   ├── insgress.yaml
 │   ├── pvc.yaml
@@ -152,7 +154,7 @@ jupyter-chart/
 │   └── secret.yaml
 └── values.yaml
 
-2 directories, 8 files
+2 directories, 9 files
 ```
 
 Modifions le metadata du chart :
@@ -618,3 +620,4 @@ spec:
     ```
 
 Le chart de référence à ce stade [est disponible](https://gitlab.in2p3.fr/si-apc/tp-kubernetes-si/-/tree/main/files/charts/step3)
+
diff --git a/files/charts/step0/jupyter-chart/templates/_helpers.tpl b/files/charts/step0/jupyter-chart/templates/_helpers.tpl
new file mode 100644
index 0000000..c4247a1
--- /dev/null
+++ b/files/charts/step0/jupyter-chart/templates/_helpers.tpl
@@ -0,0 +1,62 @@
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "jupyter-chart.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 "jupyter-chart.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 "jupyter-chart.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+{{/*
+Common labels
+*/}}
+{{- define "jupyter-chart.labels" -}}
+helm.sh/chart: {{ include "jupyter-chart.chart" . }}
+{{ include "jupyter-chart.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+{{/*
+Selector labels
+*/}}
+{{- define "jupyter-chart.selectorLabels" -}}
+app.kubernetes.io/name: {{ include "jupyter-chart.name" . }}
+app.kubernetes.io/instance: {{ .Release.Name }}
+{{- end }}
+
+{{/*
+Create the name of the service account to use
+*/}}
+{{- define "jupyter-chart.serviceAccountName" -}}
+{{- if .Values.serviceAccount.create }}
+{{- default (include "jupyter-chart.fullname" .) .Values.serviceAccount.name }}
+{{- else }}
+{{- default "default" .Values.serviceAccount.name }}
+{{- end }}
+{{- end }}
diff --git a/files/charts/step1/jupyter-chart/templates/_helpers.tpl b/files/charts/step1/jupyter-chart/templates/_helpers.tpl
new file mode 100644
index 0000000..c4247a1
--- /dev/null
+++ b/files/charts/step1/jupyter-chart/templates/_helpers.tpl
@@ -0,0 +1,62 @@
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "jupyter-chart.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 "jupyter-chart.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 "jupyter-chart.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+{{/*
+Common labels
+*/}}
+{{- define "jupyter-chart.labels" -}}
+helm.sh/chart: {{ include "jupyter-chart.chart" . }}
+{{ include "jupyter-chart.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+{{/*
+Selector labels
+*/}}
+{{- define "jupyter-chart.selectorLabels" -}}
+app.kubernetes.io/name: {{ include "jupyter-chart.name" . }}
+app.kubernetes.io/instance: {{ .Release.Name }}
+{{- end }}
+
+{{/*
+Create the name of the service account to use
+*/}}
+{{- define "jupyter-chart.serviceAccountName" -}}
+{{- if .Values.serviceAccount.create }}
+{{- default (include "jupyter-chart.fullname" .) .Values.serviceAccount.name }}
+{{- else }}
+{{- default "default" .Values.serviceAccount.name }}
+{{- end }}
+{{- end }}
diff --git a/files/charts/step2/jupyter-chart/templates/_helpers.tpl b/files/charts/step2/jupyter-chart/templates/_helpers.tpl
new file mode 100644
index 0000000..c4247a1
--- /dev/null
+++ b/files/charts/step2/jupyter-chart/templates/_helpers.tpl
@@ -0,0 +1,62 @@
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "jupyter-chart.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 "jupyter-chart.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 "jupyter-chart.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+{{/*
+Common labels
+*/}}
+{{- define "jupyter-chart.labels" -}}
+helm.sh/chart: {{ include "jupyter-chart.chart" . }}
+{{ include "jupyter-chart.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+{{/*
+Selector labels
+*/}}
+{{- define "jupyter-chart.selectorLabels" -}}
+app.kubernetes.io/name: {{ include "jupyter-chart.name" . }}
+app.kubernetes.io/instance: {{ .Release.Name }}
+{{- end }}
+
+{{/*
+Create the name of the service account to use
+*/}}
+{{- define "jupyter-chart.serviceAccountName" -}}
+{{- if .Values.serviceAccount.create }}
+{{- default (include "jupyter-chart.fullname" .) .Values.serviceAccount.name }}
+{{- else }}
+{{- default "default" .Values.serviceAccount.name }}
+{{- end }}
+{{- end }}
diff --git a/files/charts/step3/jupyter-chart/templates/_helpers.tpl b/files/charts/step3/jupyter-chart/templates/_helpers.tpl
new file mode 100644
index 0000000..c4247a1
--- /dev/null
+++ b/files/charts/step3/jupyter-chart/templates/_helpers.tpl
@@ -0,0 +1,62 @@
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "jupyter-chart.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 "jupyter-chart.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 "jupyter-chart.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+{{/*
+Common labels
+*/}}
+{{- define "jupyter-chart.labels" -}}
+helm.sh/chart: {{ include "jupyter-chart.chart" . }}
+{{ include "jupyter-chart.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+{{/*
+Selector labels
+*/}}
+{{- define "jupyter-chart.selectorLabels" -}}
+app.kubernetes.io/name: {{ include "jupyter-chart.name" . }}
+app.kubernetes.io/instance: {{ .Release.Name }}
+{{- end }}
+
+{{/*
+Create the name of the service account to use
+*/}}
+{{- define "jupyter-chart.serviceAccountName" -}}
+{{- if .Values.serviceAccount.create }}
+{{- default (include "jupyter-chart.fullname" .) .Values.serviceAccount.name }}
+{{- else }}
+{{- default "default" .Values.serviceAccount.name }}
+{{- end }}
+{{- end }}
-- 
GitLab