Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍日期函数(Date Functions)。
日期函数(Date Functions)
Helm 包含以下可以在模板中使用的函数: ago
, date
, dateInZone
, dateModify(mustDateModify)
, duration
, durationRound
, htmlDate
, htmlDateInZone
, now
, toDate(mustToDate)
, unixEpoch
。
1、now
显示当前日期/时间, 以 string 类型打印出来。和其他日期函数一起使用。
- 语法:
now
templates/now.yaml 文件内容
now: {{ now }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/now.yaml
---
# Source: myapp/templates/now.yaml
now: 2020-08-25 01:50:59.895593177 +0000 UTC m=+0.041493103
2、ago
ago 函数返回距time.Now的以秒为单位的间隔时间。当前时间减去过去时间。
- 语法:
ago .Arg1
ago .CreatedAt"
返回time.Duration的字符串格式:2h34m7s
templates/ago.yaml 文件内容
ago {{ ago now }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/ago.yaml
---
# Source: myapp/templates/ago.yaml
ago: 0s
3、date
格式化时间输出,Go 语言的时间格式化,日期格式化为YEAR-MONTH-DAY:
- 语法:
date .Arg1 (.Arg2)
now | date "2006-01-02"
日期格式化在Go中有一些不同。简言之,以此为基准日期:Mon Jan 2 15:04:05 MST 2006
。
将其写成你想要的格式,上面的例子中,2006-01-02 是同一个日期,却是我们需要的格式。
yaml内容:
date: {{ date "2006-01-02 15:04:05" (now) }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/date.yaml
---
# Source: myapp/templates/date.yaml
date: 2022-03-24 09:40:34
4、dateInZone
带时区的时间格式化。
- 语法:
dateInZone .Arg1 (.Arg2) .Arg3
dateInZone "2006-01-02" (now) "UTC"
yaml内容:
dateInZone: {{ dateInZone "2006-02-02 15:04:05" (now) "Asia/Shanghai" }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/dateInZone.yaml
---
# Source: myapp/templates/dateInZone.yaml
dateInZone: 2020-08-24 17:48:42
5、duration
将给定的秒数格式化为time.Duration
。
- 语法:
duration .Arg1
这会返回 1m35s。
duration 95
6、durationRound
将给定时间舍入到最重要的单位。当time.Time
计算为一个自某个时刻以来的时间,字符串和time.Duration
被解析为一个时间段。
- 语法:
durationRound .Arg1
durationRound "2h10m5s"
,这会返回2h。
durationRound "2400h10m5s"
,这会返回3mo。
下例将指定秒粗略转换成分钟 (m)
yaml内容:
durationRound: {{ durationRound "500s" }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/durationRound.yaml
---
# Source: myapp/templates/durationRound.yaml
durationRound: 8m
7、unixEpoch
将指定时间转换成 时间戳格式 输出,返回time.Time
的unix时间戳。
- 语法:
unixEpoch .Arg1
now | unixEpoch
yaml内容:
unixEpoch: {{ unixEpoch now }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/unixEpoch.yaml
---
# Source: myapp/templates/unixEpoch.yaml
unixEpoch: 1598321573
8、dateModify, mustDateModify
- 语法:
dateModify .Arg1 .Arg2
- 语法:
mustDateModify .Arg1 .Arg2
dateModify 是修改时间函数,给定一个修改日期并返回时间戳。
- 如果修改格式错误, dateModify会返回日期未定义。
- mustDateModify 与 DateModify 函数一样, mustDateModify 会返回一个错误.
now | date_modify "-1.5h"
,表示从当前时间减去一个小时三十分钟:
yaml内容:
dateNow: {{ now }}
dateModify: {{ now | date_modify "-2h" }}
mustDateModify: {{ mustDateModify "500s" now}}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/datemodify.yaml
---
# Source: myapp/templates/datemodify.yaml
dateNow: 2022-03-24 09:52:41.016693113 +0000 UTC m=+0.045451911
dateModify: 2022-03-24 07:52:41.016733093 +0000 UTC m=-7199.954508131
mustDateModify: 2022-03-24 10:01:01.483551062 +0000 UTC m=+500.048063982
9、htmlDate
htmlDate
函数用于格式化插入到HTML日期选择器输入字段的日期,格式化时间为 年-月-日。
- 语法:
htmlDate .Arg1
now | htmlDate
yaml内容:
htmlDate: {{ now | htmlDate }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/htmlDate.yaml
---
# Source: myapp/templates/htmlDate.yaml
htmlDate: 2022-03-24
10、htmlDateInZone
带时区的格式化时间:年-月-日。
- 语法:
htmlDateInZone .Arg1 .Arg2
htmlDateInZone (now) "UTC"
yaml内容:
htmlDateInZone: {{ htmlDateInZone now "Asia/Shanghai" }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/htmlDateInZone.yaml
---
# Source: myapp/templates/htmlDateInZone.yaml
htmlDateInZone: 2022-03-24
11、toDate, mustToDate
toDate 将字符串转换成日期(将 string 类型的时间转换成时间类型并按照自定义格式化输出)。
- 语法:
toDate .Arg1 .Arg2
- 语法:
mustToDate .Arg1 .Arg2
- 第一个参数是日期格式。
- 第二个参数是日期字符串。
toDate如果字符串无法转换就会返回0值。mustToDate以防无法转换会返回错误。
这在你将日期字符串转换成其他格式时很有用(使用pipe)。下面的例子会将"2017-12-31" 转换成 “31/12/2017”。
toDate "2006-01-02" "2017-12-31" | date "02/01/2006"
yaml内容:
ToDate: {{ toDate "2006-01-02 15:04:05" "2020-08-25 09:58:00" }}
运行 template:
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/toDate.yaml
---
# Source: myapp/templates/toDate.yaml
ToDate: 2022-03-25 09:58:00 +0000 UTC
评论区