ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Argo CD 通知模板内置函数完全指南:time、strings、sync 与 repo 函数详解

Argo CD 通知模板内置函数完全指南:time、strings、sync 与 repo 函数详解 Argo CD 通知模板内置函数完全指南time、strings、sync 与 repo 函数详解【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd导读Argo CD 的通知功能允许用户通过模板templates和触发器triggers组合出灵活多样的通知消息而通知表达式中可用的内置函数正是实现这种灵活性的核心能力。本文基于 Argo CD 官方文档中关于通知内置函数的说明结合当前仓库中 util/notification/expression 目录下的真实源码实现系统讲解time、strings、sync、repo四组函数的签名、语义、底层实现与实战用法帮助读者在自定义通知模板与触发器中正确、高效地使用这些函数输出符合预期的通知内容。函数体系概览Argo CD 通知模板与触发器中的表达式基于 Go 的 expr并以命名空间namespace的形式挂载内置函数。从源码看time与strings两组函数在包初始化时通过register(time, time.NewExprs())与register(strings, strings.NewExprs())全局注册见 expr.go而repo函数则依赖当前 Application 对象与 Argo CD 服务连接因此由Spawn()针对每个应用动态注入见 expr.go。sync函数同样在通知表达式环境中可用。函数的调用方式统一为{{ call .time.Now }} {{ call .time.Parse .app.status.operationState.startedAt }} {{ (call .repo.GetCommitMetadata .app.status.sync.revision).Author }}即在call关键字后以.命名空间.函数名的形式引用再传入所需的参数。返回值可以直接渲染到消息文本中也可以作为其他函数的入参嵌套使用例如先call .repo.FullNameByRepoURL再传给call .repo.QueryEscape。time时间相关函数与时区配置time命名空间提供了时间解析、获取当前时间以及一组时间常量其实现位于 util/notification/expression/time/time.go。配置本地时区time函数既可用于通知模板也可用于触发器。当使用.Local()将时间值转换为本地时间时Argo CD Notifications 使用的是argocd-notifications-controller容器所配置的本地时区。你可以通过在该容器的 Deployment 上设置TZ环境变量来指定时区apiVersion: apps/v1 kind: Deployment metadata: name: argocd-notifications-controller spec: template: spec: containers: - name: argocd-notifications-controller env: - name: TZ value: Asia/Tokyo例如一个通知模板可以按配置的本地时区格式化应用的某个时间戳{{ (call .time.Parse .app.status.operationState.startedAt).Local.Format 2006-01-02T15:04:05Z07:00 }}这里先调用time.Parse把operationState.startedAtRFC3339 格式字符串解析为time.Time再调用.Local()转换为容器所在时区的时间最后用Format按2006-01-02T15:04:05Z07:00布局输出带时区偏移的本地时间。更完整的模板上下文可参考 templates.md其中明确说明“模板可用的函数集合”并提供了使用.repo.GetCommitMetadata的模板示例。time.Now() Time执行 Go 标准库内置的 time.Now 函数返回一个 Gotime.Time实例。其底层实现直接映射了标准库函数源码位置time/time.go典型用法是生成当前时间戳例如向 GCP Pub/Sub 发送带 Unix 时间戳的通知参见 services/gcppubsub.mdtimestamp: {{(call .time.Now).Unix}}time.Parse(val string) Time将指定的字符串按 RFC3339 布局解析为 Gotime.Time实例。从源码看其实现调用了time.Parse(time.RFC3339, timestamp)如果解析失败会直接panic见 time/time.go因此在模板中使用时需保证传入的字符串确实是 RFC3339 格式。时间常量time命名空间还暴露了一组时间常量分为“时长Durations”与“时间戳布局Timestamps”两类均由 time/time.go 直接映射 Go 标准库常量Durations时长time.Nanosecond 1 time.Microsecond 1000 * Nanosecond time.Millisecond 1000 * Microsecond time.Second 1000 * Millisecond time.Minute 60 * Second time.Hour 60 * MinuteTimestamps时间戳布局用于将时间实例格式化为字符串例如time.Now().Format(time.RFC3339)。time.Layout 01/02 03:04:05PM 06 -0700 // The reference time, in numerical order. time.ANSIC Mon Jan _2 15:04:05 2006 time.UnixDate Mon Jan _2 15:04:05 MST 2006 time.RubyDate Mon Jan 02 15:04:05 -0700 2006 time.RFC822 02 Jan 06 15:04 MST time.RFC822Z 02 Jan 06 15:04 -0700 // RFC822 with numeric zone time.RFC850 Monday, 02-Jan-06 15:04:05 MST time.RFC1123 Mon, 02 Jan 2006 15:04:05 MST time.RFC1123Z Mon, 02 Jan 2006 15:04:05 -0700 // RFC1123 with numeric zone time.RFC3339 2006-01-02T15:04:05Z07:00 time.RFC3339Nano 2006-01-02T15:04:05.999999999Z07:00 time.Kitchen 3:04PM // Handy time stamps. time.Stamp Jan _2 15:04:05 time.StampMilli Jan _2 15:04:05.000 time.StampMicro Jan _2 15:04:05.000000 time.StampNano Jan _2 15:04:05.000000000strings字符串处理函数strings命名空间提供三个字符串处理函数实现位于 util/notification/expression/strings/strings.go分别是对 Go 标准库strings包同名函数的薄封装strings.ReplaceAll() string执行 Go 内置的 strings.ReplaceAll 函数签名语义为ReplaceAll(s, old, newV string) string将字符串s中所有出现的子串old替换为newV并返回新字符串见 strings/strings.go。strings.ToUpper() string执行 Go 内置的 strings.ToUpper 函数将字符串中所有字母转换为大写。strings.ToLower() string执行 Go 内置的 strings.ToLower 函数将字符串中所有字母转换为小写。这三个函数适合在通知消息中对应用名、分支名、环境名等文本做规范化处理例如把分支名统一转为小写后嵌入 URL 或标签。sync同步操作信息查询sync命名空间用于从 Argo CD Application 的同步操作中提取信息sync.GetInfoItem(app map, name string) string返回存储在 Argo CD Application 同步操作中、名称为name的info项的值。它常用于读取同步操作里携带的附加信息如同步时注入的自定义元数据以便在通知消息中展示。需要注意的是其第一个参数是 Application 对象本身在表达式中通常以.app传入。repo仓库信息函数repo命名空间提供与 Application 源仓库相关的函数实现位于 util/notification/expression/repo/repo.go并通过 expr.go 在表达式求值时针对每个 Application 注入。这些函数大多依赖argocdService见 util/notification/argocd/service.go与 repo-server 通信以获取真实仓库元数据。repo.RepoURLToHTTPS(url string) string将给定的 Git URL 转换为 HTTPS 格式。从源码看repo/repo.go其实现使用giturls.Parse解析 URL然后将 scheme 强制改为https并清除用户信息parsed.User nil。例如gitgithub.com:argoproj/argo-cd.git会被转换为https://github.com/argoproj/argo-cd.git适合直接嵌入需要 HTTPS 地址的链接。在 Google Chat 服务中有实际用例见 services/googlechat.mdtext: {{ call .repo.RepoURLToHTTPS .app.spec.source.repoURL }}repo.FullNameByRepoURL(url string) string返回仓库 URL 的完整名称(owner/repoName)当前仅支持 GitHub、GitLab 和 Bitbucket。从源码看repo/repo.go其实现先解析 URL再去除.git后缀取路径的前两段如owner/repo并拼接返回。例如 GitHub 仓库的 webhook 通知中可以这样使用见 services/webhook.mdpath: /repos/{{call .repo.FullNameByRepoURL .app.spec.source.repoURL}}/statuses/{{.app.status.operationState.operation.sync.revision}}repo.QueryEscape(s string) string对字符串进行转义使其可以安全地放入 URL 中。从源码看它直接映射了 Go 标准库的url.QueryEscape见 repo/repo.go。一个典型的组合用法是生成 GitLab 合并请求链接原文档示例/projects/{{ call .repo.QueryEscape (call .repo.FullNameByRepoURL .app.status.RepoURL) }}/merge_requestsrepo.GetCommitMetadata(sha string) CommitMetadata返回指定 commit 的元数据。该 commit 必须属于 Application 的源仓库。返回的CommitMetadata字段如下Message string— commit 消息Author string— commit 作者Date time.Time— commit 创建时间Tags []string— 关联的标签从源码看其底层调用链为先从 Application 对象中读取spec.source.repoURL与spec.project见 repo/repo.go再调用argocdService.GetCommitMetadata最终由 repo-server 通过GetRevisionMetadata返回真实元数据见 argocd/service.goDate取自元数据时间。该函数在多个通知服务中有实际用例例如 Alertmanager见 services/alertmanager.mdauthor: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}} message: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}}在 New Relic 服务中通知标题也默认使用 commit 作者与消息见 services/newrelic.md。repo.GetAppDetails() AppDetail返回 Application 的详细信息。返回的AppDetail字段如下Type string— AppDetail 类型Helm HelmAppSpec— Helm 详情字段FieldsName stringValueFiles []stringParameters []*v1alpha1.HelmParameterValues stringFileParameters []*v1alpha1.HelmFileParameter方法MethodsGetParameterValueByName(Name string)— 按名称在Parameters字段中获取值GetFileParameterPathByName(Name string)— 按名称在FileParameters字段中获取路径Kustomize *apiclient.KustomizeAppSpec— Kustomize 详情Directory *apiclient.DirectoryAppSpec— Directory 详情在仓库实现中AppDetail与CustomHelmAppSpec定义于 util/notification/expression/shared/appdetail.go。值得注意的实现细节是GetParameterValueByName会优先在 Helm 参数覆盖overrides即 Application 源码中source.helm.parameters中查找其次才在 repo-server 返回的 Helm 参数列表中查找均未命中时返回空字符串见 appdetail.goGetFileParameterPathByName则遍历FileParameters按名称返回对应的文件路径见 appdetail.go。从 argocd/service.go 的实现可以看到GetAppDetails会依次获取仓库信息、Helm 仓库列表、Kustomize 与 Helm 全局设置然后向 repo-server 发起GetAppDetails查询最后组装成上述AppDetail结构。因此该函数适用于在通知中输出应用的 Helm 参数值、Kustomize 配置或目录化应用Directory的细节。实战把函数组合进通知模板将上述函数组合进argocd-notifications-cmConfigMap 中的模板即可完成一个完整的通知定义。以下示例综合使用了repo与strings函数发送一条包含作者、消息和规范化仓库名的 Slack 通知apiVersion: v1 kind: ConfigMap metadata: name: argocd-notifications-cm data: template.commit-info: | message: | Author: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}} Message: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}} Repo: {{ call .repo.RepoURLToHTTPS .app.spec.source.repoURL }} Owner/Repo: {{ call .strings.ToLower (call .repo.FullNameByRepoURL .app.spec.source.repoURL) }}配合时间函数还可以加入部署开始时间Started At: {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format (call .time.RFC3339) }}完整的多服务模板配置可参考 templates.md 与各服务文档services/ 目录。小结time、strings、sync、repo四组函数构成了 Argo CD 通知模板/触发器表达式的主要内置能力time负责时间解析、当前时间与格式化常量strings提供字符串大小写与替换处理sync读取同步操作中的附加信息repo则通过与 repo-server 的联动提供仓库 URL 转换、归属解析、commit 元数据与应用详情等能力。从源码可以看到这些函数多为对 Go 标准库的薄封装行为可预期、可在模板中放心使用而repo组的函数底层依赖真实的 Argo CD 服务链路使用前请确保对应仓库已正确配置并被 Argo CD 访问。更多函数组合与触发条件可继续阅读 templates.md 与 triggers.md。【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表