-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcluster.tf
198 lines (167 loc) · 5.16 KB
/
cluster.tf
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
locals {
# Encryption
cluster_encryption_config = {
resources = var.cluster_encryption_config
provider_key_arn = aws_kms_key.cluster_secrets.arn
}
}
resource "aws_eks_cluster" "cluster" {
name = var.environment_name
role_arn = aws_iam_role.cluster.arn
version = var.cluster_version
vpc_config {
endpoint_private_access = var.cluster_private_access
endpoint_public_access = var.cluster_public_access
public_access_cidrs = var.cluster_public_access_cidrs
security_group_ids = [aws_security_group.cluster.id]
subnet_ids = flatten([
aws_subnet.public.*.id,
aws_subnet.private.*.id,
])
}
access_config {
authentication_mode = var.cluster_authentication_mode
}
dynamic "encryption_config" {
for_each = [local.cluster_encryption_config]
content {
resources = lookup(encryption_config.value, "resources")
provider {
key_arn = lookup(encryption_config.value, "provider_key_arn")
}
}
}
# Compute Config (conditional setup for Auto Mode)
dynamic "compute_config" {
for_each = var.eks_auto_mode_enabled ? [1] : []
content {
enabled = true
node_pools = ["system"]
node_role_arn = aws_iam_role.node.arn
}
}
# Kubernetes Network Config (Auto Mode specific)
dynamic "kubernetes_network_config" {
for_each = var.eks_auto_mode_enabled ? [1] : []
content {
elastic_load_balancing {
enabled = true
}
}
}
# Storage Config (Auto Mode specific)
dynamic "storage_config" {
for_each = var.eks_auto_mode_enabled ? [1] : []
content {
block_storage {
enabled = true
}
}
}
enabled_cluster_log_types = var.cluster_logging
depends_on = [
aws_iam_role_policy_attachment.cluster-AmazonEKSClusterPolicy,
aws_iam_role_policy_attachment.cluster-AmazonEKSServicePolicy,
aws_iam_role_policy_attachment.cluster_AmazonEKSComputePolicy,
aws_iam_role_policy_attachment.cluster_AmazonEKSBlockStoragePolicy,
aws_iam_role_policy_attachment.cluster_AmazonEKSLoadBalancingPolicy,
aws_iam_role_policy_attachment.cluster_AmazonEKSNetworkingPolicy,
]
tags = local.tags
}
resource "aws_eks_addon" "core" {
for_each = toset(flatten([
"kube-proxy",
"vpc-cni",
"coredns",
"aws-ebs-csi-driver",
var.s3_csi_driver_enabled ? ["aws-mountpoint-s3-csi-driver"] : [],
var.efs_enabled ? ["aws-efs-csi-driver"] : [],
var.cloudwatch_observability_enabled ? ["amazon-cloudwatch-observability"] : [],
]))
cluster_name = aws_eks_cluster.cluster.name
addon_name = each.key
resolve_conflicts_on_create = "OVERWRITE"
resolve_conflicts_on_update = "OVERWRITE"
depends_on = [
kubernetes_config_map.aws_auth,
aws_autoscaling_group.asg_nodes,
]
}
data "tls_certificate" "cluster" {
url = aws_eks_cluster.cluster.identity.0.oidc.0.issuer
}
resource "aws_iam_role" "cluster" {
name = "${var.environment_name}-cluster"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "eks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "cluster-AmazonEKSClusterPolicy" {
policy_arn = "arn:${local.partition}:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "cluster-AmazonEKSServicePolicy" {
policy_arn = "arn:${local.partition}:iam::aws:policy/AmazonEKSServicePolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSComputePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSComputePolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSLoadBalancingPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSLoadBalancingPolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSNetworkingPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSNetworkingPolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSBlockStoragePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSBlockStoragePolicy"
role = aws_iam_role.cluster.name
}
resource "helm_release" "calico" {
count = var.calico_enabled ? 1 : 0
name = "calico"
repository = "https://docs.tigera.io/calico/charts"
chart = "tigera-operator"
version = var.calico_version
# Set Calico-specific configuration values
set {
name = "kubernetesProvider"
value = "EKS"
}
set {
name = "cni.type"
value = "Calico"
}
set {
name = "calicoNetwork.bgp"
value = "Disabled"
}
}
resource "null_resource" "delete_aws_node" {
count = var.calico_enabled ? 1 : 0
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = <<-EOT
kubectl patch installation default --type='json' -p='[{"op": "replace", "path": "/spec/cni", "value": {"type":"Calico"} }]'
kubectl delete daemonset -n kube-system aws-node
EOT
}
}