Skip to content

Commit

Permalink
common: render template in vm.app.name (#1783)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewChubatiuk authored Nov 21, 2024
1 parent 0f5f0bb commit 8a9669e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion charts/victoria-logs-single/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Change the values according to the need of the environment in ``victoria-logs-si
</td>
</tr>
<tr>
<td>global.nameOverride</td>
<td>nameOverride</td>
<td>string</td>
<td><pre class="helm-vars-default-value" language-yaml" lang="">
<code class="language-yaml">""
Expand Down
6 changes: 4 additions & 2 deletions charts/victoria-logs-single/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ global:
compatibility:
openshift:
adaptSecurityContext: "auto"
# -- Override chart name
nameOverride: ""
cluster:
# -- K8s cluster domain suffix, uses for building storage pods' FQDN. Details are [here](https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/)
dnsDomain: cluster.local.

# -- Override chart name
nameOverride: ""

# -- Print chart notes
printNotes: true

Expand Down
2 changes: 1 addition & 1 deletion charts/victoria-metrics-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Next release

- TODO
- support template rendering in `vm.app.name` template

## 0.0.29

Expand Down
2 changes: 1 addition & 1 deletion charts/victoria-metrics-common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
type: library
description: Victoria Metrics Common - contains shared templates for all Victoria Metrics helm charts
name: victoria-metrics-common
version: 0.0.29
version: 0.0.30
sources:
- https://github.com/VictoriaMetrics/helm-charts
kubeVersion: ">=1.23.0-0"
Expand Down
2 changes: 1 addition & 1 deletion charts/victoria-metrics-common/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) ![Version: 0.0.29](https://img.shields.io/badge/Version-0.0.29-informational?style=flat-square)
![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) ![Version: 0.0.30](https://img.shields.io/badge/Version-0.0.30-informational?style=flat-square)
[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/victoriametrics)](https://artifacthub.io/packages/helm/victoriametrics/victoria-metrics-common)

Victoria Metrics Common - contains shared templates for all Victoria Metrics helm charts
Expand Down
64 changes: 29 additions & 35 deletions charts/victoria-metrics-common/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ If release name contains chart name it will be used as a full name.
{{- define "vm.cr.fullname" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $_ := set . "overrideKey" "name" -}}
{{- $fullname := include "vm.internal.fullname" . -}}
{{- $fullname := include "vm.internal.key" . -}}
{{- $_ := unset . "overrideKey" -}}
{{- if empty $fullname -}}
{{- $fullname = include "vm.fullname" . -}}
Expand All @@ -78,17 +78,13 @@ If release name contains chart name it will be used as a full name.
{{- define "vm.managed.fullname" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $_ := set . "overrideKey" "name" -}}
{{- $fullname := include "vm.internal.fullname" . -}}
{{- $fullname := include "vm.internal.key" . -}}
{{- $_ := unset . "overrideKey" -}}
{{- if empty $fullname -}}
{{- $fullname = include "vm.fullname" . -}}
{{- end -}}
{{- $prefix := .appKey -}}
{{- if kindIs "slice" $prefix -}}
{{- $prefix = last (without $prefix "spec") -}}
{{- end -}}
{{- if $prefix -}}
{{- $prefix = ternary $prefix (printf "vm%s" $prefix) (or (hasPrefix "vm" $prefix) (hasPrefix "vl" $prefix)) -}}
{{- with include "vm.internal.key.default" . -}}
{{- $prefix := ternary . (printf "vm%s" .) (or (hasPrefix "vm" .) (hasPrefix "vl" .)) -}}
{{- $fullname = printf "%s-%s" $prefix $fullname -}}
{{- end -}}
{{- $fullname = tpl $fullname . -}}
Expand All @@ -102,15 +98,11 @@ If release name contains chart name it will be used as a full name.
{{- define "vm.plain.fullname" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $_ := set . "overrideKey" "fullnameOverride" -}}
{{- $fullname := include "vm.internal.fullname" . -}}
{{- $fullname := include "vm.internal.key" . -}}
{{- $_ := unset . "overrideKey" -}}
{{- if empty $fullname -}}
{{- $fullname = include "vm.fullname" . -}}
{{- $suffix := .appKey -}}
{{- if kindIs "slice" $suffix -}}
{{- $suffix = last $suffix }}
{{- end -}}
{{- with $suffix -}}
{{- with include "vm.internal.key.default" . -}}
{{- $fullname = printf "%s-%s" $fullname . -}}
{{- end -}}
{{- end -}}
Expand All @@ -122,10 +114,10 @@ If release name contains chart name it will be used as a full name.
{{- end -}}
{{- end -}}

{{- define "vm.internal.fullname" -}}
{{- define "vm.internal.key" -}}
{{- $overrideKey := .overrideKey | default "fullnameOverride" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $fullname := "" -}}
{{- $key := "" -}}
{{- if .appKey -}}
{{- $appKey := ternary (list .appKey) .appKey (kindIs "string" .appKey) -}}
{{- $ctx := . -}}
Expand All @@ -137,13 +129,23 @@ If release name contains chart name it will be used as a full name.
{{- fail (printf "No data for appKey %s" (join "->" $appKey)) -}}
{{- end -}}
{{- if and (kindIs "map" $values) (index $values $overrideKey) -}}
{{- $fullname = index $values $overrideKey -}}
{{- $key = index $values $overrideKey -}}
{{- else if and (kindIs "map" $ctx) (index $ctx $overrideKey) -}}
{{- $fullname = index $ctx $overrideKey -}}
{{- $key = index $ctx $overrideKey -}}
{{- end -}}
{{- end }}
{{- if and (empty $key) .fallback -}}
{{- $key = include "vm.internal.key.default" . -}}
{{- end -}}
{{- end -}}
{{- $key -}}
{{- end -}}

{{- define "vm.internal.key.default" -}}
{{- with .appKey -}}
{{- $key := ternary (list .) . (kindIs "string" .) -}}
{{- last (without $key "spec") -}}
{{- end -}}
{{- $fullname -}}
{{- end -}}

{{- /* Create chart name and version as used by the chart label. */ -}}
Expand Down Expand Up @@ -191,11 +193,11 @@ If release name contains chart name it will be used as a full name.
{{- /* Common labels */ -}}
{{- define "vm.labels" -}}
{{- include "vm.validate.args" . -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $labels := fromYaml (include "vm.selectorLabels" .) -}}
{{- $labels = mergeOverwrite $labels (fromYaml (include "vm.metaLabels" .)) -}}
{{- with (include "vm.image.tag" .) -}}
{{- $_ := set $labels "app.kubernetes.io/version" . -}}
{{- $tag := include "vm.image.tag" . -}}
{{- if .app }}
{{- $_ := set $labels "app.kubernetes.io/version" $tag -}}
{{- end -}}
{{- toYaml $labels -}}
{{- end -}}
Expand All @@ -213,19 +215,11 @@ If release name contains chart name it will be used as a full name.
{{- end -}}

{{- define "vm.app.name" -}}
{{- if .appKey -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $values := $Values -}}
{{- $appKey := ternary (list .appKey) .appKey (kindIs "string" .appKey) -}}
{{- $name := last $appKey }}
{{- range $ak := $appKey }}
{{- $values = (index $values $ak) | default dict -}}
{{- if and (kindIs "map" $values) $values.name -}}
{{- $name = $values.name -}}
{{- end -}}
{{- end -}}
{{- $name -}}
{{- end -}}
{{- $_ := set . "overrideKey" "name" -}}
{{- $_ := set . "fallback" true -}}
{{- tpl (include "vm.internal.key" .) . -}}
{{- $_ := unset . "overrideKey" -}}
{{- $_ := unset . "fallback" -}}
{{- end -}}

{{- /* Selector labels */ -}}
Expand Down
35 changes: 26 additions & 9 deletions charts/victoria-metrics-common/templates/_image.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
Victoria Metrics Image
*/}}
{{- define "vm.image" -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $image := (fromYaml (include "vm.internal.image" .)).image | default dict -}}
{{- $tag := include "vm.image.tag" . -}}
{{- if empty ((.app).image).repository -}}
{{- if empty $image.repository -}}
{{- fail "cannot create image without `.repository` defined" -}}
{{- end -}}
{{- $image := tpl (printf "%s:%s" ((.app).image).repository $tag) . -}}
{{- with ((.app).image).registry | default (($Values.global).image).registry | default "" -}}
{{- $image = (printf "%s/%s" . $image) -}}
{{- $result := tpl (printf "%s:%s" $image.repository $tag) . -}}
{{- with $image.registry | default "" -}}
{{- $result = (printf "%s/%s" . $result) -}}
{{- end -}}
{{- $image -}}
{{- $result -}}
{{- end -}}

