为 Azure 容器注册表 (ACR) 的专用端点创建动态专用 DNS 区域记录

Creating dynamic private dns zone records for private endpoints of Azure Container Registry (ACR)

我正在努力为 Azure(ACR - Azure 容器注册表)中的同一资源的多个私有端点设置 动态 私有 dns 区域记录。

所以我目前已经设置了这个简单的例子。基本上,它模拟创建 ACR 并构建需要在 DNS 中注册的记录映射,以便可以正确使用 ACR。这里的问题是,需要以某种方式从每个 resource/object 的内部字段中提取值(这里是端点变量),然后正确地转换它,以便它匹配预期的输出。这是必需的,因为在我的例子中,它是模块的组成部分,需要根据创建的实际资源的值动态构建。

以下示例应该可以帮助任何想检查此问题并提供帮助的人。 要验证您是否得到正确的结果,只需键入“terraform refresh”并打印输出。

terraform {
  backend local {}
}

# simulates creation of multiple Azure Container Registry private endpoints
variable endpoints {
  type = map
  default = {
    "registry" = {
      custom_dns_configs = [
        {
          fqdn = "something.westeurope.data.azurecr.io"
          ip_addresses = ["1.1.1.1",]
        },
        {
          fqdn         = "something.azurecr.io"
          ip_addresses = ["1.1.1.2",]
        }
      ]
    },
    "registry2" = {
      custom_dns_configs = [
        {
          fqdn = "something.westeurope.data.azurecr.io"
          ip_addresses = ["1.1.2.1",]
        },
        {
          fqdn = "something.azurecr.io"
          ip_addresses = ["1.1.2.2",]
        },
      ]
    },
    #...
    # "registryN" = {...}
  }
}

# Question: How to write for block here to get out of it exactly what's in expected
#           result having in mind that there can be multiple of "registry" blocks?
#           Below line produce only single result which doesn't match expected result.
output result {
  value = {for pe in var.endpoints:
    pe.custom_dns_configs[0].fqdn => pe.custom_dns_configs[0].ip_addresses
  }
}

# Expected result:
# result = {
#   "something.westeurope.data.azurecr.io" = [
#     "1.1.1.1",
#     "1.1.2.1",
#   ]
#   "something.azurecr.io" = [
#     "1.1.1.2",
#     "1.1.2.2",
#   ]
# }

回答我自己的问题:

output result {
  value = {for k,v in { 
      for cdc in flatten(
        [for pe in var.endpoints: pe.custom_dns_configs]
      ): 
        cdc.fqdn => cdc.ip_addresses...
    }: 
      k => flatten(v)
  }
}

下面几句解释:

  • [] 和 {} - [] 生成一个元组,从传入映射中删除“注册表”键,而 {} 需要生成某种动态键
  • [for pe in var.endpoints: pe.custom_dns_configs] 只需从 var.environments 中为地图的每个元素提取内部字段。然后使用 flatten 只是为了使事情更简单,而无需深入研究不同级别的嵌套列表
  • 下一步是为 fqdn 构建新映射 -> list(ip addresses)
  • cdc.ip_addresses 后的“...”为必填项。这是允许按键对值进行分组的表示法。在这种情况下,我们至少有 2 倍相同的 fqdn,并且通过正常方式 terraform 会抱怨当键不唯一时它无法创建这样的映射。在那里添加那 3 个点可以启用此分组。
  • 然后最顶层的 for 块仅用于展平整个值输出。

现在的问题是我们是否仍想保留“registry”、“registry2”、“registryN”分组。为此,我还没有找到解决方案。