首页 > 其他分享 >转载: Template Functions and Pipelines

转载: Template Functions and Pipelines

时间:2022-11-17 20:26:25浏览次数:69  
标签:function Functions Pipelines Template quote drink favorite v1 template

原文: Helm | Template Functions and Pipelines

 

 

Template Functions and Pipelines

So far, we've seen how to place information into a template. But that information is placed into the template unmodified. Sometimes we want to transform the supplied data in a way that makes it more useable to us.

Let's start with a best practice: When injecting strings from the .Values object into the template, we ought to quote these strings. We can do that by calling the quote function in the template directive:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ quote .Values.favorite.drink }}
  food: {{ quote .Values.favorite.food }}

Template functions follow the syntax functionName arg1 arg2.... In the snippet above, quote .Values.favorite.drink calls the quote function and passes it a single argument.

Helm has over 60 available functions. Some of them are defined by the Go template language itself. Most of the others are part of the Sprig template library. We'll see many of them as we progress through the examples.

While we talk about the "Helm template language" as if it is Helm-specific, it is actually a combination of the Go template language, some extra functions, and a variety of wrappers to expose certain objects to the templates. Many resources on Go templates may be helpful as you learn about templating.

Pipelines

One of the powerful features of the template language is its concept of pipelines. Drawing on a concept from UNIX, pipelines are a tool for chaining together a series of template commands to compactly express a series of transformations. In other words, pipelines are an efficient way of getting several things done in sequence. Let's rewrite the above example using a pipeline.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | quote }}
  food: {{ .Values.favorite.food | quote }}

In this example, instead of calling quote ARGUMENT, we inverted the order. We "sent" the argument to the function using a pipeline (|): .Values.favorite.drink | quote. Using pipelines, we can chain several functions together:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | quote }}
  food: {{ .Values.favorite.food | upper | quote }}

Inverting the order is a common practice in templates. You will see .val | quote more often than quote .val. Either practice is fine.

When evaluated, that template will produce this:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: trendsetting-p-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

Note that our original pizza has now been transformed to "PIZZA".

When pipelining arguments like this, the result of the first evaluation (.Values.favorite.drink) is sent as the last argument to the function. We can modify the drink example above to illustrate with a function that takes two arguments: repeat COUNT STRING:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | repeat 5 | quote }}
  food: {{ .Values.favorite.food | upper | quote }}

The repeat function will echo the given string the given number of times, so we will get this for output:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: melting-porcup-configmap
data:
  myvalue: "Hello World"
  drink: "coffeecoffeecoffeecoffeecoffee"
  food: "PIZZA"

Using the default function

One function frequently used in templates is the default function: default DEFAULT_VALUE GIVEN_VALUE. This function allows you to specify a default value inside of the template, in case the value is omitted. Let's use it to modify the drink example above:

drink: {{ .Values.favorite.drink | default "tea" | quote }}

If we run this as normal, we'll get our coffee:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: virtuous-mink-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

Now, we will remove the favorite drink setting from values.yaml:

favorite:
  #drink: coffee
  food: pizza

Now re-running helm install --dry-run --debug fair-worm ./mychart will produce this YAML:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fair-worm-configmap
data:
  myvalue: "Hello World"
  drink: "tea"
  food: "PIZZA"

In an actual chart, all static default values should live in the values.yaml, and should not be repeated using the default command (otherwise they would be redundant). However, the default command is perfect for computed values, which cannot be declared inside values.yaml. For example:

drink: {{ .Values.favorite.drink | default (printf "%s-tea" (include "fullname" .)) }}

In some places, an if conditional guard may be better suited than default. We'll see those in the next section.

Template functions and pipelines are a powerful way to transform information and then insert it into your YAML. But sometimes it's necessary to add some template logic that is a little more sophisticated than just inserting a string. In the next section we will look at the control structures provided by the template language.

Using the lookup function

The lookup function can be used to look up resources in a running cluster. The synopsis of the lookup function is lookup apiVersion, kind, namespace, name -> resource or resource list.

parametertype
apiVersion string
kind string
namespace string
name string

Both name and namespace are optional and can be passed as an empty string ("").

The following combination of parameters are possible:

BehaviorLookup function
kubectl get pod mypod -n mynamespace lookup "v1" "Pod" "mynamespace" "mypod"
kubectl get pods -n mynamespace lookup "v1" "Pod" "mynamespace" ""
kubectl get pods --all-namespaces lookup "v1" "Pod" "" ""
kubectl get namespace mynamespace lookup "v1" "Namespace" "" "mynamespace"
kubectl get namespaces lookup "v1" "Namespace" "" ""

When lookup returns an object, it will return a dictionary. This dictionary can be further navigated to extract specific values.

The following example will return the annotations present for the mynamespace object:

(lookup "v1" "Namespace" "" "mynamespace").metadata.annotations

When lookup returns a list of objects, it is possible to access the object list via the items field:

{{ range $index, $service := (lookup "v1" "Service" "mynamespace" "").items }}
    {{/* do something with each service */}}
{{ end }}

When no object is found, an empty value is returned. This can be used to check for the existence of an object.

The lookup function uses Helm's existing Kubernetes connection configuration to query Kubernetes. If any error is returned when interacting with calling the API server (for example due to lack of permission to access a resource), helm's template processing will fail.

Keep in mind that Helm is not supposed to contact the Kubernetes API Server during a helm template or a helm install|upgrade|delete|rollback --dry-run, so the lookup function will return an empty list (i.e. dict) in such a case.

Operators are functions

For templates, the operators (eqneltgtandor and so on) are all implemented as functions. In pipelines, operations can be grouped with parentheses ((, and )).

Now we can turn from functions and pipelines to flow control with conditions, loops, and scope modifiers.

标签:function,Functions,Pipelines,Template,quote,drink,favorite,v1,template
From: https://www.cnblogs.com/panpanwelcome/p/16900658.html

相关文章

  • template
    templaterefs©xgqfrms2012-2021www.cnblogs.com/xgqfrms发布文章使用:只允许注册用户才可以访问!原创文章,版权所有©️xgqfrms,禁止转载......
  • 【Jedis】Jedis的操作与使用、与RedisTemplate的区别
    一、参考资料​​Redis命令参考—Redis命令参考​​​​Java使用Jedis操作Redis的5种数据类型_pan_junbiao的博客​​spring整合jedis_旅知知的博客​​​Jedis、RedisT......
  • Java中restTemplate的使用
    原文链接代码地址本文介绍restTemplate基础用法。Java中get和post的用法请参考:https://mp.weixin.qq.com/s/mC0D1nuCqIori5bWtLorWQ1提供get/post接口1.1Controller......
  • C++面经 : C++ 11 新特性(闭包与匿名函数) ----C++ Closure & C++ anonymous functions
    关于C++Closure闭包和C++anonymousfunctions匿名函数什么是闭包?在C++中,闭包是一个能够捕获作用域变量的未命名函数对象,它包含了需要使用的“上下文”(函数......
  • C++ template using for what?
    //Forclassusing,youcansetaseriousofmacrotopredictsomething////whenyoucallthismacro,thetemplatecouldmakeanewfunctionbyyourinput//templ......
  • 利用TemplatesImpl攻击Shiro
    前言前边已经学习了CC1,CC6,CC3,其中1和3收到JDK8U71限制的的,C6是通杀高版本的,但是通过TemplatesImpl构造的利用链,理论上可以执行任意Java代码,这是一种非常通用的代码执行......
  • 优维低代码:Storyboard Functions 微应用函数
    优维低代码技术专栏,是一个全新的、技术为主的专栏,由优维技术委员会成员执笔,基于优维7年低代码技术研发及运维成果,主要介绍低代码相关的技术原理及架构逻辑,目的是给广大运维......
  • JDBCTemplate-快速入门和执行DML语句
    Spring框架对DBC的简单封装。提供了一个DBCTemplate对象简化JDBC的开发步骤:1、导入jar包2、创建jdbcTemplate对象。依赖于数据源Datasource jdbcTemplatetemplate......
  • ListView中排序和分组(GroupTemplate)的使用实例演示
    .aspx代码如下:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="8_Group_Sort.aspx.cs"Inherits="Group_Sort"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0......
  • django template 例子
    template不是django中的内容,而是在python中的。只是在django的松偶合的思想正好用到了这个东西。即写好界面的模板,然后再写好业务逻辑,最后组合即可,这样就将界面与逻辑分开来......