{{- define "vm.image.tag" -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $image := (.app).image | default dict -}}
{{- $image := (fromYaml (include "vm.internal.image" .)).image | default dict -}}
{{- $tag := $image.tag -}}
{{- if empty $tag }}
{{- $tag = $Chart.AppVersion -}}
Expand All @@ -36,3 +34,22 @@ Victoria Metrics Image
{{- end -}}
{{- $tag -}}
{{- end -}}

{{- define "vm.internal.image" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $values := $Values -}}
{{- $ctx := . -}}
{{- with .appKey -}}
{{- $appKey := ternary (list .) . (kindIs "string" .) -}}
{{- range $ak := $appKey -}}
{{- $values = ternary (default dict) (index $values $ak | default dict) (empty $values) -}}
{{- $ctx = ternary (default dict) (index $ctx $ak | default dict) (empty $ctx) -}}
{{- if and (empty $values) (empty $ctx) -}}
{{- fail (printf "No data for appKey %s" (join "->" $appKey)) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $image := ternary $ctx.image $values.image (hasKey $ctx "image") -}}
{{- $image = mergeOverwrite (deepCopy (($Values.global).image | default dict)) ($image | default dict) -}}
{{- toYaml (dict "image" $image) -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ tests:
value: global
- equal:
path: vm.plain.fullname
value: global-spec
value: global-single
- equal:
path: vm.managed.fullname
value: vmsingle-override-2
Expand Down
6 changes: 6 additions & 0 deletions test-charts/victoria-metrics-common/tests/image_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tests:
- it: custom image tag
set:
ctx:
appKey:
- app
app:
image:
repository: vmagent
Expand All @@ -19,6 +21,8 @@ tests:
- it: custom image tag, global and local registry
set:
ctx:
appKey:
- app
app:
image:
registry: docker.io
Expand All @@ -37,6 +41,8 @@ tests:
- it: no image tag, and global registry only
set:
ctx:
appKey:
- app
app:
image:
repository: vmagent
Expand Down

0 comments on commit 8a9669e

Please sign in to comment.