nats chart provisoning
This commit is contained in:
281
opencloud/charts/nats/templates/_helpers.tpl
Normal file
281
opencloud/charts/nats/templates/_helpers.tpl
Normal file
@@ -0,0 +1,281 @@
|
||||
{{/*
|
||||
Expand the name of the chart.
|
||||
*/}}
|
||||
{{- define "nats.name" -}}
|
||||
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Create a default fully qualified app name.
|
||||
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
|
||||
If release name contains chart name it will be used as a full name.
|
||||
*/}}
|
||||
{{- define "nats.fullname" -}}
|
||||
{{- if .Values.fullnameOverride }}
|
||||
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
|
||||
{{- else }}
|
||||
{{- $name := default .Chart.Name .Values.nameOverride }}
|
||||
{{- if contains $name .Release.Name }}
|
||||
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
|
||||
{{- else }}
|
||||
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Create chart name and version as used by the chart label.
|
||||
*/}}
|
||||
{{- define "nats.chart" -}}
|
||||
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Print the namespace
|
||||
*/}}
|
||||
{{- define "nats.namespace" -}}
|
||||
{{- default .Release.Namespace .Values.namespaceOverride }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Print the namespace for the metadata section
|
||||
*/}}
|
||||
{{- define "nats.metadataNamespace" -}}
|
||||
{{- with .Values.namespaceOverride }}
|
||||
namespace: {{ . | quote }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Set default values.
|
||||
*/}}
|
||||
{{- define "nats.defaultValues" }}
|
||||
{{- if not .defaultValuesSet }}
|
||||
{{- $name := include "nats.fullname" . }}
|
||||
{{- with .Values }}
|
||||
{{- $_ := set .config.jetstream.fileStore.pvc "name" (.config.jetstream.fileStore.pvc.name | default (printf "%s-js" $name)) }}
|
||||
{{- $_ := set .config.resolver.pvc "name" (.config.resolver.pvc.name | default (printf "%s-resolver" $name)) }}
|
||||
{{- $_ := set .config.websocket.ingress "name" (.config.websocket.ingress.name | default (printf "%s-ws" $name)) }}
|
||||
{{- $_ := set .configMap "name" (.configMap.name | default (printf "%s-config" $name)) }}
|
||||
{{- $_ := set .headlessService "name" (.headlessService.name | default (printf "%s-headless" $name)) }}
|
||||
{{- $_ := set .natsBox.contentsSecret "name" (.natsBox.contentsSecret.name | default (printf "%s-box-contents" $name)) }}
|
||||
{{- $_ := set .natsBox.contextsSecret "name" (.natsBox.contextsSecret.name | default (printf "%s-box-contexts" $name)) }}
|
||||
{{- $_ := set .natsBox.deployment "name" (.natsBox.deployment.name | default (printf "%s-box" $name)) }}
|
||||
{{- $_ := set .natsBox.serviceAccount "name" (.natsBox.serviceAccount.name | default (printf "%s-box" $name)) }}
|
||||
{{- $_ := set .podDisruptionBudget "name" (.podDisruptionBudget.name | default $name) }}
|
||||
{{- $_ := set .service "name" (.service.name | default $name) }}
|
||||
{{- $_ := set .serviceAccount "name" (.serviceAccount.name | default $name) }}
|
||||
{{- $_ := set .statefulSet "name" (.statefulSet.name | default $name) }}
|
||||
{{- $_ := set .promExporter.podMonitor "name" (.promExporter.podMonitor.name | default $name) }}
|
||||
{{- end }}
|
||||
|
||||
{{- $values := get (include "tplYaml" (dict "doc" .Values "ctx" $) | fromJson) "doc" }}
|
||||
{{- $_ := set . "Values" $values }}
|
||||
|
||||
{{- $hasContentsSecret := false }}
|
||||
{{- range $ctxKey, $ctxVal := .Values.natsBox.contexts }}
|
||||
{{- range $secretKey, $secretVal := dict "creds" "nats-creds" "nkey" "nats-nkeys" "tls" "nats-certs" }}
|
||||
{{- $secret := get $ctxVal $secretKey }}
|
||||
{{- if $secret }}
|
||||
{{- $_ := set $secret "dir" ($secret.dir | default (printf "/etc/%s/%s" $secretVal $ctxKey)) }}
|
||||
{{- if and (ne $secretKey "tls") $secret.contents }}
|
||||
{{- $hasContentsSecret = true }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- $_ := set $ "hasContentsSecret" $hasContentsSecret }}
|
||||
|
||||
{{- with .Values.config }}
|
||||
{{- $config := include "nats.loadMergePatch" (merge (dict "file" "config/config.yaml" "ctx" $) .) | fromYaml }}
|
||||
{{- $_ := set $ "config" $config }}
|
||||
{{- end }}
|
||||
|
||||
{{- $_ := set . "defaultValuesSet" true }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
NATS labels
|
||||
*/}}
|
||||
{{- define "nats.labels" -}}
|
||||
{{- with .Values.global.labels -}}
|
||||
{{ toYaml . }}
|
||||
{{ end -}}
|
||||
helm.sh/chart: {{ include "nats.chart" . }}
|
||||
{{ include "nats.selectorLabels" . }}
|
||||
{{- if .Chart.AppVersion }}
|
||||
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
||||
{{- end }}
|
||||
app.kubernetes.io/managed-by: {{ .Release.Service }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
NATS selector labels
|
||||
*/}}
|
||||
{{- define "nats.selectorLabels" -}}
|
||||
app.kubernetes.io/name: {{ include "nats.name" . }}
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
app.kubernetes.io/component: nats
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
NATS Box labels
|
||||
*/}}
|
||||
{{- define "natsBox.labels" -}}
|
||||
{{- with .Values.global.labels -}}
|
||||
{{ toYaml . }}
|
||||
{{ end -}}
|
||||
helm.sh/chart: {{ include "nats.chart" . }}
|
||||
{{ include "natsBox.selectorLabels" . }}
|
||||
{{- if .Chart.AppVersion }}
|
||||
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
||||
{{- end }}
|
||||
app.kubernetes.io/managed-by: {{ .Release.Service }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
NATS Box selector labels
|
||||
*/}}
|
||||
{{- define "natsBox.selectorLabels" -}}
|
||||
app.kubernetes.io/name: {{ include "nats.name" . }}
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
app.kubernetes.io/component: nats-box
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
Print the image
|
||||
*/}}
|
||||
{{- define "nats.image" }}
|
||||
{{- $image := printf "%s:%s" .repository .tag }}
|
||||
{{- if or .registry .global.image.registry }}
|
||||
{{- $image = printf "%s/%s" (.registry | default .global.image.registry) $image }}
|
||||
{{- end -}}
|
||||
image: {{ $image }}
|
||||
{{- if or .pullPolicy .global.image.pullPolicy }}
|
||||
imagePullPolicy: {{ .pullPolicy | default .global.image.pullPolicy }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "nats.secretNames" -}}
|
||||
{{- $secrets := list }}
|
||||
{{- range $protocol := list "nats" "leafnodes" "websocket" "mqtt" "cluster" "gateway" }}
|
||||
{{- $configProtocol := get $.Values.config $protocol }}
|
||||
{{- if and (or (eq $protocol "nats") $configProtocol.enabled) $configProtocol.tls.enabled $configProtocol.tls.secretName }}
|
||||
{{- $secrets = append $secrets (merge (dict "name" (printf "%s-tls" $protocol)) $configProtocol.tls) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- toJson (dict "secretNames" $secrets) }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "natsBox.secretNames" -}}
|
||||
{{- $secrets := list }}
|
||||
{{- range $ctxKey, $ctxVal := .Values.natsBox.contexts }}
|
||||
{{- range $secretKey, $secretVal := dict "creds" "nats-creds" "nkey" "nats-nkeys" "tls" "nats-certs" }}
|
||||
{{- $secret := get $ctxVal $secretKey }}
|
||||
{{- if and $secret $secret.secretName }}
|
||||
{{- $secrets = append $secrets (merge (dict "name" (printf "ctx-%s-%s" $ctxKey $secretKey)) $secret) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- toJson (dict "secretNames" $secrets) }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "nats.tlsCAVolume" -}}
|
||||
{{- with .Values.tlsCA }}
|
||||
{{- if and .enabled (or .configMapName .secretName) }}
|
||||
- name: tls-ca
|
||||
{{- if .configMapName }}
|
||||
configMap:
|
||||
name: {{ .configMapName | quote }}
|
||||
{{- else if .secretName }}
|
||||
secret:
|
||||
secretName: {{ .secretName | quote }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "nats.tlsCAVolumeMount" -}}
|
||||
{{- with .Values.tlsCA }}
|
||||
{{- if and .enabled (or .configMapName .secretName) }}
|
||||
- name: tls-ca
|
||||
mountPath: {{ .dir | quote }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/*
|
||||
translates env var map to list
|
||||
*/}}
|
||||
{{- define "nats.env" -}}
|
||||
{{- range $k, $v := . }}
|
||||
{{- if kindIs "string" $v }}
|
||||
- name: {{ $k | quote }}
|
||||
value: {{ $v | quote }}
|
||||
{{- else if kindIs "map" $v }}
|
||||
- {{ merge (dict "name" $k) $v | toYaml | nindent 2 }}
|
||||
{{- else }}
|
||||
{{- fail (cat "env var" $k "must be string or map, got" (kindOf $v)) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- /*
|
||||
nats.loadMergePatch
|
||||
input: map with 4 keys:
|
||||
- file: name of file to load
|
||||
- ctx: context to pass to tpl
|
||||
- merge: interface{} to merge
|
||||
- patch: []interface{} valid JSON Patch document
|
||||
output: JSON encoded map with 1 key:
|
||||
- doc: interface{} patched json result
|
||||
*/}}
|
||||
{{- define "nats.loadMergePatch" -}}
|
||||
{{- $doc := tpl (.ctx.Files.Get (printf "files/%s" .file)) .ctx | fromYaml | default dict -}}
|
||||
{{- $doc = mergeOverwrite $doc (deepCopy (.merge | default dict)) -}}
|
||||
{{- get (include "jsonpatch" (dict "doc" $doc "patch" (.patch | default list)) | fromJson ) "doc" | toYaml -}}
|
||||
{{- end }}
|
||||
|
||||
|
||||
{{- /*
|
||||
nats.reloaderConfig
|
||||
input: map with 2 keys:
|
||||
- config: interface{} nats config
|
||||
- dir: dir config file is in
|
||||
output: YAML list of reloader config files
|
||||
*/}}
|
||||
{{- define "nats.reloaderConfig" -}}
|
||||
{{- $dir := trimSuffix "/" .dir -}}
|
||||
{{- with .config -}}
|
||||
{{- if kindIs "map" . -}}
|
||||
{{- range $k, $v := . -}}
|
||||
{{- if or (eq $k "cert_file") (eq $k "key_file") (eq $k "ca_file") }}
|
||||
- -config
|
||||
- {{ $v }}
|
||||
{{- else if hasSuffix "$include" $k }}
|
||||
- -config
|
||||
- {{ clean (printf "%s/%s" $dir $v) }}
|
||||
{{- else }}
|
||||
{{- include "nats.reloaderConfig" (dict "config" $v "dir" $dir) }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
|
||||
{{- /*
|
||||
nats.formatConfig
|
||||
input: map[string]interface{}
|
||||
output: string with following format rules
|
||||
1. keys ending in $natsRaw are unquoted
|
||||
2. keys ending in $natsInclude are converted to include directives
|
||||
*/}}
|
||||
{{- define "nats.formatConfig" -}}
|
||||
{{-
|
||||
(regexReplaceAll "\"<<\\s+(.*)\\s+>>\""
|
||||
(regexReplaceAll "\".*\\$include\": \"(.*)\",?" (include "toPrettyRawJson" .) "include ${1};")
|
||||
"${1}")
|
||||
-}}
|
||||
{{- end -}}
|
||||
219
opencloud/charts/nats/templates/_jsonpatch.tpl
Normal file
219
opencloud/charts/nats/templates/_jsonpatch.tpl
Normal file
@@ -0,0 +1,219 @@
|
||||
{{- /*
|
||||
jsonpatch
|
||||
input: map with 2 keys:
|
||||
- doc: interface{} valid JSON document
|
||||
- patch: []interface{} valid JSON Patch document
|
||||
output: JSON encoded map with 1 key:
|
||||
- doc: interface{} patched json result
|
||||
*/}}
|
||||
{{- define "jsonpatch" -}}
|
||||
{{- $params := fromJson (toJson .) -}}
|
||||
{{- $patches := $params.patch -}}
|
||||
{{- $docContainer := pick $params "doc" -}}
|
||||
|
||||
{{- range $patch := $patches -}}
|
||||
{{- if not (hasKey $patch "op") -}}
|
||||
{{- fail "patch is missing op key" -}}
|
||||
{{- end -}}
|
||||
{{- if and (ne $patch.op "add") (ne $patch.op "remove") (ne $patch.op "replace") (ne $patch.op "copy") (ne $patch.op "move") (ne $patch.op "test") -}}
|
||||
{{- fail (cat "patch has invalid op" $patch.op) -}}
|
||||
{{- end -}}
|
||||
{{- if not (hasKey $patch "path") -}}
|
||||
{{- fail "patch is missing path key" -}}
|
||||
{{- end -}}
|
||||
{{- if and (or (eq $patch.op "add") (eq $patch.op "replace") (eq $patch.op "test")) (not (hasKey $patch "value")) -}}
|
||||
{{- fail (cat "patch with op" $patch.op "is missing value key") -}}
|
||||
{{- end -}}
|
||||
{{- if and (or (eq $patch.op "copy") (eq $patch.op "move")) (not (hasKey $patch "from")) -}}
|
||||
{{- fail (cat "patch with op" $patch.op "is missing from key") -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $opPathKeys := list "path" -}}
|
||||
{{- if or (eq $patch.op "copy") (eq $patch.op "move") -}}
|
||||
{{- $opPathKeys = append $opPathKeys "from" -}}
|
||||
{{- end -}}
|
||||
{{- $reSlice := list -}}
|
||||
|
||||
{{- range $opPathKey := $opPathKeys -}}
|
||||
{{- $obj := $docContainer -}}
|
||||
{{- if and (eq $patch.op "copy") (eq $opPathKey "from") -}}
|
||||
{{- $obj = (fromJson (toJson $docContainer)) -}}
|
||||
{{- end -}}
|
||||
{{- $key := "doc" -}}
|
||||
{{- $lastMap := dict "root" $obj -}}
|
||||
{{- $lastKey := "root" -}}
|
||||
{{- $paths := (splitList "/" (get $patch $opPathKey)) -}}
|
||||
{{- $firstPath := index $paths 0 -}}
|
||||
{{- if ne (index $paths 0) "" -}}
|
||||
{{- fail (cat "invalid" $opPathKey (get $patch $opPathKey) "must be empty string or start with /") -}}
|
||||
{{- end -}}
|
||||
{{- $paths = slice $paths 1 -}}
|
||||
|
||||
{{- range $path := $paths -}}
|
||||
{{- $path = replace "~1" "/" $path -}}
|
||||
{{- $path = replace "~0" "~" $path -}}
|
||||
|
||||
{{- if kindIs "slice" $obj -}}
|
||||
{{- $mapObj := dict -}}
|
||||
{{- range $i, $v := $obj -}}
|
||||
{{- $_ := set $mapObj (toString $i) $v -}}
|
||||
{{- end -}}
|
||||
{{- $obj = $mapObj -}}
|
||||
{{- $_ := set $lastMap $lastKey $obj -}}
|
||||
{{- $reSlice = prepend $reSlice (dict "lastMap" $lastMap "lastKey" $lastKey "mapObj" $obj) -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if kindIs "map" $obj -}}
|
||||
{{- if not (hasKey $obj $key) -}}
|
||||
{{- fail (cat "key" $key "does not exist") -}}
|
||||
{{- end -}}
|
||||
{{- $lastKey = $key -}}
|
||||
{{- $lastMap = $obj -}}
|
||||
{{- $obj = index $obj $key -}}
|
||||
{{- $key = $path -}}
|
||||
{{- else -}}
|
||||
{{- fail (cat "cannot iterate into path" $key "on type" (kindOf $obj)) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $_ := set $patch (printf "%sKey" $opPathKey) $key -}}
|
||||
{{- $_ := set $patch (printf "%sLastKey" $opPathKey) $lastKey -}}
|
||||
{{- $_ = set $patch (printf "%sLastMap" $opPathKey) $lastMap -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if eq $patch.op "move" }}
|
||||
{{- if and (ne $patch.path $patch.from) (hasPrefix (printf "%s/" $patch.path) (printf "%s/" $patch.from)) -}}
|
||||
{{- fail (cat "from" $patch.from "may not be a child of path" $patch.path) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if or (eq $patch.op "move") (eq $patch.op "copy") (eq $patch.op "test") }}
|
||||
{{- $key := $patch.fromKey -}}
|
||||
{{- $lastMap := $patch.fromLastMap -}}
|
||||
{{- $lastKey := $patch.fromLastKey -}}
|
||||
{{- $setKey := "value" -}}
|
||||
{{- if eq $patch.op "test" }}
|
||||
{{- $key = $patch.pathKey -}}
|
||||
{{- $lastMap = $patch.pathLastMap -}}
|
||||
{{- $lastKey = $patch.pathLastKey -}}
|
||||
{{- $setKey = "testValue" -}}
|
||||
{{- end -}}
|
||||
{{- $obj := index $lastMap $lastKey -}}
|
||||
|
||||
{{- if kindIs "map" $obj -}}
|
||||
{{- if not (hasKey $obj $key) -}}
|
||||
{{- fail (cat $key "does not exist") -}}
|
||||
{{- end -}}
|
||||
{{- $_ := set $patch $setKey (index $obj $key) -}}
|
||||
|
||||
{{- else if kindIs "slice" $obj -}}
|
||||
{{- $i := atoi $key -}}
|
||||
{{- if ne $key (toString $i) -}}
|
||||
{{- fail (cat "cannot convert" $key "to int") -}}
|
||||
{{- end -}}
|
||||
{{- if lt $i 0 -}}
|
||||
{{- fail "slice index <0" -}}
|
||||
{{- else if lt $i (len $obj) -}}
|
||||
{{- $_ := set $patch $setKey (index $obj $i) -}}
|
||||
{{- else -}}
|
||||
{{- fail "slice index >= slice length" -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- else -}}
|
||||
{{- fail (cat "cannot" $patch.op $key "on type" (kindOf $obj)) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if or (eq $patch.op "remove") (eq $patch.op "replace") (eq $patch.op "move") }}
|
||||
{{- $key := $patch.pathKey -}}
|
||||
{{- $lastMap := $patch.pathLastMap -}}
|
||||
{{- $lastKey := $patch.pathLastKey -}}
|
||||
{{- if eq $patch.op "move" }}
|
||||
{{- $key = $patch.fromKey -}}
|
||||
{{- $lastMap = $patch.fromLastMap -}}
|
||||
{{- $lastKey = $patch.fromLastKey -}}
|
||||
{{- end -}}
|
||||
{{- $obj := index $lastMap $lastKey -}}
|
||||
|
||||
{{- if kindIs "map" $obj -}}
|
||||
{{- if not (hasKey $obj $key) -}}
|
||||
{{- fail (cat $key "does not exist") -}}
|
||||
{{- end -}}
|
||||
{{- $_ := unset $obj $key -}}
|
||||
|
||||
{{- else if kindIs "slice" $obj -}}
|
||||
{{- $i := atoi $key -}}
|
||||
{{- if ne $key (toString $i) -}}
|
||||
{{- fail (cat "cannot convert" $key "to int") -}}
|
||||
{{- end -}}
|
||||
{{- if lt $i 0 -}}
|
||||
{{- fail "slice index <0" -}}
|
||||
{{- else if eq $i 0 -}}
|
||||
{{- $_ := set $lastMap $lastKey (slice $obj 1) -}}
|
||||
{{- else if lt $i (sub (len $obj) 1) -}}
|
||||
{{- $_ := set $lastMap $lastKey (concat (slice $obj 0 $i) (slice $obj (add $i 1) (len $obj))) -}}
|
||||
{{- else if eq $i (sub (len $obj) 1) -}}
|
||||
{{- $_ := set $lastMap $lastKey (slice $obj 0 (sub (len $obj) 1)) -}}
|
||||
{{- else -}}
|
||||
{{- fail "slice index >= slice length" -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- else -}}
|
||||
{{- fail (cat "cannot" $patch.op $key "on type" (kindOf $obj)) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if or (eq $patch.op "add") (eq $patch.op "replace") (eq $patch.op "move") (eq $patch.op "copy") }}
|
||||
{{- $key := $patch.pathKey -}}
|
||||
{{- $lastMap := $patch.pathLastMap -}}
|
||||
{{- $lastKey := $patch.pathLastKey -}}
|
||||
{{- $value := $patch.value -}}
|
||||
{{- $obj := index $lastMap $lastKey -}}
|
||||
|
||||
{{- if kindIs "map" $obj -}}
|
||||
{{- $_ := set $obj $key $value -}}
|
||||
|
||||
{{- else if kindIs "slice" $obj -}}
|
||||
{{- $i := 0 -}}
|
||||
{{- if eq $key "-" -}}
|
||||
{{- $i = len $obj -}}
|
||||
{{- else -}}
|
||||
{{- $i = atoi $key -}}
|
||||
{{- if ne $key (toString $i) -}}
|
||||
{{- fail (cat "cannot convert" $key "to int") -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if lt $i 0 -}}
|
||||
{{- fail "slice index <0" -}}
|
||||
{{- else if eq $i 0 -}}
|
||||
{{- $_ := set $lastMap $lastKey (prepend $obj $value) -}}
|
||||
{{- else if lt $i (len $obj) -}}
|
||||
{{- $_ := set $lastMap $lastKey (concat (append (slice $obj 0 $i) $value) (slice $obj $i)) -}}
|
||||
{{- else if eq $i (len $obj) -}}
|
||||
{{- $_ := set $lastMap $lastKey (append $obj $value) -}}
|
||||
{{- else -}}
|
||||
{{- fail "slice index > slice length" -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- else -}}
|
||||
{{- fail (cat "cannot" $patch.op $key "on type" (kindOf $obj)) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if eq $patch.op "test" }}
|
||||
{{- if not (deepEqual $patch.value $patch.testValue) }}
|
||||
{{- fail (cat "test failed, expected" (toJson $patch.value) "but got" (toJson $patch.testValue)) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- range $reSliceOp := $reSlice -}}
|
||||
{{- $sliceObj := list -}}
|
||||
{{- range $i := until (len $reSliceOp.mapObj) -}}
|
||||
{{- $sliceObj = append $sliceObj (index $reSliceOp.mapObj (toString $i)) -}}
|
||||
{{- end -}}
|
||||
{{- $_ := set $reSliceOp.lastMap $reSliceOp.lastKey $sliceObj -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- end -}}
|
||||
{{- toJson $docContainer -}}
|
||||
{{- end -}}
|
||||
28
opencloud/charts/nats/templates/_toPrettyRawJson.tpl
Normal file
28
opencloud/charts/nats/templates/_toPrettyRawJson.tpl
Normal file
@@ -0,0 +1,28 @@
|
||||
{{- /*
|
||||
toPrettyRawJson
|
||||
input: interface{} valid JSON document
|
||||
output: pretty raw JSON string
|
||||
*/}}
|
||||
{{- define "toPrettyRawJson" -}}
|
||||
{{- include "toPrettyRawJsonStr" (toPrettyJson .) -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /*
|
||||
toPrettyRawJsonStr
|
||||
input: pretty JSON string
|
||||
output: pretty raw JSON string
|
||||
*/}}
|
||||
{{- define "toPrettyRawJsonStr" -}}
|
||||
{{- $s :=
|
||||
(regexReplaceAll "([^\\\\](?:\\\\\\\\)*)\\\\u003e"
|
||||
(regexReplaceAll "([^\\\\](?:\\\\\\\\)*)\\\\u003c"
|
||||
(regexReplaceAll "([^\\\\](?:\\\\\\\\)*)\\\\u0026" . "${1}&")
|
||||
"${1}<")
|
||||
"${1}>")
|
||||
-}}
|
||||
{{- if regexMatch "([^\\\\](?:\\\\\\\\)*)\\\\u00(26|3c|3e)" $s -}}
|
||||
{{- include "toPrettyRawJsonStr" $s -}}
|
||||
{{- else -}}
|
||||
{{- $s -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
114
opencloud/charts/nats/templates/_tplYaml.tpl
Normal file
114
opencloud/charts/nats/templates/_tplYaml.tpl
Normal file
@@ -0,0 +1,114 @@
|
||||
{{- /*
|
||||
tplYaml
|
||||
input: map with 2 keys:
|
||||
- doc: interface{}
|
||||
- ctx: context to pass to tpl function
|
||||
output: JSON encoded map with 1 key:
|
||||
- doc: interface{} with any keys called tpl or tplSpread values templated and replaced
|
||||
|
||||
maps matching the following syntax will be templated and parsed as YAML
|
||||
{
|
||||
$tplYaml: string
|
||||
}
|
||||
|
||||
maps matching the follow syntax will be templated, parsed as YAML, and spread into the parent map/slice
|
||||
{
|
||||
$tplYamlSpread: string
|
||||
}
|
||||
*/}}
|
||||
{{- define "tplYaml" -}}
|
||||
{{- $patch := get (include "tplYamlItr" (dict "ctx" .ctx "parentKind" "" "parentPath" "" "path" "/" "value" .doc) | fromJson) "patch" -}}
|
||||
{{- include "jsonpatch" (dict "doc" .doc "patch" $patch) -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /*
|
||||
tplYamlItr
|
||||
input: map with 4 keys:
|
||||
- path: string JSONPath to current element
|
||||
- parentKind: string kind of parent element
|
||||
- parentPath: string JSONPath to parent element
|
||||
- value: interface{}
|
||||
- ctx: context to pass to tpl function
|
||||
output: JSON encoded map with 1 key:
|
||||
- patch: list of patches to apply in order to template
|
||||
*/}}
|
||||
{{- define "tplYamlItr" -}}
|
||||
{{- $params := . -}}
|
||||
{{- $kind := kindOf $params.value -}}
|
||||
{{- $patch := list -}}
|
||||
{{- $joinPath := $params.path -}}
|
||||
{{- if eq $params.path "/" -}}
|
||||
{{- $joinPath = "" -}}
|
||||
{{- end -}}
|
||||
{{- $joinParentPath := $params.parentPath -}}
|
||||
{{- if eq $params.parentPath "/" -}}
|
||||
{{- $joinParentPath = "" -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if eq $kind "slice" -}}
|
||||
{{- $iAdj := 0 -}}
|
||||
{{- range $i, $v := $params.value -}}
|
||||
{{- $iPath := printf "%s/%d" $joinPath (add $i $iAdj) -}}
|
||||
{{- $itrPatch := get (include "tplYamlItr" (dict "ctx" $params.ctx "parentKind" $kind "parentPath" $params.path "path" $iPath "value" $v) | fromJson) "patch" -}}
|
||||
{{- $itrLen := len $itrPatch -}}
|
||||
{{- if gt $itrLen 0 -}}
|
||||
{{- $patch = concat $patch $itrPatch -}}
|
||||
{{- if eq (get (index $itrPatch 0) "op") "remove" -}}
|
||||
{{- $iAdj = add $iAdj (sub $itrLen 2) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- else if eq $kind "map" -}}
|
||||
{{- if and (eq (len $params.value) 1) (or (hasKey $params.value "$tplYaml") (hasKey $params.value "$tplYamlSpread")) -}}
|
||||
{{- $tpl := get $params.value "$tplYaml" -}}
|
||||
{{- $spread := false -}}
|
||||
{{- if hasKey $params.value "$tplYamlSpread" -}}
|
||||
{{- if eq $params.path "/" -}}
|
||||
{{- fail "cannot $tplYamlSpread on root object" -}}
|
||||
{{- end -}}
|
||||
{{- $tpl = get $params.value "$tplYamlSpread" -}}
|
||||
{{- $spread = true -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $res := tpl $tpl $params.ctx -}}
|
||||
{{- $res = get (fromYaml (tpl "tpl: {{ nindent 2 .res }}" (merge (dict "res" $res) $params.ctx))) "tpl" -}}
|
||||
|
||||
{{- if eq $spread false -}}
|
||||
{{- $patch = append $patch (dict "op" "replace" "path" $params.path "value" $res) -}}
|
||||
{{- else -}}
|
||||
{{- $resKind := kindOf $res -}}
|
||||
{{- if and (ne $resKind "invalid") (ne $resKind $params.parentKind) -}}
|
||||
{{- fail (cat "can only $tplYamlSpread slice onto a slice or map onto a map; attempted to spread" $resKind "on" $params.parentKind "at path" $params.path) -}}
|
||||
{{- end -}}
|
||||
{{- $patch = append $patch (dict "op" "remove" "path" $params.path) -}}
|
||||
{{- if eq $resKind "invalid" -}}
|
||||
{{- /* no-op */ -}}
|
||||
{{- else if eq $resKind "slice" -}}
|
||||
{{- range $v := reverse $res -}}
|
||||
{{- $patch = append $patch (dict "op" "add" "path" $params.path "value" $v) -}}
|
||||
{{- end -}}
|
||||
{{- else -}}
|
||||
{{- range $k, $v := $res -}}
|
||||
{{- $kPath := replace "~" "~0" $k -}}
|
||||
{{- $kPath = replace "/" "~1" $kPath -}}
|
||||
{{- $kPath = printf "%s/%s" $joinParentPath $kPath -}}
|
||||
{{- $patch = append $patch (dict "op" "add" "path" $kPath "value" $v) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- else -}}
|
||||
{{- range $k, $v := $params.value -}}
|
||||
{{- $kPath := replace "~" "~0" $k -}}
|
||||
{{- $kPath = replace "/" "~1" $kPath -}}
|
||||
{{- $kPath = printf "%s/%s" $joinPath $kPath -}}
|
||||
{{- $itrPatch := get (include "tplYamlItr" (dict "ctx" $params.ctx "parentKind" $kind "parentPath" $params.path "path" $kPath "value" $v) | fromJson) "patch" -}}
|
||||
{{- if gt (len $itrPatch) 0 -}}
|
||||
{{- $patch = concat $patch $itrPatch -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- toJson (dict "patch" $patch) -}}
|
||||
{{- end -}}
|
||||
4
opencloud/charts/nats/templates/config-map.yaml
Normal file
4
opencloud/charts/nats/templates/config-map.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.configMap }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "config-map.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
5
opencloud/charts/nats/templates/extra-resources.yaml
Normal file
5
opencloud/charts/nats/templates/extra-resources.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- range .Values.extraResources }}
|
||||
---
|
||||
{{ . | toYaml }}
|
||||
{{- end }}
|
||||
4
opencloud/charts/nats/templates/headless-service.yaml
Normal file
4
opencloud/charts/nats/templates/headless-service.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.headlessService }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "headless-service.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
6
opencloud/charts/nats/templates/ingress.yaml
Normal file
6
opencloud/charts/nats/templates/ingress.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.config.websocket.ingress }}
|
||||
{{- if and .enabled .hosts $.Values.config.websocket.enabled $.Values.service.enabled $.Values.service.ports.websocket.enabled }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "ingress.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,10 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- if .hasContentsSecret }}
|
||||
{{- with .Values.natsBox }}
|
||||
{{- if .enabled }}
|
||||
{{- with .contentsSecret}}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "nats-box/contents-secret.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,8 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.natsBox }}
|
||||
{{- if .enabled }}
|
||||
{{- with .contextsSecret}}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "nats-box/contexts-secret/contexts-secret.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
8
opencloud/charts/nats/templates/nats-box/deployment.yaml
Normal file
8
opencloud/charts/nats/templates/nats-box/deployment.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.natsBox }}
|
||||
{{- if .enabled }}
|
||||
{{- with .deployment }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "nats-box/deployment/deployment.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,8 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- if .Values.natsBox.enabled }}
|
||||
{{- with .Values.natsBox.serviceAccount }}
|
||||
{{- if .enabled }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "nats-box/service-account.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,6 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.podDisruptionBudget }}
|
||||
{{- if .enabled }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "pod-disruption-budget.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
8
opencloud/charts/nats/templates/pod-monitor.yaml
Normal file
8
opencloud/charts/nats/templates/pod-monitor.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.promExporter }}
|
||||
{{- if and .enabled .podMonitor.enabled }}
|
||||
{{- with .podMonitor }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "pod-monitor.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
6
opencloud/charts/nats/templates/service-account.yaml
Normal file
6
opencloud/charts/nats/templates/service-account.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.serviceAccount }}
|
||||
{{- if .enabled }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "service-account.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
6
opencloud/charts/nats/templates/service.yaml
Normal file
6
opencloud/charts/nats/templates/service.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.service }}
|
||||
{{- if .enabled }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "service.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
4
opencloud/charts/nats/templates/stateful-set.yaml
Normal file
4
opencloud/charts/nats/templates/stateful-set.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.statefulSet }}
|
||||
{{- include "nats.loadMergePatch" (merge (dict "file" "stateful-set/stateful-set.yaml" "ctx" $) .) }}
|
||||
{{- end }}
|
||||
37
opencloud/charts/nats/templates/tests/request-reply.yaml
Normal file
37
opencloud/charts/nats/templates/tests/request-reply.yaml
Normal file
@@ -0,0 +1,37 @@
|
||||
{{- include "nats.defaultValues" . }}
|
||||
{{- with .Values.natsBox | deepCopy }}
|
||||
{{- $natsBox := . }}
|
||||
{{- if .enabled -}}
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
{{- with .container }}
|
||||
{{- $_ := set . "merge" (dict
|
||||
"args" (list
|
||||
"sh"
|
||||
"-ec"
|
||||
"nats reply --echo echo & pid=\"$!\"; sleep 1; nats request echo hi > /tmp/resp; kill \"$pid\"; wait; grep -qF hi /tmp/resp"
|
||||
)
|
||||
) }}
|
||||
{{- $_ := set . "patch" list }}
|
||||
{{- end }}
|
||||
{{- with .podTemplate }}
|
||||
{{- $_ := set . "merge" (dict
|
||||
"metadata" (dict
|
||||
"name" (printf "%s-test-request-reply" $.Values.statefulSet.name)
|
||||
"labels" (dict
|
||||
"app.kubernetes.io/component" "test-request-reply"
|
||||
)
|
||||
"annotations" (dict
|
||||
"helm.sh/hook" "test"
|
||||
"helm.sh/hook-delete-policy" "before-hook-creation,hook-succeeded"
|
||||
)
|
||||
)
|
||||
"spec" (dict
|
||||
"restartPolicy" "Never"
|
||||
)
|
||||
) }}
|
||||
{{- $_ := set . "patch" list }}
|
||||
{{ include "nats.loadMergePatch" (merge (dict "file" "nats-box/deployment/pod-template.yaml" "ctx" (merge (dict "Values" (dict "natsBox" $natsBox)) $)) .) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
Reference in New Issue
Block a user