用于重新部署的Terraform ConfigMap哈希

时间:2019-08-06 11:06:32

标签: kubernetes terraform terraform-provider-kubernetes

我正在使用带有Kubernetes Provider的Terraform。 现在,当创建 ConfigMap 时,我希望它们的名称具有内容后缀。 通常它是内容的哈希值。

这样,它应该在使用时强制实施部署。

所以我希望它的工作类似于:

resource "kubernetes_config_map" "prometheus_config" {
  metadata {
    name      = "prometheus-" + computeHash(file("${path.module}/alerts.yml"), file("${path.module}/recordings.yml"), "abcd")
  }

  data = {
    "foo" = file("${path.module}/alerts.yml")
    "bar" = file("${path.module}/recordings.yml")
    "txt" = "abcd"
  }
}

有什么方法可以实现自定义功能,例如computeHash? 还是通过另一种方式实现?

3 个答案:

答案 0 :(得分:1)

无法在Terraform中实现 custom 哈希函数,但是Terraform具有许多实现不同标准哈希函数的内置函数。

例如,要使用base64编码的SHA256哈希,您可以使用函数base64sha256编写如下内容:

  name = "prometheus-" + base64sha256(join("\n", [
    file("${path.module}/alerts.yml"),
    file("${path.module}/recordings.yml"),
    "abcd",
  ])

由于file函数返回一个字符串,因此所有引用的文件必须包含有效的UTF-8文本。然后,散列将采用文件中Unicode字符的UTF-8编码。

base64sha256的文档页面包括指向各种其他“哈希和加密函数”的导航链接,其中一些实现了其他哈希算法。

如果您的目标是将所有内容都包含在data映射中,则可以通过将其分解为Local Value并随后对映射的字符串表示(例如JSON)进行哈希处理来避免重复序列化:

locals {
  prometheus_config = {
    "foo" = file("${path.module}/alerts.yml")
    "bar" = file("${path.module}/recordings.yml")
    "txt" = "abcd"
  }
}

resource "kubernetes_config_map" "prometheus_config" {
  metadata {
    name = "prometheus-" + base64sha256(jsonencode(local.prometheus_config))
  }

  data = local.prometheus_config
}

答案 1 :(得分:1)

kubernetes_config_map resource返回一个resource_version attribute as part of the metadata。如链接文档中所述:

  

一个不透明的值,代表此配置映射的内部版本,客户端可以使用该值来确定何时更改了配置映射。有关更多信息,请参见Kubernetes reference

您可以通过直接基于kubernetes_deployment resource中的该值进行插值来触发部署。

我亲自将值放入容器规范中的环境变量中,然后在配置映射更改时触发部署重新部署。通过调整kubernetes_deployment文档中给出的示例,可以得出:

resource "kubernetes_deployment" "example" {
  metadata {
    name = "terraform-example"
    labels = {
      test = "MyExampleApp"
    }
  }

  spec {
    replicas = 3

    selector {
      match_labels = {
        test = "MyExampleApp"
      }
    }

    template {
      metadata {
        labels = {
          test = "MyExampleApp"
        }
      }

      spec {
        container {
          image = "nginx:1.7.8"
          name  = "example"

          env = [
            {
              name  = "configmap"
              value = "${kubernetes_config_map.example.metadata.0.resource_version}"
            },
          ]

          resources {
            limits {
              cpu    = "0.5"
              memory = "512Mi"
            }
            requests {
              cpu    = "250m"
              memory = "50Mi"
            }
          }

          liveness_probe {
            http_get {
              path = "/nginx_status"
              port = 80

              http_header {
                name  = "X-Custom-Header"
                value = "Awesome"
              }
            }

            initial_delay_seconds = 3
            period_seconds        = 3
          }
        }
      }
    }
  }
}

值得注意的是,此方法当前的不幸行为是需要2个apply来触发部署,因为Terraform仅在第一个apply中看到对配置映射的更改,但随后出现了plan上方将显示部署规范的容器env已更改,这将触发部署。我还没有足够深入地了解Kubernetes提供程序为何以这种方式工作,因为Terraform应该能够看到部署依赖于配置映射,并且这种情况将会改变。

答案 2 :(得分:0)

如果您的部署也位于Terraform中,则可以通过在部署的标签或环境中对configmap数据进行散列来轻松实现它:

arr.push(4) <-- arr is now [1,2,3,4], but its address in memory doesn't need to change, it's still just an array that references 1, 2, 3, and now 4

如果部署不在terraform之外,您也可以直接在部署对象内部进行部署,即:

env {
  name  = "prometheus_cfgmap_version"
  value = base64sha256(jsonencode(kubernetes_config_map.prometheus_config.data))
}
相关问题