From 7721c5dc55d00d47f02c53c0215457cc1d868b66 Mon Sep 17 00:00:00 2001 From: nerahou Date: Tue, 16 Apr 2024 09:41:10 +0200 Subject: [PATCH] manage eniconfig labels --- main.tf | 18 +++++++++++++++++- node_group_advanced.tf | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 576a1fe..fa515c8 100644 --- a/main.tf +++ b/main.tf @@ -25,7 +25,7 @@ locals { node_group_labels = [ for key, node_group in var.node_groups : { - for k, v in lookup(node_group, "labels", {}) : + for k, v in lookup(node_group, "labels", { "k8s.amazonaws.com/eniConfig" : data.aws_subnet.subnets[key].availability_zone }) : key => { "k8s.io/cluster-autoscaler/node-template/label/${k}" : v (k) : v @@ -65,6 +65,14 @@ data "aws_caller_identity" "current" {} # This datasource is used to get the region currently used by the AWS provider data "aws_region" "current" {} +data "aws_subnet" "subnets" { + for_each = var.node_groups + filter { + name = "subnet-id" + values = lookup(each.value, "subnet_ids", []) + } +} + # Cluster resource "aws_eks_cluster" "quortex" { name = var.cluster_name @@ -164,6 +172,10 @@ resource "aws_eks_node_group" "quortex" { "k8s.io/cluster-autoscaler/node-template/label/nodegroup" = each.key, # tag required for scaling to/from 0 } : {}, { "nodegroup" = each.key }, + length(lookup(each.value, "subnet_ids", [])) == 1 ? + { + "k8s.amazonaws.com/eniConfig" : data.aws_subnet.subnets[each.key].availability_zone + } : {}, lookup(each.value, "labels", {}), lookup(each.value, "tags", {}), var.tags @@ -173,6 +185,10 @@ resource "aws_eks_node_group" "quortex" { { "nodegroup" = each.key }, + length(lookup(each.value, "subnet_ids", [])) == 1 ? + { + "k8s.amazonaws.com/eniConfig" : data.aws_subnet.subnets[each.key].availability_zone + } : {}, lookup(each.value, "labels", {}) ) diff --git a/node_group_advanced.tf b/node_group_advanced.tf index bc2d466..a483d02 100644 --- a/node_group_advanced.tf +++ b/node_group_advanced.tf @@ -35,6 +35,14 @@ locals { } } +data "aws_subnet" "subnets_advanced" { + for_each = var.node_groups_advanced + filter { + name = "subnet-id" + values = lookup(each.value, "subnet_ids", []) + } +} + # Get preferred instance types for node_groups_advanced with instance_filter preferred data "aws_ec2_instance_type_offering" "preferred" { for_each = local.filtered_nodegroups_advanced_preferred @@ -115,6 +123,10 @@ resource "aws_launch_template" "quortex_launch_tpl" { "eks.amazonaws.com/nodegroup" = each.key, "nodegroup" = each.key }, + length(lookup(each.value, "subnet_ids", [])) == 1 ? + { + "k8s.amazonaws.com/eniConfig" : data.aws_subnet.subnets_advanced[each.key].availability_zone + } : {}, # User-specified labels lookup(each.value, "labels", {}), ) @@ -274,6 +286,10 @@ resource "aws_autoscaling_group" "quortex_asg_advanced" { }, # the following tags must be set on the ASG, and must match the k8s node labels/taints, for the autoscaler to be able to scale up from 0 { for k, v in lookup(each.value, "labels", {}) : "k8s.io/cluster-autoscaler/node-template/label/${k}" => v }, + length(lookup(each.value, "subnet_ids", [])) == 1 ? + { + "k8s.io/cluster-autoscaler/node-template/label/k8s.amazonaws.com/eniConfig" : data.aws_subnet.subnets_advanced[each.key].availability_zone + } : {}, { for k, v in lookup(each.value, "taints", {}) : "k8s.io/cluster-autoscaler/node-template/taint/${k}" => v }, ) : {} iterator = tag