How do you use Garden variables with Kustomize?
# 🌱|help-and-getting-started
c
I have a Build action that provides Docker images to a Kubernetes Deployment, and I store the output of that Build action on the Deployment as a variable like
variables.SOME_IMAGE
so that the YAML files can substitute any instances of
${var.SOME_IMAGE}
and deploy with the right image. When I'm using Kustomize, however, it doesn't look like Kustomize is given access to Garden variables. So I can no longer use the
${var.SOME_IMAGE}
method I was using beforehand to get custom images into a Kustomize deployment. Is there a canonical way to access Garden variables when using Kustomize? Is this a bug or intended behavior?
q
Hi @clever-policeman-58407, to give me some idea, are you able to reproduce what your relevant
garden.yml
files look like for deploying your Kustomize manifests? Do you know of the
patchResources
field we expose for Kustomize? It's intended for cases like this. https://docs.garden.io/reference/module-types/kubernetes#patchresources You'd then reference the image https://docs.garden.io/other-plugins/container#referencing-from-other-actions https://docs.garden.io/reference/action-types/build/container#usd-actions.build.less-than-name-greater-than.outputs.deploymentimageid e.g.
${actions.build.<name>.outputs.deploymentImageId}
c
Ah, brilliant. This solves my usecase. For the sake of future searchers, this is an example of my usage with the completed solution:
Copy code
kind: Deploy
type: kubernetes
name: admin-api
variables:
  ADMIN_IMAGE: ${actions.build.admin-api.outputs.deployment-image-name}:${actions.build.admin-api.version}
dependencies:
  - build.admin-api
spec:
  kustomize:
    path: ./kubernetes/overlays/${environment.name}
  patchResources:
    - name: admin-api
      kind: Deployment
      patch:
        spec:
          template:
            spec:
              containers:
                - name: api-container
                  image: ${var.ADMIN_IMAGE}
              initContainers:
                - name: init-api
                  image: ${var.ADMIN_IMAGE}
f
hi @quaint-dress-831 Great stuff! I'm facing a similar issue with other variables while using
kustomize
. I create the variables in the environment declaration and when I refer to them they come empty
""
. Example:
${var.ABCD}
Are there any additional steps to be able to load variable values declared in the garden
Project
under
environment
->
variables
in kustomization.yaml and the other kubernetes manifests while using
kustomize
? Thanks a lot!
q
Hi @famous-fireman-34051 I'm no longer working at garden.io so I'll call on @freezing-pharmacist-34446 for visibility 🙂
f
thank you!
s
Hi @famous-fireman-34051! We currently don't support Garden templating in Kustomize files—the
patchResources
field is the recommended way to accomplish this in Garden, and can serve the same role as Kustomize. Copying your Kustomize overlays into
patchResources
should be relatively straightforward (see the code examples previously posted in this thread), and then you can take it from there—hope this helps!
f
hi @swift-garage-61180 THanks a lot for your reply.