-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain.tf
More file actions
511 lines (457 loc) · 14.4 KB
/
Copy pathmain.tf
File metadata and controls
511 lines (457 loc) · 14.4 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
500
501
502
503
504
505
506
507
508
509
510
511
# 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
}
locals {
aws_eks = "aws eks --region ${var.region}"
}
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 Group — 2x t3.medium with node-color=blue label
resource "aws_eks_node_group" "this" {
cluster_name = aws_eks_cluster.this.name
node_group_name = "cwagent-otel-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 = 2
max_size = 2
min_size = 2
}
ami_type = var.ami_type
capacity_type = "ON_DEMAND"
disk_size = 20
instance_types = [var.instance_type]
labels = {
"ci-test.example.com/node-color" = "blue"
}
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,
]
}
# EKS Node IAM Role
resource "aws_iam_role" "node_role" {
name = "cwagent-otel-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
}
# Pod Identity IAM Role
resource "aws_iam_role" "pod_identity_role" {
name = "cwagent-otel-pod-identity-${module.common.testing_id}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "pods.eks.amazonaws.com" }
Action = ["sts:AssumeRole", "sts:TagSession"]
}]
})
}
resource "aws_iam_role_policy_attachment" "pod_identity_CloudWatchAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = aws_iam_role.pod_identity_role.name
}
# --- EKS Addon: Pod Identity agent ---
resource "aws_eks_addon" "pod_identity_agent" {
depends_on = [aws_eks_node_group.this]
cluster_name = aws_eks_cluster.this.name
addon_name = "eks-pod-identity-agent"
}
# --- Update kubeconfig ---
resource "null_resource" "kubectl" {
depends_on = [aws_eks_cluster.this, aws_eks_node_group.this]
provisioner "local-exec" {
command = "${local.aws_eks} update-kubeconfig --name ${aws_eks_cluster.this.name}"
}
}
# --- KEDA Helm install ---
resource "helm_release" "keda" {
name = "keda"
repository = "https://kedacore.github.io/charts"
chart = "keda"
version = var.keda_version
namespace = "keda"
create_namespace = true
set = [
{ name = "resources.operator.requests.cpu", value = "100m" },
{ name = "resources.operator.requests.memory", value = "128Mi" },
{ name = "prometheus.metricServer.enabled", value = "true" },
{ name = "prometheus.operator.enabled", value = "true" },
]
depends_on = [
aws_eks_node_group.this,
null_resource.kubectl,
]
}
# --- KEDA test workload: nginx-keda with cron ScaledObject ---
resource "kubernetes_deployment_v1" "nginx_keda" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "nginx-keda"
namespace = "default"
}
spec {
replicas = 1
selector { match_labels = { app = "nginx-keda" } }
template {
metadata { labels = { app = "nginx-keda" } }
spec {
node_selector = { "ci-test.example.com/node-color" = "blue" }
container {
name = "nginx"
image = "public.ecr.aws/nginx/nginx:latest"
port { container_port = 80 }
resources { requests = { cpu = "10m", memory = "32Mi" } }
}
}
}
}
}
resource "null_resource" "keda_scaled_object" {
depends_on = [helm_release.keda, kubernetes_deployment_v1.nginx_keda]
provisioner "local-exec" {
command = <<-EOT
cat <<'EOF' | kubectl apply -f -
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: nginx-keda-scaledobject
namespace: default
spec:
scaleTargetRef:
name: nginx-keda
minReplicaCount: 1
maxReplicaCount: 3
pollingInterval: 15
triggers:
- type: cron
metadata:
timezone: "UTC"
start: "0 0 * * *"
end: "59 23 * * *"
desiredReplicas: "1"
EOF
EOT
}
provisioner "local-exec" {
when = destroy
command = "kubectl delete scaledobject nginx-keda-scaledobject -n default --timeout=60s 2>/dev/null || true"
}
}
resource "null_resource" "keda_scaled_job" {
depends_on = [helm_release.keda, null_resource.kubectl]
provisioner "local-exec" {
command = <<-EOT
cat <<'EOF' | kubectl apply -f -
apiVersion: keda.sh/v1alpha1
kind: ScaledJob
metadata:
name: keda-test-scaledjob
namespace: default
spec:
jobTargetRef:
template:
spec:
nodeSelector:
ci-test.example.com/node-color: blue
containers:
- name: echo
image: busybox:1.36
command: ["echo", "keda-scaled-job-test"]
resources:
requests:
cpu: 10m
memory: 16Mi
restartPolicy: Never
backoffLimit: 1
pollingInterval: 30
maxReplicaCount: 1
triggers:
- type: cron
metadata:
timezone: "UTC"
start: "0 0 * * *"
end: "59 23 * * *"
desiredReplicas: "1"
EOF
EOT
}
provisioner "local-exec" {
when = destroy
command = "kubectl delete scaledjob keda-test-scaledjob -n default --timeout=60s 2>/dev/null || true"
}
}
# --- Helm chart install ---
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 },
{ name = "otelContainerInsights.enabled", value = "true" },
{ name = "otelContainerInsights.solutions.keda.enabled", value = "true" },
]
depends_on = [
aws_eks_addon.pod_identity_agent,
null_resource.kubectl,
data.external.clone_helm_chart,
helm_release.keda,
]
}
# --- Pod Identity association (after Helm creates the service account) ---
resource "aws_eks_pod_identity_association" "cloudwatch_agent" {
depends_on = [helm_release.aws_observability]
cluster_name = aws_eks_cluster.this.name
namespace = "amazon-cloudwatch"
service_account = "cloudwatch-agent"
role_arn = aws_iam_role.pod_identity_role.arn
}
# --- Patch agent image (both DaemonSet and cluster-scraper) ---
resource "null_resource" "update_image" {
depends_on = [helm_release.aws_observability, null_resource.kubectl]
triggers = { timestamp = timestamp() }
provisioner "local-exec" {
command = <<-EOT
sleep 30
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 -n amazon-cloudwatch patch AmazonCloudWatchAgent cloudwatch-agent-cluster-scraper --type='json' \
-p='[{"op": "replace", "path": "/spec/image", "value": "${var.cwagent_image_repo}:${var.cwagent_image_tag}"}]' 2>/dev/null || true
sleep 10
EOT
}
}
# --- Restart pods to pick up Pod Identity + new image ---
resource "null_resource" "restart_pods" {
depends_on = [aws_eks_pod_identity_association.cloudwatch_agent, null_resource.update_image]
triggers = { timestamp = timestamp() }
provisioner "local-exec" {
command = <<-EOT
kubectl -n amazon-cloudwatch rollout restart daemonset/cloudwatch-agent
kubectl -n amazon-cloudwatch rollout restart deployment/cloudwatch-agent-cluster-scraper 2>/dev/null || true
kubectl -n amazon-cloudwatch rollout status daemonset/cloudwatch-agent --timeout=120s
kubectl -n amazon-cloudwatch rollout status deployment/cloudwatch-agent-cluster-scraper --timeout=120s 2>/dev/null || true
EOT
}
}
# --- Test workload: nginx ---
resource "kubernetes_deployment_v1" "nginx_test" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "nginx-test"
namespace = "default"
}
spec {
replicas = 1
selector { match_labels = { app = "nginx-test" } }
template {
metadata { labels = { app = "nginx-test" } }
spec {
container {
name = "nginx"
image = "public.ecr.aws/nginx/nginx:latest"
port { container_port = 80 }
}
}
}
}
}
# --- KSM test workloads ---
resource "kubernetes_stateful_set_v1" "ksm_statefulset" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "ksm-test-statefulset"
namespace = "default"
}
spec {
replicas = 1
service_name = kubernetes_service_v1.ksm_statefulset_headless.metadata[0].name
selector { match_labels = { app = "ksm-test-statefulset" } }
template {
metadata { labels = { app = "ksm-test-statefulset" } }
spec {
node_selector = { "ci-test.example.com/node-color" = "blue" }
container {
name = "pause"
image = "registry.k8s.io/pause:3.9"
resources { requests = { cpu = "10m", memory = "16Mi" } }
}
}
}
}
}
resource "kubernetes_service_v1" "ksm_statefulset_headless" {
metadata {
name = "ksm-test-statefulset"
namespace = "default"
}
spec {
cluster_ip = "None"
selector = { app = "ksm-test-statefulset" }
port {
port = 80
name = "placeholder"
}
}
}
resource "kubernetes_cron_job_v1" "ksm_cronjob" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "ksm-test-cronjob"
namespace = "default"
}
spec {
schedule = "*/5 * * * *"
successful_jobs_history_limit = 1
failed_jobs_history_limit = 1
job_template {
metadata {}
spec {
template {
metadata { labels = { app = "ksm-test-cronjob" } }
spec {
node_selector = { "ci-test.example.com/node-color" = "blue" }
restart_policy = "Never"
container {
name = "echo"
image = "busybox:1.36"
command = ["echo", "ksm-test"]
resources { requests = { cpu = "10m", memory = "16Mi" } }
}
}
}
}
}
}
}
resource "kubernetes_job_v1" "ksm_job" {
depends_on = [aws_eks_node_group.this]
metadata {
name = "ksm-test-job"
namespace = "default"
}
spec {
ttl_seconds_after_finished = 86400
template {
metadata { labels = { app = "ksm-test-job" } }
spec {
node_selector = { "ci-test.example.com/node-color" = "blue" }
restart_policy = "Never"
container {
name = "echo"
image = "busybox:1.36"
command = ["echo", "ksm-test"]
resources { requests = { cpu = "10m", memory = "16Mi" } }
}
}
}
}
}
resource "null_resource" "ksm_replicaset" {
depends_on = [aws_eks_node_group.this, null_resource.kubectl]
provisioner "local-exec" {
command = <<-EOT
cat <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: ksm-test-replicaset
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: ksm-test-replicaset
template:
metadata:
labels:
app: ksm-test-replicaset
spec:
nodeSelector:
ci-test.example.com/node-color: blue
containers:
- name: pause
image: registry.k8s.io/pause:3.9
resources:
requests:
cpu: 10m
memory: 16Mi
EOF
EOT
}
}
# --- Test runner ---
resource "null_resource" "validator" {
depends_on = [
null_resource.restart_pods,
kubernetes_deployment_v1.nginx_test,
null_resource.keda_scaled_object,
null_resource.keda_scaled_job,
]
triggers = { always_run = timestamp() }
provisioner "local-exec" {
command = <<-EOT
echo "Running OTEL standard cluster integration tests"
cd ../../../..
echo "Waiting 3 minutes for metrics to propagate..."
sleep 180
go test -tags integration -timeout 1h -v ${var.test_dir} \
-eksClusterName=${aws_eks_cluster.this.name} \
-computeType=EKS \
-eksDeploymentStrategy=DAEMON \
-region=${var.region}
echo "Running OTEL solutions tests (KEDA)..."
echo "Waiting 2 additional minutes for KEDA metrics to propagate..."
sleep 120
go test -tags integration -timeout 1h -v ./test/otel/solutions/keda/... \
-eksClusterName=${aws_eks_cluster.this.name} \
-computeType=EKS \
-eksDeploymentStrategy=DAEMON \
-region=${var.region}
EOT
}
}