|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +module "common" { |
| 5 | + source = "../../../common" |
| 6 | + cwagent_image_repo = var.cwagent_image_repo |
| 7 | + cwagent_image_tag = var.cwagent_image_tag |
| 8 | +} |
| 9 | + |
| 10 | +module "basic_components" { |
| 11 | + source = "../../../basic_components" |
| 12 | + region = var.region |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +data "aws_eks_cluster_auth" "this" { |
| 17 | + name = aws_eks_cluster.this.name |
| 18 | +} |
| 19 | + |
| 20 | +locals { |
| 21 | + aws_eks = "aws eks --region ${var.region}" |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_eks_cluster" "this" { |
| 25 | + name = "cwagent-eks-integ-${module.common.testing_id}" |
| 26 | + role_arn = module.basic_components.role_arn |
| 27 | + version = var.k8s_version |
| 28 | + vpc_config { |
| 29 | + subnet_ids = module.basic_components.public_subnet_ids |
| 30 | + security_group_ids = [module.basic_components.security_group] |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +# EKS Node Groups |
| 35 | +resource "aws_eks_node_group" "this" { |
| 36 | + cluster_name = aws_eks_cluster.this.name |
| 37 | + node_group_name = "cwagent-addon-eks-integ-node-${module.common.testing_id}" |
| 38 | + node_role_arn = aws_iam_role.node_role.arn |
| 39 | + subnet_ids = module.basic_components.public_subnet_ids |
| 40 | + |
| 41 | + scaling_config { |
| 42 | + desired_size = 1 |
| 43 | + max_size = 1 |
| 44 | + min_size = 1 |
| 45 | + } |
| 46 | + |
| 47 | + ami_type = var.ami_type |
| 48 | + capacity_type = "ON_DEMAND" |
| 49 | + disk_size = 20 |
| 50 | + instance_types = [var.instance_type] |
| 51 | + |
| 52 | + depends_on = [ |
| 53 | + aws_iam_role_policy_attachment.node_AmazonEC2ContainerRegistryReadOnly, |
| 54 | + aws_iam_role_policy_attachment.node_AmazonEKS_CNI_Policy, |
| 55 | + aws_iam_role_policy_attachment.node_AmazonEKSWorkerNodePolicy, |
| 56 | + aws_iam_role_policy_attachment.node_CloudWatchAgentServerPolicy |
| 57 | + ] |
| 58 | +} |
| 59 | + |
| 60 | +# EKS Node IAM Role |
| 61 | +resource "aws_iam_role" "node_role" { |
| 62 | + name = "cwagent-addon-eks-Worker-Role-${module.common.testing_id}" |
| 63 | + |
| 64 | + assume_role_policy = <<POLICY |
| 65 | +{ |
| 66 | + "Version": "2012-10-17", |
| 67 | + "Statement": [ |
| 68 | + { |
| 69 | + "Effect": "Allow", |
| 70 | + "Principal": { |
| 71 | + "Service": "ec2.amazonaws.com" |
| 72 | + }, |
| 73 | + "Action": "sts:AssumeRole" |
| 74 | + } |
| 75 | + ] |
| 76 | +} |
| 77 | +POLICY |
| 78 | +} |
| 79 | + |
| 80 | +resource "aws_iam_role_policy_attachment" "node_AmazonEKSWorkerNodePolicy" { |
| 81 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" |
| 82 | + role = aws_iam_role.node_role.name |
| 83 | +} |
| 84 | + |
| 85 | +resource "aws_iam_role_policy_attachment" "node_AmazonEKS_CNI_Policy" { |
| 86 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" |
| 87 | + role = aws_iam_role.node_role.name |
| 88 | +} |
| 89 | + |
| 90 | +resource "aws_iam_role_policy_attachment" "node_AmazonEC2ContainerRegistryReadOnly" { |
| 91 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" |
| 92 | + role = aws_iam_role.node_role.name |
| 93 | +} |
| 94 | + |
| 95 | +resource "aws_iam_role_policy_attachment" "node_CloudWatchAgentServerPolicy" { |
| 96 | + policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy" |
| 97 | + role = aws_iam_role.node_role.name |
| 98 | +} |
| 99 | + |
| 100 | +resource "aws_iam_role_policy_attachment" "node_AmazonEBSCSIDriverPolicy" { |
| 101 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy" |
| 102 | + role = aws_iam_role.node_role.name |
| 103 | +} |
| 104 | + |
| 105 | +resource "null_resource" "kubectl" { |
| 106 | + depends_on = [ |
| 107 | + aws_eks_cluster.this, |
| 108 | + aws_eks_node_group.this |
| 109 | + ] |
| 110 | + provisioner "local-exec" { |
| 111 | + command = <<-EOT |
| 112 | + ${local.aws_eks} update-kubeconfig --name ${aws_eks_cluster.this.name} |
| 113 | + ${local.aws_eks} list-clusters --output text |
| 114 | + ${local.aws_eks} describe-cluster --name ${aws_eks_cluster.this.name} --output text |
| 115 | + EOT |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +resource "aws_eks_addon" "ebs_csi_addon" { |
| 120 | + depends_on = [aws_eks_node_group.this] |
| 121 | + cluster_name = aws_eks_cluster.this.name |
| 122 | + addon_name = "aws-ebs-csi-driver" |
| 123 | + configuration_values = jsonencode({ |
| 124 | + node = { |
| 125 | + enableMetrics = true |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +resource "null_resource" "clone_helm_chart" { |
| 131 | + triggers = { |
| 132 | + timestamp = "${timestamp()}" # Forces re-run on every apply |
| 133 | + } |
| 134 | + provisioner "local-exec" { |
| 135 | + command = <<-EOT |
| 136 | + if [ ! -d "./helm-charts" ]; then |
| 137 | + git clone -b ${var.helm_chart_branch} https://github.com/aws-observability/helm-charts.git ./helm-charts |
| 138 | + fi |
| 139 | + EOT |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +resource "helm_release" "aws_observability" { |
| 144 | + name = "amazon-cloudwatch-observability" |
| 145 | + chart = "./helm-charts/charts/amazon-cloudwatch-observability" |
| 146 | + namespace = "amazon-cloudwatch" |
| 147 | + create_namespace = true |
| 148 | + |
| 149 | + set { |
| 150 | + name = "clusterName" |
| 151 | + value = aws_eks_cluster.this.name |
| 152 | + } |
| 153 | + |
| 154 | + set { |
| 155 | + name = "region" |
| 156 | + value = "us-west-2" |
| 157 | + } |
| 158 | + depends_on = [ |
| 159 | + aws_eks_cluster.this, |
| 160 | + aws_eks_node_group.this, |
| 161 | + null_resource.clone_helm_chart, |
| 162 | + ] |
| 163 | +} |
| 164 | + |
| 165 | +resource "null_resource" "update_image" { |
| 166 | + depends_on = [helm_release.aws_observability, null_resource.kubectl] |
| 167 | + triggers = { |
| 168 | + timestamp = "${timestamp()}" # Forces re-run on every apply |
| 169 | + } |
| 170 | + provisioner "local-exec" { |
| 171 | + command = <<-EOT |
| 172 | + 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}"}]' |
| 173 | + sleep 10 |
| 174 | + EOT |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +resource "kubernetes_storage_class" "ebs_sc" { |
| 179 | + depends_on = [aws_eks_addon.ebs_csi_addon] |
| 180 | + metadata { |
| 181 | + name = "ebs-sc-${module.common.testing_id}" |
| 182 | + } |
| 183 | + |
| 184 | + storage_provisioner = "ebs.csi.aws.com" |
| 185 | + volume_binding_mode = "WaitForFirstConsumer" |
| 186 | + |
| 187 | + parameters = { |
| 188 | + type = "gp3" |
| 189 | + fsType = "ext4" |
| 190 | + encrypted = "true" |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +resource "kubernetes_deployment" "ebs_deployment" { |
| 195 | + depends_on = [kubernetes_storage_class.ebs_sc] |
| 196 | + metadata { |
| 197 | + name = "app" |
| 198 | + } |
| 199 | + |
| 200 | + spec { |
| 201 | + replicas = 1 |
| 202 | + |
| 203 | + selector { |
| 204 | + match_labels = { |
| 205 | + app = "app" |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + template { |
| 210 | + metadata { |
| 211 | + labels = { |
| 212 | + app = "app" |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + spec { |
| 217 | + container { |
| 218 | + name = "app" |
| 219 | + image = "public.ecr.aws/amazonlinux/amazonlinux" |
| 220 | + command = ["/bin/bash", "-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"] |
| 221 | + |
| 222 | + volume_mount { |
| 223 | + name = "persistent-storage" |
| 224 | + mount_path = "/data" |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + volume { |
| 229 | + name = "persistent-storage" |
| 230 | + ephemeral { |
| 231 | + volume_claim_template { |
| 232 | + spec { |
| 233 | + access_modes = ["ReadWriteOnce"] |
| 234 | + storage_class_name = kubernetes_storage_class.ebs_sc.metadata[0].name |
| 235 | + resources { |
| 236 | + requests = { |
| 237 | + storage = "5Gi" |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +# Get the single instance ID of the node in the node group |
| 250 | +data "aws_instances" "eks_node" { |
| 251 | + depends_on = [ |
| 252 | + aws_eks_node_group.this |
| 253 | + ] |
| 254 | + filter { |
| 255 | + name = "tag:eks:nodegroup-name" |
| 256 | + values = [aws_eks_node_group.this.node_group_name] |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +# Retrieve details of the single instance to get private DNS |
| 261 | +data "aws_instance" "eks_node_detail" { |
| 262 | + depends_on = [ |
| 263 | + data.aws_instances.eks_node |
| 264 | + ] |
| 265 | + instance_id = data.aws_instances.eks_node.ids[0] |
| 266 | +} |
| 267 | + |
| 268 | +resource "null_resource" "validator" { |
| 269 | + depends_on = [ |
| 270 | + aws_eks_node_group.this, |
| 271 | + aws_eks_addon.ebs_csi_addon, |
| 272 | + helm_release.aws_observability, |
| 273 | + null_resource.update_image, |
| 274 | + kubernetes_deployment.ebs_deployment, |
| 275 | + ] |
| 276 | + |
| 277 | + triggers = { |
| 278 | + always_run = timestamp() |
| 279 | + } |
| 280 | + |
| 281 | + provisioner "local-exec" { |
| 282 | + command = <<-EOT |
| 283 | + echo "Validating CloudWatch Agent with EBS CSI NVMe metrics" |
| 284 | + cd ../../../.. |
| 285 | + go test ${var.test_dir} -timeout 1h -eksClusterName=${aws_eks_cluster.this.name} -computeType=EKS -v -eksDeploymentStrategy=DAEMON -instanceId=${data.aws_instance.eks_node_detail.instance_id} |
| 286 | + EOT |
| 287 | + } |
| 288 | +} |
| 289 | + |
0 commit comments