Skip to content

Commit cc09121

Browse files
committed
feat: add support TPL in fields
1 parent 2f880f1 commit cc09121

File tree

7 files changed

+135
-33
lines changed

7 files changed

+135
-33
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Example: Using CloudNative PG Chart as a Subchart with Global Values and TPL supporting
2+
#
3+
# This example demonstrates how to use this chart as a subchart in a parent chart,
4+
# leveraging global values for dynamic configuration. This is useful when multiple
5+
# charts need to share common values like database names, backup buckets, AWS regions, etc.
6+
#
7+
8+
# Global values (typically defined in parent chart's values.yaml)
9+
# These are shared across multiple subcharts
10+
global:
11+
accountId: "123456789012"
12+
region: us-east-1
13+
resourcePrefix: prod-
14+
15+
# Database configuration (used across subcharts)
16+
dbName: application_db
17+
dbOwner: app_owner
18+
backupBucket: postgresql-backups
19+
iamRole: ekes-postgres-role
20+
environment: production
21+
project: mobile-app
22+
23+
# PostgreSQL Cluster Configuration (subchart-specific)
24+
type: postgresql
25+
mode: standalone
26+
27+
cluster:
28+
instances: 3
29+
30+
# Initialize database with dynamic name from globals
31+
initdb:
32+
database: "{{ .Values.global.dbName }}"
33+
owner: "{{ .Values.global.dbOwner }}"
34+
encoding: UTF8
35+
36+
storage:
37+
size: 10Gi
38+
storageClass: gp3
39+
40+
walStorage:
41+
enabled: true
42+
size: 10Gi
43+
storageClass: gp3
44+
45+
primaryUpdateMethod: switchover
46+
primaryUpdateStrategy: unsupervised
47+
48+
postgresql: {}
49+
50+
# Service account with IAM role annotation from globals
51+
serviceAccountTemplate:
52+
metadata:
53+
annotations:
54+
iamRoleArn: "arn:aws:iam::{{ .Values.global.accountId }}:role/{{ .Values.global.iamRole }}"
55+
labels:
56+
environment: "{{ .Values.global.environment }}"
57+
project: "{{ .Values.global.project }}"
58+
59+
# Backups configuration with dynamic bucket name from globals
60+
backups:
61+
enabled: true
62+
provider: s3
63+
s3:
64+
region: "{{ .Values.global.region }}"
65+
bucket: "{{ .Values.global.resourcePrefix }}{{ .Values.global.backupBucket }}"
66+
path: "/{{ .Release.Name }}"
67+
inheritFromIAMRole: true
68+
69+
scheduledBackups:
70+
- name: daily-full
71+
schedule: "0 6 * * *"
72+
method: barmanObjectStore
73+
retentionPolicy: "14d"
74+
secret:
75+
name: "{{ .Release.Name }}-backup-s3-creds"

charts/cluster/templates/_backup.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ backup:
1717
{{- end }}
1818
jobs: {{ .Values.backups.data.jobs }}
1919

20-
{{- $d := dict "chartFullname" (include "cluster.fullname" .) "scope" .Values.backups "secretPrefix" "backup" }}
20+
{{- $d := dict "chartFullname" (include "cluster.fullname" .) "scope" .Values.backups "secretPrefix" "backup" "context" $ }}
2121
{{- include "cluster.barmanObjectStoreConfig" $d | nindent 2 }}
2222
{{- end }}
2323
{{- end }}

charts/cluster/templates/_barman_object_store.tpl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,81 @@
11
{{- define "cluster.barmanObjectStoreConfig" -}}
22

33
{{- if .scope.endpointURL }}
4-
endpointURL: {{ .scope.endpointURL | quote }}
4+
endpointURL: {{ include "tpl" (dict "value" .scope.endpointURL "context" .context) | quote }}
55
{{- end }}
66

