From f898566ff328ace43a5ae14908d7eebfac20d055 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Tue, 27 Dec 2022 13:55:25 +0200 Subject: [PATCH] extract more names --- .../common/1.0.0/templates/class/_service.tpl | 6 +- .../1.0.0/templates/class/_serviceAccount.tpl | 13 ++-- .../templates/lib/certificate/_certSecret.tpl | 23 +----- .../1.0.0/templates/lib/general/_names.tpl | 70 +++++++++++++++++++ 4 files changed, 77 insertions(+), 35 deletions(-) diff --git a/library/common/1.0.0/templates/class/_service.tpl b/library/common/1.0.0/templates/class/_service.tpl index 489232bdfe..0339a01153 100644 --- a/library/common/1.0.0/templates/class/_service.tpl +++ b/library/common/1.0.0/templates/class/_service.tpl @@ -5,11 +5,7 @@ {{- $root := .root -}} {{- $defaultServiceType := $root.Values.global.defaults.serviceType -}} {{- $defaultPortProtocol := $root.Values.global.defaults.portProtocol -}} - {{- $svcName := include "ix.v1.common.names.fullname" $root -}} - - {{- if and (hasKey $svcValues "nameOverride") $svcValues.nameOverride -}} - {{- $svcName = (printf "%v-%v" $svcName $svcValues.nameOverride) -}} - {{- end -}} + {{- $svcName := include "ix.v1.common.names.service" (dict "root" $root "svcValues" $svcValues) -}} {{- $svcType := $svcValues.type | default $defaultServiceType -}} {{- if $root.Values.hostNetwork -}} diff --git a/library/common/1.0.0/templates/class/_serviceAccount.tpl b/library/common/1.0.0/templates/class/_serviceAccount.tpl index 29097f172b..ecbf279de3 100644 --- a/library/common/1.0.0/templates/class/_serviceAccount.tpl +++ b/library/common/1.0.0/templates/class/_serviceAccount.tpl @@ -1,26 +1,21 @@ {{/* Template for a ServiceAccount object, can only be called by the spawner */}} {{/* A serviceAccount object and "root" is passed from the spawner */}} {{- define "ix.v1.common.class.serviceAccount" -}} - {{- $values := .serviceAccount -}} + {{- $saValues := .serviceAccount -}} {{- $root := .root -}} - {{- $saName := include "ix.v1.common.names.fullname" $root -}} - - {{- if and (hasKey $values "nameOverride") $values.nameOverride -}} - {{- $saName = (printf "%v-%v" $saName $values.nameOverride) -}} - {{- end }} - + {{- $saName := include "ix.v1.common.names.serviceAccount" (dict "root" $root "saValues" $saValues) }} --- apiVersion: {{ include "ix.v1.common.capabilities.serviceAccount.apiVersion" $root }} kind: ServiceAccount metadata: name: {{ $saName }} - {{- $labels := (mustMerge ($values.labels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}} + {{- $labels := (mustMerge ($saValues.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 ($values.annotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}} + {{- $annotations := (mustMerge ($saValues.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 }} diff --git a/library/common/1.0.0/templates/lib/certificate/_certSecret.tpl b/library/common/1.0.0/templates/lib/certificate/_certSecret.tpl index e2d9b31726..4b9efb3962 100644 --- a/library/common/1.0.0/templates/lib/certificate/_certSecret.tpl +++ b/library/common/1.0.0/templates/lib/certificate/_certSecret.tpl @@ -5,33 +5,14 @@ {{- $tlsCrtKey := "tls.crt" -}} {{- $tlsPrivateKey := "tls.key" -}} - - {{- if ne $name ($name | lower) -}} - {{- fail (printf "Certificate has invalid name (%s). Name must be lowercase." $name) -}} - {{- end -}} - {{- if contains "_" $name -}} - {{- fail (printf "Certificate has invalid name (%s). Name cannot contain underscores (_)" $name) -}} - {{- end -}} - - {{/* Default to $name if there is not a nameOverride given */}} - {{- if not $cert.nameOverride -}} - {{- $_ := set $cert "nameOverride" $name -}} - {{- end -}} - {{- if not (hasKey $cert "id") -}} {{/* This is something that should not happen when using this library */}} {{- fail (printf "Certificate (%s) has no key" $cert.nameOverride) -}} {{- end -}} {{/* It can only happen when consuing this function within this library */}} {{- $certID := (toString $cert.id) -}} {{- if (include "ix.v1.common.certificate.exists" (dict "root" $root "certID" $certID)) -}} - {{/* Generate secret name here so we can pass it to persistence if needed */}} - {{- $secretName := include "ix.v1.common.names.fullname" $root -}} - {{- if $cert.nameOverride -}} - {{- $secretName = (printf "%v-%v-%v-%v" $secretName $cert.nameOverride "ixcert" $certID) -}} - {{- else -}} - {{- $secretName = (printf "%v-%v-%v" $secretName "ixcert" $certID) -}} - {{- end -}} - {{- $secretName = (printf "%v-%v" $secretName $root.Release.Revision) -}} + {{/* Generate secret name here so we can pass it to persistenc */}} + {{- $secretName := include "ix.v1.common.names.certificateSecret" (dict "root" $root "certValues" $cert "certName" $name "certID" $certID) -}} {{- include "ix.v1.common.certificate.persistence" (dict "root" $root "secretName" $secretName "cert" $cert "certID" $certID "tlsKey" $tlsCrtKey "type" "cert") -}} {{- include "ix.v1.common.certificate.persistence" (dict "root" $root "secretName" $secretName "cert" $cert "certID" $certID "tlsKey" $tlsPrivateKey "type" "key") -}} diff --git a/library/common/1.0.0/templates/lib/general/_names.tpl b/library/common/1.0.0/templates/lib/general/_names.tpl index c3dd1105a2..1e64e9d70e 100644 --- a/library/common/1.0.0/templates/lib/general/_names.tpl +++ b/library/common/1.0.0/templates/lib/general/_names.tpl @@ -88,6 +88,24 @@ nameOverride applies only to the current chart {{- $serviceAccountName -}} {{- end -}} +{{/* Returns the service name */}} +{{- define "ix.v1.common.names.serviceAccount" -}} + {{- $root := .root -}} + {{- $saValues := .saValues -}} + + {{- if or (not $root) (not $saValues) -}} + {{- fail "Named function did not receive required values" -}} + {{- end -}} + + {{- $saName := include "ix.v1.common.names.fullname" $root -}} + + {{- if and (hasKey $saValues "nameOverride") $saValues.nameOverride -}} + {{- $saName = (printf "%v-%v" $saName $saValues.nameOverride) -}} + {{- end -}} + + {{- $saName -}} +{{- end -}} + {{/* Returns the pvcName. */}} {{- define "ix.v1.common.names.pvc" -}} {{- $root := .root -}} @@ -170,3 +188,55 @@ nameOverride applies only to the current chart {{- $generatedName -}} {{- end -}} + +{{/* Returns the name for certificate secret */}} +{{- define "ix.v1.common.names.certificateSecret" -}} + {{- $root := .root -}} + {{- $certName := .certName -}} + {{- $certValues := .certValues -}} + {{- $certID := .certID -}} + + {{- if or (not $root) (not $certName) (not $certValues) (not $certID) -}} + {{- fail "Named function did not receive required values" -}} + {{- end -}} + + {{- if ne $certName ($certName | lower) -}} + {{- fail (printf "Certificate has invalid name (%s). Name must be lowercase." $certName) -}} + {{- end -}} + {{- if contains "_" $certName -}} + {{- fail (printf "Certificate has invalid name (%s). Name cannot contain underscores (_)" $certName) -}} + {{- end -}} + + {{/* Default to $name if there is not a nameOverride given */}} + {{- if not $certValues.nameOverride -}} + {{- $_ := set $certValues "nameOverride" $certName -}} + {{- end -}} + + {{- $secretName := include "ix.v1.common.names.fullname" $root -}} + {{- if $certValues.nameOverride -}} + {{- $secretName = (printf "%v-%v-%v-%v" $secretName $certValues.nameOverride "ixcert" $certID) -}} + {{- else -}} + {{- $secretName = (printf "%v-%v-%v" $secretName "ixcert" $certID) -}} + {{- end -}} + {{- $secretName = (printf "%v-%v" $secretName $root.Release.Revision) -}} + + {{- $secretName -}} +{{- end -}} + +{{/* Returns the serviceName. */}} +{{- define "ix.v1.common.names.service" -}} + {{- $root := .root -}} + {{- $svcValues := .svcValues -}} + + {{- if or (not $root) (not $svcValues) -}} + {{- fail "Named function did not receive required values" -}} + {{- end -}} + + {{- $svcName := include "ix.v1.common.names.fullname" $root -}} + + {{- if and (hasKey $svcValues "nameOverride") $svcValues.nameOverride -}} + {{- $svcName = (printf "%v-%v" $svcName $svcValues.nameOverride) -}} + {{- end -}} + + {{- $svcName -}} +{{- end -}}