-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
232 lines (193 loc) · 5.96 KB
/
main.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
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
// EKS Tools configuration
data "aws_region" "current" {}
data "aws_eks_cluster" "eks-cluster" {
name = var.eks_cluster_name
}
locals {
oidc = {
url = replace(var.iam_oidc_provider_url, "https://", "")
arn = var.iam_oidc_provider_arn
}
}
// AWS Load Balancer Controller Installation
module "alb_controller" {
source = "Young-ook/eks/aws//modules/lb-controller"
version = "1.7.10"
oidc = local.oidc
helm = {
vars = {
clusterName = var.eks_cluster_name
}
}
}
// AWS External DNS Ingress Controller Installation
module "external_dns" {
source = "lablabs/eks-external-dns/aws"
version = "1.1.1"
cluster_identity_oidc_issuer = data.aws_eks_cluster.eks-cluster.identity[0].oidc[0].issuer
cluster_identity_oidc_issuer_arn = var.iam_oidc_provider_arn
settings = {
"domainFilters[0]" = var.domain
}
}
// AWS Container Inshights Installation
module "container-insights" {
source = "Young-ook/eks/aws//modules/container-insights"
version = "1.7.10"
cluster_name = data.aws_eks_cluster.eks-cluster.name
oidc = local.oidc
features = {
enable_metrics = var.enable-metrics
enable_logs = var.enable-logs
}
}
// Metrics server Installation (for Horizontal Pod Autoscaler)
module "metrics-server" {
source = "Young-ook/eks/aws//modules/metrics-server"
version = "1.7.10"
oidc = local.oidc
helm = {
repository = "https://kubernetes-sigs.github.io/metrics-server/"
version = "3.8.2"
}
}
// Karpenter Installation
// Inspired from: https://karpenter.sh/v0.22.1/getting-started/getting-started-with-terraform/
module "karpenter" {
source = "terraform-aws-modules/eks/aws//modules/karpenter"
version = "19.5.1"
cluster_name = var.eks_cluster_name
irsa_oidc_provider_arn = var.iam_oidc_provider_arn
irsa_namespace_service_accounts = ["karpenter:karpenter"]
# Since Karpenter is running on an EKS Managed Node group,
# we can re-use the role that was created for the node group
create_iam_role = false
iam_role_arn = var.eks-node-group-iam-role-arn
create_irsa = true
# due to names too long we need to provide short ones
irsa_name = substr("KarpenterIRSA-${var.eks_cluster_name}", 0, 37)
}
provider "aws" {
region = "us-east-1"
alias = "virginia"
}
data "aws_ecrpublic_authorization_token" "token" {
provider = aws.virginia
}
resource "helm_release" "karpenter" {
namespace = "karpenter"
create_namespace = true
name = "karpenter"
repository = "oci://public.ecr.aws/karpenter"
repository_username = data.aws_ecrpublic_authorization_token.token.user_name
repository_password = data.aws_ecrpublic_authorization_token.token.password
chart = "karpenter"
version = "v0.24.0"
set {
name = "settings.aws.clusterName"
value = var.eks_cluster_name
}
set {
name = "settings.aws.clusterEndpoint"
value = data.aws_eks_cluster.eks-cluster.endpoint
}
set {
name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = module.karpenter.irsa_arn
}
set {
name = "settings.aws.defaultInstanceProfile"
value = module.karpenter.instance_profile_name
}
set {
name = "settings.aws.interruptionQueueName"
value = module.karpenter.queue_name
}
}
# Workaround - https://github.com/hashicorp/terraform-provider-kubernetes/issues/1380#issuecomment-967022975
# Use all instance types as fallback
resource "kubectl_manifest" "karpenter_provisioner" {
yaml_body = <<-YAML
apiVersion: karpenter.sh/v1alpha5
kind: Provisioner
metadata:
name: default
spec:
consolidation:
enabled: true
weight: 1
ttlSecondsUntilExpired: 604800
requirements:
- key: karpenter.k8s.aws/instance-category
operator: In
values: ["t", "c", "m", "r"]
# Blocklist for instance types that give us problems in the past
- key: node.kubernetes.io/instance-type
operator: NotIn
values: ["m1.small"]
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
- key: kubernetes.io/arch
operator: In
values: ["amd64"]
limits:
resources:
cpu: 64
provider:
subnetSelector:
karpenter.sh/discovery: "true"
securityGroupSelector:
aws-ids: ${data.aws_eks_cluster.eks-cluster.vpc_config[0].cluster_security_group_id}
tags:
karpenter.sh/discovery: ${var.eks_cluster_name}
YAML
depends_on = [
helm_release.karpenter
]
}
# Workaround - https://github.com/hashicorp/terraform-provider-kubernetes/issues/1380#issuecomment-967022975
# provisioner with cheapest instance types
resource "kubectl_manifest" "karpenter_provisioner_cheap" {
yaml_body = <<-YAML
apiVersion: karpenter.sh/v1alpha5
kind: Provisioner
metadata:
name: cheap-instances
spec:
consolidation:
enabled: true
weight: 100
ttlSecondsUntilExpired: 604800
requirements:
- key: karpenter.k8s.aws/instance-category
operator: In
values: ["t", "c", "m", "r"]
- key: karpenter.k8s.aws/instance-size
operator: In
values: ["nano", "micro", "small", "medium"]
# Blocklist for instance types that give us problems in the past
- key: node.kubernetes.io/instance-type
operator: NotIn
values: ["m1.small"]
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
- key: kubernetes.io/arch
operator: In
values: ["amd64"]
limits:
resources:
cpu: 64
provider:
subnetSelector:
karpenter.sh/discovery: "true"
securityGroupSelector:
aws-ids: ${data.aws_eks_cluster.eks-cluster.vpc_config[0].cluster_security_group_id}
tags:
karpenter.sh/discovery: ${var.eks_cluster_name}
YAML
depends_on = [
helm_release.karpenter
]
}