0

So i was going through a code of some one else and just could not make sense of it,, if may be some can of this,

"${replace(jsonencode(list(local.cadvisor_container_definition)), "/\"(\\d{2,5}|true|false)\"/", "$1")}"

locals {
  cadvisor_container_definition = {
    name         = "${local.cadvisor_container}"
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = "8080",
        hostPort = "8080"
      }
    ]
    mountPoints = [
      {
        sourceVolume  = "root",
        containerPath = "/rootfs",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_run",
        containerPath = "/var/run",
        readOnly      = "false"
      },
      {
        sourceVolume  = "sys",
        containerPath = "/sys",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_lib_docker",
        containerPath = "/var/lib/docker",
        readOnly      = "true"
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup",
        readOnly      = "true"
      }
    ]
    memory            = "${lookup(var.cadvisor_memory, var.env)}"
    memoryReservation = "${lookup(var.cadvisor_memory_reservation, var.env)}"
  }
}```
1

It seems like the intent of this expression is to take the result of encoding that data structure which contains numbers and boolean values in quotes and produce a new version with those values unquoted.

For example, anywhere the result contains the sequence "true" it would be replaced with true, quotes removed to produce a real JSON boolean value rather than a string.

A more direct way to achieve that result would be to set the values in cadvisor_container_definition to be of the intended type immediately:

locals {
  cadvisor_container_definition = {
    name         = local.cadvisor_container
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = 8080
        hostPort      = 8080
      },
    ]
    mountPoints = [
      {
        sourceVolume  = "root"
        containerPath = "/rootfs"
        readOnly      = true
      },
      {
        sourceVolume  = "var_run"
        containerPath = "/var/run"
        readOnly      = false
      },
      {
        sourceVolume  = "sys"
        containerPath = "/sys"
        readOnly      = true
      },
      {
        sourceVolume  = "var_lib_docker"
        containerPath = "/var/lib/docker"
        readOnly      = true
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup"
        readOnly      = true
      }
    ]
    memory            = var.cadvisor_memory[var.env]
    memoryReservation = var.cadvisor_memory_reservation[var.env]
  }
}

I changed the literal values in the above to their numeric or boolean equivalents. I can't see from what you shared whether memory and memoryReservation are also supposed to be numbers, but if they are then that can be arranged by writing tonumber(var.cadvisor_memory[var.env]) and tonumber(var.cadvisor_memory_reservation[var.env]).

The above examples use Terraform 0.12 syntax. It's possible that the configuration you are looking at here is written for Terraform 0.11 instead, which would explain this unusual string substitution technique because the Terraform 0.11 version of jsonencode suffered from Terraform 0.11's tendency to convert all primitive-typed values to strings. If you are still using Terraform 0.11 then the strange expression you found is likely still needed, but if you are using Terraform 0.12 then a change like I described above should work to simplify the confusing expression to just a straightforward jsonencode:

jsonencode([local.cadvisor_container_definition])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.