mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-05-02 21:34:38 +02: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:
@@ -42,7 +42,7 @@ module "self_managed_node_group" {
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3 |
|
||||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.2 |
|
||||
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.40 |
|
||||
|
||||
## Providers
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -38,13 +38,18 @@ variable "cluster_auth_base64" {
|
||||
default = ""
|
||||
}
|
||||
|
||||
# Currently only used by AL2023 since it can be IPv4 or IPv6
|
||||
variable "cluster_service_cidr" {
|
||||
description = "The CIDR block (IPv4 or IPv6) used by the cluster to assign Kubernetes service IP addresses. This is derived from the cluster itself"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "cluster_ip_family" {
|
||||
description = "The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6`"
|
||||
type = string
|
||||
default = "ipv4"
|
||||
}
|
||||
|
||||
variable "pre_bootstrap_user_data" {
|
||||
description = "User data that is injected into the user data script ahead of the EKS bootstrap script. Not used when `platform` = `bottlerocket`"
|
||||
type = string
|
||||
@@ -546,12 +551,6 @@ variable "create_iam_instance_profile" {
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "cluster_ip_family" {
|
||||
description = "The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6`"
|
||||
type = string
|
||||
default = "ipv4"
|
||||
}
|
||||
|
||||
variable "iam_instance_profile_arn" {
|
||||
description = "Amazon Resource Name (ARN) of an existing IAM instance profile that provides permissions for the node group. Required if `create_iam_instance_profile` = `false`"
|
||||
type = string
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
terraform {
|
||||
required_version = ">= 1.3"
|
||||
required_version = ">= 1.3.2"
|
||||
|
||||
required_providers {
|
||||
aws = {
|
||||
|
||||
Reference in New Issue
Block a user