77
{{- if or (.scope.endpointCA.create) (.scope.endpointCA.name) }}
88
endpointCA:
9-
name: {{.scope.endpointCA.name }}
9+
name: {{ include "tpl" (dict "value" .scope.endpointCA.name "context" .context) }}
1010
key: {{ .scope.endpointCA.key }}
1111
{{- end }}
1212

1313
{{- if .scope.destinationPath }}
14-
destinationPath: {{ .scope.destinationPath }}
14+
destinationPath: {{ include "tpl" (dict "value" .scope.destinationPath "context" .context) | quote }}
1515
{{- end }}
1616

1717
{{- if eq .scope.provider "s3" }}
1818
{{- if empty .scope.endpointURL }}
19-
endpointURL: "https://s3.{{ required "You need to specify S3 region if endpointURL is not specified." .scope.s3.region }}.amazonaws.com"
19+
{{- $region := include "tpl" (dict "value" (required "You need to specify S3 region if endpointURL is not specified." .scope.s3.region) "context" .context) }}
20+
endpointURL: {{ printf "https://s3.%s.amazonaws.com" $region | quote }}
2021
{{- end }}
2122
{{- if empty .scope.destinationPath }}
22-
destinationPath: "s3://{{ required "You need to specify S3 bucket if destinationPath is not specified." .scope.s3.bucket }}{{ .scope.s3.path }}"
23+
{{- $bucket := include "tpl" (dict "value" (required "You need to specify S3 bucket if destinationPath is not specified." .scope.s3.bucket) "context" .context) }}
24+
{{- $path := include "tpl" (dict "value" .scope.s3.path "context" .context) }}
25+
destinationPath: {{ printf "s3://%s%s" $bucket $path | quote }}
2326
{{- end }}
2427
{{- $secretName := coalesce .scope.secret.name (printf "%s-%s-s3-creds" .chartFullname .secretPrefix) }}
2528
s3Credentials:
2629
{{- if .scope.s3.inheritFromIAMRole }}
2730
inheritFromIAMRole: true
2831
{{- else }}
2932
accessKeyId:
30-
name: {{ $secretName }}
33+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
3134
key: ACCESS_KEY_ID
3235
secretAccessKey:
33-
name: {{ $secretName }}
36+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
3437
key: ACCESS_SECRET_KEY
3538
{{- end }}
3639
{{- else if eq .scope.provider "azure" }}
40+
3741
{{- if empty .scope.destinationPath }}
38-
destinationPath: "https://{{ required "You need to specify Azure storageAccount if destinationPath is not specified." .scope.azure.storageAccount }}.{{ .scope.azure.serviceName }}.core.windows.net/{{ .scope.azure.containerName }}{{ .scope.azure.path }}"
42+
{{- $storageAccount := include "tpl" (dict "value" (required "You need to specify Azure storageAccount if destinationPath is not specified." .scope.azure.storageAccount) "context" .context) }}
43+
{{- $containerName := include "tpl" (dict "value" .scope.azure.containerName "context" .context) }}
44+
destinationPath: {{ printf "https://%s.%s.core.windows.net/%s%s" $storageAccount .scope.azure.serviceName $containerName .scope.azure.path | quote }}
3945
{{- end }}
4046
{{- $secretName := coalesce .scope.secret.name (printf "%s-%s-azure-creds" .chartFullname .secretPrefix) }}
4147
azureCredentials:
4248
{{- if .scope.azure.inheritFromAzureAD }}
4349
inheritFromAzureAD: true
4450
{{- else if .scope.azure.connectionString }}
4551
connectionString:
46-
name: {{ $secretName }}
52+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
4753
key: AZURE_CONNECTION_STRING
4854
{{- else }}
4955
storageAccount:
50-
name: {{ $secretName }}
56+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
5157
key: AZURE_STORAGE_ACCOUNT
5258
{{- if .scope.azure.storageKey }}
5359
storageKey:
54-
name: {{ $secretName }}
60+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
5561
key: AZURE_STORAGE_KEY
5662
{{- else }}
5763
storageSasToken:
58-
name: {{ $secretName }}
64+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
5965
key: AZURE_STORAGE_SAS_TOKEN
6066
{{- end }}
6167
{{- end }}
6268
{{- else if eq .scope.provider "google" }}
6369
{{- if empty .scope.destinationPath }}
64-
destinationPath: "gs://{{ required "You need to specify Google storage bucket if destinationPath is not specified." .scope.google.bucket }}{{ .scope.google.path }}"
70+
{{- $bucket := include "tpl" (dict "value" (required "You need to specify Google storage bucket if destinationPath is not specified." .scope.google.bucket) "context" .context) }}
71+
destinationPath: {{ printf "gs://%s%s" $bucket .scope.google.path | quote }}
6572
{{- end }}
6673
{{- $secretName := coalesce .scope.secret.name (printf "%s-%s-google-creds" .chartFullname .secretPrefix) }}
6774
googleCredentials:
6875
gkeEnvironment: {{ .scope.google.gkeEnvironment }}
6976
{{- if not .scope.google.gkeEnvironment }}
7077
applicationCredentials:
71-
name: {{ $secretName }}
78+
name: {{ include "tpl" (dict "value" $secretName "context" .context) }}
7279
key: APPLICATION_CREDENTIALS
7380
{{- end }}
7481
{{- end -}}

