-
Notifications
You must be signed in to change notification settings - Fork 45
/
helm_karpenter.tf
263 lines (223 loc) · 7.66 KB
/
helm_karpenter.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
resource "helm_release" "karpenter" {
count = var.karpenter_toggle ? 1 : 0
namespace = "karpenter"
create_namespace = true
name = "karpenter"
repository = "oci://public.ecr.aws/karpenter"
chart = "karpenter"
version = "1.0.5"
set {
name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = aws_iam_role.karpenter_role[count.index].arn
}
set {
name = "settings.clusterName"
value = var.cluster_name
}
set {
name = "settings.clusterEndpoint"
value = aws_eks_cluster.main.endpoint
}
set {
name = "settings.interruptionQueue"
value = aws_sqs_queue.karpenter_termination_handler[count.index].name
}
set {
name = "aws.defaultInstanceProfile"
value = aws_iam_instance_profile.nodes.name
}
depends_on = [
aws_eks_cluster.main,
kubernetes_config_map.aws-auth,
aws_eks_node_group.main
]
}
resource "kubectl_manifest" "karpenter_node_pool" {
count = var.karpenter_toggle ? 1 : 0
yaml_body = templatefile(
"${path.module}/helm/karpenter/templates/node-pool.yaml.tpl", {
EKS_CLUSTER = var.cluster_name,
CAPACITY_TYPE = var.karpenter_capacity_type
INSTANCE_FAMILY = var.karpenter_instance_family
INSTANCE_SIZES = var.karpenter_instance_sizes
AVAILABILITY_ZONES = var.karpenter_availability_zones
})
depends_on = [
helm_release.karpenter
]
}
resource "kubectl_manifest" "karpenter_node_class" {
count = var.karpenter_toggle ? 1 : 0
yaml_body = templatefile(
"${path.module}/helm/karpenter/templates/node-class.yaml.tpl", {
EKS_CLUSTER = var.cluster_name
AMI_FAMILY = var.karpenter_ec2_node_family
SECURITY_GROUPS = [
aws_security_group.cluster_sg.id,
aws_security_group.cluster_nodes_sg.id,
aws_eks_cluster.main.vpc_config[0].cluster_security_group_id
]
INSTANCE_PROFILE = aws_iam_instance_profile.nodes.name
AMI_ID = data.aws_ssm_parameter.eks.value
EKS_SUBNETS = [
aws_subnet.private_subnet_1a.id,
aws_subnet.private_subnet_1b.id,
aws_subnet.private_subnet_1c.id
]
LAUNCH_TEMPLATE = format("%s-karpenter", var.cluster_name)
})
depends_on = [
helm_release.karpenter
]
}
resource "aws_launch_template" "karpenter" {
count = var.karpenter_toggle ? 1 : 0
image_id = data.aws_ssm_parameter.eks.value
name = format("%s-karpenter", var.cluster_name)
update_default_version = true
vpc_security_group_ids = [
aws_eks_cluster.main.vpc_config[0].cluster_security_group_id
]
user_data = base64encode(templatefile(
"${path.module}/helm/karpenter/templates/user-data.sh.tpl",
{
CLUSTER_NAME = var.cluster_name,
CLUSTER_ID = var.cluster_name,
APISERVER_ENDPOINT = aws_eks_cluster.main.endpoint,
B64_CLUSTER_CA = aws_eks_cluster.main.certificate_authority.0.data
}
))
iam_instance_profile {
name = aws_iam_instance_profile.nodes.name
}
tag_specifications {
resource_type = "instance"
tags = {
"Name" : format("%s-karpanter", var.cluster_name)
"aws-node-termination-handler/managed" = "true"
}
}
}
resource "aws_sqs_queue" "karpenter_termination_handler" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-karpenter", var.cluster_name)
delay_seconds = 0
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
visibility_timeout_seconds = 60
}
resource "aws_sqs_queue_policy" "karpenter_termination_handler" {
count = var.karpenter_toggle ? 1 : 0
queue_url = aws_sqs_queue.karpenter_termination_handler[count.index].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"sqs:SendMessage"
],
"Resource": [
"${aws_sqs_queue.karpenter_termination_handler[count.index].arn}"
]
}
]
}
EOF
}
resource "aws_cloudwatch_event_rule" "karpenter_termination_handler_instance_terminate" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-node-termination-handler-instance-terminate", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.autoscaling"]
detail-type = [
"EC2 Instance-terminate Lifecycle Action"
]
})
}
resource "aws_cloudwatch_event_target" "karpenter_termination_handler_instance_terminate" {
count = var.karpenter_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.karpenter_termination_handler_instance_terminate[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.karpenter_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "karpenter_termination_handler_scheduled_change" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-node-termination-handler-scheduled-change", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.health"]
detail-type = [
"AWS Health Event"
]
detail = {
service = [
"EC2"
]
eventTypeCategory = [
"scheduledChange"
]
}
})
}
resource "aws_cloudwatch_event_target" "karpenter_termination_handler_scheduled_change" {
count = var.karpenter_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.karpenter_termination_handler_scheduled_change[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.karpenter_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "karpenter_termination_handler_spot_termination" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-node-termination-handler-spot-termination", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Spot Instance Interruption Warning"
]
})
}
resource "aws_cloudwatch_event_target" "karpenter_termination_handler_spot_termination" {
count = var.karpenter_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.karpenter_termination_handler_spot_termination[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.karpenter_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "karpenter_termination_handler_rebalance" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-node-termination-handler-rebalance", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Instance Rebalance Recommendation"
]
})
}
resource "aws_cloudwatch_event_target" "karpenter_termination_handler_rebalance" {
count = var.karpenter_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.karpenter_termination_handler_rebalance[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.karpenter_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "karpenter_termination_handler_state_change" {
count = var.karpenter_toggle ? 1 : 0
name = format("%s-node-termination-handler-state-change", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Instance State-change Notification"
]
})
}
resource "aws_cloudwatch_event_target" "karpenter_termination_handler_state_change" {
count = var.karpenter_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.karpenter_termination_handler_state_change[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.karpenter_termination_handler[count.index].arn
}