Skip to content

Commit

Permalink
Merge pull request #1359 from porter-dev/ym/cloudql_connections
Browse files Browse the repository at this point in the history
fix: connections templating for cloud sql proxy
  • Loading branch information
yosefmih authored Aug 1, 2024
2 parents 69d37ef + 848d2f9 commit cbd02e2
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 13 deletions.
28 changes: 28 additions & 0 deletions applications/job/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,31 @@ Name of the service account json secret to use with the CloudSQL proxy
{{- define "cloudsql.serviceAccountJSONSecret" -}}
{{- default (printf "cloudsql-secret-%s" (include "docker-template.fullname" .)) .Values.cloudsql.serviceAccountJSONSecret }}
{{- end }}

{{/*
The connection string to be passed to the CloudSQL proxy.
For backwards compatibility, this concatenates targets from cloudsql.connectionName/dbPort, cloudsql.additionalConnection.connectionName/dbPort in addition to the cloudsql.connections list
*/}}
{{- define "cloudsql.connectionString" -}}
{{- $singleConnection := .Values.cloudsql.connectionName -}}
{{- $additionalConnection := .Values.cloudsql.additionalConnection -}}
{{- $connections := default (list) .Values.cloudsql.connections -}}
{{- $hasConnections := or $singleConnection (gt (len $connections) 0) $additionalConnection.enabled -}}
{{- if $hasConnections -}}

{{- if $singleConnection -}}
{{- $singleConnection -}}=tcp:{{.Values.cloudsql.dbPort }}
{{- end -}}

{{- if $additionalConnection.enabled -}}
{{- if $singleConnection }},{{ end -}}
{{ $additionalConnection.connectionName }}=tcp:{{ $additionalConnection.dbPort }}
{{- end -}}

{{- range $index, $conn := $connections -}}
{{- if or $index $singleConnection $additionalConnection.enabled }},{{ end -}}
{{ $conn.name }}=tcp:{{ $conn.port }}
{{- end -}}

{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion applications/job/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ spec:
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- "/cloud_sql_proxy"
- "-instances={{ .Values.cloudsql.connectionName }}=tcp:{{ .Values.cloudsql.dbPort }}"
- "-instances={{- include "cloudsql.connectionString" . -}}"
- "-credential_file=/secrets/service_account.json"
{{ if .Values.terminationGracePeriodSeconds }}
- "-term_timeout={{ .Values.terminationGracePeriodSeconds }}s"
Expand Down
2 changes: 1 addition & 1 deletion applications/job/templates/hook-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ data:
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- "/cloud_sql_proxy"
- "-instances={{ .Values.cloudsql.connectionName }}=tcp:{{ .Values.cloudsql.dbPort }}"
- "-instances={{- include "cloudsql.connectionString" . -}}"
- "-credential_file=/secrets/service_account.json"
{{ if .Values.terminationGracePeriodSeconds }}
- "-term_timeout={{ .Values.terminationGracePeriodSeconds }}s"
Expand Down
9 changes: 8 additions & 1 deletion applications/job/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ paused: false

cloudsql:
enabled: false
connectionName: ""
connections: []
# - name: "abcdedfg"
# port: 34343
connectionName: "" # deprecated, keeping for backwards compatibility. use connections instead.
dbPort: 5432
serviceAccountJSON: ""
serviceAccountJSONSecret: ""
additionalConnection: # deprecated, keeping for backwards compatibility. use connections instead.
enabled: false
dbPort: 5432
connectionName: ""

# Set this for enabling DNS extensions over TCP
# We enable this by default.
Expand Down
29 changes: 29 additions & 0 deletions applications/web/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,32 @@ Name of the service account json secret to use with the CloudSQL proxy
{{- define "cloudsql.serviceAccountJSONSecret" -}}
{{- default (printf "cloudsql-secret-%s" (include "docker-template.fullname" .)) .Values.cloudsql.serviceAccountJSONSecret }}
{{- end }}
{{/*
The connection string to be passed to the CloudSQL proxy.
For backwards compatibility, this concatenates targets from cloudsql.connectionName/dbPort, cloudsql.additionalConnection.connectionName/dbPort in addition to the cloudsql.connections list
*/}}
{{- define "cloudsql.connectionString" -}}
{{- $singleConnection := .Values.cloudsql.connectionName -}}
{{- $additionalConnection := .Values.cloudsql.additionalConnection -}}
{{- $connections := default (list) .Values.cloudsql.connections -}}
{{- $hasConnections := or $singleConnection (gt (len $connections) 0) $additionalConnection.enabled -}}
{{- if $hasConnections -}}
{{- if $singleConnection -}}
{{- $singleConnection -}}=tcp:{{.Values.cloudsql.dbPort }}
{{- end -}}
{{- if $additionalConnection.enabled -}}
{{- if $singleConnection }},{{ end -}}
{{ $additionalConnection.connectionName }}=tcp:{{ $additionalConnection.dbPort }}
{{- end -}}
{{- range $index, $conn := $connections -}}
{{- if or $index $singleConnection $additionalConnection.enabled }},{{ end -}}
{{ $conn.name }}=tcp:{{ $conn.port }}
{{- end -}}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ spec:
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- "/cloud_sql_proxy"
- "-instances={{ $.Values.cloudsql.connectionName }}=tcp:{{ $.Values.cloudsql.dbPort }}"
- "-instances={{- include "cloudsql.connectionString" . -}}"
- "-credential_file=/secrets/service_account.json"
{{ if $.Values.terminationGracePeriodSeconds }}
- "-term_timeout={{ $.Values.terminationGracePeriodSeconds }}s"
Expand Down
6 changes: 1 addition & 5 deletions applications/web/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,7 @@ spec:
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- "/cloud_sql_proxy"
{{- if .Values.cloudsql.additionalConnection.enabled }}
- "-instances={{ .Values.cloudsql.connectionName }}=tcp:{{ .Values.cloudsql.dbPort }},{{ .Values.cloudsql.additionalConnection.connectionName }}=tcp:{{ .Values.cloudsql.additionalConnection.dbPort }}"
{{- else }}
- "-instances={{ .Values.cloudsql.connectionName }}=tcp:{{ .Values.cloudsql.dbPort }}"
{{- end }}
- "-instances={{- include "cloudsql.connectionString" . -}}"
- "-credential_file=/secrets/service_account.json"
{{ if .Values.terminationGracePeriodSeconds }}
- "-term_timeout={{ .Values.terminationGracePeriodSeconds }}s"
Expand Down
7 changes: 5 additions & 2 deletions applications/web/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@ pvc:

cloudsql:
enabled: false
connectionName: ""
connections: []
# - name: "abcdedfg"
# port: 34343
connectionName: "" # deprecated, keeping for backwards compatibility. use connections instead.
dbPort: 5432
serviceAccountJSON: ""
serviceAccountJSONSecret: ""
additionalConnection:
additionalConnection: # deprecated, keeping for backwards compatibility. use connections instead.
enabled: false
dbPort: 5432
connectionName: ""
Expand Down
28 changes: 28 additions & 0 deletions applications/worker/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,31 @@ Name of the service account json secret to use with the CloudSQL proxy
{{- define "cloudsql.serviceAccountJSONSecret" -}}
{{- default (printf "cloudsql-secret-%s" (include "docker-template.fullname" .)) .Values.cloudsql.serviceAccountJSONSecret }}
{{- end }}

{{/*
The connection string to be passed to the CloudSQL proxy.
For backwards compatibility, this concatenates targets from cloudsql.connectionName/dbPort, cloudsql.additionalConnection.connectionName/dbPort in addition to the cloudsql.connections list
*/}}
{{- define "cloudsql.connectionString" -}}
{{- $singleConnection := .Values.cloudsql.connectionName -}}
{{- $additionalConnection := .Values.cloudsql.additionalConnection -}}
{{- $connections := default (list) .Values.cloudsql.connections -}}
{{- $hasConnections := or $singleConnection (gt (len $connections) 0) $additionalConnection.enabled -}}
{{- if $hasConnections -}}

{{- if $singleConnection -}}
{{- $singleConnection -}}=tcp:{{.Values.cloudsql.dbPort }}
{{- end -}}

{{- if $additionalConnection.enabled -}}
{{- if $singleConnection }},{{ end -}}
{{ $additionalConnection.connectionName }}=tcp:{{ $additionalConnection.dbPort }}
{{- end -}}

{{- range $index, $conn := $connections -}}
{{- if or $index $singleConnection $additionalConnection.enabled }},{{ end -}}
{{ $conn.name }}=tcp:{{ $conn.port }}
{{- end -}}

{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion applications/worker/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ spec:
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- "/cloud_sql_proxy"
- "-instances={{ .Values.cloudsql.connectionName }}=tcp:{{ .Values.cloudsql.dbPort }}"
- "-instances={{- include "cloudsql.connectionString" . -}}"
- "-credential_file=/secrets/service_account.json"
{{ if .Values.terminationGracePeriodSeconds }}
- "-term_timeout={{ .Values.terminationGracePeriodSeconds }}s"
Expand Down
9 changes: 8 additions & 1 deletion applications/worker/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,17 @@ pvc:

cloudsql:
enabled: false
connectionName: ""
connections: []
# - name: "abcdedfg"
# port: 34343
connectionName: "" # deprecated, keeping for backwards compatibility. use connections instead.
dbPort: 5432
serviceAccountJSON: ""
serviceAccountJSONSecret: ""
additionalConnection: # deprecated, keeping for backwards compatibility. use connections instead.
enabled: false
dbPort: 5432
connectionName: ""

# Set this to add entries to the /etc/hosts file
# Format: hostAliases: [{ip: <IP>, hostnames: [<HOSTNAME>,..]},..]
Expand Down

0 comments on commit cbd02e2

Please sign in to comment.