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

inconsistent behaviour for resources that can have repeated blocks defined with HCL2 #1006

Open
crivetechie opened this issue Sep 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@crivetechie
Copy link

Hello, we are having some issues writing policies for resource that may contain repeated blocks.

It looks like the behaviour of conftest is to produce an object when there's a single block and an array if the block is repeated.

See the example below definining two kubernetes_deployment resources, the first using a single container block and the second using 2 containers blocks

resource "kubernetes_deployment" "example-single-block" {
  spec {
    template {
      spec {
        container {
          name = "one"
        }
      }
    }
  }
}

resource "kubernetes_deployment" "example-multiple-blocks" {
  spec {
    template {
      spec {
        container {
          name = "one"
        }
        container {
          name = "two"
        }
      }
    }
  }
}

the above is parsed as

"resource": {
    "kubernetes_deployment": {
      "example-multiple-blocks": {
        "spec": {
          "template": {
            "spec": {
              "container": [
                {
                  "name": "one"
                },
                {
                  "name": "two"
                }
              ]
            }
          }
        }
      },
      "example-single-block": {
        "spec": {
          "template": {
            "spec": {
              "container": {
                "name": "one"
              }
            }
          }
        }
      }
    }

as you can see spec.template.spec.container in example-multiple-blocks is an array, it is an object in example-single-block

Accoriding to #266 I was expecting this to be resolved but it isn't, looks like conftest is using version 0.3.1 of hcl2json lib which doesn't include the fix.
I am wondering if there was a decision to lock to [email protected] to avoid introducing breaking changes and if there's any plan for upgrading to latest version. Thank you!

@crivetechie crivetechie changed the title inconsistent behaviour for resources with repeated blocks defined with HCL2 inconsistent behaviour for resources that can have repeated blocks defined with HCL2 Sep 19, 2024
@jalseth
Copy link
Member

jalseth commented Sep 25, 2024

@crivetechie We don't lock dependencies, but we typically only bump them if there is a reported issue. Can you confirm if updating this dependency solves your problem?

@jalseth jalseth added the bug Something isn't working label Sep 25, 2024
@crivetechie
Copy link
Author

crivetechie commented Sep 27, 2024

@jalseth updating the dependency solves the problem, however it introducing a list of changes that will break existing policies.

I've opened a PR in my fork to demonstrate the impact of upgrading to latest version: https://github.com/crivetechie/conftest/pull/1/files

@jalseth
Copy link
Member

jalseth commented Oct 17, 2024

I think we should bump if the new version is more accurate according to the Terraform JSON syntax specification [1]. I haven't had time to review the spec in detail, but if you can confirm that the new output is aligned with the spec, feel free to open a PR to bump the dep.

[1] https://developer.hashicorp.com/terraform/language/syntax/json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants