Using CDK8S, YTT or Gomplate with ArgoCD Through "Config Management Plugins"
Alireza Nazari
Posted on January 28, 2024
Problem
Are you looking to use other yaml templating tools with Argocd, like ytt, cdk8s or gomplate to template your yaml files? Or do you need to customize Helm command that is used in ArgoCD? Perhaps with different parameters? (e.g.: passing - post-renderer to helm command)
While Argocd offers out-of-the-box support for Helm charts and Kustomize manifests, if you require more functionality, you need to somehow extend Argocd functionalities.
Solution
Argocd offers a solution to this problem. And that is "Config Management Plugins". By writing plugins, you can install your own tool(s) and define your own command to build and template Kubernetes manifests. However, it's easier said than done.
So in this article, we are going to create our own plugin and use "gomplate" for rendering the manifests. Although you can of course use different tools.
Installation
I'm using helm to install Argocd. So most of the configurations will be in the helm values.
First I create the namespace in which I'd like to deploy my ArgoCD resources:
kubectl create namespace argocd
This namespace will used for ArgoCD resources and your application will be deployed in its own namespace.
configs:# -- Plugin yaml files to be added to argocd-cmp-cmcmp:create:trueplugins:gomplate:generate:command:[sh,-c]args:# here we define our custom command to render the yaml file-cat ./* | gomplate -d ip=https://ipinfo.iorepoServer:extraContainers:-name:gomplatecommand:-"/var/run/argocd/argocd-cmp-server"image:alpine:3securityContext:runAsNonRoot:truerunAsUser:999volumeMounts:-mountPath:/usr/binname:cmp-tmp-mountPath:/var/run/argocdname:var-files-mountPath:/home/argocd/cmp-server/pluginsname:plugins-mountPath:/home/argocd/cmp-server/config/plugin.yamlname:argocd-cmp-cmsubPath:gomplate.yamlinitContainers:-name:install-gomplateimage:alpine:3command:["sh","-c"]args:# here we install gomplate binary-apk add --no-cache gomplatevolumeMounts:-mountPath:/usr/binname:cmp-tmp# -- Additional volumes to the repo server podvolumes:# This is the config map all the CMPs are defined-name:argocd-cmp-cmconfigMap:name:argocd-cmp-cm-name:cmp-tmpemptyDir:{}
ArgoCD CRDs will be installed out of the box. If you would like to install them separately, you can pass --set crds.install=false to the installation command.
Perfect! Now we have ArgoCD with our Config Management Plugins called gomplate installed.
Usage
Now we can use this plugin in our Application/Applicationset manifest to render the yaml files. Here is an example:
apiVersion:argoproj.io/v1alpha1kind:Applicationmetadata:name:my-awesome-appnamespace:argocdspec:project:defaultsource:repoURL:https://github.com/ali-de7/argocd-cmp.gittargetRevision:mainpath:my-awesome-appplugin:name:gomplateenv:-name:CUSTOM_ENV_VARIABLE# argocd prepends the env vars with ARGOCD_ENV_value:bardestination:server:https://kubernetes.default.svcnamespace:my-awesome-app
We apply this manifest with the following command:
kubectl apply -f application.yaml
Now let's use Gomplate syntax in our awesome application and generate Kubernetes secrets through Gomplate datesets:
apiVersion:v1kind:Secretmetadata:name:my-secretnamespace:{{getenv "ARGOCD_APP_NAMESPACE" "default"}}type:Opaquedata:password:{{(ds "ip").country | base64.Encode}}foo:{{getenv "ARGOCD_ENV_CUSTOM_ENV_VARIABLE" | base64.Encode}}# argocd prepends the env vars with ARGOCD_ENV_
Super! Now we just need to sync our application in ArgoCD.
Final notes
You can find the example in the following Github repo: