Search code examples
terraformkubernetes-helmory

How to add container with values to helm release


Using the helm_release provider, I want to attach a sidecar container to a helm release. Specifically, I want to attach the sqlproxy container to the keto release. Below is my initial attempt but it does not pass values for sqlproxy config. How to add a container with values to a helm release?

locals {
  db_username = "postgres"
  sql_port = "5432"
}

resource "helm_release" "keto" {
  name       = "ory"
  repository = "https://k8s.ory.sh/helm/charts"
  chart      = "keto"

  values = [
    <<EOT
    serviceAccount:
      create: false
      name: ${module.service_account.value.id}
    job:
      serviceAccount:
        create: false
        name: ${module.service_account.value.id}
    keto:
      config:
        dsn: postgres://${local.db_username}:${random_password.password.result}@pg-sqlproxy-gcloud-sqlproxy:${local.sql_port}/db
    deploy:
      extraContainers:
        name: gcloud-sqlproxy
        image: # what to place here? how to add extra values here?
    EOT
  ]
}

The documentation for keto showed it offers a deployment:extraContainers field where I can set the name and the image for a container; however, unsure how to pass the additional values needed for sqlproxy


Solution

  • How any particular settings are used are fairly chart-specific, and there's not a generic recipe for things like adding sidecar containers.

    In this particular chart, the extraContainers setting is used as

          containers:
            - name: {{ .Chart.Name }}
              # ... about another 100 lines of settings ...
            {{- if $extraContainers }}
              {{- tpl $extraContainers . | nindent 8 }}
            {{- end }}
    

    That is, if extraContainers is set, it must be a string; it is run through the Helm tpl extension function to render Go text/template markup using the top-level Helm object; and the result is indented to the correct level in the Deployment spec.

    In the YAML you show, you need to end the extraContainers: line with a |, creating a YAML block scalar (a multi-line string). Under that you need to put a complete YAML list of container specs. You can include template blocks there if you want to.

    deploy:
      extraContainers: |
        - name: gcloud-sqlproxy
          image: gcr.io/cloudsql-docker/gce-proxy
          env:
            - name: KETO_CONFIG_DSN
              value: {{ .Values.keto.config.dsn }}
    

    Note that you've provided a couple of links to a separate Helm chart for the proxy. That intrinsically will create a separate Helm release and objects like Kubernetes Deployments for it; it cannot attach it as a sidecar to other Deployments, and you can't use any of that chart's settings in a sidecar-container configuration.