-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
161 lines (136 loc) · 3.93 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
terraform {
cloud {
organization = "jameswcurtin"
workspaces {
tags = ["digitalocean", "kubernetes"]
}
}
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "2.26.0"
}
helm = {
source = "hashicorp/helm"
version = "=2.8.0"
}
kubectl = {
source = "gavinbunney/kubectl"
version = "=1.14.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.17.0"
}
random = {
source = "hashicorp/random"
version = "= 3.4.3"
}
}
}
resource "random_id" "cluster_id" {
byte_length = 4
}
# Loadbalancer must be created before k8s cluster. If the k8s cluster is created
# first, it dynamically generates a LB and terraform is unable to access the IP.
resource "digitalocean_loadbalancer" "this" {
name = "lb-${var.region}-${random_id.cluster_id.hex}"
region = var.region
enable_proxy_protocol = true
forwarding_rule {
entry_port = 80
entry_protocol = "http"
target_port = 80
target_protocol = "http"
}
# The forwarding rules defined here are a placeholder, as they will be dynamically
# set once the k8s cluster is created. Therefore, changes made outside of TF should be ignored
lifecycle {
ignore_changes = [
forwarding_rule,
]
}
}
resource "digitalocean_kubernetes_cluster" "this" {
name = "k8s-${var.region}-${random_id.cluster_id.hex}"
region = var.region
auto_upgrade = var.auto_upgrade
surge_upgrade = var.surge_upgrade
version = data.digitalocean_kubernetes_versions.do_k8s_versions.latest_version
maintenance_policy {
start_time = var.maintenance_start_time
day = var.maintenance_day
}
node_pool {
name = "worker-pool"
size = var.node_size
auto_scale = var.node_pool_autoscales
min_nodes = var.min_nodes
max_nodes = var.max_nodes
}
depends_on = [
digitalocean_loadbalancer.this
]
# Ignoring version, which is auto-upgraded by the cluster.
lifecycle {
ignore_changes = [
version,
]
}
}
# Due to k8s limitation, pods cannot communicate via the IP of an external LB.
# Therefore, need to define an A record associated with the LP to allow for
# pod-to-pod communication (in this case necessary for cert-manager)
# See https://docs.digitalocean.com/products/kubernetes/how-to/configure-load-balancers/#accessing-by-hostname
resource "digitalocean_record" "loadbalancer_subdomain" {
domain = var.external_domain
type = "A"
name = var.loadbalancer_subdomain
value = digitalocean_loadbalancer.this.ip
ttl = 60
depends_on = [
digitalocean_loadbalancer.this
]
}
module "ingress_controller" {
source = "./modules/ingress-controller"
loadbalancer_hostname = "${var.loadbalancer_subdomain}.${var.external_domain}"
loadbalancer_id = digitalocean_loadbalancer.this.id
loadbalancer_name = "lb-${var.region}-${random_id.cluster_id.hex}"
}
module "external_dns" {
source = "./modules/external-dns"
do_token = var.do_token
external_dns_owner_id = var.external_dns_owner_id
external_domain = var.external_domain
depends_on = [
module.ingress_controller,
]
}
module "cert_automation" {
source = "./modules/cert-automation"
letsencrypt_email = var.letsencrypt_email
depends_on = [
module.ingress_controller,
]
}
# Uncomment for an example of the DNS record and TLS cert automation in action
# module "example_deployment" {
# source = "./modules/example-deployment"
# external_domain = var.external_domain
# depends_on = [
# module.external_dns,
# module.cert_automation,
# ]
# }
# Optional Services
module "ntfy" {
source = "./modules/ntfy"
external_domain = var.external_domain
deployment_subdomain = var.ntfy_subdomain
timezone = var.timezone
depends_on = [
module.external_dns,
module.cert_automation,
]
}