Skip to content

Commit 708d941

Browse files
committed
fix: Correct DNS Server of kubelet service when using custom AMI (terraform-aws-modules#1717)
The PR (terraform-aws-modules#1580) is passing the "apiserver-endpoint" and "b64-cluster-ca", which causes the SERVICE_IPV4_CIDR empty (https://github.com/awslabs/amazon-eks-ami/blob/v20211206/files/bootstrap.sh#L366). Because of that, the script fallbacks always to 10.100.0.10 or 172.20.0.10. Defining the ipv4 cidr ensures that the bootstrap script configures the DNS server correctly on the kubelet service, allowing pods to resolve DNS names.
1 parent 8d33a46 commit 708d941

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

locals.tf

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ locals {
66
cluster_name = coalescelist(aws_eks_cluster.this[*].name, [""])[0]
77
cluster_endpoint = coalescelist(aws_eks_cluster.this[*].endpoint, [""])[0]
88
cluster_auth_base64 = coalescelist(aws_eks_cluster.this[*].certificate_authority[0].data, [""])[0]
9+
cluster_service_ipv4_cidr = coalescelist(aws_eks_cluster.this[*].kubernetes_network_config[0].service_ipv4_cidr, [""])[0]
910
cluster_oidc_issuer_url = flatten(concat(aws_eks_cluster.this[*].identity[*].oidc[0].issuer, [""]))[0]
1011
cluster_primary_security_group_id = coalescelist(aws_eks_cluster.this[*].vpc_config[0].cluster_security_group_id, [""])[0]
1112

modules/node_groups/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ No modules.
9494
| <a name="input_cluster_auth_base64"></a> [cluster\_auth\_base64](#input\_cluster\_auth\_base64) | Base64 encoded CA of parent cluster | `string` | `""` | no |
9595
| <a name="input_cluster_endpoint"></a> [cluster\_endpoint](#input\_cluster\_endpoint) | Endpoint of parent cluster | `string` | `""` | no |
9696
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of parent cluster | `string` | `""` | no |
97+
| <a name="input_cluster_service_ipv4_cidr"></a> [cluster\_service\_ipv4\_cidr](#input\_cluster\_service\_ipv4\_cidr) | service ipv4 cidr for the kubernetes cluster | `string` | `null` | no |
9798
| <a name="input_create_eks"></a> [create\_eks](#input\_create\_eks) | Controls if EKS resources should be created (it affects almost all resources) | `bool` | `true` | no |
9899
| <a name="input_default_iam_role_arn"></a> [default\_iam\_role\_arn](#input\_default\_iam\_role\_arn) | ARN of the default IAM worker role to use if one is not specified in `var.node_groups` or `var.node_groups_defaults` | `string` | `""` | no |
99100
| <a name="input_ebs_optimized_not_supported"></a> [ebs\_optimized\_not\_supported](#input\_ebs\_optimized\_not\_supported) | List of instance types that do not support EBS optimization | `list(string)` | `[]` | no |

modules/node_groups/launch_template.tf

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ data "cloudinit_config" "workers_userdata" {
99
content_type = "text/x-shellscript"
1010
content = templatefile("${path.module}/templates/userdata.sh.tpl",
1111
{
12-
cluster_name = var.cluster_name
13-
cluster_endpoint = var.cluster_endpoint
14-
cluster_auth_base64 = var.cluster_auth_base64
15-
ami_id = lookup(each.value, "ami_id", "")
16-
ami_is_eks_optimized = each.value["ami_is_eks_optimized"]
17-
bootstrap_env = each.value["bootstrap_env"]
18-
kubelet_extra_args = each.value["kubelet_extra_args"]
19-
pre_userdata = each.value["pre_userdata"]
20-
capacity_type = lookup(each.value, "capacity_type", "ON_DEMAND")
21-
append_labels = length(lookup(each.value, "k8s_labels", {})) > 0 ? ",${join(",", [for k, v in lookup(each.value, "k8s_labels", {}) : "${k}=${v}"])}" : ""
12+
cluster_name = var.cluster_name
13+
cluster_endpoint = var.cluster_endpoint
14+
cluster_auth_base64 = var.cluster_auth_base64
15+
cluster_service_ipv4_cidr = var.cluster_service_ipv4_cidr
16+
ami_id = lookup(each.value, "ami_id", "")
17+
ami_is_eks_optimized = each.value["ami_is_eks_optimized"]
18+
bootstrap_env = each.value["bootstrap_env"]
19+
kubelet_extra_args = each.value["kubelet_extra_args"]
20+
pre_userdata = each.value["pre_userdata"]
21+
capacity_type = lookup(each.value, "capacity_type", "ON_DEMAND")
22+
append_labels = length(lookup(each.value, "k8s_labels", {})) > 0 ? ",${join(",", [for k, v in lookup(each.value, "k8s_labels", {}) : "${k}=${v}"])}" : ""
2223
}
2324
)
2425
}

modules/node_groups/templates/userdata.sh.tpl

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ KUBELET_EXTRA_ARGS='--node-labels=eks.amazonaws.com/nodegroup-image=${ami_id},ek
2929
${pre_userdata}
3030
%{ if length(ami_id) > 0 && ami_is_eks_optimized ~}
3131

32+
# The bootstrap.sh script doesn't allow to pass service ipv4 cidr as an argument.
33+
# Therefore the environment variable SERVICE_IPV4_CIDR is exported to be used by the script.
34+
export SERVICE_IPV4_CIDR=${cluster_service_ipv4_cidr}
35+
3236
# Call bootstrap for EKS optimised custom AMI
3337
/etc/eks/bootstrap.sh ${cluster_name} --apiserver-endpoint "$${API_SERVER_URL}" --b64-cluster-ca "$${B64_CLUSTER_CA}" --kubelet-extra-args "$${KUBELET_EXTRA_ARGS}"
3438
%{ endif ~}

modules/node_groups/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ variable "cluster_auth_base64" {
2222
default = ""
2323
}
2424

25+
variable "cluster_service_ipv4_cidr" {
26+
description = "service ipv4 cidr for the kubernetes cluster"
27+
type = string
28+
default = null
29+
}
30+
2531
variable "default_iam_role_arn" {
2632
description = "ARN of the default IAM worker role to use if one is not specified in `var.node_groups` or `var.node_groups_defaults`"
2733
type = string

node_groups.tf

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ module "node_groups" {
33

44
create_eks = var.create_eks
55

6-
cluster_name = local.cluster_name
7-
cluster_endpoint = local.cluster_endpoint
8-
cluster_auth_base64 = local.cluster_auth_base64
6+
cluster_name = local.cluster_name
7+
cluster_endpoint = local.cluster_endpoint
8+
cluster_auth_base64 = local.cluster_auth_base64
9+
cluster_service_ipv4_cidr = local.cluster_service_ipv4_cidr
910

1011
default_iam_role_arn = coalescelist(aws_iam_role.workers[*].arn, [""])[0]
1112
ebs_optimized_not_supported = local.ebs_optimized_not_supported

0 commit comments

Comments
 (0)