Skip to content

Commit

Permalink
common: added more templates (#1358)
Browse files Browse the repository at this point in the history
* common: added more generic template functions

* fix changelog

* support nested global context

* check built-in variables in both root and The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

Environment variables:

| Name                               | Description                                                                                                |
|------------------------------------|------------------------------------------------------------------------------------------------------------|
| $HELM_CACHE_HOME                   | set an alternative location for storing cached files.                                                      |
| $HELM_CONFIG_HOME                  | set an alternative location for storing Helm configuration.                                                |
| $HELM_DATA_HOME                    | set an alternative location for storing Helm data.                                                         |
| $HELM_DEBUG                        | indicate whether or not Helm is running in Debug mode                                                      |
| $HELM_DRIVER                       | set the backend storage driver. Values are: configmap, secret, memory, sql.                                |
| $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use.                                               |
| $HELM_MAX_HISTORY                  | set the maximum number of helm release history.                                                            |
| $HELM_NAMESPACE                    | set the namespace used for the helm operations.                                                            |
| $HELM_NO_PLUGINS                   | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                                                 |
| $HELM_PLUGINS                      | set the path to the plugins directory                                                                      |
| $HELM_REGISTRY_CONFIG              | set the path to the registry config file.                                                                  |
| $HELM_REPOSITORY_CACHE             | set the path to the repository cache directory                                                             |
| $HELM_REPOSITORY_CONFIG            | set the path to the repositories file.                                                                     |
| $KUBECONFIG                        | set an alternative Kubernetes configuration file (default "~/.kube/config")                                |
| $HELM_KUBEAPISERVER                | set the Kubernetes API Server Endpoint for authentication                                                  |
| $HELM_KUBECAFILE                   | set the Kubernetes certificate authority file.                                                             |
| $HELM_KUBEASGROUPS                 | set the Groups to use for impersonation using a comma-separated list.                                      |
| $HELM_KUBEASUSER                   | set the Username to impersonate for the operation.                                                         |
| $HELM_KUBECONTEXT                  | set the name of the kubeconfig context.                                                                    |
| $HELM_KUBETOKEN                    | set the Bearer KubeToken used for authentication.                                                          |
| $HELM_KUBEINSECURE_SKIP_TLS_VERIFY | indicate if the Kubernetes API server's certificate validation should be skipped (insecure)                |
| $HELM_KUBETLS_SERVER_NAME          | set the server name used to validate the Kubernetes API server certificate                                 |
| $HELM_BURST_LIMIT                  | set the default burst limit in the case the server contains many CRDs (default 100, -1 to disable)         |
| $HELM_QPS                          | set the Queries Per Second in cases where a high number of calls exceed the option for higher burst values |

Helm stores cache, configuration, and data based on the following configuration order:

- If a HELM_*_HOME environment variable is set, it will be used
- Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used
- When no other location is set a default location will be used based on the operating system

By default, the default directories depend on the Operating System. The defaults are listed below:

| Operating System | Cache Path                | Configuration Path             | Data Path               |
|------------------|---------------------------|--------------------------------|-------------------------|
| Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm |
| macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      |
| Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |

Usage:
  helm [command]

Available Commands:
  completion  generate autocompletion scripts for the specified shell
  create      create a new chart with the given name
  dependency  manage a chart's dependencies
  env         helm client environment information
  get         download extended information of a named release
  help        Help about any command
  history     fetch release history
  install     install a chart
  lint        examine a chart for possible issues
  list        list releases
  package     package a chart directory into a chart archive
  plugin      install, list, or uninstall Helm plugins
  pull        download a chart from a repository and (optionally) unpack it in local directory
  push        push a chart to remote
  registry    login to or logout from a registry
  repo        add, list, remove, update, and index chart repositories
  rollback    roll back a release to a previous revision
  search      search for a keyword in charts
  show        show information of a chart
  status      display the status of the named release
  template    locally render templates
  test        run tests for a release
  uninstall   uninstall a release
  unittest    unittest for helm charts
  upgrade     upgrade a release
  verify      verify that a chart at the given path has been signed and is valid
  version     print the client version information

Flags:
      --burst-limit int                 client-side default throttling limit (default 100)
      --debug                           enable verbose output
  -h, --help                            help for helm
      --kube-apiserver string           the address and the port for the Kubernetes API server
      --kube-as-group stringArray       group to impersonate for the operation, this flag can be repeated to specify multiple groups.
      --kube-as-user string             username to impersonate for the operation
      --kube-ca-file string             the certificate authority file for the Kubernetes API server connection
      --kube-context string             name of the kubeconfig context to use
      --kube-insecure-skip-tls-verify   if true, the Kubernetes API server's certificate will not be checked for validity. This will make your HTTPS connections insecure
      --kube-tls-server-name string     server name to use for Kubernetes API server certificate validation. If it is not provided, the hostname used to contact the server is used
      --kube-token string               bearer token used for authentication
      --kubeconfig string               path to the kubeconfig file
  -n, --namespace string                namespace scope for this request
      --qps float32                     queries per second used when communicating with the Kubernetes API, not including bursting
      --registry-config string          path to the registry config file (default "/Users/wachy/Library/Preferences/helm/registry/config.json")
      --repository-cache string         path to the file containing cached repository indexes (default "/Users/wachy/Library/Caches/helm/repository")
      --repository-config string        path to the file containing repository names and URLs (default "/Users/wachy/Library/Preferences/helm/repositories.yaml")

Use "helm [command] --help" for more information about a command. context

* pr comments
  • Loading branch information
AndrewChubatiuk authored Aug 29, 2024
1 parent 90a7674 commit 9db5f46
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 68 deletions.
10 changes: 9 additions & 1 deletion charts/victoria-metrics-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

## Next release

- TODO
- Added `vm.service` for unified service name generation
- Added `vm.url` to construct service base url
- Added `vm.name` for chart name
- Added `vm.fullname` which is actively used in resource name construction
- Added `vm.chart` to construct chart name label value
- Added `vm.labels` for common labels
- Added `vm.sa` for service account name
- Added `vm.release` for release name
- Added `vm.selectorLabels` for common selector labels

## 0.0.7

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.7
version: 0.0.8
sources:
- https://github.com/VictoriaMetrics/helm-charts
kubeVersion: ">=1.23.0-0"
Expand Down
41 changes: 22 additions & 19 deletions charts/victoria-metrics-common/templates/_enterprise.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{{- define "vm.license.secret.key" -}}
{{- ((.Values.license).secret).key | default (((.Values.global).license).secret).key | default "" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- (($Values.license).secret).key | default ((($Values.global).license).secret).key | default "" -}}
{{- end -}}

{{- define "vm.license.secret.name" -}}
{{- ((.Values.license).secret).name | default (((.Values.global).license).secret).name | default "" -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- (($Values.license).secret).name | default ((($Values.global).license).secret).name | default "" -}}
{{- end -}}

{{- define "vm.license.key" -}}
{{- (.Values.license).key | default ((.Values.global).license).key | default "" -}}
{{- $Values := (.helm).Values | default .Values }}
{{- ($Values.license).key | default (($Values.global).license).key | default "" -}}
{{- end -}}

{{- define "vm.enterprise.only" -}}
Expand All @@ -27,38 +30,38 @@
Return license volume mount
*/}}
{{- define "vm.license.volume" -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if and $licenseSecretName $licenseSecretKey -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if and $licenseSecretName $licenseSecretKey -}}
- name: license-key
secret:
secretName: {{ $licenseSecretName }}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Return license volume mount for container
*/}}
{{- define "vm.license.mount" -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if and $licenseSecretName $licenseSecretKey -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if and $licenseSecretName $licenseSecretKey -}}
- name: license-key
mountPath: /etc/vm-license-key
readOnly: true
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Return license flag if necessary.
*/}}
{{- define "vm.license.flag" -}}
{{- $licenseKey := (include "vm.license.key" .) -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if $licenseKey -}}
license: {{ $licenseKey }}
{{- else if and $licenseSecretName $licenseSecretKey -}}
licenseFile: /etc/vm-license-key/{{ $licenseSecretKey }}
{{- end -}}
{{- $licenseKey := (include "vm.license.key" .) -}}
{{- $licenseSecretKey := (include "vm.license.secret.key" .) -}}
{{- $licenseSecretName := (include "vm.license.secret.name" .) -}}
{{- if $licenseKey -}}
license: {{ $licenseKey }}
{{- else if and $licenseSecretName $licenseSecretKey -}}
licenseFile: /etc/vm-license-key/{{ $licenseSecretKey }}
{{- end -}}
{{- end -}}
115 changes: 115 additions & 0 deletions charts/victoria-metrics-common/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{{- define "vm.validate.args" -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- if empty $Chart -}}
{{- fail "invalid template data" -}}
{{- end -}}
{{- end -}}

{{- /* Expand the name of the chart. */ -}}
{{- define "vm.name" -}}
{{- include "vm.validate.args" . -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $Values.nameOverride | default $Values.global.nameOverride | default $Chart.Name | 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 "vm.fullname" -}}
{{- include "vm.validate.args" . -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $Release := (.helm).Release | default .Release -}}
{{- $appKey := .appKey -}}
{{- $fullname := default list -}}
{{- if $Values.fullnameOverride -}}
{{- $fullname = append $fullname .Values.fullnameOverride -}}
{{- else if and $appKey (dig $Chart.Name $appKey "fullnameOverride" "" ($Values.global)) -}}
{{- $fullname = append $fullname (index .Values.global $Chart.Name $appKey "fullnameOverride") -}}
{{- else }}
{{- $fullname = append $fullname $Release.Name -}}
{{- $name := default $Chart.Name $Values.nameOverride -}}
{{- if not (contains $name ($fullname | join "-")) -}}
{{- $fullname = append $fullname $name -}}
{{- end -}}
{{- if $appKey -}}
{{- $suffix := (index $Values $appKey "name") | default (dig $Chart.Name $appKey "name" "" $Values.global) -}}
{{- if $suffix -}}
{{- $fullname = append $fullname $suffix -}}
{{- end -}}
{{- end -}}
{{- end }}
{{- $fullname | join "-" | trunc 63 | trimSuffix "-" -}}
{{- end }}

