From bf290931ce034a85e9fe694c010203d69450c087 Mon Sep 17 00:00:00 2001 From: Andrea Pulvirenti Date: Sat, 21 Oct 2023 12:03:32 +0200 Subject: [PATCH] existing configmap provided Andrea Pulvirenti --- .../templates/_helpers.tpl | 55 +++++++++++++++++++ .../templates/configmap.yaml | 13 +++++ .../templates/deployment.yaml | 16 ++++-- .../templates/persistentvolume.yaml | 10 +++- .../values.yaml | 4 ++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 charts/nfs-subdir-external-provisioner/templates/configmap.yaml diff --git a/charts/nfs-subdir-external-provisioner/templates/_helpers.tpl b/charts/nfs-subdir-external-provisioner/templates/_helpers.tpl index 5c76a13a..656fb34f 100644 --- a/charts/nfs-subdir-external-provisioner/templates/_helpers.tpl +++ b/charts/nfs-subdir-external-provisioner/templates/_helpers.tpl @@ -1,3 +1,11 @@ + +{{/* + Allow namespace to be overriden +*/}} +{{- define "common.name.namespace" -}} +{{- default .Release.Namespace .Values.namespaceOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + {{/* vim: set filetype=mustache: */}} {{/* Expand the name of the chart. @@ -101,3 +109,50 @@ Selector labels app: {{ template "nfs-subdir-external-provisioner.name" . }} release: {{ .Release.Name }} {{- end }} + +{{/* +Return true if an existing configmap name has been provided +*/}} +{{- define "nfs.existing-configmap-provided" -}} +{{- if .Values.nfs.existingConfigMap.name -}} + {{- true -}} +{{- end -}} +{{- end -}} + +{{/* +Return the existing NFS Configmap Name +*/}} +{{- define "nfs.existing-configmap" -}} + {{- if .Values.nfs.existingConfigMap.name -}} + {{- printf "%s" (tpl .Values.nfs.existingConfigMap.name $) -}} + {{- else -}} + {{- printf "\n%s CONFIGMAP ERROR: no existing configmap has been provided" }} + {{- end -}} +{{- end }} + + +{{/* +Reuses the value from an existing configmap, otherwise sets its value to a default value. + +Usage: +{{ include "common.configmap.lookup" (dict "secret" "configmap-name" "key" "keyName" "defaultValue" .Values.myValue "context" $) }} + +Params: + - configmapName - String - Required - Name of the 'Secret' resource where the password is stored. + - key - String - Required - Name of the key in the secret. + - defaultValue - String - Required - The path to the validating value in the values.yaml, e.g: "mysql.password". Will pick first parameter with a defined value. + - context - Context - Required - Parent context. + +*/}} +{{- define "common.configmap.lookup" -}} +{{- $value := "" -}} +{{- $configmapData := (lookup "v1" "ConfigMap" (include "common.names.namespace" .context) .configmapName).data -}} +{{- if and $configmapData (hasKey $configmapData .key) -}} + {{- $value = index $configmapData .key -}} +{{- else if .defaultValue -}} + {{- $value = .defaultValue | toString -}} +{{- end -}} +{{- if $value -}} +{{- printf "%s" $value -}} +{{- end -}} +{{- end -}} diff --git a/charts/nfs-subdir-external-provisioner/templates/configmap.yaml b/charts/nfs-subdir-external-provisioner/templates/configmap.yaml new file mode 100644 index 00000000..a060a5c4 --- /dev/null +++ b/charts/nfs-subdir-external-provisioner/templates/configmap.yaml @@ -0,0 +1,13 @@ +{{ if (include "nfs.existing-configmap-provided" .) }} +{{- $existingConfigMap := (lookup "v1" "ConfigMap" .Values.nfs.existingConfigMap.namespace .Values.nfs.existingConfigMap.name).data | default dict }} +{{- if not $existingConfigMap }} +apiVersion: v1 +data: + path: {{ .Values.nfs.path }} + server: {{ .Values.nfs.server}} +immutable: false +kind: ConfigMap +metadata: + name: {{ template "nfs.existing-configmap" . }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/nfs-subdir-external-provisioner/templates/deployment.yaml b/charts/nfs-subdir-external-provisioner/templates/deployment.yaml index 15a574b2..da23bca2 100644 --- a/charts/nfs-subdir-external-provisioner/templates/deployment.yaml +++ b/charts/nfs-subdir-external-provisioner/templates/deployment.yaml @@ -1,3 +1,11 @@ + +{{- $server := .Values.nfs.server -}} +{{- $path := .Values.nfs.path -}} +{{- if (include "nfs.existing-configmap-provided" .) -}} +{{- $server := (include "common.configmap.lookup" (dict "config" .Values.nfs.existingConfigMap.name "key" "server" "defaultValue" .Values.nfs.server "context")) -}} +{{- $path := (include "common.configmap.lookup" (dict "config" .Values.nfs.existingConfigMap.name "key" "path" "defaultValue" .Values.nfs.path "context")) -}} +{{- end -}} + apiVersion: apps/v1 kind: Deployment metadata: @@ -54,9 +62,9 @@ spec: - name: PROVISIONER_NAME value: {{ template "nfs-subdir-external-provisioner.provisionerName" . }} - name: NFS_SERVER - value: {{ .Values.nfs.server }} + value: {{ $server }} - name: NFS_PATH - value: {{ .Values.nfs.path }} + value: {{ $path }} {{- if eq .Values.leaderElection.enabled false }} - name: ENABLE_LEADER_ELECTION value: "false" @@ -74,8 +82,8 @@ spec: claimName: pvc-{{ template "nfs-subdir-external-provisioner.fullname" . }} {{- else }} nfs: - server: {{ .Values.nfs.server }} - path: {{ .Values.nfs.path }} + server: {{ $server}} + path: {{ $path }} {{- end }} {{- if and (.Values.tolerations) (semverCompare "^1.6-0" .Capabilities.KubeVersion.GitVersion) }} tolerations: diff --git a/charts/nfs-subdir-external-provisioner/templates/persistentvolume.yaml b/charts/nfs-subdir-external-provisioner/templates/persistentvolume.yaml index 9d6ba4d5..a25430ef 100644 --- a/charts/nfs-subdir-external-provisioner/templates/persistentvolume.yaml +++ b/charts/nfs-subdir-external-provisioner/templates/persistentvolume.yaml @@ -1,4 +1,10 @@ {{ if .Values.nfs.mountOptions -}} +{{- $server := .Values.nfs.server -}} +{{- $path := .Values.nfs.path -}} +{{- if (include "nfs.existing-configmap-provided" .) -}} +{{- $server := (include "common.configmap.lookup" (dict "config" .Values.nfs.existingConfigMap.name "key" "server" "defaultValue" .Values.nfs.server "context")) -}} +{{- $path := (include "common.configmap.lookup" (dict "config" .Values.nfs.existingConfigMap.name "key" "path" "defaultValue" .Values.nfs.path "context")) -}} +{{- end -}} apiVersion: v1 kind: PersistentVolume metadata: @@ -21,6 +27,6 @@ spec: {{- end }} {{- end }} nfs: - server: {{ .Values.nfs.server }} - path: {{ .Values.nfs.path }} + server: {{ $server }} + path: {{ $path }} {{ end -}} diff --git a/charts/nfs-subdir-external-provisioner/values.yaml b/charts/nfs-subdir-external-provisioner/values.yaml index b7d11878..3cd934b3 100644 --- a/charts/nfs-subdir-external-provisioner/values.yaml +++ b/charts/nfs-subdir-external-provisioner/values.yaml @@ -14,6 +14,10 @@ nfs: volumeName: nfs-subdir-external-provisioner-root # Reclaim policy for the main nfs volume reclaimPolicy: Retain + # existing configmap in order to get server and path + existingConfigMap: + name: + namespace: default # For creating the StorageClass automatically: storageClass: