大网数据广告活动
服务器运维问答

Is there a regex function for kubernetes helm templates available?

· 52CE 我爱测

I'm trying to implement an ingress helm template encoding the current application version into the url. This shall allow users to do soft migration by providing them different versions of the application. Example: mydomain.org/1.…

问题原文

原帖链接 · Oliver Probst · CC BY-SA 3.0

I'm trying to implement an ingress helm template encoding the current application version into the url. This shall allow users to do soft migration by providing them different versions of the application.

Example:

mydomain.org/1.0/applicationpath
mydomain.org/1.1/applicationpath
mydomain.org/2.0/applicationpath

To prevent configuring this manually in the ingress on each update, I would prefer a regex determining the major.minor version automatically. The application is revised according to Semver 2.0.0, so a pattern like \d+.\d+ should do the work.

I tried something like this:

apiVersion: extensions/v1beta1
kind: Ingress
spec:
  backend:
    serviceName: {{ .Release.Name }}-my-srv
    servicePort: 80
  rules:
  - host: {{ .Values.app.ingress.host }}
    http:
      paths:
        - path: /{{ .Chart.AppVersion | regex \d+.\d+ }} 

Unfortunately I couldn't find a function for regular expressions. Is there an option to achieve this with a helm template?

已采纳解答

原帖链接 · phemmer · CC BY-SA 3.0

Yes there is. However it is unfortunately undocumented.
Helm uses the sprig library to provide additional functions for templating. What is undocumented is many of the sprig functions that have been added to the code (ref issue #84), and the regex functions are some of them. You can find these functions here: https://github.com/Masterminds/sprig/blob/master/regex.go

Thus you can do {{ .Chart.AppVersion | regexFind "\d+\.\d+" }}

(note that I also added a \ before the . to escape it as . means to match any character, which does not seem to be your intent)

 

Not your use case, but one item of note for others which might be interested in using the regex* functions from sprig, is that unfortunately the sprig owner made the subject string the second argument of all the functions, and some of the functions have additional arguments after it (e.g. the regexReplaceAll function). The reason why this is a problem is that when a function is used in a pipeline, the result of the previous element in the pipeline becomes the last argument of the function. So this does not work: "foo subject string" | regexReplaceAll "foo" "bar", as it'll treat bar as the string to operate on, and foo subject string as the string to replace any occurrences of foo with (which in this example there would not be any).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。