如何管理kubernetes_config_map的数据而不发生冲突

inn6fuwd  于 2022-11-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(134)

最近在使用terraform-aws-eks模块时遇到问题,我们希望在该模块中创建aws-auth配置Map并设置mapRolesmapAccounts字段,但在其他地方管理mapUsers
kubernetes_config_map_v1_data似乎是为此量身定做的,但是我们一直遇到冲突问题,其中由kubernetes_config_map_v1_data创建的字段一直希望被kubernetes_config_map资源破坏。
我们找到了一个解决方案,并把答案放在下面给其他有这个问题的人。

4ktjp1zp

4ktjp1zp1#

terraform-aws-eks模块版本17
在地形模块X1 M1 N1 X内:

resource "kubernetes_config_map" "aws_auth" {
  count = var.create_eks && var.manage_aws_auth ? 1 : 0

  metadata {
    name      = "aws-auth"
    namespace = "kube-system"
    labels = merge(
      {
        "app.kubernetes.io/managed-by" = "Terraform"
        # / are replaced by . because label validator fails in this lib
        # https://github.com/kubernetes/apimachinery/blob/1bdd76d09076d4dc0362456e59c8f551f5f24a72/pkg/util/validation/validation.go#L166
        "terraform.io/module" = "terraform-aws-modules.eks.aws"
      },
      var.aws_auth_additional_labels
    )
  }

  lifecycle {
    ignore_changes = [
      data,
    ]
  }

  depends_on = [data.http.wait_for_cluster[0]]
}

resource "kubernetes_config_map_v1_data" "aws_auth" {
  count = var.create_eks && var.manage_aws_auth ? 1 : 0

  metadata {
    name      = "aws-auth"
    namespace = "kube-system"
  }

  data = {
    mapRoles = yamlencode(
      distinct(concat(
        local.configmap_roles,
        var.map_roles,
      ))
    )
    mapAccounts = yamlencode(var.map_accounts)
  }

  field_manager = "aws-eks-module"

  depends_on = [data.http.wait_for_cluster[0], kubernetes_config_map.aws_auth[0]]
}

从另一个回购:

resource "kubernetes_config_map_v1_data" "aws_auth_example" {
  metadata {
    name      = "aws-auth"
    namespace = "kube-system"
  }

  data = {
    mapUsers = yamlencode(var.users)
  }

  field_manager = "example"
}

相关问题