Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform variables as JSON object broken #38

Closed
haraldsk opened this issue Dec 19, 2022 · 2 comments · Fixed by #52
Closed

Terraform variables as JSON object broken #38

haraldsk opened this issue Dec 19, 2022 · 2 comments · Fixed by #52

Comments

@haraldsk
Copy link
Contributor

Setting the string JSON in spec.forProvider.varFiles[*].format does not work.

The provider does not properly interpret the string and writes the configuration file into the provider as /tf/<uuid>/crossplane-provider-terraform-0.tfvars when it should be named /tf/<uuid>/crossplane-provider-terraform-0.tfvars.json this causes
terraform to break, because it tries to ingest the JSON formatted variable file as Terraform HCL.

Steps to reproduce:

  1. Create a configmap with JSON data.
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-fancy-name
  namespace: default
data:
  provider.tfvars.json: |
    {
      "name": "my-fancy-thing"
    }
  1. Apply following to composition
    - name: terraform-inline-workspace
      base:
        apiVersion: tf.upbound.io/v1beta1
        kind: Workspace
        spec:
          forProvider:
            source: Remote
            module: git::https://github.com/myorg/my-repo
            varFiles:
              - source: ConfigMapKey
                configMapKeyRef:
                  name:  my-fancy-name
                  namespace: default
                  key: provider.tfvars.json
                format: JSON

After debugging the provider locally it's likely due to a faulty comparison of memory addresses instead of a regular string comparison. In the following code:

if vf.Format == &v1beta1.VarFileFormatJSON {

I have tested the patch below and it solved the issue for me:

@@ -395,10 +396,13 @@ func (c *external) options(ctx context.Context, p v1beta1.WorkspaceParameters) (

        for _, vf := range p.VarFiles {
                fmt := terraform.HCL
-               if vf.Format == &v1beta1.VarFileFormatJSON {
+               if *vf.Format == v1beta1.VarFileFormatJSON {
                        fmt = terraform.JSON
                }

I can submit this as a PR if needed.

@ytsarev
Copy link
Member

ytsarev commented Dec 19, 2022

@haraldsk great catch, thank you! We will happily review the PR :)

@linuxbsdfreak
Copy link

@haraldsk I have a requirement for providing an encrypted sops json file to a terraform input via a configmap. Would be nice to have it in the next version. So that i can test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants