helm中template与include区别

相同与不同点

1、本质上 template和include都是操作而不是函数,数据只是进行了内联插入这个操作。
2、无法将template调用的输出传递给其他函数。
3、include调用的输出可以通过管道符传递给其他函数。

例子

  • _helpers.tpl中定义方法
#_helpers.tpl
{
    
    {
    
    /* Generate basic labels */}}
{
    
    {
    
    - define "mychart.app" -}}
app_name: {
    
    {
    
     .Chart.Name }}
app_version: "{
    
    { .Chart.Version }}"
{
    
    {
    
    - end -}}
  • 在configmap.yaml通过template插入方法中的内容
#configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
  labels:
    {
    
    {
    
     template "mychart.app" . }}
data:
  myvalue: "Hello World"
  {
    
    {
    
    - range $key, $val := .Values.favorite }}
  {
    
    {
    
     $key }}: {
    
    {
    
     $val | quote }}
  {
    
    {
    
    - end }}
{
    
    {
    
     template "mychart.app" . }}
  • 结果输出1
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: measly-whippet-configmap
  labels:
    app_name: mychart
app_version: "0.1.0+1478129847"
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "pizza"
  app_name: mychart
app_version: "0.1.0+1478129847"

结果分析:可以看到已经,调用的方法之后,插入的内容没有严格的按照对其格式排版

  • 使用include插入内容
apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
  labels:
{
    
    {
    
     include "mychart.app" . | indent 4 }}
data:
  myvalue: "Hello World"
  {
    
    {
    
    - range $key, $val := .Values.favorite }}
  {
    
    {
    
     $key }}: {
    
    {
    
     $val | quote }}
  {
    
    {
    
    - end }}
{
    
    {
    
     include "mychart.app" . | indent 2 }}
  • 结果输出2
# Source: mychart/templates/configmap.yaml
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: edgy-mole-configmap
  labels:
    app_name: mychart
    app_version: "0.1.0+1478129987"
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "pizza"
  app_name: mychart
  app_version: "0.1.0+1478129987"

可以看到已经对齐了

官方解释

参考helm官网

Because template is an action, and not a function, there is no way to pass the output of a template call to other functions; the data is simply inserted inline.,To work around this case, Helm provides an alternative to template that will import the contents of a template into the present pipeline where it can be passed along to other functions in the pipeline.。
It is considered preferable to use include over template in Helm templates simply so that the output formatting can be handled better for YAML documents.

猜你喜欢

转载自blog.csdn.net/qq_26884501/article/details/108150545