{{- /* Create chart name and version as used by the chart label. */ -}}
{{- define "vm.chart" -}}
{{- include "vm.validate.args" . -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- printf "%s-%s" $Chart.Name $Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- /* Create the name of the service account to use */ -}}
{{- define "vm.sa.name" -}}
{{- include "vm.validate.args" . -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- if $Values.serviceAccount.create }}
{{- default (include "vm.fullname" .) $Values.serviceAccount.name }}
{{- else -}}
{{- default "default" $Values.serviceAccount.name -}}
{{- end }}
{{- end }}

{{- define "vm.metaLabels" -}}
{{- include "vm.validate.args" . -}}
{{- $Release := (.helm).Release | default .Release -}}
{{- $labels := .extraLabels | default dict -}}
{{- $_ := set $labels "helm.sh/chart" (include "vm.chart" .) -}}
{{- $_ := set $labels "app.kubernetes.io/managed-by" $Release.Service -}}
{{- toYaml $labels -}}
{{- end -}}

{{- /* 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 $Chart.AppVersion -}}
{{- $_ := set $labels "app.kubernetes.io/version" ($Chart.AppVersion) -}}
{{- end -}}
{{- toYaml $labels -}}
{{- end -}}

{{- define "vm.release" -}}
{{- include "vm.validate.args" . -}}
{{- $Release := (.helm).Release | default .Release -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- default $Release.Name $Values.argocdReleaseOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "vm.app.name" -}}
{{- if .appKey -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- if (index $Values .appKey).name -}}
{{- (index $Values .appKey).name -}}
{{- else if dig $Chart.Name .appKey "name" "" ($Values.global) -}}
{{- index $Values.global $Chart.Name .appKey "name" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- /* Selector labels */ -}}
{{- define "vm.selectorLabels" -}}
{{- $labels := .extraLabels | default dict -}}
{{- $_ := set $labels "app.kubernetes.io/name" (include "vm.name" .) -}}
{{- $_ := set $labels "app.kubernetes.io/instance" (include "vm.release" .) -}}
{{- with (include "vm.app.name" .) -}}
{{- $_ := set $labels "app" . -}}
{{- end -}}
{{- toYaml $labels -}}
{{- end }}
26 changes: 14 additions & 12 deletions charts/victoria-metrics-common/templates/_image.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
Victoria Metrics Image
*/}}
{{- define "vm.image" -}}
{{- $image := (tpl (printf "%s:%s" .app.image.repository (.app.image.tag | default .Chart.AppVersion)) .) -}}
{{- $license := .Values.license | default dict }}
{{- if and (or $license.key .Values.eula (dig "secret" "name" "" $license)) (empty .app.image.tag) -}}
{{- $_ := set .app.image "variant" "enterprise" -}}
{{- end -}}
{{- with .app.image.variant -}}
{{- $image = (printf "%s-%s" $image .) -}}
{{- end -}}
{{- with .app.image.registry | default ((.Values.global).image).registry | default "" -}}
{{- $image = (printf "%s/%s" . $image) -}}
{{- end -}}
{{- $image -}}
{{- $Chart := (.helm).Chart | default .Chart -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $image := (tpl (printf "%s:%s" .app.image.repository (.app.image.tag | default $Chart.AppVersion)) .) -}}
{{- $license := $Values.license | default dict }}
{{- if and (or $license.key $Values.eula (dig "secret" "name" "" $license)) (empty .app.image.tag) -}}
{{- $_ := set .app.image "variant" "enterprise" -}}
{{- end -}}
{{- with .app.image.variant -}}
{{- $image = (printf "%s-%s" $image .) -}}
{{- end -}}
{{- with .app.image.registry | default (($Values.global).image).registry | default "" -}}
{{- $image = (printf "%s/%s" . $image) -}}
{{- end -}}
{{- $image -}}
{{- end -}}
12 changes: 6 additions & 6 deletions charts/victoria-metrics-common/templates/_ingress.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{- define "vm.ingress.port" }}
{{- $port := dict "name" "http" }}
{{- with .port }}
{{- $numberTypes := list "int" "float64" }}
{{- $port = dict (ternary "number" "name" (has (kindOf .) $numberTypes)) . }}
{{- end -}}
{{- toYaml $port -}}
{{- $port := dict "name" "http" }}
{{- with .port }}
{{- $numberTypes := list "int" "float64" }}
{{- $port = dict (ternary "number" "name" (has (kindOf .) $numberTypes)) . }}
{{- end -}}
{{- toYaml $port -}}
{{- end }}
60 changes: 31 additions & 29 deletions charts/victoria-metrics-common/templates/_pod.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{- define "vm.port.from.flag" -}}
{{- $port := .default -}}
{{- with .flag -}}
{{- $port = regexReplaceAll ".*:(\\d+)" . "${1}" -}}
{{- end -}}
{{- $port -}}
{{- $port := .default -}}
{{- with .flag -}}
{{- $port = regexReplaceAll ".*:(\\d+)" . "${1}" -}}
{{- end -}}
{{- $port -}}
{{- end }}

