Helm内置变量的使用

https://helm.sh/zh/docs/chart_template_guide/builtin_objects/

  • Release.Name:实例的名称,helm install指定的名字
  • Release.Namespace:应用实例的命名空间
  • Release.IsUpgrade:如果当前对实例的操作是更新或者回滚,这个变量的值就会被置为true
  • Release.IsInstall:如果当前对实例的操作是安装,则这变量被置为true
  • Release.Revision:此次修订的版本号,从1开始,每次升级回滚都会增加1
  • Chart:Chart.yaml文件中的内容,可以使用Chart.version表示应用版本,Chart.Name表示Chart的名称

Helm常用函数的使用

http://masterminds.github.io/sprig/strings.html

trim

The trim function removes space from e ither side of a string:

trim函数会删除字符串两边的空格:

trim "   hello    "

The above produces hello

values.yaml:

trimabc: "  abc    "

templates/deployment.yaml

trim: {{ .Values.trimabc | trim  }}

result:trimabc: abc

trimAll

Remove given characters from the front or back of a string:
从字符串的前面或后面删除给定的字符:

trimAll "$" "$5.00"

The above returns 5.00 (as a string).

values.yaml:

trimalltest: xxxtest

templates/deployment.yaml

trimall: {{ .Values.trimalltest | trimAll "x" }}

result:trimalltest: test

trimSuffix

Trim just the suffix from a string:
修剪字符串的后缀:

trimSuffix "-" "hello-"

The above returns hello

trimPrefix

Trim just the prefix from a string:
修剪字符串的前缀:

trimPrefix "-" "-hello"

The above returns hello

upper

Convert the entire string to uppercase:
将整个字符串转换为大写:

upper "hello"

The above returns HELLO

lower

Convert the entire string to lowercase:
将整个字符串转换为小写:

lower "HELLO"

The above returns hello·

title

Convert to title case:
转换为标题大小写:

title "hello world"

The above returns Hello World

untitle

Remove title casing. untitle "Hello World" produces hello world.

repeat

Repeat a string multiple times:
将一个字符串重复多次:

repeat 3 "hello"

The above returns hellohellohello

substr

Get a substring from a string. It takes three parameters:
从字符串中获取子字符串。它有三个参数:

  • start (int)
  • end (int)
  • string (string)
substr 0 5 "hello world"

The above returns hello

nospace

Remove all whitespace from a string.
删除字符串中的所有空格。

nospace "hello w o r l d"

The above returns helloworld

trunc

Truncate a string (and add no suffix)
截断字符串(不添加后缀)

trunc 5 "hello world"

The above produces hello.

trunc -5 "hello world"

The above produces world.

abbrev

Truncate a string with ellipses (...)
用省略号截断字符串
Parameters:

  • max length
  • the string
abbrev 5 "hello world"

The above returns he..., since it counts the width of the ellipses against the maximum length.

abbrevboth

Abbreviate both sides:
缩写两边:

abbrevboth 5 10 "1234 5678 9123"

the above produces ...5678...
It takes:

  • left offset
  • max length
  • the string

initials

Given multiple words, take the first letter of each word and combine.
给定多个单词,将每个单词的首字母组合起来。

initials "First Try"

The above returns FT

randAlphaNum, randAlpha, randNumeric, and randAscii

These four functions generate cryptographically secure (uses crypto/rand) random strings, but with different base character sets:
这四个函数生成加密安全(使用crypto/rand)的随机字符串,但使用不同的基本字符集:

  • randAlphaNum uses 0-9a-zA-Z
  • randAlpha uses a-zA-Z
  • randNumeric uses 0-9
  • randAscii uses all printable ASCII characters

Each of them takes one parameter: the integer length of the string.

randNumeric 3

The above will produce a random string with three digits.

wrap

Wrap text at a given column count:
在给定的列数处换行文本:

wrap 80 $someText

The above will wrap the string in $someText at 80 columns.

wrapWith

wrapWith works as wrap, but lets you specify the string to wrap with. (wrap uses \n)
wrapWith的工作原理与换行相同,但允许您指定要使用的字符串。

wrapWith 5 "\t" "Hello World"

The above produces hello world (where the whitespace is an ASCII tab character)

contains

Test to see if one string is contained inside of another:
测试一个字符串是否包含在另一个字符串中:

contains "cat" "catch"

The above returns true because catch contains cat.

hasPrefix and hasSuffix

The hasPrefix and hasSuffix functions test whether a string has a given prefix or suffix:
hasPrefix和hasSuffix函数测试字符串是否具有给定的前缀或后缀:

hasPrefix "cat" "catch"

The above returns true because catch has the prefix cat.

quote and squote

These functions wrap a string in double quotes (quote) or single quotes (squote).
这些函数用双引号(quote)或单引号(squote)包装字符串。

cat

The cat function concatenates multiple strings together into one, separating them with spaces:
cat函数将多个字符串连接成一个字符串,用空格分隔它们:

cat "hello" "beautiful" "world"

The above produces hello beautiful world

indent

The indent function indents every line in a given string to the specified indent width. This is useful when aligning multi-line strings:
indent函数将给定字符串中的每一行缩进到指定的缩进宽度。这在对齐多行字符串时很有用:

indent 4 $lots_of_text

The above will indent every line of text by 4 space characters.

nindent

The nindent function is the same as the indent function, but prepends a new line to the beginning of the string.
nindent函数与indent函数相同,只是在字符串的开头加一个新行。

nindent 4 $lots_of_text

The above will indent every line of text by 4 space characters and add a new line to the beginning.