charts/cluster/templates/_bootstrap.tpl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
bootstrap:
44
initdb:
55
{{- with .Values.cluster.initdb }}
6-
{{- with (omit . "postInitApplicationSQL" "owner" "import") }}
6+
{{- with (omit . "postInitApplicationSQL" "owner" "import" "database") }}
77
{{- . | toYaml | nindent 4 }}
88
{{- end }}
99
{{- end }}
10+
{{- if .Values.cluster.initdb.database }}
11+
database: {{ include "tpl" (dict "value" .Values.cluster.initdb.database "context" $) | quote }}
12+
{{- end }}
1013
{{- if .Values.cluster.initdb.owner }}
11-
owner: {{ tpl .Values.cluster.initdb.owner . }}
14+
owner: {{ include "tpl" (dict "value" .Values.cluster.initdb.owner "context" $) }}
1215
{{- end }}
1316
{{- if or (eq .Values.type "postgis") (eq .Values.type "timescaledb") (not (empty .Values.cluster.initdb.postInitApplicationSQL)) }}
1417
postInitApplicationSQL:
@@ -102,7 +105,7 @@ externalClusters:
102105
- name: objectStoreRecoveryCluster
103106
barmanObjectStore:
104107
serverName: {{ .Values.recovery.clusterName }}
105-
{{- $d := dict "chartFullname" (include "cluster.fullname" .) "scope" .Values.recovery "secretPrefix" "recovery" -}}
108+
{{- $d := dict "chartFullname" (include "cluster.fullname" .) "scope" .Values.recovery "secretPrefix" "recovery" "context" $ -}}
106109
{{- include "cluster.barmanObjectStoreConfig" $d | nindent 4 }}
107110
{{- end }}
108111
{{- end }}

