-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.tf
164 lines (138 loc) · 5.08 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
resource "random_pet" "current" {
prefix = "tf-jx"
separator = "-"
keepers = {
# Keep the name consistent on executions
cluster_name = var.cluster_name
}
}
locals {
cluster_name = var.cluster_name != "" ? var.cluster_name : random_pet.current.id
}
provider "aws" {
region = var.region
}
data "aws_availability_zones" "available" {}
// This will create a vpc using the official vpc module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.9.0"
name = var.vpc_name
cidr = var.vpc_cidr_block
azs = data.aws_availability_zones.available.names
public_subnets = var.public_subnets
private_subnets = var.private_subnets
enable_dns_hostnames = true
enable_nat_gateway = var.enable_nat_gateway
single_nat_gateway = var.single_nat_gateway
tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
}
public_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/elb" = "1"
}
private_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}
}
// This will create the eks cluster using the official eks module
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "20.20.0"
cluster_name = local.cluster_name
cluster_version = var.cluster_version
subnet_ids = (var.cluster_in_private_subnet ? module.vpc.private_subnets : module.vpc.public_subnets)
vpc_id = module.vpc.vpc_id
enable_irsa = true
# Cluster access entry
# To add the current caller identity as an administrator
enable_cluster_creator_admin_permissions = true
eks_managed_node_groups = {
eks-jx-node-group = {
ami_type = var.ami_type
desired_size = 3
min_size = 2
max_size = 5
instance_types = [var.node_machine_type]
k8s_labels = {
"jenkins-x.io/name" = local.cluster_name
"jenkins-x.io/part-of" = "jx-platform"
"jenkins-x.io/managed-by" = "terraform"
}
}
}
cluster_addons = {
coredns = {}
eks-pod-identity-agent = {}
kube-proxy = {}
vpc-cni = {}
}
cluster_endpoint_private_access = var.cluster_endpoint_private_access
cluster_endpoint_public_access = var.cluster_endpoint_public_access
}
module "iam-assumable-role-ebs-csi" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "~> v3.8.0"
create_role = true
role_name = "${module.eks.cluster_name}-ebs-csi"
provider_url = module.eks.cluster_oidc_issuer_url
role_policy_arns = ["arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"]
oidc_fully_qualified_subjects = ["system:serviceaccount:kube-system:ebs-csi-controller-sa"]
}
data "aws_eks_addon_version" "ebs-csi" {
addon_name = "aws-ebs-csi-driver"
kubernetes_version = module.eks.cluster_version
most_recent = false
}
resource "aws_eks_addon" "ebs-csi" {
cluster_name = module.eks.cluster_name
addon_name = "aws-ebs-csi-driver"
service_account_role_arn = module.iam-assumable-role-ebs-csi.this_iam_role_arn
addon_version = data.aws_eks_addon_version.ebs-csi.version
configuration_values = jsonencode({
defaultStorageClass = {
enabled = true
}
})
resolve_conflicts_on_create = "OVERWRITE"
resolve_conflicts_on_update = "OVERWRITE"
}
provider "helm" {
kubernetes {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
}
}
}
provider "kubernetes" {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
}
}
// The VPC and EKS resources have been created, just install the cloud resources required by jx
module "eks-jx" {
source = "github.com/jenkins-x/terraform-aws-eks-jx?ref=v4.1.0"
region = var.region
use_asm = var.use_asm
create_asm_role = var.use_asm
use_vault = !var.use_asm
jx_git_url = var.jx_git_url
jx_bot_username = var.jx_bot_username
jx_bot_token = var.jx_bot_token
nginx_chart_version = var.nginx_chart_version
force_destroy = var.force_destroy
cluster_name = module.eks.cluster_name
cluster_oidc_issuer_url = module.eks.cluster_oidc_issuer_url
}