mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-03-25 02:41:03 +01:00
fix: Ensure the correct service CIDR and IP family is used in the rendered user data (#2963)
* fix: Ensuring the correct service CIDR and IP family is used in the rendered user data * chore: Updates from testing and validating * chore: Fix example destroy instructions * fix: Only require `cluster_service_cidr` when `create = true` * chore: Clean up commented out code and add note on check length
This commit is contained in:
@@ -27,6 +27,7 @@ module "user_data" {
|
||||
cluster_name = var.cluster_name
|
||||
cluster_endpoint = var.cluster_endpoint
|
||||
cluster_auth_base64 = var.cluster_auth_base64
|
||||
cluster_ip_family = var.cluster_ip_family
|
||||
cluster_service_cidr = var.cluster_service_cidr
|
||||
|
||||
enable_bootstrap_user_data = true
|
||||
@@ -41,12 +42,14 @@ module "user_data" {
|
||||
################################################################################
|
||||
|
||||
data "aws_ec2_instance_type" "this" {
|
||||
count = var.create && var.enable_efa_support && local.instance_type_provided ? 1 : 0
|
||||
count = local.enable_efa_support ? 1 : 0
|
||||
|
||||
instance_type = var.instance_type
|
||||
}
|
||||
|
||||
locals {
|
||||
enable_efa_support = var.create && var.enable_efa_support && local.instance_type_provided
|
||||
|
||||
instance_type_provided = var.instance_type != ""
|
||||
num_network_cards = try(data.aws_ec2_instance_type.this[0].maximum_network_cards, 0)
|
||||
|
||||
@@ -60,7 +63,7 @@ locals {
|
||||
}
|
||||
]
|
||||
|
||||
network_interfaces = var.enable_efa_support && local.instance_type_provided ? local.efa_network_interfaces : var.network_interfaces
|
||||
network_interfaces = local.enable_efa_support ? local.efa_network_interfaces : var.network_interfaces
|
||||
}
|
||||
|
||||
################################################################################
|
||||
@@ -71,7 +74,7 @@ locals {
|
||||
launch_template_name = coalesce(var.launch_template_name, "${var.name}-node-group")
|
||||
security_group_ids = compact(concat([var.cluster_primary_security_group_id], var.vpc_security_group_ids))
|
||||
|
||||
placement = var.create && var.enable_efa_support ? { group_name = aws_placement_group.this[0].name } : var.placement
|
||||
placement = local.enable_efa_support ? { group_name = aws_placement_group.this[0].name } : var.placement
|
||||
}
|
||||
|
||||
resource "aws_launch_template" "this" {
|
||||
@@ -695,7 +698,7 @@ resource "aws_autoscaling_group" "this" {
|
||||
|
||||
target_group_arns = var.target_group_arns
|
||||
termination_policies = var.termination_policies
|
||||
vpc_zone_identifier = var.enable_efa_support ? data.aws_subnets.efa[0].ids : var.subnet_ids
|
||||
vpc_zone_identifier = local.enable_efa_support ? data.aws_subnets.efa[0].ids : var.subnet_ids
|
||||
wait_for_capacity_timeout = var.wait_for_capacity_timeout
|
||||
wait_for_elb_capacity = var.wait_for_elb_capacity
|
||||
|
||||
@@ -734,13 +737,21 @@ resource "aws_autoscaling_group" "this" {
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
create_iam_instance_profile = var.create && var.create_iam_instance_profile
|
||||
|
||||
iam_role_name = coalesce(var.iam_role_name, "${var.name}-node-group")
|
||||
iam_role_policy_prefix = "arn:${data.aws_partition.current.partition}:iam::aws:policy"
|
||||
cni_policy = var.cluster_ip_family == "ipv6" ? "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/AmazonEKS_CNI_IPv6_Policy" : "${local.iam_role_policy_prefix}/AmazonEKS_CNI_Policy"
|
||||
|
||||
ipv4_cni_policy = { for k, v in {
|
||||
AmazonEKS_CNI_Policy = "${local.iam_role_policy_prefix}/AmazonEKS_CNI_Policy"
|
||||
} : k => v if var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv4" }
|
||||
ipv6_cni_policy = { for k, v in {
|
||||
AmazonEKS_CNI_IPv6_Policy = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/AmazonEKS_CNI_IPv6_Policy"
|
||||
} : k => v if var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv6" }
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "assume_role_policy" {
|
||||
count = var.create && var.create_iam_instance_profile ? 1 : 0
|
||||
count = local.create_iam_instance_profile ? 1 : 0
|
||||
|
||||
statement {
|
||||
sid = "EKSNodeAssumeRole"
|
||||
@@ -754,7 +765,7 @@ data "aws_iam_policy_document" "assume_role_policy" {
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "this" {
|
||||
count = var.create && var.create_iam_instance_profile ? 1 : 0
|
||||
count = local.create_iam_instance_profile ? 1 : 0
|
||||
|
||||
name = var.iam_role_use_name_prefix ? null : local.iam_role_name
|
||||
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
|
||||
@@ -770,26 +781,28 @@ resource "aws_iam_role" "this" {
|
||||
|
||||
# Policies attached ref https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group
|
||||
resource "aws_iam_role_policy_attachment" "this" {
|
||||
for_each = { for k, v in {
|
||||
AmazonEKSWorkerNodePolicy = "${local.iam_role_policy_prefix}/AmazonEKSWorkerNodePolicy"
|
||||
AmazonEC2ContainerRegistryReadOnly = "${local.iam_role_policy_prefix}/AmazonEC2ContainerRegistryReadOnly"
|
||||
AmazonEKS_CNI_IPv6_Policy = var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv6" ? local.cni_policy : ""
|
||||
AmazonEKS_CNI_Policy = var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv4" ? local.cni_policy : ""
|
||||
} : k => v if var.create && var.create_iam_instance_profile && v != "" }
|
||||
for_each = { for k, v in merge(
|
||||
{
|
||||
AmazonEKSWorkerNodePolicy = "${local.iam_role_policy_prefix}/AmazonEKSWorkerNodePolicy"
|
||||
AmazonEC2ContainerRegistryReadOnly = "${local.iam_role_policy_prefix}/AmazonEC2ContainerRegistryReadOnly"
|
||||
},
|
||||
local.ipv4_cni_policy,
|
||||
local.ipv6_cni_policy
|
||||
) : k => v if local.create_iam_instance_profile }
|
||||
|
||||
policy_arn = each.value
|
||||
role = aws_iam_role.this[0].name
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy_attachment" "additional" {
|
||||
for_each = { for k, v in var.iam_role_additional_policies : k => v if var.create && var.create_iam_instance_profile }
|
||||
for_each = { for k, v in var.iam_role_additional_policies : k => v if local.create_iam_instance_profile }
|
||||
|
||||
policy_arn = each.value
|
||||
role = aws_iam_role.this[0].name
|
||||
}
|
||||
|
||||
resource "aws_iam_instance_profile" "this" {
|
||||
count = var.create && var.create_iam_instance_profile ? 1 : 0
|
||||
count = local.create_iam_instance_profile ? 1 : 0
|
||||
|
||||
role = aws_iam_role.this[0].name
|
||||
|
||||
@@ -809,7 +822,7 @@ resource "aws_iam_instance_profile" "this" {
|
||||
################################################################################
|
||||
|
||||
resource "aws_placement_group" "this" {
|
||||
count = var.create && var.enable_efa_support ? 1 : 0
|
||||
count = local.enable_efa_support ? 1 : 0
|
||||
|
||||
name = "${var.cluster_name}-${var.name}"
|
||||
strategy = "cluster"
|
||||
@@ -828,7 +841,7 @@ resource "aws_placement_group" "this" {
|
||||
|
||||
# Find the availability zones supported by the instance type
|
||||
data "aws_ec2_instance_type_offerings" "this" {
|
||||
count = var.create && var.enable_efa_support ? 1 : 0
|
||||
count = local.enable_efa_support ? 1 : 0
|
||||
|
||||
filter {
|
||||
name = "instance-type"
|
||||
@@ -841,7 +854,7 @@ data "aws_ec2_instance_type_offerings" "this" {
|
||||
# Reverse the lookup to find one of the subnets provided based on the availability
|
||||
# availability zone ID of the queried instance type (supported)
|
||||
data "aws_subnets" "efa" {
|
||||
count = var.create && var.enable_efa_support ? 1 : 0
|
||||
count = local.enable_efa_support ? 1 : 0
|
||||
|
||||
filter {
|
||||
name = "subnet-id"
|
||||
|
||||
Reference in New Issue
Block a user