Let's learn how to write the template file within the chart. There are no strict rules for the template file name, only the extension .yaml is used, and the helper is .tpl.
Helpers can define reusable objects throughout the chart. Template directives must be enclosed in {{ and }} , for example, template directive {{ .Release.Name }} assigns a release name to the template..
built-in object
Release : Specifies the release itself. Release.Name: Release name Release.Namespace: namespace to be released (unless overridden in manifest) Release.IsUpgrade: set to true if current operation is upgrade or rollback Release.IsInstall: set to true if the current action is an install Release.Revision: The revision number of this Release. At first installation, this value is 1 and is incremented each time an upgrade or rollback is performed.
Release.Service: The service that renders the current template. Helm is always Helm Values : Values passed to the template from the values.yaml file and user-supplied files. By default, Values itself is empty. Chart : Contents of Chart.yaml file. All data in Chart.yaml is accessible here. For example, {{ .Chart.Name }}-{{ .Chart.Version }} outputs mychart-0.1.0 . Files : Provides access to all non-special files within the chart. It cannot be used to access templates, but can be used to access other files within the chart.
Files.Get is a function that gets a file by name. (.Files.Get config.ini) Files.GetBytes is a function that gets the contents of a file as a byte array, not a string. Useful when dealing with things like images. Files.Glob is a function that returns a list of files whose names match a given shell glob pattern. Files.Lines is a function that reads a file line by line. This is useful for iterating through each line in a file. Files.AsSecrets is a function that returns the file body as a Base64 encoded string (secret). Files.AsConfig is a function that returns the body of the file as a configmap. Capabilities : Provides information about the capabilities supported by the Kubernetes Cluster. Capabilities.APIVersions is a set of versions Capabilities.APIVersions.Has $version means whether a version (eg batch/v1) or resource (eg apps/v1/Deployment) is available in the cluster.
Capabilities.KubeVersion and Capabilities.KubeVersion.Version are the Kubernetes versions Capabilities.KubeVersion.Major is the Kubernetes major version Capabilities.KubeVersion.Minor is the Kubernetes minor version Template : Contains information about the current template being executed. Template.Name: path to the namespace file for the current template (e.g. mychart/templates/mytemplate.yaml) Template.BasePath: The namespace path to the template directory for the current chart (e.g. mychart/templates)
|
Built-in objects always start with a capital letter.
Value file
The Values object provides access to the values passed to the chart, usually through the values.yaml file within the chart. Of course, a separate yaml file passed through the -f option during helm install can also be accessed through the Values object. Also, individual values passed through --set can be accessed in the same way. I will explain with an example where the following values are set in the values.yaml file.
favoriteDrink: coffee
In the template, you can use the above values as follows
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favoriteDrink }}
|
This template is rendered as below when chart is installed. (The release name is installed as myconfig)
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig-configmap
data:
myvalue: "Hello World"
drink: coffee
|
Template function & Pipline
The built-in object assigns an immutable value to the Template. However, in reality, there are many cases where the value must be processed and used instead of being used as it is. To do this, we use functions and pipline. Let's use the function in the template, using the values.yaml file we used before.
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ quote .Values.favoriteDrink }}
|
The template function is used like function name argument1 argument2 ... . There are various functions (Template Function List) that can be used in the template, and various condition processing (Flow Control) is also possible.
pipline
One of the powerful features of templates is the concept of pipline. It is a tool that can express compactly by connecting template functions based on the pipline concept of unix. If the above example is expressed in pipline, it is as follows.
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favoriteDrink | quote }}
|
Using pipline changes the order. You will be passing the preceding argument to the function behind it. An example of linking a function via a pileline is:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favoriteDrink | upper | quote }}
|
Rendering it gives the following result:
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig-configmap
data:
myvalue: "Hello World"
drink: "COFFEE"
|
After converting coffee, the favoriteDrink value, to uppercase letters, it is enclosed in quotation marks.
'Kubernetes' 카테고리의 다른 글
[Kubernetis] Access API with curl (95) | 2024.03.20 |
---|---|
[Kubernetis] Building a MiniKube Environment on a Single Node (89) | 2024.03.16 |
[Cloud] 12. using Kubernetes Helm (17) | 2023.02.12 |
[Cloud] 11. Using Kubernetes Volume (31) | 2023.02.11 |
[Cloud] 10. Using Kubernetes Configmap (23) | 2023.02.10 |