I am trying to extract a certain string obtained from values.yaml
in _helper.tpl
. Basically, I am trying to extract the string localhost
from mongodb://localhost:30010
In my _helper.tpl
{{- define "myservice.mongodbcache.bindip" -}}
{{- regexFind "\/\.(.*):" ( .Values.myservice.cachedb.uri | toString ) -}}
{{- end -}}
my values.yml
file
myservice:
cachedb:
uri: "mongodb://localhost:30010"
and in my configmap
, I want to use it the following way
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-myservice-configmap
namespace: {{ .Release.Namespace }}
data:
myservice.mongodbcache.bind.ip: {{ template "myservice.mongodbcache.bindip" }}
However, I keep getting this error, when I try to dry run
Error: parse error in "tree-helm/templates/_helpers.tpl": template: tree-helm/templates/_helpers.tpl:35: invalid syntax
Line 35 is the one with regexFind
To get the host from a URL, you would need to use Lookahead and Lookbehind. Please see the example
Unfortunately, you can not write this type of regex in helm. You will get the following error:
Error: rendering template failed: regexp: Compile(`(?<=://)(.*?)(?=:)`): error parsing regexp: invalid or unsupported Perl syntax: `(?<`
From chart template guide:
It is considered preferable to use include over template in Helm templates simply so that the output formatting can be handled better for YAML documents.
Working with include, you can get the hostname or ip the following way:
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-myservice-configmap
namespace: {{ .Release.Namespace }}
data:
myservice.mongodbcache.bind.ip: {{- include "myservice.mongodbcache.bindip" . -}}
_helpers.tpl
{{- define "myservice.mongodbcache.bindip" -}}
{{- $match := .Values.myservice.cachedb.uri | toString | regexFind "//.*:" -}}
{{- $match | trimAll ":" | trimAll "/" -}}
{{- end -}}
Ore even have it all in one line.
Result
$ helm install --debug --dry-run .
....
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: billowing-boxer-myservice-configmap
namespace: default
data:
myservice.mongodbcache.bind.ip:localhost