Project: Kapitan (Provisioning / Automation & Configuration)
File: 2023-08-27.md

author: Alessandro De Maria authorghuser: ademariag read_time: 10m date: 2023-08-27 We have worked hard to bring out a brand new way of experience Kapitan, through something that we call Although the concept is something we've introduced in 2020 with our blog post , the sheer amount of new capabilities (and frankly, the embarassing lack of documentation and examples) forces me to show you the new capabilities using a practicle example: deploying . We are going to deploy Keda using the helm chart approach. While Kapitan supports a native way to deploy helm charts using the helm input type, we are going instead to use a generator based approach using the "`charts`" generator. This tutorial will show you how to configure kapitan to: download a helm chart compile a helm chart modify a helm chart using mutations The content of this tutorial is already available on the <!-- more --> ```yaml parameters: keda: params: application_version: 2.11.2 serviceaccountname: keda-operator chart_name: keda chart_version: 2.11.2 chartdir: system/sources/charts/${keda:params:chartname}/${keda:params:chartname}/${keda:params:chartversion}/${keda:params:application_version} namespace: keda helm_values: {} ... ``` !!! tip "Override Helm Values" As an example we could be passing to helm an override to the default `values` parameters to make the operator deploy 2 replicas. ```yaml helm_values: operator: replicaCount: 2 ``` Kapitan supports downloading dependencies, including helm charts. When Kapitan is run with the `--fetch`, it will download the dependency if not already present. Use `--force-fetch` if you want to download it every time. Learn more about ```yaml ... kapitan: dependencies: type: helm outputpath: ${keda:params:chartdir} source: https://kedacore.github.io/charts version: ${keda:params:chart_version} chartname: ${keda:params:chartname} ... ``` !!! tip "Parameter interpolation" Notice how we are using parameter interpolation from the previously defined `keda.params` section. This will make it easier in the future to override some aspects of the configuration on a per-target base. ```yaml ... charts: keda: chartdir: ${keda:params:chartdir} helm_params: namespace: ${keda:params:namespace} name: ${keda:params:chart_name} helmvalues: ${keda:params:helmvalues} ``` Before we can see any effect, we need to attach the class to a target. We will create a simple target which looks like ```yaml classes: common components.keda ``` Now when we run `kapitan compile` we will see the chart being donwloaded and the manifests being produced. ```shell ./kapitan compile -t keda --fetch Dependency keda: saved to system/sources/charts/keda/keda/2.11.2/2.11.2 Rendered inventory (1.87s) Compiled keda (2.09s) ``` !!! note "`kapitan compile` breakdown" `--fetch` tells kapitan to fetch the chart if it is not found locally `-t keda` tells kapitan to compile only the previously defined

target ```shell ls -l compiled/keda/manifests/ total 660 -rw-r--r-- 1 ademaria root 659081 Aug 29 10:25 keda-bundle.yml -rw-r--r-- 1 ademaria root 79 Aug 29 10:25 keda-namespace.yml -rw-r--r-- 1 ademaria root 7092 Aug 29 10:25 keda-rbac.yml -rw-r--r-- 1 ademaria root 1783 Aug 29 10:25 keda-service.yml ``` Now let's do a couple of things that would not be easy to do with helm natively. You can already notice that the content of the chart is being splitted into multiple files: this is because the Generator is configured to separate different resources types into different files for convenience and consistency. The mechanism behing it is the "Mutation" of type "bundle" which tells Kapitan which file to save a resource into. Here are some example "mutation" which separates different `kinds` into different files ```yaml mutations: bundle: conditions: kind: [Ingress] filename: '{content.component_name}-ingress' ... conditions: kind: [HorizontalPodAutoscaler, PodDisruptionBudget, VerticalPodAutoscaler] filename: '{content.component_name}-scaling' conditions: kind: ['*'] filename: '{content.component_name}-bundle' ``` !!! tip "Catch-all rule" Notice the catchall rule at the end that puts everything that has not matched into the `bundle.yml` file Currently most of the keda related resources are bundled into the `-bundle.yml` file Instead, we want to separate them into their own file. Let's add this configuration: ```yaml charts: keda: chartdir: ${keda:params:chartdir} ... mutations: bundle: conditions: kind: [CustomResourceDefinition] filename: '{content.component_name}-crds' ``` Upon compile, you can now see that the CRD are being moved to a different file: ```shell ls -l compiled/keda/manifests/ total 664 -rw-r--r-- 1 ademaria root 11405 Aug 29 10:56 keda-bundle.yml -rw-r--r-- 1 ademaria root 647672 Aug 29 10:56 keda-crds.yml -rw-r--r-- 1 ademaria root 79 Aug 29 10:56 keda-namespace.yml -rw-r--r-- 1 ademaria root 7092 Aug 29 10:56 keda-rbac.yml -rw-r--r-- 1 ademaria root 1783 Aug 29 10:56 keda-service.yml ``` As we are using Argo, we want to pass a special `argocd.argoproj.io/sync-options` annotation to the CRD only so that ArgoCD can handle them properly. For this we are going to use the `patch` mutation: ```yaml ... mutations: ... patch: conditions: kind: [CustomResourceDefinition] patch: metadata: annotations: argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true,Replace=true ``` Upon compile, you can now see that the CRDs have been modified as required: ```shell diff --git a/compiled/keda/manifests/keda-crds.yml b/compiled/keda/manifests/keda-crds.yml index 2662bf3..9306c3a 100644 a/compiled/keda/manifests/keda-crds.yml +++ b/compiled/keda/manifests/keda-crds.yml @@ -2,6 +2,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true,Replace=true controller-gen.kubebuilder.io/version: v0.12.0 ``` With this tutorial have explored some capabilities of Kapitan to manage and perform changes to helm charts. Next tutorial will show how to make use of Keda and deploy a generator for Keda resources