Skip to content

Commit a429b61

Browse files
author
Grzegorz Lisowski
committed
feat: Add option to migrate to worker group module
1 parent 2221909 commit a429b61

File tree

14 files changed

+1370
-8
lines changed

14 files changed

+1370
-8
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
160160
| kubernetes | >= 1.11.1 |
161161
| local | >= 1.4 |
162162
| null | >= 2.1 |
163+
| random | >= 2.1 |
164+
| template | >= 2.1 |
163165

164166
## Inputs
165167

@@ -220,6 +222,8 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
220222
| worker\_create\_initial\_lifecycle\_hooks | Whether to create initial lifecycle hooks provided in worker groups. | `bool` | `false` | no |
221223
| worker\_create\_security\_group | Whether to create a security group for the workers or attach the workers to `worker_security_group_id`. | `bool` | `true` | no |
222224
| worker\_groups | A map of maps defining worker group configurations to be defined using AWS Launch Templates. See workers\_group\_defaults for valid keys. | `any` | `{}` | no |
225+
| worker\_groups\_launch\_template\_legacy | A list of maps defining worker group configurations to be defined using AWS Launch Templates. See workers\_group\_defaults for valid keys. | `any` | `[]` | no |
226+
| worker\_groups\_legacy | A list of maps defining worker group configurations to be defined using AWS Launch Configurations. See workers\_group\_defaults for valid keys. | `any` | `[]` | no |
223227
| worker\_security\_group\_id | If provided, all workers will be attached to this security group. If not given, a security group will be created with necessary ingress/egress to work with the EKS cluster. | `string` | `""` | no |
224228
| worker\_sg\_ingress\_from\_port | Minimum port number from which pods will accept communication. Must be changed to a lower value if some pods in your cluster will expose a port lower than 1025 (e.g. 22, 80, or 443). | `number` | `1025` | no |
225229
| workers\_additional\_policies | Additional policies to be added to workers | `list(string)` | `[]` | no |
@@ -256,6 +260,15 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
256260
| worker\_groups | Outputs from EKS worker groups. Map of maps, keyed by var.worker\_groups keys |
257261
| worker\_iam\_instance\_profile\_arns | default IAM instance profile ARN for EKS worker groups |
258262
| worker\_iam\_instance\_profile\_names | default IAM instance profile name for EKS worker groups |
263+
| worker\_iam\_role\_arn | default IAM role ARN for EKS worker groups |
264+
| worker\_iam\_role\_name | default IAM role name for EKS worker groups |
259265
| worker\_security\_group\_id | Security group ID attached to the EKS workers. |
266+
| workers\_asg\_arns | IDs of the autoscaling groups containing workers. |
267+
| workers\_asg\_names | Names of the autoscaling groups containing workers. |
268+
| workers\_default\_ami\_id | ID of the default worker group AMI |
269+
| workers\_launch\_template\_arns | ARNs of the worker launch templates. |
270+
| workers\_launch\_template\_ids | IDs of the worker launch templates. |
271+
| workers\_launch\_template\_latest\_versions | Latest versions of the worker launch templates. |
272+
| workers\_user\_data | User data of worker groups |
260273

261274
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

aws_auth.tf

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
data "aws_caller_identity" "current" {}
22

