当前位置: 首页 > news >正文

k8s的包管理工具(4)--helm3子模板的使用

文章目录

  • 一、子模板的定义
    • 1. yaml文件中定义
    • 2. tpl文件中定义
  • 二、子模板的调用
    • 1. include调用(推荐)
    • 2. template调用

一、子模板的定义

在helm3中,子模板有两种定义方法:

  1. templates/xx.yaml文件中定义
  2. templates/xx.tpl文件内定义

定义子模板通过define定义,语法如下:

{{- define "子模板名" }}
...
{{- end }}

在实际使用中推荐在helpers.tpl文件中定义,结构更加清晰。

1. yaml文件中定义

[root@k8s-master m1]# cat chart6/templates/configmap.yaml
{{- define "childTemplates" }}labels:name: testdate: {{ now | date "2006-01-02" }}
{{- end }}

2. tpl文件中定义

{{- define "childTemplates" }}labels:name: {{ .Values.labels.name }}date: {{ now | date "2006-01-02" }}
{{- end }}

注意:如果使用了templates/目录的.tpl文件,应该使用rm -rf templates/*删除掉当前目录内的所有内容,重新创建需要的文件,否则会出错。 报错内容为:

Error: INSTALLATION FAILED: template: chart6/templates/tests/test-connection.yaml:4:12: executing "chart6/templates/tests/test-connection.yaml" at <include "chart6.fullname" .>: error calling include: template: no template "chart6.fullname" associated with template "gotpl"
helm.go:88: [debug] template: chart6/templates/tests/test-connection.yaml:4:12: executing "chart6/templates/tests/test-connection.yaml" at <include "chart6.fullname" .>: error calling include: template: no template "chart6.fullname" associated with template "gotpl"

二、子模板的调用

子模板的调用有两种方式,分别是templateinclude但更推荐使用include调用子模板。因为include支持管道,能够对返回值进行处理。

调用子模板语法如下:

{{- template/include "子模板名" . }}

.表示当前的作用域,其中调用的对象可以从整个chart的上下文中去获取值,比如values.yaml

1. include调用(推荐)

调用后处理结果:在helpers.tpl文件中定义子模板,在主yaml文件中调用
并对结果进行处理

_helpers.tpl文件

[root@k8s-master m1]# cat mychart/templates/_helpers.tpl 
# 默认子模板为顶格对齐
{{- define "childTemplates" }}
labels:date: {{ now | date "2006-01-02" }}
{{- end }}

configmap.yaml文件

apiVersion: v1
kind: ConfigMap
metadata:name: {{ .Release.Name }}{{- include "childTemplates" . | indent 2}}   # 缩进2个字符
data:value1: test

试运行结果

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: mychart  labels:date: 2025-11-04
data:value1: test

包含对象的调用:在helpers.tpl文件中定义子模板,在主yaml文件中调用

values.yml文件

[root@k8s-master m1]# cat mychart/values.yaml 
labels:app: test

configmap.yaml文件

[root@k8s-master m1]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:name: {{ .Release.Name }}{{- include "childTemplates" . }}
data:value1: test

_helpers.tpl文件

[root@k8s-master m1]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:name: {{ .Release.Name }}{{- template "childTemplates" . }}   # 注意. 缺少会导致变量无法获取值
data:value1: test

试运行

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: mychartlabels:app: testdate: 2025-11-04
data:value1: test

2. template调用

templates/configmap.yaml文件中定义子模板并直接调用

[root@k8s-master m1]# cat chart6/templates/configmap.yaml
{{- define "childTemplates" }}labels:app: testdate: {{ now | date "2006-01-02" }}
{{- end }}apiVersion: v1
kind: ConfigMap
metadata:name: {{ .Release.Name }}{{- template "childTemplates" . }}
data:value1: test

试运行查看调用结果

# Source: chart6/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: chart6labels:name: testdate: 2025-11-04
data:value1: test
http://www.dtcms.com/a/568946.html

相关文章:

  • 【C++】关于list的使用底层实现
  • Ubuntu安装单节点MicroK8s
  • Ubuntu22.04单节点部署k8s(无需外网)
  • 网站策划和运营微信最火的公众号排行
  • 北京景网站建设北京网站被处罚
  • 【深度学习新浪潮】llama.cpp完全适配Qwen3-0.6B/8B模型!从转换到部署保姆级教程
  • 24.NAT Server
  • Spring中@Controller与@RestController核心解析
  • 中企动力做的网站价格区间做网站v赚钱
  • uni-app中的地图引入(map)
  • Milvus:Json字段详解(十)
  • 八千字 《宠知汇》HarmonyOS应用案例热点技术解析
  • Box64 模拟器 让Steam 在 RISC-V 运行
  • 基于Django的智慧园区管理系统开发全解析
  • 2025上海国际汽车灯光及智能座舱展览将带来哪些新技术与新体验?
  • uniapp + Vue2 + Vuex + 持久化存储
  • 企业网站备案需要多久中文wordpress 主题
  • 香港 SFC 新规解读:虚拟资产交易平台迈向共享流动性与产品多元化时代
  • LegionSpace黑客松指南(一):项目开发流程指引
  • 网络注册公司怎么注册seo关键词推广价格
  • 阿拉伯语与中文对照词汇表PDF识别错误自动修正系统
  • 城市更新第一步:PDF转CAD,将历史图纸一键转化为设计复用底图
  • 矛盾论局事物本质内在逻辑洞察矛盾化解冲突拥抱矛盾智慧破局实战应用电子书籍PDF
  • 四层神经网络(含反向传播 Backpropagation)的完整数值计算+流程图示例
  • 第二部分(上):套接字
  • 深度学习Adam优化器核心概念全解析:参数,梯度,一阶动量,二阶动量
  • 网站模板哪里下载网站设计合同附件
  • 学习Linux——网络——网卡
  • 《原神》运行卡顿解决方案:游戏运行库合集一键安装指南
  • Java + Spring Boot + Redis技术栈,在实际使用缓存时遇到 缓存击穿、缓存穿透、缓存雪崩