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 00000000..bdb31ab5 Binary files /dev/null and b/content/posts/dry-with-helm-library-chart/examples/cat/charts/animal-0.1.0.tgz differ 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 00000000..cb523428 Binary files /dev/null and b/content/posts/dry-with-helm-library-chart/examples/dog/charts/animal-0.1.0.tgz differ 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