33
locals {
4+
## DEPRECATED section which should be removed when users will be done migration to
5+
## worker nodes managed via maps. When updating remember about proper update in modules/worker_groups
6+
7+
auth_launch_template_worker_roles = [
8+
for index in range(0, var.create_eks ? local.worker_group_launch_template_legacy_count : 0) : {
9+
worker_role_arn = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/${element(
10+
coalescelist(
11+
aws_iam_instance_profile.workers_launch_template.*.role,
12+
data.aws_iam_instance_profile.custom_worker_group_launch_template_iam_instance_profile.*.role_name,
13+
[""]
14+
),
15+
index
16+
)}"
17+
platform = lookup(
18+
var.worker_groups_launch_template_legacy[index],
19+
"platform",
20+
local.workers_group_defaults["platform"]
21+
)
22+
}
23+
]
24+
25+
auth_worker_roles = [
26+
for index in range(0, var.create_eks ? local.worker_group_count_legacy : 0) : {
27+
worker_role_arn = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/${element(
28+
coalescelist(
29+
aws_iam_instance_profile.workers.*.role,
30+
data.aws_iam_instance_profile.custom_worker_group_iam_instance_profile.*.role_name,
31+
[""]
32+
),
33+
index,
34+
)}"
35+
platform = lookup(
36+
var.worker_groups_legacy[index],
37+
"platform",
38+
local.workers_group_defaults["platform"]
39+
)
40+
}
41+
]
42+
## ~DEPRECATED
43+
444
# Convert to format needed by aws-auth ConfigMap
545
configmap_roles = [
646
for role in concat(
47+
local.auth_launch_template_worker_roles,
48+
local.auth_worker_roles,
749
module.worker_groups.aws_auth_roles,
850
module.node_groups.aws_auth_roles,
951
module.fargate.aws_auth_roles,
@@ -35,7 +77,7 @@ resource "kubernetes_config_map" "aws_auth" {
3577
labels = merge(
3678
{
3779
"app.kubernetes.io/managed-by" = "Terraform"
38-
# / are replaced by . because label validator fails in this lib
80+
# / are replaced by . because label validator fails in this lib
3981
# https://github.com/kubernetes/apimachinery/blob/1bdd76d09076d4dc0362456e59c8f551f5f24a72/pkg/util/validation/validation.go#L166
4082
"terraform.io/module" = "terraform-aws-modules.eks.aws"
4183
},

data.tf

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,150 @@ data "aws_iam_policy_document" "cluster_assume_role_policy" {
2929
}
3030

3131
data "aws_iam_role" "custom_cluster_iam_role" {
32-
count = !var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
32+
count = ! var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
3333
name = var.cluster_iam_role_name
3434
}
3535

3636
data "aws_partition" "current" {}
37+
38+
## DEPRECATED section which should be removed when users will be done migration to
39+
## worker nodes managed via maps. When updating remember about proper update in modules/worker_groups
40+
41+
data "aws_ami" "eks_worker" {
42+
filter {
43+
name = "name"
44+
values = [local.worker_ami_name_filter]
45+
}
46+
47+
most_recent = true
48+
49+
owners = [var.worker_ami_owner_id]
50+
}
51+
52+
data "aws_ami" "eks_worker_windows" {
53+
filter {
54+
name = "name"
55+
values = [local.worker_ami_name_filter_windows]
56+
}
57+
58+
filter {
59+
name = "platform"
60+
values = ["windows"]
61+
}
62+
63+
most_recent = true
64+
65+
owners = [var.worker_ami_owner_id_windows]
66+
}
67+
68+
data "template_file" "userdata" {
69+
count = var.create_eks ? local.worker_group_count_legacy : 0
70+
template = lookup(
71+
var.worker_groups_legacy[count.index],
72+
"userdata_template_file",
73+
file(
74+
lookup(var.worker_groups_legacy[count.index], "platform", local.workers_group_defaults["platform"]) == "windows"
75+
? "${path.module}/templates/userdata_windows.tpl"
76+
: "${path.module}/templates/userdata.sh.tpl"
77+
)
78+
)
79+
80+
vars = merge({
81+
platform = lookup(var.worker_groups_legacy[count.index], "platform", local.workers_group_defaults["platform"])
82+
cluster_name = coalescelist(aws_eks_cluster.this[*].name, [""])[0]
83+
endpoint = coalescelist(aws_eks_cluster.this[*].endpoint, [""])[0]
84+
cluster_auth_base64 = coalescelist(aws_eks_cluster.this[*].certificate_authority[0].data, [""])[0]
85+
pre_userdata = lookup(
86+
var.worker_groups_legacy[count.index],
87+
"pre_userdata",
88+
local.workers_group_defaults["pre_userdata"],
89+
)
90+
additional_userdata = lookup(
91+
var.worker_groups_legacy[count.index],
92+
"additional_userdata",
93+
local.workers_group_defaults["additional_userdata"],
94+
)
95+
bootstrap_extra_args = lookup(
96+
var.worker_groups_legacy[count.index],
97+
"bootstrap_extra_args",
98+
local.workers_group_defaults["bootstrap_extra_args"],
99+
)
100+
kubelet_extra_args = lookup(
101+
var.worker_groups_legacy[count.index],
102+
"kubelet_extra_args",
103+
local.workers_group_defaults["kubelet_extra_args"],
104+
)
105+
},
106+
lookup(
107+
var.worker_groups_legacy[count.index],
108+
"userdata_template_extra_args",
109+
local.workers_group_defaults["userdata_template_extra_args"]
110+
)
111+
)
112+
}
113+
114+
data "template_file" "launch_template_userdata" {
115+
count = var.create_eks ? local.worker_group_launch_template_legacy_count : 0
116+
template = lookup(
117+
var.worker_groups_launch_template_legacy[count.index],
118+
"userdata_template_file",
119+
file(
120+
lookup(var.worker_groups_launch_template_legacy[count.index], "platform", local.workers_group_defaults["platform"]) == "windows"
121+
? "${path.module}/templates/userdata_windows.tpl"
122+
: "${path.module}/templates/userdata.sh.tpl"
123+
)
124+
)
125+
126+
vars = merge({
127+
platform = lookup(var.worker_groups_launch_template_legacy[count.index], "platform", local.workers_group_defaults["platform"])
128+
cluster_name = coalescelist(aws_eks_cluster.this[*].name, [""])[0]
129+
endpoint = coalescelist(aws_eks_cluster.this[*].endpoint, [""])[0]
130+
cluster_auth_base64 = coalescelist(aws_eks_cluster.this[*].certificate_authority[0].data, [""])[0]
131+
pre_userdata = lookup(
132+
var.worker_groups_launch_template_legacy[count.index],
133+
"pre_userdata",
134+
local.workers_group_defaults["pre_userdata"],
135+
)
136+
additional_userdata = lookup(
137+
var.worker_groups_launch_template_legacy[count.index],
138+
"additional_userdata",
139+
local.workers_group_defaults["additional_userdata"],
140+
)
141+
bootstrap_extra_args = lookup(
142+
var.worker_groups_launch_template_legacy[count.index],
143+
"bootstrap_extra_args",
144+
local.workers_group_defaults["bootstrap_extra_args"],
145+
)
146+
kubelet_extra_args = lookup(
147+
var.worker_groups_launch_template_legacy[count.index],
148+
"kubelet_extra_args",
149+
local.workers_group_defaults["kubelet_extra_args"],
150+
)
151+
},
152+
lookup(
153+
var.worker_groups_launch_template_legacy[count.index],
154+
"userdata_template_extra_args",
155+
local.workers_group_defaults["userdata_template_extra_args"]
156+
)
157+
)
158+
}
159+
160+
data "aws_iam_instance_profile" "custom_worker_group_iam_instance_profile" {
161+
count = var.manage_worker_iam_resources ? 0 : local.worker_group_count_legacy
162+
name = lookup(
163+
var.worker_groups_legacy[count.index],
164+
"iam_instance_profile_name",
165+
local.workers_group_defaults["iam_instance_profile_name"],
166+
)
167+
}
168+
169+
data "aws_iam_instance_profile" "custom_worker_group_launch_template_iam_instance_profile" {
170+
count = var.manage_worker_iam_resources ? 0 : local.worker_group_launch_template_legacy_count
171+
name = lookup(
172+
var.worker_groups_launch_template_legacy[count.index],
173+
"iam_instance_profile_name",
174+
local.workers_group_defaults["iam_instance_profile_name"],
175+
)
176+
}
177+
178+
## ~DEPRECATED

examples/basic/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ module "eks" {
136136

137137
vpc_id = module.vpc.vpc_id
138138

139+
// worker_groups_legacy = [
140+
// {
141+
// name = "worker-group-1"
142+
// instance_type = "t3.small"
143+
// additional_userdata = "echo foo bar"
144+
// asg_desired_capacity = 2
145+
// additional_security_group_ids = [aws_security_group.worker_group_mgmt_one.id]
146+
// root_volume_type = "gp2"
147+
// },
148+
// {
149+
// name = "worker-group-2"
150+
// instance_type = "t3.medium"
151+
// additional_userdata = "echo foo bar"
152+
// additional_security_group_ids = [aws_security_group.worker_group_mgmt_two.id]
153+
// asg_desired_capacity = 1
154+
// root_volume_type = "gp2"
155+
// },
156+
// ]
157+
139158
worker_groups = {
140159
worker-group-1 = {
141160
instance_type = "t3.small"

examples/launch_templates/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ module "eks" {
7676
subnets = module.vpc.public_subnets
7777
vpc_id = module.vpc.vpc_id
7878

79+
// worker_groups_launch_template_legacy = [
80+
// {
81+
// name = "worker-group-1"
82+
// instance_type = "t3.small"
83+
// asg_desired_capacity = 2
84+
// public_ip = true
85+
// },
86+
// {
87+
// name = "worker-group-2"
88+
// instance_type = "t3.medium"
89+
// asg_desired_capacity = 1
90+
// public_ip = true
91+
// },
92+
// ]
93+
7994
worker_groups = {
8095
worker-group-1 = {
8196
instance_type = "t3.small"

0 commit comments

Comments
 (0)