-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain.tf
More file actions
499 lines (462 loc) · 13.6 KB
/
Copy pathmain.tf
File metadata and controls
499 lines (462 loc) · 13.6 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
module "common" {
source = "../../../common"
cwagent_image_repo = var.cwagent_image_repo
cwagent_image_tag = var.cwagent_image_tag
}
module "basic_components" {
source = "../../../basic_components"
region = var.region
}
data "aws_eks_cluster_auth" "this" {
name = aws_eks_cluster.this.name
}
resource "aws_eks_cluster" "this" {
name = "cwagent-eks-integ-${module.common.testing_id}"
role_arn = module.basic_components.role_arn
version = var.k8s_version
enabled_cluster_log_types = [
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler"
]
vpc_config {
subnet_ids = module.basic_components.public_subnet_ids
security_group_ids = [module.basic_components.security_group]
}
}
# EKS Node Groups
resource "aws_launch_template" "node" {
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
}
}
resource "aws_eks_node_group" "this" {
cluster_name = aws_eks_cluster.this.name
node_group_name = "cwagent-eks-integ-node"
node_role_arn = aws_iam_role.node_role.arn
subnet_ids = module.basic_components.public_subnet_ids
scaling_config {
desired_size = 1
max_size = 1
min_size = 1
}
ami_type = var.ami_type
capacity_type = "ON_DEMAND"
instance_types = ["t3.medium"]
launch_template {
id = aws_launch_template.node.id
version = aws_launch_template.node.latest_version
}
depends_on = [
aws_iam_role_policy_attachment.node_AmazonEC2ContainerRegistryReadOnly,
aws_iam_role_policy_attachment.node_AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.node_AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.node_CloudWatchAgentServerPolicy
]
}
# EKS Node IAM Role
resource "aws_iam_role" "node_role" {
name = "cwagent-eks-Worker-Role-${module.common.testing_id}"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com"
},
Action = "sts:AssumeRole"
}
]
})
}
resource "aws_iam_role_policy_attachment" "node_AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.node_role.name
}
resource "aws_iam_role_policy_attachment" "node_AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.node_role.name
}
resource "aws_iam_role_policy_attachment" "node_AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.node_role.name
}
resource "aws_iam_role_policy_attachment" "node_CloudWatchAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = aws_iam_role.node_role.name
}
# TODO: these security groups be created once and then reused
# EKS Cluster Security Group
resource "aws_security_group" "eks_cluster_sg" {
name = "cwagent-eks-cluster-sg-${module.common.testing_id}"
description = "Cluster communication with worker nodes"
vpc_id = module.basic_components.vpc_id
}
resource "aws_security_group_rule" "cluster_inbound" {
description = "Allow worker nodes to communicate with the cluster API Server"
from_port = 443
protocol = "tcp"
security_group_id = aws_security_group.eks_cluster_sg.id
source_security_group_id = aws_security_group.eks_nodes_sg.id
to_port = 443
type = "ingress"
}
resource "aws_security_group_rule" "cluster_outbound" {
description = "Allow cluster API Server to communicate with the worker nodes"
from_port = 1024
protocol = "tcp"
security_group_id = aws_security_group.eks_cluster_sg.id
source_security_group_id = aws_security_group.eks_nodes_sg.id
to_port = 65535
type = "egress"
}
# EKS Node Security Group
resource "aws_security_group" "eks_nodes_sg" {
name = "cwagent-eks-node-sg-${module.common.testing_id}"
description = "Security group for all nodes in the cluster"
vpc_id = module.basic_components.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group_rule" "nodes_internal" {
description = "Allow nodes to communicate with each other"
from_port = 0
protocol = "-1"
security_group_id = aws_security_group.eks_nodes_sg.id
source_security_group_id = aws_security_group.eks_nodes_sg.id
to_port = 65535
type = "ingress"
}
resource "aws_security_group_rule" "nodes_cluster_inbound" {
description = "Allow worker Kubelets and pods to receive communication from the cluster control plane"
from_port = 1025
protocol = "tcp"
security_group_id = aws_security_group.eks_nodes_sg.id
source_security_group_id = aws_security_group.eks_cluster_sg.id
to_port = 65535
type = "ingress"
}
resource "kubernetes_namespace" "namespace" {
metadata {
name = "amazon-cloudwatch"
}
}
# TODO: how do we support different deployment types? Should they be in separate terraform
# files, and spawn separate tests?
resource "kubernetes_daemonset" "service" {
depends_on = [
kubernetes_namespace.namespace,
kubernetes_config_map.cwagentconfig,
kubernetes_service_account.cwagentservice,
aws_eks_node_group.this
]
metadata {
name = "cloudwatch-agent"
namespace = "amazon-cloudwatch"
}
spec {
selector {
match_labels = {
"name" : "cloudwatch-agent"
}
}
template {
metadata {
labels = {
"name" : "cloudwatch-agent"
}
}
spec {
node_selector = {
"kubernetes.io/os" : "linux"
}
container {
name = "cwagent"
image = "${var.cwagent_image_repo}:${var.cwagent_image_tag}"
image_pull_policy = "Always"
resources {
limits = {
"cpu" : "200m",
"memory" : "200Mi"
}
requests = {
"cpu" : "200m",
"memory" : "200Mi"
}
}
port {
container_port = 25888
host_port = 25888
protocol = "UDP"
}
env {
name = "HOST_IP"
value_from {
field_ref {
field_path = "status.hostIP"
}
}
}
env {
name = "HOST_NAME"
value_from {
field_ref {
field_path = "spec.nodeName"
}
}
}
env {
name = "K8S_NAMESPACE"
value_from {
field_ref {
field_path = "metadata.namespace"
}
}
}
volume_mount {
mount_path = "/etc/cwagentconfig"
name = "cwagentconfig"
}
volume_mount {
mount_path = "/rootfs"
name = "rootfs"
read_only = true
}
volume_mount {
mount_path = "/var/run/docker.sock"
name = "dockersock"
read_only = true
}
volume_mount {
mount_path = "/var/lib/docker"
name = "varlibdocker"
read_only = true
}
volume_mount {
mount_path = "/run/containerd/containerd.sock"
name = "containerdsock"
read_only = true
}
volume_mount {
mount_path = "/sys"
name = "sys"
read_only = true
}
volume_mount {
mount_path = "/dev/disk"
name = "devdisk"
read_only = true
}
}
volume {
name = "cwagentconfig"
config_map {
name = "cwagentconfig"
}
}
volume {
name = "rootfs"
host_path {
path = "/"
}
}
volume {
name = "dockersock"
host_path {
path = "/var/run/docker.sock"
}
}
volume {
name = "varlibdocker"
host_path {
path = "/var/lib/docker"
}
}
volume {
name = "containerdsock"
host_path {
path = "/run/containerd/containerd.sock"
}
}
volume {
name = "sys"
host_path {
path = "/sys"
}
}
volume {
name = "devdisk"
host_path {
path = "/dev/disk"
}
}
container {
name = "emf-eks-testing"
image = "alpine/socat:latest"
image_pull_policy = "Always"
resources {
limits = {
"cpu" : "50m",
"memory" : "50Mi"
}
requests = {
"cpu" : "50m",
"memory" : "50Mi"
}
}
command = [
"/bin/sh",
"-c",
"while true; do CURRENT_TIME=\"$(date +%s%3N)\"; TIMESTAMP=\"$(($CURRENT_TIME *1000))\"; echo '{\"_aws\":{\"Timestamp\":'\"$${TIMESTAMP}\"',\"LogGroupName\":\"EMFEKSLogGroup\",\"CloudWatchMetrics\":[{\"Namespace\":\"EMFEKSNameSpace\",\"Dimensions\":[[\"Type\",\"ClusterName\"]],\"Metrics\":[{\"Name\":\"EMFCounter\",\"Unit\":\"Count\"}]}]},\"Type\":\"Counter\",\"EMFCounter\":5, \"ClusterName\": \"${aws_eks_cluster.this.name}\"}' | socat -v -t 0 - UDP:0.0.0.0:25888; sleep 60; done"
]
env {
name = "HOST_IP"
value_from {
field_ref {
field_path = "status.hostIP"
}
}
}
env {
name = "HOST_NAME"
value_from {
field_ref {
field_path = "spec.nodeName"
}
}
}
env {
name = "K8S_NAMESPACE"
value_from {
field_ref {
field_path = "metadata.namespace"
}
}
}
volume_mount {
mount_path = "/etc/cwagentconfig"
name = "cwagentconfig"
}
}
service_account_name = "cloudwatch-agent"
host_network = true
termination_grace_period_seconds = 60
}
}
}
}
##########################################
# Template Files
##########################################
locals {
cwagent_config = fileexists("../../../../${var.test_dir}/resources/eks_config.json") ? "../../../../${var.test_dir}/resources/eks_config.json" : "../default_resources/default_amazon_cloudwatch_agent.json"
}
data "template_file" "cwagent_config" {
template = file(local.cwagent_config)
vars = {
}
}
resource "kubernetes_config_map" "cwagentconfig" {
depends_on = [
kubernetes_namespace.namespace,
kubernetes_service_account.cwagentservice
]
metadata {
name = "cwagentconfig"
namespace = "amazon-cloudwatch"
}
data = {
"cwagentconfig.json" : data.template_file.cwagent_config.rendered
}
}
resource "kubernetes_service_account" "cwagentservice" {
depends_on = [kubernetes_namespace.namespace]
metadata {
name = "cloudwatch-agent"
namespace = "amazon-cloudwatch"
}
}
resource "kubernetes_cluster_role" "clusterrole" {
depends_on = [kubernetes_namespace.namespace]
metadata {
name = "cloudwatch-agent-role"
}
rule {
verbs = ["list", "watch"]
resources = ["pods", "nodes", "endpoints"]
api_groups = [""]
}
rule {
verbs = ["list", "watch"]
resources = ["replicasets"]
api_groups = ["apps"]
}
rule {
verbs = ["list", "watch"]
resources = ["jobs"]
api_groups = ["batch"]
}
rule {
verbs = ["get"]
resources = ["nodes/proxy"]
api_groups = [""]
}
rule {
verbs = ["create"]
resources = ["nodes/stats", "configmaps", "events"]
api_groups = [""]
}
rule {
verbs = ["get", "update"]
resource_names = ["cwagent-clusterleader"]
resources = ["configmaps"]
api_groups = [""]
}
rule {
verbs = ["list", "watch", "get"]
resources = ["endpointslices"]
api_groups = ["discovery.k8s.io"]
}
}
resource "kubernetes_cluster_role_binding" "rolebinding" {
depends_on = [kubernetes_namespace.namespace]
metadata {
name = "cloudwatch-agent-role-binding"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "cloudwatch-agent-role"
}
subject {
kind = "ServiceAccount"
name = "cloudwatch-agent"
namespace = "amazon-cloudwatch"
}
}
resource "null_resource" "validator" {
depends_on = [
aws_eks_node_group.this,
kubernetes_daemonset.service,
kubernetes_cluster_role_binding.rolebinding,
kubernetes_service_account.cwagentservice,
]
provisioner "local-exec" {
command = <<-EOT
echo "Validating EKS metrics/logs for EMF"
cd ../../../..
go test ${var.test_dir} -eksClusterName=${aws_eks_cluster.this.name} -computeType=EKS -v -eksDeploymentStrategy=DAEMON
EOT
}
}