helmのチュートリアルをちょっとやった

## ダウンロード

```
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get | bash
```

## Helm開始

```
helm init
```

## tillerデプロイ

```
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
```

出力

```
NAME                             READY   STATUS    RESTARTS   AGE
tiller-deploy-6b549fbc4d-8c5gz   1/1     Running   0          46s
```

## helm create

```
helm create mychart
```

```
~/D/helm ❯❯❯ tree mychart
mychart
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 9 files
```

##  Templateファイルの作成
```
rm -fr mychart/templates/*
echo "" > mychart/values.yaml
```

## ConfigMap作成
```
vim mychart/templates/configmap.yaml
```



```
apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart-configmap
date:
  myvalue: "Hello World"
```

## helm install

```
helm install ./mychart
```

```
NAME:   vehement-tarsier
LAST DEPLOYED: Mon Feb 17 12:46:05 2020
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME               AGE
mychart-configmap  0s
```

## 変数化

```
vim mychart/templates/configmap.yaml
```

`.Release.Name` は予約語

```
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
```

### ドライラン

```
helm install --debug --dry-run ./mychart
```

出力結果

```
[debug] Created tunnel using local port: '53525'

[debug] SERVER: "127.0.0.1:53525"

[debug] Original chart version: ""
[debug] CHART PATH: /Users/yuta/Desktop/helm/mychart

NAME:   quarreling-seahorse
REVISION: 1
RELEASED: Mon Feb 17 12:49:39 2020
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
{}

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: quarreling-seahorse-configmap
data:
  myvalue: "Hello World"
```

## values.yamlを使った変数化

```
vim mychart/values.yaml
```

```
favoriteDrink: coffee
```

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

### ドライラン

```
helm install --debug --dry-run ./mychart
```

```
[debug] Created tunnel using local port: '53998'

[debug] SERVER: "127.0.0.1:53998"

[debug] Original chart version: ""
[debug] CHART PATH: /Users/yuta/Desktop/helm/mychart

NAME:   mewing-abalone
REVISION: 1
RELEASED: Mon Feb 17 12:53:05 2020
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favoriteDrink: coffee

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mewing-abalone-configmap
data:
  myvalue: "Hello World"
  drink: coffee
```


## 変数の構造化

```mychart/values.yaml
favorite:
  drink: coffee
  food: pizza
```

```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink }}
  food: {{ .Values.favorite.food }}
```

### ドライラン

```
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: virulent-antelope-configmap
data:
  myvalue: "Hello World"
  drink: coffee
  food: pizza
```

## パイプライン

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

### ドライラン

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

## デフォルト値

```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea"  | quote}}
  food: {{ .Values.favorite.food | upper | quote}}
```

```mychart/values.yaml
favorite:
  #drink: coffee
  food: pizza
```

### ドライラン

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

## if文

```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea"  | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee" }}mug: true{{ end }}
```

### ドライラン

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

## 変数スコープの変更

`.Values.favorite.drink` ⇨ `.drink`

```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea"  | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
```

## Range

for文

```mychart/values.yaml
favorite:
  drink: coffee
  food: pizza
pizzaToppings:
  - mushrooms
  - cheese
  - pappers
  - onions
```

```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea"  | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
  toppings: |-
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }}
```

### ドライラン

```
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart-1581971691-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  toppings: |-
    - "Mushrooms"
    - "Cheese"
    - "Pappers"
    - "Onions"
```

## variables

template内で変数を代入する


```mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- $relname := .Release.Name -}}
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea"  | quote }}
  food: {{ .food | upper | quote }}
  release: {{ $relname }}
  {{- end }}
```

### ドライラン

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