terraform-provider-helm icon indicating copy to clipboard operation
terraform-provider-helm copied to clipboard

Unable to use json-encoded string value in set block

Open thomas-riccardi opened this issue 3 years ago • 2 comments

Terraform, Provider, Kubernetes and Helm Versions

Terraform version: v1.0.9
Provider version: v2.3.0
Kubernetes version: v1.19.13

Affected Resource(s)

  • helm_release

Terraform Configuration Files

resource "helm_release" "rabbitmq" {
  name    = "rabbitmq"
  chart   = "bitnami/rabbitmq"
  version = "7.5.6"
  wait    = false

  # OK
  set {
    name  = "podAnnotations.mystandardannotation"
    value = "foo"
  }

  # KO: Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: ValidationError(StatefulSet.spec.template.metadata.annotations.myjsonannotation_jsonencode): invalid type for io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.annotations: got "array", expected "string"
  set {
    name  = "podAnnotations.myjsonannotation_jsonencode"
    type  = "string"
    value = jsonencode({ foo = "bar" })
  }

  # KO: same as with jsonencode; the plan even reconstructs jsonencode
  # set {
  #   name  = "podAnnotations.myjsonannotation_manual_string"
  #   value = "{\"foo\": \"bar\"}"
  #   type  = "string"
  # }

  # KO: Error: failed parsing key "podAnnotations.mypartiallyjsonannotation_manual_string" with value {"foo": "bar"} with more than json, key " with more than json" has no value
  # set {
  #   name  = "podAnnotations.mypartiallyjsonannotation_manual_string"
  #   value = "{\"foo\": \"bar\"} with more than json"
  #   type  = "string"
  # }
}

Steps to Reproduce

  1. terraform apply

Expected Behavior

I expect to get a working apply, with a string containing a json-encoded data as the value of a (helm, then k8s) annotation.

Actual Behavior

3 scenarios (comment/uncomment the set accordingly):

  • jsonencoded() in value fails, even with type = "string": Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: ValidationError(StatefulSet.spec.template.metadata.annotations.myjsonannotation_jsonencode): invalid type for io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.annotations: got "array", expected "string"
  • manual string containing json-encoded data: same as with jsonencoded(); worse: terraform seems to have detected the json-encoded during plan:
        + set {
            + name  = "podAnnotations.myjsonannotation_manual_string"
            + type  = "string"
            + value = jsonencode(
                  {
                    + foo = "bar"
                  }
              )
          }
    
  • even worse: a string that contains json bits, and other bits: here starts with json, ends with something else (I didn't test more cases): Error: failed parsing key "podAnnotations.mypartiallyjsonannotation_manual_string" with value {"foo": "bar"} with more than json, key " with more than json" has no value

There is something fishy going on with set blocks... I would expect that terraform takes the string value as-is, and nothing more.

Important Factoids

References

  • same issue as #669, closed too early...

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

thomas-riccardi avatar Nov 04 '21 16:11 thomas-riccardi

Workaround

Passing via values seems to work, but we need an extra yamlencode():

  values = [
    yamlencode({
      podAnnotations = {
        myjsonannotation_from_values = jsonencode({ foo = "bar" })
      }
    }),
  ]

thomas-riccardi avatar Nov 04 '21 16:11 thomas-riccardi

Any update on this?

Cheers

nourspace avatar Sep 12 '22 17:09 nourspace

Thanks for bringing this up!

Facing this issue right now. My problem now is that I need to pass sensitive data into the chart which set_sensitive would allow me.

https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release#set_sensitive

Seems there is no workaround available for that.

dhohengassner avatar Dec 07 '22 15:12 dhohengassner

I have exactly the same issue

generalovmaksim avatar Dec 08 '22 12:12 generalovmaksim

I have the same issue when trying to install cilium helm chart. I was trying to configure the helm chart like this

extraConfig:
  aws-instance-limit-mapping: |
    {
     "c6in.4xlarge": "8,30,30"
    }

by using this terraform code

  locals {
    aws_instance_limit_mapping = {
      "c6in.4xlarge": "8,30,30"
    }
  }
 ...
  # This won't work
  set {
    name = "extraConfig.aws-instance-limit-mapping"
    value = jsonencode(local.aws_instance_limit_mapping)
  }

But I got the error

Error: error validating "": error validating data: ValidationError(ConfigMap.data.aws-instance-limit-mapping): invalid type for io.k8s.api.core.v1.ConfigMap.data: got "array", expected "string"

tobernguyen avatar Jan 19 '23 23:01 tobernguyen

Marking this issue as stale due to inactivity. If this issue receives no comments in the next 30 days it will automatically be closed. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. This helps our maintainers find and focus on the active issues. Maintainers may also remove the stale label at their discretion. Thank you!

github-actions[bot] avatar Jan 20 '24 00:01 github-actions[bot]