Skip to content

Commit

Permalink
SMP-1338: add functions for external redis env and connection string (#…
Browse files Browse the repository at this point in the history
…24)

* add functions for external redis env and connection string

* bump chart version

* modify connection string methods to support edge cases

* add comments for helper functions
  • Loading branch information
kapilgarg1996 committed May 26, 2023
1 parent b4bff29 commit 12fa850
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: library
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.15
version: 1.0.16

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
40 changes: 37 additions & 3 deletions src/common/templates/_databaseconnections.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
{{- $installed := (pluck $type .context.Values.global.database | first ).installed }}
{{- $protocol := (pluck $type .context.Values.global.database | first ).protocol }}
{{- $extraArgs:= (pluck $type .context.Values.global.database | first ).extraArgs }}
{{- $userVariableName := default (printf "%s_USER" $type) .userVariableName -}}
{{- $passwordVariableName := default (printf "%s_PASSWORD" $type) .passwordVariableName -}}
{{- if $installed }}
{{- $namespace := .context.Release.Namespace }}
{{- if .context.Values.global.ha -}}
Expand All @@ -34,7 +36,7 @@
{{- end }}
{{- else }}
{{- $args := (printf "/%s?%s" .database $extraArgs )}}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "extraArgs" $args )}}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "extraArgs" $args "userVariableName" $userVariableName "passwordVariableName" $passwordVariableName)}}
{{- end }}
{{- end }}

Expand Down Expand Up @@ -67,11 +69,13 @@
{{- $hosts := (pluck $type .context.Values.global.database | first ).hosts }}
{{- $protocol := (pluck $type .context.Values.global.database | first ).protocol }}
{{- $installed := (pluck $type .context.Values.global.database | first).installed }}
{{- $userVariableName := default (printf "%s_USER" $type) .userVariableName -}}
{{- $passwordVariableName := default (printf "%s_PASSWORD" $type) .passwordVariableName -}}
{{- if $installed }}
{{- $connectionString := (printf "%s://$(%s_USER):$(%s_PASSWORD)@%s" "postgres" $dbType $dbType "postgres:5432") }}
{{- printf "%s" $connectionString }}
{{- else }}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol )}}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "userVariableName" $userVariableName "passwordVariableName" $passwordVariableName)}}
{{- end }}
{{- end }}

Expand Down Expand Up @@ -99,6 +103,36 @@
{{- define "harnesscommon.dbconnection.timescaleConnection" }}
{{- $type := "timescaledb" }}
{{- $hosts := (pluck $type .context.Values.global.database | first ).hosts }}
{{- $userVariableName := default (printf "%s_USER" $type) .userVariableName -}}
{{- $passwordVariableName := default (printf "%s_PASSWORD" $type) .passwordVariableName -}}
{{- $protocol := (pluck $type .context.Values.global.database | first ).protocol }}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "extraArgs" "/harness") }}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "extraArgs" "/harness" "userVariableName" $userVariableName "passwordVariableName" $passwordVariableName) }}
{{- end}}

{{/* Generates Redis environment variables
{{ include "harnesscommon.dbconnection.redisEnv" (dict "context" .Values.global.database.redis "userVariableName" "REDIS_USER" "passwordVariableName" "REDIS_PASSWORD") | nident 10 }}
*/}}
{{- define "harnesscommon.dbconnection.redisEnv" }}
{{- $type := "redis" }}
{{- $passwordSecret := .context.secretName }}
{{- $passwordKey := .context.passwordKey }}
{{- $userKey := .context.userKey }}
{{- $installed := .context.installed }}
{{- if not $installed }}
{{- include "harnesscommon.dbconnection.dbenvuser" (dict "type" $type "secret" $passwordSecret "userKey" $userKey "variableName" .userVariableName ) }}
{{- include "harnesscommon.dbconnection.dbenvpassword" (dict "type" $type "secret" $passwordSecret "passwordKey" $passwordKey "variableName" .passwordVariableName ) }}
{{- end }}
{{- end }}

{{/* Generates Redis Connection string. If userVariableName or passwordVariableName is not provided, a connection string is generated without creds
{{ include "harnesscommon.dbconnection.redisConnection" (dict "context" .Values.global.database.redis "userVariableName" "REDIS_USER" "passwordVariableName" "REDIS_PASSWORD" )}}
*/}}
{{- define "harnesscommon.dbconnection.redisConnection" -}}
{{- $type := "redis" -}}
{{- $hosts := .context.hosts -}}
{{- $protocol := .context.protocol -}}
{{- if and (.context.installed) (empty $hosts) -}}
{{- $hosts = list "redis-sentinel-harness-announce-0:26379" "redis-sentinel-harness-announce-1:26379" "redis-sentinel-harness-announce-2:26379" }}
{{- end -}}
{{- include "harnesscommon.dbconnection.connection" (dict "type" $type "hosts" $hosts "protocol" $protocol "extraArgs" .context.extraArgs "userVariableName" .userVariableName "passwordVariableName" .passwordVariableName "connectionType" "list") }}
{{- end -}}
58 changes: 45 additions & 13 deletions src/common/templates/_databasehelpers.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

