From 17fa74c78f87d8a6560ffffbbc23762fa8b01dff Mon Sep 17 00:00:00 2001 From: Arzianghanchi Date: Thu, 27 Mar 2025 18:14:15 +0000 Subject: [PATCH] feat: support multiple ConfigMap objects in Helm chart --- .../helmchart/config/override-values-v2.yaml | 17 +++++++++ charts/helmchart/templates/NOTES.txt | 38 +++++++++++-------- charts/helmchart/templates/configmap.yaml | 19 ++++++---- charts/helmchart/templates/deployment.yaml | 10 +++-- 4 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 charts/helmchart/config/override-values-v2.yaml diff --git a/charts/helmchart/config/override-values-v2.yaml b/charts/helmchart/config/override-values-v2.yaml new file mode 100644 index 0000000..6c047d2 --- /dev/null +++ b/charts/helmchart/config/override-values-v2.yaml @@ -0,0 +1,17 @@ +service: + service1: + type: ClusterIP + port: 80 + containerPort: 8080 + targetPort: 8080 +configmaps: + enabled: true + config: + - name: config1 + data: + key1: "value1" + key2: "value2" + - name: config2 + data: + keyA: "valueA" + keyB: "valueB" \ No newline at end of file diff --git a/charts/helmchart/templates/NOTES.txt b/charts/helmchart/templates/NOTES.txt index 41d5cff..64b9e97 100644 --- a/charts/helmchart/templates/NOTES.txt +++ b/charts/helmchart/templates/NOTES.txt @@ -1,22 +1,30 @@ -1. Get the application URL by running these commands: {{- if .Values.ingress.enabled }} -{{- range $host := .Values.ingress.hosts }} +{{- range .Values.ingress.hosts }} {{- range .paths }} - http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $.host }}{{ .path }} {{- end }} {{- end }} -{{- else if contains "NodePort" .Values.service.type }} - export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helmchart.fullname" . }}) - export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") +{{- end }} + +{{- range $name, $service := .Values.service }} +{{- if eq $service.type "NodePort" }} + export NODE_PORT=$(kubectl get --namespace {{ $.Release.Namespace }} -o jsonpath='{.spec.ports[0].nodePort}' services {{ include "helmchart.fullname" $ }}) + export NODE_IP=$(kubectl get nodes --namespace {{ $.Release.Namespace }} -o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}') echo http://$NODE_IP:$NODE_PORT -{{- else if contains "LoadBalancer" .Values.service.type }} - NOTE: It may take a few minutes for the LoadBalancer IP to be available. - You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "helmchart.fullname" . }}' - export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helmchart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") - echo http://$SERVICE_IP:{{ .Values.service.port }} -{{- else if contains "ClusterIP" .Values.service.type }} - export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "helmchart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") - export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") +{{- end }} + +{{- if eq $service.type "LoadBalancer" }} + echo "NOTE: It may take a few minutes for the LoadBalancer IP to be available." + echo "You can watch the status by running:" + echo "kubectl get --namespace {{ $.Release.Namespace }} svc -w {{ include "helmchart.fullname" $ }}" + export SERVICE_IP=$(kubectl get svc --namespace {{ $.Release.Namespace }} {{ include "helmchart.fullname" $ }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}') + echo http://$SERVICE_IP:{{ $service.port }} +{{- end }} + +{{- if eq $service.type "ClusterIP" }} + export POD_NAME=$(kubectl get pods --namespace {{ $.Release.Namespace }} -l "app.kubernetes.io/name={{ include "helmchart.name" $ }},app.kubernetes.io/instance={{ $.Release.Name }}" -o jsonpath='{.items[0].metadata.name}') + export CONTAINER_PORT=$(kubectl get pod --namespace {{ $.Release.Namespace }} $POD_NAME -o jsonpath='{.spec.containers[0].ports[0].containerPort}') echo "Visit http://127.0.0.1:8080 to use your application" - kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT + kubectl --namespace {{ $.Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT {{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/helmchart/templates/configmap.yaml b/charts/helmchart/templates/configmap.yaml index 0784c86..cafabfb 100644 --- a/charts/helmchart/templates/configmap.yaml +++ b/charts/helmchart/templates/configmap.yaml @@ -1,15 +1,18 @@ -{{- if .Values.configmap.enabled }} +{{- if .Values.configmaps.enabled }} + {{- range .Values.configmaps.config}} +--- apiVersion: v1 kind: ConfigMap metadata: - name: {{ include "helmchart.fullname" . }}-config - namespace: {{ include "helmchart.namespace" . }} + name: {{ include "helmchart.fullname" $ }}-{{.name}}-config + namespace: {{ include "helmchart.namespace" $ }} labels: {{- include "helmchart.labels" $ | nindent 4 }} annotations: - "helm.sh/hook-weight": "4" + "helm.sh/hook-weight": "4" data: -{{- with .Values.configmap.configs }} - {{- toYaml . | nindent 2 -}} - {{- end}} -{{- end}} + {{- range $key, $val := .data }} + {{ $key }}: {{ $val | quote }} + {{- end }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/helmchart/templates/deployment.yaml b/charts/helmchart/templates/deployment.yaml index 4bc2aff..f3cfbd1 100644 --- a/charts/helmchart/templates/deployment.yaml +++ b/charts/helmchart/templates/deployment.yaml @@ -44,17 +44,21 @@ spec: {{- end }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: + {{- range $key, $value := .Values.service}} - name: http - containerPort: {{ .Values.service.containerPort | default .Values.service.port }} + containerPort: {{ $value.containerPort | default $value.port }} protocol: TCP + {{- end }} {{- if .Values.commands }} command: {{- toYaml .Values.commands | nindent 12 }} {{- end }} envFrom: - {{- if .Values.configmap.enabled }} + {{- if .Values.configmaps.enabled }} + {{- range .Values.configmaps.config }} - configMapRef: - name: {{ include "helmchart.fullname" . }}-config + name: {{ include "helmchart.fullname" $ }}-{{ .name }}-config + {{- end }} {{- end }} {{- if .Values.secret.enabled }} - secretRef: