Skip to content

Commit 4fb5944

Browse files
authored
Use New Fargate Profile For Each Run And Allow More Than One Tests At The Same Time (#416) (#430)
1 parent d7fbfec commit 4fb5944

File tree

10 files changed

+109
-41
lines changed

10 files changed

+109
-41
lines changed

terraform/eks/container-insights-agent/aoc_service_fargate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
kind: Service
33
metadata:
44
name: adot-collector-service
5-
namespace: default
5+
namespace: ${Namespace}
66
labels:
77
app: aws-adot
88
component: adot-collector

terraform/eks/container-insights-agent/cluster_role_binding_fargate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
subjects:
66
- kind: ServiceAccount
77
name: adot-collector-service-account
8-
namespace: default
8+
namespace: ${Namespace}
99
roleRef:
1010
kind: ClusterRole
1111
name: adotcol-admin-role

terraform/eks/container-insights-agent/config_map_fargate.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
kind: ConfigMap
33
metadata:
44
name: adot-collector-config
5-
namespace: default
5+
namespace: ${Namespace}
66
labels:
77
app: aws-adot
88
component: adot-collector-config
@@ -39,19 +39,19 @@ data:
3939
regex: (.+)
4040
target_label: __metrics_path__
4141
# Changes the default metrics path to kubelet's proxy cadvdisor metrics endpoint
42-
replacement: /api/v1/nodes/$${1}/proxy/metrics/cadvisor
42+
replacement: /api/v1/nodes/$$${1}/proxy/metrics/cadvisor
4343
metric_relabel_configs:
4444
# extract readable container/pod name from id field
4545
- action: replace
4646
source_labels: [id]
4747
regex: '^/machine\.slice/machine-rkt\\x2d([^\\]+)\\.+/([^/]+)\.service$'
4848
target_label: rkt_container_name
49-
replacement: '$${2}-$${1}'
49+
replacement: '$$${2}-$$${1}'
5050
- action: replace
5151
source_labels: [id]
5252
regex: '^/system\.slice/(.+)\.service$'
5353
target_label: systemd_service_name
54-
replacement: '$${1}'
54+
replacement: '$$${1}'
5555
processors:
5656
# rename labels which apply to all metrics and are used in metricstransform/rename processor
5757
metricstransform/label_1:

terraform/eks/container-insights-agent/logs_sample_fargate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: flblogproducer2eks
55
labels:
66
app: flblogproducer2eks
7-
namespace: default
7+
namespace: ${Namespace}
88
spec:
99
replicas: 1
1010
selector:

terraform/eks/container-insights-agent/service_account_fargate.tpl renamed to terraform/eks/container-insights-agent/service_account_fargate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v1
22
kind: ServiceAccount
33
metadata:
44
name: adot-collector-service-account
5-
namespace: default
5+
namespace: ${Namespace}
66
annotations:
77
eks.amazonaws.com/role-arn: ${RoleArn}

terraform/eks/container-insights-agent/stateful_set_fargate.tpl renamed to terraform/eks/container-insights-agent/stateful_set_fargate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: apps/v1
22
kind: StatefulSet
33
metadata:
44
name: adot-collector
5-
namespace: default
5+
namespace: ${Namespace}
66
labels:
77
app: aws-adot
88
component: adot-collector
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
data "template_file" "cluster_role_file" {
22
template = file("./container-insights-agent/cluster_role.tpl")
33
vars = {
4-
NAMESPACE = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name
4+
NAMESPACE = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name
55
}
6+
depends_on = [aws_eks_fargate_profile.test_profile]
67
}
78

89
data "template_file" "cluster_role_binding_file" {
910
template = file("./container-insights-agent/cluster_role_binding.tpl")
1011
vars = {
11-
NAMESPACE = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name,
12+
NAMESPACE = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name,
1213
SERVICE_ACCOUNT = kubernetes_service_account.aoc-agent-role.metadata[0].name
1314
}
15+
depends_on = [aws_eks_fargate_profile.test_profile]
1416
}
1517

1618
data "template_file" "config_map_file" {
1719
template = file("./container-insights-agent/config_map.tpl")
1820
vars = {
19-
NAMESPACE = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name
21+
NAMESPACE = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name
2022
}
23+
depends_on = [aws_eks_fargate_profile.test_profile]
2124
}
2225

2326
data "template_file" "daemonset_file" {
2427
template = file("./container-insights-agent/daemonset.tpl")
2528
vars = {
26-
NAMESPACE = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name
29+
NAMESPACE = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name
2730
SERVICE_ACCOUNT = kubernetes_service_account.aoc-agent-role.metadata[0].name
2831
OTELIMAGE = module.common.aoc_image
2932
REGION = var.region
3033
}
34+
depends_on = [aws_eks_fargate_profile.test_profile]
3135
}
3236

3337
resource "kubectl_manifest" "service_account" {
3438
count = var.aoc_base_scenario == "infra" && var.deployment_type == "fargate" ? 1 : 0
35-
yaml_body = templatefile("./container-insights-agent/service_account_fargate.tpl",
39+
yaml_body = templatefile("./container-insights-agent/service_account_fargate.yml",
3640
{
37-
RoleArn : module.iam_assumable_role_admin.iam_role_arn
41+
RoleArn : module.iam_assumable_role_admin.iam_role_arn,
42+
Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace
3843
})
3944
depends_on = [
40-
module.iam_assumable_role_admin
45+
module.iam_assumable_role_admin,
46+
aws_eks_fargate_profile.test_profile
4147
]
4248
}
4349

@@ -46,14 +52,16 @@ resource "kubectl_manifest" "cluster_role" {
4652
}
4753

4854
resource "kubectl_manifest" "cluster_role_binding" {
49-
yaml_body = var.deployment_type == "fargate" ? file("./container-insights-agent/cluster_role_binding_fargate.yml") : data.template_file.cluster_role_binding_file.rendered
55+
yaml_body = var.deployment_type == "fargate" ? templatefile("./container-insights-agent/cluster_role_binding_fargate.yml", { Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace }) : data.template_file.cluster_role_binding_file.rendered
5056
depends_on = [
51-
kubectl_manifest.cluster_role
57+
kubectl_manifest.cluster_role,
58+
aws_eks_fargate_profile.test_profile
5259
]
5360
}
5461

5562
resource "kubectl_manifest" "config_map" {
56-
yaml_body = var.deployment_type == "fargate" ? file("./container-insights-agent/config_map_fargate.yml") : data.template_file.config_map_file.rendered
63+
yaml_body = var.deployment_type == "fargate" ? templatefile("./container-insights-agent/config_map_fargate.yml", { Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace }) : data.template_file.config_map_file.rendered
64+
depends_on = [aws_eks_fargate_profile.test_profile]
5765
}
5866

5967
resource "kubectl_manifest" "daemonset" {
@@ -67,25 +75,28 @@ resource "kubectl_manifest" "daemonset" {
6775

6876
resource "kubectl_manifest" "aoc_service_deploy" {
6977
count = var.aoc_base_scenario == "infra" && var.deployment_type == "fargate" ? 1 : 0
70-
yaml_body = file("./container-insights-agent/aoc_service_fargate.yml")
78+
yaml_body = templatefile("./container-insights-agent/aoc_service_fargate.yml", { Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace })
7179
depends_on = [
72-
kubectl_manifest.config_map
80+
kubectl_manifest.config_map,
81+
aws_eks_fargate_profile.test_profile
7382
]
7483
}
7584

7685
resource "kubectl_manifest" "aoc_fargate_deploy" {
7786
count = var.aoc_base_scenario == "infra" && var.deployment_type == "fargate" ? 1 : 0
78-
yaml_body = templatefile("./container-insights-agent/stateful_set_fargate.tpl",
79-
{ ClusterName : var.eks_cluster_name, AocRepo : var.aoc_image_repo, AocTag : var.aoc_version })
87+
yaml_body = templatefile("./container-insights-agent/stateful_set_fargate.yml",
88+
{ ClusterName : var.eks_cluster_name, AocRepo : var.aoc_image_repo, AocTag : var.aoc_version, Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace })
8089
depends_on = [
81-
kubectl_manifest.aoc_service_deploy
90+
kubectl_manifest.aoc_service_deploy,
91+
aws_eks_fargate_profile.test_profile
8292
]
8393
}
8494

8595
resource "kubectl_manifest" "logs_sample_fargate_deploy" {
8696
count = var.aoc_base_scenario == "infra" && var.deployment_type == "fargate" ? 1 : 0
87-
yaml_body = file("./container-insights-agent/logs_sample_fargate.yml")
97+
yaml_body = templatefile("./container-insights-agent/logs_sample_fargate.yml", { Namespace : tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace })
8898
depends_on = [
89-
kubectl_manifest.aoc_fargate_deploy
99+
kubectl_manifest.aoc_fargate_deploy,
100+
aws_eks_fargate_profile.test_profile
90101
]
91102
}

terraform/eks/main.tf

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,54 @@ resource "kubernetes_namespace" "aoc_ns" {
102102
}
103103
}
104104

105+
# create a unique fargate namespace for each run
106+
resource "kubernetes_namespace" "aoc_fargate_ns" {
107+
metadata {
108+
name = "aoc-fargate-ns-${module.common.testing_id}"
109+
}
110+
}
111+
112+
resource "aws_iam_role" "fargate_profile_file" {
113+
name = "fargate-profile-role-${module.common.testing_id}"
114+
managed_policy_arns = ["arn:aws:iam::aws:policy/AmazonEKSFargatePodExecutionRolePolicy"]
115+
116+
# Terraform's "jsonencode" function converts a
117+
# Terraform expression result to valid JSON syntax.
118+
assume_role_policy = jsonencode({
119+
Version = "2012-10-17"
120+
Statement = [
121+
{
122+
Action = "sts:AssumeRole"
123+
Effect = "Allow"
124+
Principal = {
125+
Service = "eks-fargate-pods.amazonaws.com"
126+
}
127+
},
128+
]
129+
})
130+
}
131+
132+
data "aws_subnet_ids" "private_subnets" {
133+
vpc_id = data.aws_eks_cluster.testing_cluster.vpc_config[0].vpc_id
134+
filter {
135+
name = "mapPublicIpOnLaunch"
136+
values = ["false"] # insert values here
137+
}
138+
}
139+
140+
resource "aws_eks_fargate_profile" "test_profile" {
141+
cluster_name = var.eks_cluster_name
142+
fargate_profile_name = "fp-aoc-${module.common.testing_id}"
143+
pod_execution_role_arn = aws_iam_role.fargate_profile_file.arn
144+
subnet_ids = data.aws_subnet_ids.private_subnets.ids
145+
146+
selector {
147+
namespace = kubernetes_namespace.aoc_fargate_ns.metadata[0].name
148+
}
149+
150+
depends_on = [aws_iam_role.fargate_profile_file, kubernetes_namespace.aoc_fargate_ns]
151+
}
152+
105153
resource "kubernetes_service_account" "aoc-role" {
106154
metadata {
107155
name = "aoc-role-${module.common.testing_id}"
@@ -114,14 +162,14 @@ resource "kubernetes_service_account" "aoc-role" {
114162
resource "kubernetes_service_account" "aoc-fargate-role" {
115163
metadata {
116164
name = "aoc-fargate-role-${module.common.testing_id}"
117-
namespace = "default"
165+
namespace = tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace
118166
annotations = {
119167
"eks.amazonaws.com/role-arn" : module.iam_assumable_role_admin.iam_role_arn
120168
}
121169
}
122170

123171
automount_service_account_token = true
124-
depends_on = [module.iam_assumable_role_admin]
172+
depends_on = [module.iam_assumable_role_admin, aws_eks_fargate_profile.test_profile]
125173
}
126174

127175
module "iam_assumable_role_admin" {
@@ -152,17 +200,19 @@ resource "kubernetes_cluster_role_binding" "aoc-role-binding" {
152200
subject {
153201
kind = "ServiceAccount"
154202
name = var.deployment_type == "fargate" ? "aoc-fargate-role-${module.common.testing_id}" : "aoc-role-${module.common.testing_id}"
155-
namespace = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name
203+
namespace = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name
156204
}
205+
depends_on = [aws_eks_fargate_profile.test_profile]
157206
}
158207

159208
resource "kubernetes_service_account" "aoc-agent-role" {
160209
metadata {
161210
name = "aoc-agent-${module.common.testing_id}"
162-
namespace = var.deployment_type == "fargate" ? "default" : kubernetes_namespace.aoc_ns.metadata[0].name
211+
namespace = var.deployment_type == "fargate" ? tolist(aws_eks_fargate_profile.test_profile.selector)[0].namespace : kubernetes_namespace.aoc_ns.metadata[0].name
163212
}
164213

165214
automount_service_account_token = true
215+
depends_on = [aws_eks_fargate_profile.test_profile]
166216
}
167217

168218
module "adot_operator" {
@@ -222,5 +272,6 @@ module "validator" {
222272

223273
depends_on = [
224274
module.aoc_oltp,
225-
module.adot_operator]
275+
module.adot_operator,
276+
kubectl_manifest.logs_sample_fargate_deploy]
226277
}

0 commit comments

Comments
 (0)