1

I'm quite new to terraform so my problem might be naive but still, I do not know how to solve it.
I have a terraform script that uses helm_release (https://www.terraform.io/docs/providers/helm/r/release.html) problem is that I have deployment configuration in a separate git repository. Let's say: https://project.git/configuration.
What I would like to achieve is to be able to add all files from e.g. https://project.git/configuration/dev (for dev env deployment) to config map.
The structure of terraform module is:

+project
+-helm
+--templates
+---configmap.yaml
+---deployment.yaml
+---ingress.yaml
+---service.yaml
+--chart.yaml
+--values.yaml
+-helm.tf
+-other tf files

I need all files from the configuration repository to be placed under

+project
+-helm
+--configuration

As only then i'm able to use this:

apiVersion: v1
kind: ConfigMap
metadata:
...
data:
{{- (.Files.Glob "configuration/*").AsConfig | nindent 2 }}

in my configmap.yaml, helm requires those files to be placed in chart directory.

1
  • I'm also keen of making https: // project.git/configuration a terraform module. So I can use it as a submodule. But the problem remains the same, how to make those files available in project/helm/configuration Commented Jun 3, 2020 at 13:45

2 Answers 2

1

You can only use the Helm .Files mechanism for files that are actually bundled with the chart proper, not external configuration you're trying to inject.

If you're planning to use Terraform to manage everything, one option is to have Terraform create a kubernetes_config_map that has the file content you need, and pass a reference to that ${kubernetes_config_map.with_the_files.name} to the Helm chart.

If you want to principally use Helm, but pass the files in externally, you can pass those as values. If you have Helm values.yaml syntax:

configFiles:
  test.yaml: |
    key: value
    key2: value2

You can emit that into a ConfigMap. You could iterate over the configFiles, or use the lightly-documented Helm toYaml extension to just render the whole thing.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "mychart.name" . }}
data:
{{ .Values.configFiles | toYaml | indent 2 }}

Once you've done that, you can pass these in via helm install --set options, or the equivalent Terraform helm_release set_string setting:

resource "helm_release" "chart" {
  name = "chart"
  chart = "..."
  set_string {
    name = "configFiles.test\\.yaml"
    value = "${file("${path.module}/test.yaml")}"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank a lot. This looks great. I overengineered my solution a bit but your approach looks easier and I will try it for sure. What I currently did is that I made configuration repository a minimal terraform module (takes the environment name as input and returns the full path to deployment configuration directory e.g. ${path.module}/dev) In the main terraform, I'm just copying all the files from there using local exec (in null_resource) to the directory where the helm is and call helm_release. Works, but IMHO just smells bad.
1

After many tries... I came with the simplest solution there is. I used .gitmodules (https://git-scm.com/book/en/v2/Git-Tools-Submodules). During projects git clone, the configuration is available as a submodule in ./project/helm/configuration. No terraform tricks required. The only thing that had to be done, and is a bit implicit is that specific git setting had to be set to force submodule download during clone (by default directory is empty).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.