-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain.tf
More file actions
417 lines (365 loc) · 11.8 KB
/
Copy pathmain.tf
File metadata and controls
417 lines (365 loc) · 11.8 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
module "common" {
source = "../../../../common"
}
module "basic_components" {
source = "../../../../basic_components"
region = var.region
}
locals {
aws_eks = "aws eks --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
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-${module.common.testing_id}"
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 = [var.instance_type]
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.pod_CloudWatchAgentServerPolicy
]
}
resource "aws_eks_addon" "pod_identity_addon" {
cluster_name = aws_eks_cluster.this.name
addon_name = "eks-pod-identity-agent"
depends_on = [aws_eks_node_group.this]
}
# EKS Node IAM Role
resource "aws_iam_role" "node_role" {
name = "cwagent-eks-Worker-Role-${module.common.testing_id}"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
data "aws_iam_policy_document" "pod-identity-policy" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["pods.eks.amazonaws.com"]
}
actions = [
"sts:AssumeRole",
"sts:TagSession"
]
}
}
resource "aws_iam_role" "pod-identity-role" {
name = "cwagent-eks-pod-identity-role-${module.common.testing_id}"
assume_role_policy = data.aws_iam_policy_document.pod-identity-policy.json
}
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" "pod_CloudWatchAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = aws_iam_role.pod-identity-role.name
}
resource "aws_eks_pod_identity_association" "association" {
cluster_name = aws_eks_cluster.this.name
namespace = "amazon-cloudwatch"
service_account = "cloudwatch-agent"
role_arn = aws_iam_role.pod-identity-role.arn
depends_on = [aws_eks_cluster.this]
}
# 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"
}
data "external" "clone_helm_chart" {
program = ["bash", "-c", <<-EOT
rm -rf ./helm-charts
git clone -b ${var.helm_chart_branch} https://github.com/aws-observability/helm-charts.git ./helm-charts
echo '{"status":"ready"}'
EOT
]
}
resource "helm_release" "aws_observability" {
name = "amazon-cloudwatch-observability"
chart = "./helm-charts/charts/amazon-cloudwatch-observability"
namespace = "amazon-cloudwatch"
create_namespace = true
set = [
{
name = "clusterName"
value = aws_eks_cluster.this.name
},
{
name = "region"
value = var.region
}
]
depends_on = [
aws_eks_cluster.this,
aws_eks_node_group.this,
aws_eks_addon.pod_identity_addon,
aws_eks_pod_identity_association.association,
data.external.clone_helm_chart,
]
}
resource "null_resource" "kubectl" {
depends_on = [
aws_eks_cluster.this,
aws_eks_node_group.this,
]
provisioner "local-exec" {
command = <<-EOT
${local.aws_eks} update-kubeconfig --name ${aws_eks_cluster.this.name}
${local.aws_eks} list-clusters --output text
${local.aws_eks} describe-cluster --name ${aws_eks_cluster.this.name} --output text
EOT
}
}
resource "null_resource" "update_image" {
depends_on = [helm_release.aws_observability, null_resource.kubectl]
triggers = {
timestamp = "${timestamp()}" # Forces re-run on every apply
}
provisioner "local-exec" {
command = <<-EOT
kubectl -n amazon-cloudwatch patch AmazonCloudWatchAgent cloudwatch-agent --type='json' -p='[{"op": "replace", "path": "/spec/image", "value": "${var.cwagent_image_repo}:${var.cwagent_image_tag}"}]'
kubectl set image deployment/amazon-cloudwatch-observability-controller-manager -n amazon-cloudwatch manager=public.ecr.aws/cloudwatch-agent/cloudwatch-agent-operator:latest
sleep 10
EOT
}
}
resource "kubernetes_pod" "log_generator" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "log-generator"
namespace = "default"
}
spec {
container {
name = "log-generator"
image = "busybox"
# Run shell script that generate a log line every second
command = ["/bin/sh", "-c"]
args = ["while true; do echo \"Log entry at $(date)\"; sleep 1; done"]
}
restart_policy = "Always"
}
}
# Storage Class for PV testing (using hostPath for simplicity)
resource "kubernetes_storage_class" "test_storage_class" {
depends_on = [helm_release.aws_observability]
metadata {
name = "test-storage-class-${module.common.testing_id}"
}
storage_provisioner = "kubernetes.io/no-provisioner"
volume_binding_mode = "WaitForFirstConsumer"
}
# PersistentVolume
resource "kubernetes_persistent_volume" "test_pv" {
depends_on = [kubernetes_storage_class.test_storage_class]
metadata {
name = "test-pv-${module.common.testing_id}"
}
spec {
capacity = {
storage = "1Gi"
}
access_modes = ["ReadWriteOnce"]
storage_class_name = kubernetes_storage_class.test_storage_class.metadata[0].name
persistent_volume_source {
host_path {
path = "/tmp/test-pv"
}
}
}
}
# PersistentVolumeClaim
resource "kubernetes_persistent_volume_claim" "test_pvc" {
depends_on = [
helm_release.aws_observability,
kubernetes_persistent_volume.test_pv
]
metadata {
name = "test-pvc-${module.common.testing_id}"
namespace = "amazon-cloudwatch"
}
spec {
access_modes = ["ReadWriteOnce"]
storage_class_name = kubernetes_storage_class.test_storage_class.metadata[0].name
volume_name = kubernetes_persistent_volume.test_pv.metadata[0].name
resources {
requests = {
storage = "1Gi"
}
}
}
}
# Pod that uses the PVC (to trigger bound status and generate metrics)
resource "kubernetes_pod" "test_pod_with_pvc" {
depends_on = [kubernetes_persistent_volume_claim.test_pvc]
metadata {
name = "test-pod-with-pvc-${module.common.testing_id}"
namespace = "amazon-cloudwatch"
labels = {
app = "pv-test"
}
}
spec {
termination_grace_period_seconds = 0
container {
name = "test-container"
image = "busybox"
command = ["sleep", "3600"]
volume_mount {
name = "test-volume"
mount_path = "/data"
}
}
volume {
name = "test-volume"
persistent_volume_claim {
claim_name = kubernetes_persistent_volume_claim.test_pvc.metadata[0].name
}
}
restart_policy = "Always"
}
}
# Get the single instance ID of the node in the node group
data "aws_instances" "eks_node" {
depends_on = [
aws_eks_node_group.this
]
filter {
name = "tag:eks:nodegroup-name"
values = [aws_eks_node_group.this.node_group_name]
}
}
# Retrieve details of the single instance to get private DNS
data "aws_instance" "eks_node_detail" {
depends_on = [
data.aws_instances.eks_node
]
instance_id = data.aws_instances.eks_node.ids[0]
}
resource "null_resource" "validator" {
depends_on = [
aws_eks_node_group.this,
aws_eks_addon.pod_identity_addon,
helm_release.aws_observability,
null_resource.update_image,
kubernetes_pod.log_generator,
kubernetes_pod.test_pod_with_pvc,
]
triggers = {
always_run = timestamp()
}
provisioner "local-exec" {
command = <<-EOT
echo "Validating CloudWatch Agent with pod identity credential"
cd ../../../../..
go test ./test/metric_value_benchmark -timeout 1h -eksClusterName=${aws_eks_cluster.this.name} -computeType=EKS -v -eksDeploymentStrategy=PODIDENTITY -instanceId=${data.aws_instance.eks_node_detail.instance_id} &&
go test ./test/fluent -eksClusterName=${aws_eks_cluster.this.name} -computeType=EKS -v -eksDeploymentStrategy=DAEMON
EOT
}
}