Skip to content

Commit

Permalink
manage eniconfig labels
Browse files Browse the repository at this point in the history
  • Loading branch information
nerahou committed Apr 16, 2024
1 parent a4f5a73 commit 7721c5d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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", {})
)

Expand Down
16 changes: 16 additions & 0 deletions node_group_advanced.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", {}),
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7721c5d

Please sign in to comment.