charts/cluster/templates/_external_source_cluster.tpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33
{{- $config := last . -}}
44
- name: {{ first . }}
55
connectionParameters:
6-
host: {{ $config.host | quote }}
7-
port: {{ $config.port | quote }}
8-
user: {{ $config.username | quote }}
6+
host: {{ include "tpl" (dict "value" $config.host "context" $) | quote }}
7+
port: {{ include "tpl" (dict "value" $config.port "context" $) | quote }}
8+
user: {{ include "tpl" (dict "value" $config.username "context" $) | quote }}
99
{{- with $config.database }}
10-
dbname: {{ . | quote }}
10+
dbname: {{ include "tpl" (dict "value" . "context" $) | quote }}
1111
{{- end }}
12-
sslmode: {{ $config.sslMode | quote }}
12+
sslmode: {{ include "tpl" (dict "value" $config.sslMode "context" $) | quote }}
1313
{{- if $config.passwordSecret.name }}
1414
password:
15-
name: {{ $config.passwordSecret.name }}
15+
name: {{ include "tpl" (dict "value" $config.passwordSecret.name "context" $) }}
1616
key: {{ $config.passwordSecret.key }}
1717
{{- end }}
1818
{{- if $config.sslKeySecret.name }}
1919
sslKey:
20-
name: {{ $config.sslKeySecret.name }}
20+
name: {{ include "tpl" (dict "value" $config.sslKeySecret.name "context" $) }}
2121
key: {{ $config.sslKeySecret.key }}
2222
{{- end }}
2323
{{- if $config.sslCertSecret.name }}
2424
sslCert:
25-
name: {{ $config.sslCertSecret.name }}
25+
name: {{ include "tpl" (dict "value" $config.sslCertSecret.name "context" $) }}
2626
key: {{ $config.sslCertSecret.key }}
2727
{{- end }}
2828
{{- if $config.sslRootCertSecret.name }}
2929
sslRootCert:
30-
name: {{ $config.sslRootCertSecret.name }}
30+
name: {{ include "tpl" (dict "value" $config.sslRootCertSecret.name "context" $) }}
3131
key: {{ $config.sslRootCertSecret.key }}
3232
{{- end }}
3333
{{- end }}

charts/cluster/templates/_helpers.tpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,20 @@ Postgres GID
144144
{{- 26 -}}
145145
{{- end -}}
146146
{{- end -}}
147+
148+
{{/*
149+
Renders a value that contains template expressions.
150+
151+
This helper processes values through the Helm template engine, allowing dynamic values
152+
to be used in configuration. It handles both string values and complex objects.
153+
154+
Usage:
155+
{{ include "tpl" (dict "value" .Values.path.to.the.Value "context" $) }}
156+
*/}}
157+
{{- define "tpl" -}}
158+
{{- if typeIs "string" .value }}
159+
{{- tpl .value .context }}
160+
{{- else }}
161+
{{- tpl (.value | toYaml) .context }}
162+
{{- end }}
163+
{{- end -}}

charts/cluster/templates/cluster.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ spec:
7979
{{- end }}
8080
{{- with .Values.cluster.postgresql.pg_hba }}
8181
pg_hba:
82-
{{- toYaml . | nindent 6 }}
82+
{{- include "tpl" (dict "value" . "context" $) | nindent 6 }}
8383
{{- end }}
8484
{{- with .Values.cluster.postgresql.pg_ident }}
8585
pg_ident:
86-
{{- toYaml . | nindent 6 }}
86+
{{- include "tpl" (dict "value" . "context" $) | nindent 6 }}
8787
{{- end }}
8888
{{- with .Values.cluster.postgresql.ldap }}
8989
ldap:
@@ -95,24 +95,24 @@ spec:
9595
{{ end }}
9696
{{- with .Values.cluster.postgresql.parameters }}
9797
parameters:
98-
{{- toYaml . | nindent 6 }}
98+
{{- include "tpl" (dict "value" . "context" $) | nindent 6 }}
9999
{{- end }}
100100

101101
{{- if not (and (empty .Values.cluster.roles) (empty .Values.cluster.services)) }}
102102
managed:
103103
{{- with .Values.cluster.services }}
104104
services:
105-
{{- toYaml . | nindent 6 }}
105+
{{- include "tpl" (dict "value" . "context" $) | nindent 6 }}
106106
{{ end }}
107107
{{- with .Values.cluster.roles }}
108108
roles:
109-
{{- toYaml . | nindent 6 }}
109+
{{- include "tpl" (dict "value" . "context" $) | nindent 6 }}
110110
{{ end }}
111111
{{- end }}
112112

113113
{{- with .Values.cluster.serviceAccountTemplate }}
114114
serviceAccountTemplate:
115-
{{- toYaml . | nindent 4 }}
115+
{{- include "tpl" (dict "value" . "context" $) | nindent 4 }}
116116
{{- end }}
117117

118118
monitoring:

0 commit comments

Comments
 (0)