From 3a19b4a64f352971a60566323bf4aebcd0528e04 Mon Sep 17 00:00:00 2001 From: chivy1204 Date: Thu, 25 Aug 2022 16:40:25 +0700 Subject: [PATCH] Add dry with library chart blog --- .hugo_build.lock | 0 .../examples/animal/.helmignore | 23 ++++ .../examples/animal/Chart.yaml | 6 + .../examples/animal/templates/_configmap.yaml | 10 ++ .../examples/animal/templates/_util.yaml | 6 + .../examples/cat/.helmignore | 23 ++++ .../examples/cat/Chart.lock | 6 + .../examples/cat/Chart.yaml | 10 ++ .../examples/cat/charts/animal-0.1.0.tgz | Bin 0 -> 776 bytes .../examples/cat/templates/configmap.yaml | 5 + .../examples/dog/.helmignore | 23 ++++ .../examples/dog/Chart.lock | 6 + .../examples/dog/Chart.yaml | 10 ++ .../examples/dog/charts/animal-0.1.0.tgz | Bin 0 -> 776 bytes .../examples/dog/templates/configmap.yaml | 5 + .../dry-with-helm-library-chart/index.md | 120 ++++++++++++++++++ data/authors/vync.json | 4 + 17 files changed, 257 insertions(+) create mode 100644 .hugo_build.lock create mode 100644 content/posts/dry-with-helm-library-chart/examples/animal/.helmignore create mode 100644 content/posts/dry-with-helm-library-chart/examples/animal/Chart.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/animal/templates/_configmap.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/animal/templates/_util.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/cat/.helmignore create mode 100644 content/posts/dry-with-helm-library-chart/examples/cat/Chart.lock create mode 100644 content/posts/dry-with-helm-library-chart/examples/cat/Chart.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/cat/charts/animal-0.1.0.tgz create mode 100644 content/posts/dry-with-helm-library-chart/examples/cat/templates/configmap.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/dog/.helmignore create mode 100644 content/posts/dry-with-helm-library-chart/examples/dog/Chart.lock create mode 100644 content/posts/dry-with-helm-library-chart/examples/dog/Chart.yaml create mode 100644 content/posts/dry-with-helm-library-chart/examples/dog/charts/animal-0.1.0.tgz create mode 100644 content/posts/dry-with-helm-library-chart/examples/dog/templates/configmap.yaml create mode 100644 content/posts/dry-with-helm-library-chart/index.md create mode 100644 data/authors/vync.json diff --git a/.hugo_build.lock b/.hugo_build.lock new file mode 100644 index 00000000..e69de29b diff --git a/content/posts/dry-with-helm-library-chart/examples/animal/.helmignore b/content/posts/dry-with-helm-library-chart/examples/animal/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/animal/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/content/posts/dry-with-helm-library-chart/examples/animal/Chart.yaml b/content/posts/dry-with-helm-library-chart/examples/animal/Chart.yaml new file mode 100644 index 00000000..d401443f --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/animal/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: animal +description: A Helm chart for Kubernetes +type: library +version: 0.1.0 +appVersion: "1.16.0" diff --git a/content/posts/dry-with-helm-library-chart/examples/animal/templates/_configmap.yaml b/content/posts/dry-with-helm-library-chart/examples/animal/templates/_configmap.yaml new file mode 100644 index 00000000..eb6b9f74 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/animal/templates/_configmap.yaml @@ -0,0 +1,10 @@ +{{- define "animal.configmap.tpl" -}} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name | printf "%s-%s" .Chart.Name }} +data: {} +{{- end -}} +{{- define "animal.configmap" -}} +{{- include "animal.util.merge" (append . "animal.configmap.tpl") -}} +{{- end -}} \ No newline at end of file diff --git a/content/posts/dry-with-helm-library-chart/examples/animal/templates/_util.yaml b/content/posts/dry-with-helm-library-chart/examples/animal/templates/_util.yaml new file mode 100644 index 00000000..50108d32 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/animal/templates/_util.yaml @@ -0,0 +1,6 @@ +{{- define "animal.util.merge" -}} +{{- $top := first . -}} +{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}} +{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}} +{{- toYaml (merge $overrides $tpl) -}} +{{- end -}} \ No newline at end of file diff --git a/content/posts/dry-with-helm-library-chart/examples/cat/.helmignore b/content/posts/dry-with-helm-library-chart/examples/cat/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/cat/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/content/posts/dry-with-helm-library-chart/examples/cat/Chart.lock b/content/posts/dry-with-helm-library-chart/examples/cat/Chart.lock new file mode 100644 index 00000000..613cfae7 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/cat/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: animal + repository: file://../animal + version: 0.1.0 +digest: sha256:e527733b67bfe8dc2bc6f0f1275a09b35febb0f6404271c4853c724b6f970a5e +generated: "2022-08-25T14:46:53.22254756+07:00" diff --git a/content/posts/dry-with-helm-library-chart/examples/cat/Chart.yaml b/content/posts/dry-with-helm-library-chart/examples/cat/Chart.yaml new file mode 100644 index 00000000..eeabc354 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/cat/Chart.yaml @@ -0,0 +1,10 @@ +apiVersion: v2 +name: cat +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 +appVersion: "1.16.0" +dependencies: +- name: animal + version: 0.1.0 + repository: file://../animal diff --git a/content/posts/dry-with-helm-library-chart/examples/cat/charts/animal-0.1.0.tgz b/content/posts/dry-with-helm-library-chart/examples/cat/charts/animal-0.1.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..bdb31ab5dbe0884433a31f5376ea46f4f9a6de35 GIT binary patch literal 776 zcmV+j1NZzNiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PL6FZ|gJ^$NTKR;?Q!Xc4CgxW}Q@t2egX>Li`XzlX$v1O->Wv z`Il^`yQ8=K?I_8*jTPtvr2~=ggVQ+n_Z+w9-J5JJqeti1{$ zUS35yzZj(1Or-Nnh|B3fq|zkd`vSfqr6F(11yH}#Udp-6!l)U<%Epjfq%h zxnQro=w6R{;6IXf3L}N&U2e@vmKxpBwu3|bXH)Tl|BGxs?fE~2Hk)%OXeAB7u&V|B zQ~=SbA)Ifw?036F%5J487I4*$`xYJ36tTdF3kJ~b0-Fu+A4w4g;@_br_z2ER6IU>N z6VBg+A@J@mr+v8HvUWObwya5rOwoq@tK^1{Bc#byT|5n`qf}gzFKGy4bgpsZf3mvC zqsv3g*Zw_Dt$qI6ukWaD0*CmYU0%N6Kf4f@J^!cBA0YOepB){aM(belIjp1)5%^QT zU6c2+pwK?`R{xAz!Fb2ySek-90EcwadQ9MB6Ao(?VO+>O!sJ;Drw+JG54%L$&21O( zJio^Ne~=w9r)m+*M>?SxZ$& zQ-Z_%7E218jo_zB1_-rt*2e&$BBh{Ib_p6|UP)7a2A&i~xh8NJt7rXa3N`{mB{oM1 z7&}iZ`GE?!ld*zNCLF#u>K?3VXErH;BM(X%;*7t&es>+MCpLmBtF<+7b9D`c^nvkG z#-we#^ci3N;*+-BbyQ{2G`ridHp!FG67$>I!AdF$?2Lyy$Ikc?Z`m1-y7_FqEZNzw zYy>yxrL6;e{r1bixbyZs<&kk&5GLJlZ{IV%4!JET>CZX6diDA@>$d;^0RR8C&_vz< G6aWC)_j&dJ literal 0 HcmV?d00001 diff --git a/content/posts/dry-with-helm-library-chart/examples/cat/templates/configmap.yaml b/content/posts/dry-with-helm-library-chart/examples/cat/templates/configmap.yaml new file mode 100644 index 00000000..2d74ad27 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/cat/templates/configmap.yaml @@ -0,0 +1,5 @@ +{{- include "animal.configmap" (list . "cat.configmap") -}} +{{- define "cat.configmap" -}} +data: + speak: "meow meow" +{{- end -}} \ No newline at end of file diff --git a/content/posts/dry-with-helm-library-chart/examples/dog/.helmignore b/content/posts/dry-with-helm-library-chart/examples/dog/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/dog/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/content/posts/dry-with-helm-library-chart/examples/dog/Chart.lock b/content/posts/dry-with-helm-library-chart/examples/dog/Chart.lock new file mode 100644 index 00000000..d125c1e5 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/dog/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: animal + repository: file://../animal + version: 0.1.0 +digest: sha256:e527733b67bfe8dc2bc6f0f1275a09b35febb0f6404271c4853c724b6f970a5e +generated: "2022-08-25T14:46:34.212337189+07:00" diff --git a/content/posts/dry-with-helm-library-chart/examples/dog/Chart.yaml b/content/posts/dry-with-helm-library-chart/examples/dog/Chart.yaml new file mode 100644 index 00000000..103c861e --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/dog/Chart.yaml @@ -0,0 +1,10 @@ +apiVersion: v2 +name: dog +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 +appVersion: "1.16.0" +dependencies: +- name: animal + version: 0.1.0 + repository: file://../animal \ No newline at end of file diff --git a/content/posts/dry-with-helm-library-chart/examples/dog/charts/animal-0.1.0.tgz b/content/posts/dry-with-helm-library-chart/examples/dog/charts/animal-0.1.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..cb523428736d893cedd56b96a69d9e8a027a3015 GIT binary patch literal 776 zcmV+j1NZzNiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PL4tkK;5HhI{Q_acEho9Wjp6X1Y=(F3>Cz2=O5Xjl|XJG&xPY z<1g7xXNPY2?I_7~W@v$4P`V)UUO0_ozvs9;j~kvi2&3 z*uRQ&elCr#1_0Ci9 zlneH;7v1YgkNii{PGO{wyv?my$x@>`*>-S@|7zQH47*z3 z&jk>j8p7pv%YL^@r0hxL^R?F0k1E|B)17ApRX{f)C)lG;syP zSK;zi7y|G9a@vR6Eo-O4X3Ls{$P{hZze;ZSG(wtO)y4CWI!eVg`I3e(M&}wg{wJ%O zJh?o^{Mf(8xpl~Y`}LjlP2d>+v)8Zp_|L9V(er-}{Q+Vx`PtF&X|xU&pTkP}5P?7U z+ckMF3kvO1Z}rcp6^wUGj-@H+J#a`Tt;YmDG~uvT5ypkgBTQbzaO#N5^tem3-Q0Eo zFY{~c|M$2b=#hV3k*3;=@9OM6LcJTf`&wBpPq7nRnF_Jd{qJ^#k;JzXQ%UY^J zni3r5cUV&3Yy>}5GC-)Evpxn06)6R!vP;ky^GcfXGw`G^$~A$*SiR^+Q?L;jDzQ0A zz}R_O$@f&iy^IxnGU4#OQ4e5EJF`g%9C=XE5NG_&&D)!3J+Tp7Tdl2u+v^)Bqz{aj zGA3=?rO){C7oW84uA?fGrrF(wwMm|hmYCnw4pvf8U>7{xJ9fdBc*iby)Xit>Wyvmn zWh1ynFKr#*>o;Ep#+|qCD36TGf-vcZd;5;@b;xZ&Nq^4i)vMRPS-%AU0RR6{>b{}? G6aWArGJ(bb literal 0 HcmV?d00001 diff --git a/content/posts/dry-with-helm-library-chart/examples/dog/templates/configmap.yaml b/content/posts/dry-with-helm-library-chart/examples/dog/templates/configmap.yaml new file mode 100644 index 00000000..20f04908 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/examples/dog/templates/configmap.yaml @@ -0,0 +1,5 @@ +{{- include "animal.configmap" (list . "dog.configmap") -}} +{{- define "dog.configmap" -}} +data: + speak: "bow wow" +{{- end -}} \ No newline at end of file diff --git a/content/posts/dry-with-helm-library-chart/index.md b/content/posts/dry-with-helm-library-chart/index.md new file mode 100644 index 00000000..eda89f32 --- /dev/null +++ b/content/posts/dry-with-helm-library-chart/index.md @@ -0,0 +1,120 @@ ++++ +date = "2022-08-25T10:00:00+07:00" +author = "vync" +description = "Using library chart to avoid repeating helm code" +title = "DRY with library chart" +categories = ["Helm", "Library Chart"] +tags = ["Helm", "Library Chart"] +slug = "dry-with-helm-library-chart" ++++ +***Helm Charts help us define, install, and upgrade even the most complex Kubernetes application. However, when we have many services, we can easily repeat the helm code. In this blog, we use library chart to avoid that.*** +#### **Why we need library chart?** +The more services we have, the more times helm code may be repeated. For examples, we have two services ***dog*** and ***cat*** and we need to define **ConfigMap** for them as below. + +*dog/templates/configmap.yaml* +```yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name | printf "%s-%s" .Chart.Name }} +data: + speak: "bow wow" +``` + +*cat/templates/configmap.yaml* +```yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name | printf "%s-%s" .Chart.Name }} +data: + speak: "meow meow" +``` +As we can see the *ConfigMap* of them have the same structure and **data** field is different only. So we need the method to create the common structure for their configs. +#### **How we create library chart?** +We create a common chart for ***dog*** and ***cat*** and name it as ***animal***. +```bash +helm create animal +``` +Go to *animal/Chart.yaml* and change type from *application* to *library*. +```yaml +apiVersion: v2 +name: animal +description: A Helm chart for Kubernetes +type: application => library +version: 0.1.0 +appVersion: "1.16.0" +``` +Now, we can carry out creating common *ConfigMap* for both ***dog*** and ***cat*** services by creating *animal/templates/_configmap.yaml*. By convention, the common files must be beginning with an underscore(_). In this file, we define **animal.configmap.tpl** as below. +```yaml +{{- define "animal.configmap.tpl" -}} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name | printf "%s-%s" .Chart.Name }} +data: {} +{{- end -}} +``` +After defining common *ConfigMap* in **animal library**, we need define a function to merge it with specific config in each service. So we continue to define **animal.util.merge** in *animal/templates/_util.yaml*. +```yaml +{{- define "animal.util.merge" -}} +{{- $top := first . -}} +{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}} +{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}} +{{- toYaml (merge $overrides $tpl) -}} +{{- end -}} +``` +Then we call the merge function in *animal/templates/_configmap.yaml* by adding +```yaml +{{- define "animal.configmap" -}} +{{- include "animal.util.merge" (append . "animal.configmap.tpl") -}} +{{- end -}} +``` +Therefore, *animal/templates/_configmap.yaml* file will become +```yaml +{{- define "animal.configmap.tpl" -}} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name | printf "%s-%s" .Chart.Name }} +data: {} +{{- end -}} +{{- define "animal.configmap" -}} +{{- include "animal.util.merge" (append . "animal.configmap.tpl") -}} +{{- end -}} +``` +From now, we have already completed the definition of the library **animal**. +#### **Using the library chart to specific chart** +First of all, when we want to use other things, we need import them. So in *dog/Chart.yaml* and *cat/Chart.yaml* must be added **animal dependency** as follow. +```yaml +dependencies: +- name: animal + version: 0.1.0 + repository: file://../animal +``` +After importing it, we need to run *helm dependency update* in each service. +```bash +helm dependency update dog/ +helm dependency update cat/ +``` +Then we can use the common *ConfigMap* in *dog/templates/configmap.yaml* and *cat/templates/configmap.yaml* as below. + +*dog/templates/configmap.yaml* +```yaml +{{- include "animal.configmap" (list . "dog.configmap") -}} +{{- define "dog.configmap" -}} +data: + speak: "bow wow" +{{- end -}} +``` +*cat/templates/configmap.yaml* +```yaml +{{- include "animal.configmap" (list . "cat.configmap") -}} +{{- define "cat.configmap" -}} +data: + speak: "meow meow" +{{- end -}} +``` +Finally, we use *helm install* to test them. +#### **Summary** +A library chart is a type of Helm chart that defines chart primitives or definitions which can be shared by Helm templates in other charts. This allows users to share snippets of code that can be re-used across charts, avoiding repetition and keeping charts DRY. Thank you for your reading. diff --git a/data/authors/vync.json b/data/authors/vync.json new file mode 100644 index 00000000..02a95bc4 --- /dev/null +++ b/data/authors/vync.json @@ -0,0 +1,4 @@ +{ + "name": "vync", + "bio": "Platform Engineer at Manabie" +} \ No newline at end of file