{{- /*
Expand All @@ -12,28 +12,30 @@ Usage:
{{- include "vm.compatibility.isOpenshift" . -}}
*/ -}}
{{- define "vm.compatibility.isOpenshift" -}}
{{- if .Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
{{- true -}}
{{- end -}}
{{- $Capabilities := (.helm).Capabilities | default .Capabilities -}}
{{- if $Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
{{- true -}}
{{- end -}}
{{- end -}}

{{- /*
Render a compatible securityContext depending on the platform. By default it is maintained as it is. In other platforms like Openshift we remove default user/group values that do not work out of the box with the restricted-v1 SCC
Usage:
{{- include "vm.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) -}}
{{- include "vm.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "helm" $) -}}
*/ -}}
{{- define "vm.compatibility.renderSecurityContext" -}}
{{- $adaptedContext := .secContext -}}
{{- $adaptSecurityCtx := ((((.context.Values).global).compatibility).openshift).adaptSecurityContext | default "" -}}
{{- if or (eq $adaptSecurityCtx "force") (and (eq $adaptSecurityCtx "auto") (include "vm.compatibility.isOpenshift" .context)) -}}
{{- /* Remove incompatible user/group values that do not work in Openshift out of the box */ -}}
{{- $adaptedContext = omit $adaptedContext "fsGroup" "runAsUser" "runAsGroup" -}}
{{- if not .secContext.seLinuxOptions -}}
{{- /* If it is an empty object, we remove it from the resulting context because it causes validation issues */ -}}
{{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
{{- $adaptedContext := .secContext -}}
{{- $Values := (.helm).Values | default .Values -}}
{{- $adaptSecurityCtx := (((($Values).global).compatibility).openshift).adaptSecurityContext | default "" -}}
{{- if or (eq $adaptSecurityCtx "force") (and (eq $adaptSecurityCtx "auto") (include "vm.compatibility.isOpenshift" .)) -}}
{{- /* Remove incompatible user/group values that do not work in Openshift out of the box */ -}}
{{- $adaptedContext = omit $adaptedContext "fsGroup" "runAsUser" "runAsGroup" -}}
{{- if not $adaptedContext.seLinuxOptions -}}
{{- /* If it is an empty object, we remove it from the resulting context because it causes validation issues */ -}}
{{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- omit $adaptedContext "enabled" | toYaml -}}
{{- omit $adaptedContext "enabled" | toYaml -}}
{{- end -}}

{{- /*
Expand Down Expand Up @@ -68,31 +70,31 @@ Render probe
HTTP GET probe path
*/ -}}
{{- define "vm.probe.http.path" -}}
{{- index .app.extraArgs "http.pathPrefix" | default "" | trimSuffix "/" -}}/health
{{- index .app.extraArgs "http.pathPrefix" | default "" | trimSuffix "/" -}}/health
{{- end -}}

{{- /*
HTTP GET probe scheme
*/ -}}
{{- define "vm.probe.http.scheme" -}}
{{- ternary "HTTPS" "HTTP" (.app.extraArgs.tls | default false) -}}
{{- ternary "HTTPS" "HTTP" (.app.extraArgs.tls | default false) -}}
{{- end -}}

{{- /*
Net probe port
*/ -}}
{{- define "vm.probe.port" -}}
{{- dig "ports" "name" "http" (.app | dict) -}}
{{- dig "ports" "name" "http" (.app | dict) -}}
{{- end -}}

{{- define "vm.arg" -}}
{{- if empty .value }}
{{ .key }}
{{- else if and (kindIs "bool" .value) .value -}}
-{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}
{{- else -}}
-{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}={{ .value }}
{{- end -}}
{{- if empty .value }}
{{- .key -}}
{{- else if and (kindIs "bool" .value) .value -}}
-{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}
{{- else -}}
-{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}={{ .value }}
{{- end -}}
{{- end -}}

{{- /*
Expand Down
Loading

0 comments on commit 9db5f46

Please sign in to comment.