Helm Template

raccoonback·2020년 3월 10일
3

kubernetes

목록 보기
1/3
post-thumbnail

Helm Template

IF 조건문

  • eq, ne, lt, gt, and, or, not 조건 오퍼레이션 제공
{{ if eq .Values.image.name '1' }}
	{{ ... }}
{{ else if eq .Values.image.name '2' }}
{{ else }}
	{{ ... }}
{{ end }}

Whitespace 제거

  • {{- : 줄바꿈 포함한 왼쪽 공백 모두 제거
  • -}} : 줄바꿈 포함한 오른쪽 공백 모두 제거

Helm 의 예제를 통해 살펴보면

drink: {{ .Values.favorite.drink | default "tea" | quote }}
food: {{ .Values.favorite.food | upper | quote }}
{{ if eq .Values.favorite.drink "coffee" }}
mug: true
{{ end }}

위 템플릿은 아래와 같이 공백을 포함해 렌더링됨.

drink: "coffee"
food: "PIZZA"

mug: true

공백을 제거하기 위해 - 추가

drink: {{ .Values.favorite.drink | default "tea" | quote }}
food: {{ .Values.favorite.food | upper | quote }}
{{- if eq .Values.favorite.drink "coffee" }}
mug: true
{{- end }}

아래 * 만큼 {{-, -}} 이 공백(줄바꿈 포함) 제거하여

drink: {{ .Values.favorite.drink | default "tea" | quote }}
food: {{ .Values.favorite.food | upper | quote }}*
*
mug: true*
*

결과적으로 아래와 같이 공백 제거됨.

drink: {{ .Values.favorite.drink | default "tea" | quote }}
food: {{ .Values.favorite.food | upper | quote }}
mug: true

Range 반복문

{{- range .Values.datas }}
	- {{ . | title | quote }}
{{- end }}
  • .Values.datas 의 리스트 값을 반복해 참조 가능
{{- range $index, $value := .Values.datas }}
	- {{ . | title | quote }}
{{- end }}

or

{{- range $key, $value := .Values.datas }}
	- {{ . | title | quote }}
{{- end }}
  • 위와 같이 인덱스(or key), value을 따로 추출해서 반복 가능

Scope 변경

  • .Values.image.name 을 참조한다면, 아래와 같이, with 이용해 해당 범위안에서는 특정 범위로 스코프를 변경 가능
{{ with .Values.image }}	// .Values.image 로 스코프 지정
	{{ .name }}		// .Values.image 없이 .name 참조 가능
{{ end }}
  • 만약 스코프 내부에서 외부 값을 참조하고 싶다면, 반드시 스코프 외부에서 변수 선언하여 참조해야 함.
{{ $temp := .Release.Name }}
{{ with .Values.image }}	
	{{ $temp }}		외부 스코프 값 참조
{{ end }}

Define

  • _ 로 시작하는 파일 또는 template 에서 특정 항목들을 정의할 수 있다.
  • 전역적으로 선언되어,여러 template 에서 include 해 참조 가능
{{ define "MY.NAME" }}  // 외부에서 "MY.NAME" 으로 참조하므로, 고유한 prefix 를 붙일 것으로 권장
  # body of template here
{{ end }}

Template

  • define 으로 선언한 'mychart.labels' 을 추가
  • template 은 pipeline으로 함수 호출 안되기 때문에 indent 에 유의(정의하는 쪽에서 indent 를 설정, 사용하는 측은 변경 어려움)
  • template 보다는 include 권장
  • template 두 번째 인자(.) 는 "mychart.labels" 내부에서의 스코프 지정

{{ template "mychart.labels" . }}

Include

  • template 과 유사 하나 pipeline 이용해 다중으로 함수 호출 가능하여, 사용측에서 indent 설정 가능
  • include 두 번째 인자(.) 는 "mychart.labels" 내부에서의 스코프 지정

{{ include "mychart.labels" . | nindent 4 }}

지원하는 함수

toYaml

  • .Values.image 값을 yaml 형식으로 변경
  • toJson 도 가능
{{ toYaml .Values.image }}

default

  • 'temp' 디폴트값 설정
{{ default 'temp' }}

nindent

  • indent 4 로 설정
{{ nindent 4 }}

quote

  • string 타입으로 변경
{{ quote .Values.image.name }}

pipeline

  • 함수를 연속적으로 사용해야 하는 경우 | 이용
{{ .Values.image.name | upper | default 'temp' | nindent 4 }}

repeat

  • .Values.image.name 값을 3번 반복해 추가
{{ .Values.image.name | repeat 3 }}
profile
한번도 실수하지 않은 사람은, 한번도 새로운 것을 시도하지 않은 사람이다.

0개의 댓글