From 54f862d5d058569c90b5185b3dab31c343241ef9 Mon Sep 17 00:00:00 2001 From: Xi Bai Date: Wed, 29 May 2024 11:54:43 +0100 Subject: [PATCH] enrich node group names with the cluster name --- cluster/eks.tf | 4 ++-- cluster/outputs.tf | 4 ++-- config/data.tf | 22 +++++++++++----------- config/karpenter.tf | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cluster/eks.tf b/cluster/eks.tf index a20eccb..c55849f 100644 --- a/cluster/eks.tf +++ b/cluster/eks.tf @@ -36,7 +36,7 @@ module "ebs_csi_irsa" { locals { dmz_node_group = { - dmz = { + "dmz-${var.eks_cluster_name}" = { desired_size = var.dmz_node_size["desired"] min_size = var.dmz_node_size["min"] max_size = var.dmz_node_size["max"] @@ -70,7 +70,7 @@ locals { } worker_node_group = { - worker = { + "worker-${var.eks_cluster_name}" = { desired_size = var.worker_node_size["desired"] min_size = var.worker_node_size["min"] max_size = var.worker_node_size["max"] diff --git a/cluster/outputs.tf b/cluster/outputs.tf index 51ca86c..2ce3e18 100644 --- a/cluster/outputs.tf +++ b/cluster/outputs.tf @@ -7,11 +7,11 @@ output "radar_base_eks_cluser_endpoint" { } output "radar_base_eks_dmz_node_group_name" { - value = var.create_dmz_node_group ? element(split(":", module.eks.eks_managed_node_groups.dmz.node_group_id), 1) : null + value = var.create_dmz_node_group ? element(split(":", module.eks.eks_managed_node_groups["dmz-${var.eks_cluster_name}"].node_group_id), 1) : null } output "radar_base_eks_worker_node_group_name" { - value = element(split(":", module.eks.eks_managed_node_groups.worker.node_group_id), 1) + value = element(split(":", module.eks.eks_managed_node_groups["worker-${var.eks_cluster_name}"].node_group_id), 1) } output "radar_base_vpc_public_subnets" { diff --git a/config/data.tf b/config/data.tf index a2db3fe..5a83ed8 100644 --- a/config/data.tf +++ b/config/data.tf @@ -49,19 +49,15 @@ data "aws_eks_cluster_auth" "main" { name = var.eks_cluster_name } -data "aws_autoscaling_groups" "main" { - filter { - name = "tag:eks:cluster-name" - values = [var.eks_cluster_name] - } +data "aws_eks_node_groups" "main" { + cluster_name = var.eks_cluster_name } -data "aws_eks_node_group" "worker" { - cluster_name = var.eks_cluster_name - node_group_name = join("-", [ - element(split("-", [for asg in data.aws_autoscaling_groups.main.names : asg if startswith(asg, "eks-worker-")][0]), 1), - element(split("-", [for asg in data.aws_autoscaling_groups.main.names : asg if startswith(asg, "eks-worker-")][0]), 2), - ]) # This is really hacky and there's gonna be a better way of extracting this. +data "aws_eks_node_group" "main" { + for_each = data.aws_eks_node_groups.main.names + + cluster_name = var.eks_cluster_name + node_group_name = each.value } locals { @@ -83,4 +79,8 @@ locals { "s3", ] + worker_node_group = [ + for name in keys(data.aws_eks_node_group.main) : data.aws_eks_node_group.main[name] if startswith(name, "worker-${var.eks_cluster_name}-") + ][0] # There is only one worker node group so be this + } diff --git a/config/karpenter.tf b/config/karpenter.tf index 0708926..305076b 100644 --- a/config/karpenter.tf +++ b/config/karpenter.tf @@ -10,7 +10,7 @@ module "karpenter" { irsa_namespace_service_accounts = ["karpenter:karpenter"] create_iam_role = false - iam_role_arn = data.aws_eks_node_group.worker.node_role_arn + iam_role_arn = local.worker_node_group.node_role_arn tags = merge(tomap({ "Name" : "${var.eks_cluster_name}-karpenter" }), var.common_tags) }