{{/* Generates db user environment reference. If variableName is not provided, default one is generated using db type.
Secret and userValue are mutually exclusive
{{ include "harnesscommon.dbconnection.dbenvuser" (dict "type" "redis" "variableName" "REDIS_USER" "userValue" "test-user" "secret" "redis-secret" "userKey" "redis-user-key" )}}
*/}}
{{- define "harnesscommon.dbconnection.dbenvuser" }}
{{- $dbType := upper .type }}
- name: {{ printf "%s_USER" $dbType }}
{{- $name := default (printf "%s_USER" $dbType) .variableName }}
- name: {{ $name }}
{{- if .userValue }}
value: {{ printf "%s" .userValue }}
{{- else }}
Expand All @@ -12,26 +17,53 @@
{{- end }}
{{- end }}

{{/* Generates db password environment reference. If variableName is not provided, default one is generated using db type.
{{ include "harnesscommon.dbconnection.dbenvpassword" (dict "type" "redis" "variableName" "REDIS_PASSWORD" "secret" "redis-secret" "passwordKey" "redis-password-key" )}}
*/}}
{{- define "harnesscommon.dbconnection.dbenvpassword" }}
{{- $dbType := upper .type }}
- name: {{ printf "%s_PASSWORD" $dbType }}
{{- $name := default (printf "%s_PASSWORD" $dbType) .variableName }}
- name: {{ $name }}
valueFrom:
secretKeyRef:
name: {{ printf "%s" .secret }}
key: {{ printf "%s" .passwordKey }}
{{- end }}

{{- define "harnesscommon.dbconnection.connection" }}
{{- $dbType := upper .type }}
{{- $firsthost := (index .hosts 0) }}
{{- $protocol := .protocol }}
{{- $extraArgs := .extraArgs }}
{{- $connectionString := (printf "%s://$(%s_USER):$(%s_PASSWORD)@%s" $protocol $dbType $dbType $firsthost) }}
{{- range $host := (rest .hosts) }}
{{- $connectionString = printf "%s,%s" $connectionString $host }}
{{- end}}
{{- if $extraArgs }}
{{- $connectionString = (printf "%s%s" $connectionString $extraArgs ) }}
{{/* Generates db connection string. If userVariableName or passwordVariableName is not provided, no credentials are added to connection string.
If connectionType is set other than "string" then protocol and credentials are added to every host
{{ include "harnesscommon.dbconnection.connection" (dict "type" "redis" "userVariableName" "REDIS_USER" "passwordVariableName" "REDIS_PASSWORD" "hosts" (list "redis-1:6379" "redis-2:6379") "protocol" "redis" "connectionType" "string" )}}
*/}}
{{- define "harnesscommon.dbconnection.connection" -}}
{{- $dbType := upper .type -}}
{{- $firsthost := (index .hosts 0) -}}
{{- $protocol := .protocol -}}
{{- $extraArgs := .extraArgs -}}
{{- $connectionString := (include "harnesscommon.dbconnection.singleConnectString" (dict "protocol" $protocol "host" $firsthost "userVariableName" .userVariableName "passwordVariableName" .passwordVariableName) ) }}
{{- range $host := (rest .hosts) -}}
{{- $connectionType := default "string" .connectionType }}
{{- if eq $connectionType "string" }}
{{- $connectionString = printf "%s,%s" $connectionString $host -}}
{{- else }}
{{- $connectionString = printf "%s,%s" $connectionString (include "harnesscommon.dbconnection.singleConnectString" . ) -}}
{{- end }}
{{- end -}}
{{- if $extraArgs -}}
{{- $connectionString = (printf "%s%s" $connectionString $extraArgs ) -}}
{{- end -}}
{{- printf "%s" $connectionString -}}
{{- end -}}

{{- define "harnesscommon.dbconnection.singleConnectString" }}
{{- $connectionString := "" }}
{{- if empty .protocol }}
{{- $connectionString = (printf "%s" .host) }}
{{- else }}
{{- if or (empty .userVariableName) (empty .passwordVariableName) }}
{{- $connectionString = (printf "%s://%s" .protocol .host) -}}
{{- else }}
{{- $connectionString = (printf "%s://$(%s):$(%s)@%s" .protocol .userVariableName .passwordVariableName .host) -}}
{{- end }}
{{- end }}
{{- printf "%s" $connectionString }}
{{- end }}

0 comments on commit 12fa850

Please sign in to comment.