replace

Perform simple string replacement.
执行简单的字符串替换。
It takes three arguments:

  • string to replace
  • string to replace with
  • source string
"I Am Henry VIII" | replace " " "-"

The above will produce I-Am-Henry-VIII

plural

Pluralize a string.
将字符串复数化。

len $fish | plural "one anchovy" "many anchovies"

In the above, if the length of the string is 1, the first argument will be printed (one anchovy). Otherwise, the second argument will be printed (many anchovies).
The arguments are:

  • singular string
  • plural string
  • length integer

NOTE: Sprig does not currently support languages with more complex pluralization rules. And 0 is considered a plural because the English language treats it as such (zero anchovies). The Sprig developers are working on a solution for better internationalization.

snakecase

Convert string from camelCase to snake_case.
将字符串从camelCase转换为snake_case。

snakecase "FirstName"

This above will produce first_name.

camelcase

Convert string from snake_case to CamelCase
将字符串从snake_case转换为CamelCase

camelcase "http_server"

This above will produce HttpServer.

kebabcase

Convert string from camelCase to kebab-case.
将字符串从camelCase转换为kebab-case。

kebabcase "FirstName"

This above will produce first-name.

swapcase

Swap the case of a string using a word based algorithm.
使用基于单词的算法交换字符串的大小写。
Conversion algorithm:

  • Upper case character converts to Lower case
  • Title case character converts to Lower case
  • Lower case character after Whitespace or at start converts to Title case
  • Other Lower case character converts to Upper case
  • Whitespace is defined by unicode.IsSpace(char)
swapcase "This Is A.Test"

This above will produce tHIS iS a.tEST.

shuffle

Shuffle a string.
打乱字符串。

shuffle "hello"

The above will randomize the letters in hello, perhaps producing oelhl.

regexMatch, mustRegexMatch

Returns true if the input string contains any match of the regular expression.
如果输入字符串包含正则表达式的任何匹配项,则返回true。

regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" "test@acme.com"

The above produces true
regexMatch panics if there is a problem and mustRegexMatch returns an error to the template engine if there is a problem.

regexFindAll, mustRegexFindAll

Returns a slice of all matches of the regular expression in the input string. The last parameter n determines the number of substrings to return, where -1 means return all matches
返回输入字符串中正则表达式所有匹配项的切片。最后一个参数n决定要返回的子字符串的数量,其中-1表示返回所有匹配

regexFindAll "[2,4,6,8]" "123456789" -1

The above produces [2 4 6 8]
regexFindAll panics if there is a problem and mustRegexFindAll returns an error to the template engine if there is a problem.

regexFind, mustRegexFind

Return the first (left most) match of the regular expression in the input string
返回输入字符串中正则表达式的第一个(最左边)匹配项

regexFind "[a-zA-Z][1-9]" "abcd1234"

The above produces d1
regexFind panics if there is a problem and mustRegexFind returns an error to the template engine if there is a problem.

regexReplaceAll, mustRegexReplaceAll

Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. Inside string replacement, signs are interpreted as in Expand, so for instance1 represents the text of the first submatch
返回输入字符串的副本,用替换字符串replacement替换正则表达式的匹配项。在字符串替换中,符号被解释为Expand,因此例如1表示第一个子匹配的文本

regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W"

The above produces -W-xxW-
regexReplaceAll panics if there is a problem and mustRegexReplaceAll returns an error to the template engine if there is a problem.

regexReplaceAllLiteral, mustRegexReplaceAllLiteral

Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement The replacement string is substituted directly, without using Expand
返回输入字符串的副本,用替换字符串replace替换正则表达式的匹配项。替换字符串被直接替换,不使用Expand

regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"

The above produces -${1}-${1}-
regexReplaceAllLiteral panics if there is a problem and mustRegexReplaceAllLiteral returns an error to the template engine if there is a problem.

regexSplit, mustRegexSplit

Slices the input string into substrings separated by the expression and returns a slice of the substrings between those expression matches. The last parameter n determines the number of substrings to return, where -1 means return all matches
将输入字符串切片为由表达式分隔的子字符串,并返回这些表达式匹配之间的子字符串切片。最后一个参数n决定要返回的子字符串的数量,其中-1表示返回所有匹配

regexSplit "z+" "pizza" -1

The above produces [pi a]
regexSplit panics if there is a problem and mustRegexSplit returns an error to the template engine if there is a problem.

regexQuoteMeta

Returns a string that escapes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text.
返回一个字符串,该字符串转义参数文本中的所有正则表达式元字符;返回的字符串是匹配文本的正则表达式。

regexQuoteMeta "1.2.3"

The above produces 1\.2\.3

Type Conversion Functions

The following type conversion functions are provided by Sprig:

  • atoi: Convert a string to an integer.
  • float64: Convert to a float64.
  • int: Convert to an int at the system’s width.
  • int64: Convert to an int64.
  • toDecimal: Convert a unix octal to a int64.
  • toString: Convert to a string.
  • toStrings: Convert a list, slice, or array to a list of strings.

Only atoi requires that the input be a specific type. The others will attempt to convert from any type to the destination type. For example, int64 can convert floats to ints, and it can also convert strings to ints.

toStrings

Given a list-like collection, produce a slice of strings.

list 1 2 3 | toStrings

The above converts 1 to "1", 2 to "2", and so on, and then returns them as a list.

toDecimal

Given a unix octal permission, produce a decimal.

"0777" | toDecimal

The above converts 0777 to 511 and returns the value as an int64.