Kustomize vars

Jeff Yen
Mar 26, 2021

更輕鬆在kustomize 使用vars

最一開始我們要釐清,在kustomize一定要先定義變數在某個yaml設定檔中(configmap, deployment, service, etc…),然後在另一個yaml再引用。

A var in kustomize is a reflection mechanism, allowing a value
defined in one YAML configuration field (e.g. an IP address) to
be copied to other locations in the YAML. It has a source spec
and any number of targets.

目前我要設定ingress的host為變數。

然後在kustomize.yaml生成一個configmap去準備變數VAR1。

field reference預設值是 metadata.name 也就是檔案名稱,如果下面案例中我沒有設定filedpath: data.VAR1 ingress.spec.rules.host就會變成my-configmap-xxxxxx

如果有設定就會變成我們在configmap設定好的

不過這只是只有單一變數的情況下,如果有複數變數的情況下,就要多設定kustomizeconfig.yaml定義多的field

假如沒有定義的情況如下

這裡可以看到VAR2是沒有被取代的

這裡我多準備一個檔案 var_references.yaml

然後在kustomization.yaml引用設定檔

雖然第一個變數不用引用也找得到,不過為了美觀和維護,強烈建議還是要寫在varReference中。

Ref:

--

--