simplify configmap and secret classes

This commit is contained in:
Stavros kois
2023-01-26 14:10:05 +02:00
parent f83ed45877
commit 564115d7bf
6 changed files with 49 additions and 39 deletions

View File

@@ -1,30 +1,26 @@
{{- define "ix.v1.common.class.configmap" -}}
{{- $configName := .configName -}}
{{- $data := .data -}}
{{- $contentType := .contentType -}}
{{- $configLabels := .labels -}}
{{- $configAnnotations := .annotations -}}
{{- $values := .values -}}
{{- $root := .root }}
---
apiVersion: {{ include "ix.v1.common.capabilities.configMap.apiVersion" $root }}
kind: ConfigMap
metadata:
name: {{ $configName }}
{{- $labels := (mustMerge ($configLabels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}}
name: {{ $values.name }}
{{- $labels := (mustMerge ($values.labels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.labels.render" (dict "root" $root "labels" $labels) | trim) }}
labels:
{{- . | nindent 4 }}
{{- end -}}
{{- $annotations := (mustMerge ($configAnnotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}}
{{- $annotations := (mustMerge ($values.annotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.annotations.render" (dict "root" $root "annotations" $annotations) | trim) }}
annotations:
{{- . | nindent 4 }}
{{- end }}
data:
{{- if eq $contentType "yaml" }}
{{- $data | nindent 2 }}
{{- if eq $values.contentType "yaml" }}
{{- $values.data | nindent 2 }}
{{- else -}} {{/* This should never happen, unless there is a mistake in the caller of this class */}}
{{- fail (printf "Invalid content type (%s) for configmap. Valid types are scalar and key_value" $contentType) -}}
{{- fail (printf "Invalid content type (%s) for configmap. Valid types are yaml" $values.contentType) -}}
{{- end -}}
{{- end -}}

View File

@@ -1,51 +1,46 @@
{{- define "ix.v1.common.class.secret" -}}
{{- $secretName := .secretName -}}
{{- $data := .data -}}
{{- $contentType := .contentType -}}
{{- $secretType := .secretType -}} {{/* Optional */}}
{{- $secretLabels := .labels -}}
{{- $secretAnnotations := .annotations -}}
{{- $values := .values -}}
{{- $root := .root -}}
{{- $typeClass := "Opaque" -}} {{/* Default to Opaque */}}
{{- if eq $contentType "certificate" -}} {{/* Certificate content has specific type */}}
{{- if eq $values.contentType "certificate" -}} {{/* Certificate content has specific type */}}
{{- $typeClass = (include "ix.v1.common.capabilities.secret.certificate.type" $root) -}}
{{- else if eq $contentType "pullSecret" -}} {{/* imagePullSecrets content has specific type */}}
{{- else if eq $values.contentType "pullSecret" -}} {{/* imagePullSecrets content has specific type */}}
{{- $typeClass = (include "ix.v1.common.capabilities.secret.imagePullSecret.type" $root) -}}
{{- end -}}
{{- if $secretType -}} {{/* If custom type is defined */}}
{{- $typeClass = $secretType -}}
{{- if $values.secretType -}} {{/* If custom type is defined */}}
{{- $typeClass = $values.secretType -}}
{{- end }}
---
apiVersion: {{ include "ix.v1.common.capabilities.secret.apiVersion" $root }}
kind: Secret
type: {{ $typeClass }}
metadata:
name: {{ $secretName }}
{{- $labels := (mustMerge ($secretLabels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}}
name: {{ $values.name }}
{{- $labels := (mustMerge ($values.labels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.labels.render" (dict "root" $root "labels" $labels) | trim) }}
labels:
{{- . | nindent 4 }}
{{- end -}}
{{- $annotations := (mustMerge ($secretAnnotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}}
{{- $annotations := (mustMerge ($values.annotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.annotations.render" (dict "root" $root "annotations" $annotations) | trim) }}
annotations:
{{- . | nindent 4 }}
{{- end -}}
{{- if (mustHas $contentType (list "pullSecret" "certificate")) }}
{{- if (mustHas $values.contentType (list "pullSecret" "certificate")) }}
data:
{{- if eq $contentType "pullSecret" }}
.dockerconfigjson: {{ $data | toJson | b64enc }}
{{- else if eq $contentType "certificate" }}
{{- range $k, $v := $data }}
{{- if eq $values.contentType "pullSecret" }}
.dockerconfigjson: {{ $values.data | toJson | b64enc }}
{{- else if eq $values.contentType "certificate" }}
{{- range $k, $v := $values.data }}
{{- $k | nindent 2 }}: {{ $v | b64enc }}
{{- end -}}
{{- end -}}
{{- else if eq $contentType "yaml" }}
{{- else if eq $values.contentType "yaml" }}
stringData:
{{- $data | nindent 2 }}
{{- $values.data | nindent 2 }}
{{- else -}}
{{- fail (printf "Invalid content type (%s) for secret. Valid types are pullSecret, certificate, scalar and key_value" $contentType) -}}
{{- fail (printf "Invalid content type (%s) for secret. Valid types are pullSecret, certificate, scalar and key_value" $values.contentType) -}}
{{- end -}}
{{- end -}}

View File

@@ -21,7 +21,12 @@
{{- $_ := set $certData $tlsCrtKey (include "ix.v1.common.certificate.get" (dict "root" $root "cert" $cert "key" "certificate")) -}}
{{- $_ := set $certData $tlsPrivateKey (include "ix.v1.common.certificate.get" (dict "root" $root "cert" $cert "key" "privatekey")) -}}
{{- $values := dict -}}
{{- $_ := set $values "data" $certData -}}
{{- $_ := set $values "name" $secretName -}}
{{- $_ := set $values "contentType" "certificate" -}}
{{/* Create the Secret */}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "secretName" $secretName "data" $certData "contentType" "certificate") -}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "values" $values) -}}
{{- end -}}
{{- end -}}

View File

@@ -55,12 +55,18 @@
{{/* Convert to Yaml before sending to classes */}}
{{- $classData = toYaml $classData -}}
{{- $contentType := "yaml" -}}
{{- $values := dict -}}
{{- $_ := set $values "data" $classData -}}
{{- $_ := set $values "name" $objectName -}}
{{- $_ := set $values "contentType" "yaml" -}}
{{- $_ := set $values "labels" $objectData.labels -}}
{{- $_ := set $values "annotations" $objectData.annotations -}}
{{- $_ := set $values "secretType" $objectData.secretType -}}
{{/* Create ConfigMap or Secret */}}
{{- if eq $objectType "configmap" -}}
{{- include "ix.v1.common.class.configmap" (dict "root" $root "configName" $objectName "contentType" $contentType "data" $classData "labels" $objectData.labels "annotations" $objectData.annotations) -}}
{{- include "ix.v1.common.class.configmap" (dict "root" $root "values" $values) -}}
{{- else if eq $objectType "secret" -}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "secretName" $objectName "secretType" $objectData.secretType "contentType" $contentType "data" $classData "labels" $objectData.labels "annotations" $objectData.annotations) -}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "values" $values) -}}
{{- end -}}
{{- end -}}

View File

@@ -39,7 +39,12 @@
{{- $_ := set $registrySecret "auths" dict -}}
{{- $_ := set $registrySecret.auths (printf "%s" .registry) $registry -}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "secretName" $secretName "data" $registrySecret "contentType" "pullSecret") -}}
{{- $values := dict -}}
{{- $_ := set $values "data" $registrySecret -}}
{{- $_ := set $values "name" $secretName -}}
{{- $_ := set $values "contentType" "pullSecret" -}}
{{- include "ix.v1.common.class.secret" (dict "root" $root "values" $values) -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -23,8 +23,11 @@
{{- if $data -}}
{{/* Create the ConfigMap */}}
{{- $data := toYaml $data -}}
{{- include "ix.v1.common.class.configmap" (dict "root" $root "configName" "portal" "contentType" "yaml" "data" $data) -}}
{{- $values := dict -}}
{{- $_ := set $values "data" (toYaml $data) -}}
{{- $_ := set $values "contentType" "yaml" -}}
{{- $_ := set $values "name" "portal" -}}
{{- include "ix.v1.common.class.configmap" (dict "root" $root "values" $values) -}}
{{- end -}}
{{- end -}}
{{- end -}}