This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
181 lines (149 loc) · 4.83 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
/*
Copyright 2019 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
provider "hcloud" {}
locals {
kubeapi_endpoint = var.disable_kubeapi_loadbalancer ? hcloud_server_network.control_plane.0.ip : hcloud_load_balancer.load_balancer.0.ipv4
loadbalancer_count = var.disable_kubeapi_loadbalancer ? 0 : 1
image = var.image == "" ? var.image_references[var.os].image_name : var.image
worker_os = var.worker_os == "" ? var.image_references[var.os].worker_os : var.worker_os
ssh_username = var.ssh_username == "" ? var.image_references[var.os].ssh_username : var.ssh_username
cluster_autoscaler_min_replicas = var.cluster_autoscaler_min_replicas > 0 ? var.cluster_autoscaler_min_replicas : var.initial_machinedeployment_replicas
cluster_autoscaler_max_replicas = var.cluster_autoscaler_max_replicas > 0 ? var.cluster_autoscaler_max_replicas : var.initial_machinedeployment_replicas
}
resource "hcloud_ssh_key" "kubeone" {
name = "kubeone-${var.cluster_name}"
public_key = file(var.ssh_public_key_file)
}
resource "hcloud_network" "net" {
name = var.cluster_name
ip_range = var.ip_range
}
resource "hcloud_firewall" "cluster" {
name = "${var.cluster_name}-fw"
labels = {
"kubeone_cluster_name" = var.cluster_name
}
apply_to {
label_selector = "kubeone_cluster_name=${var.cluster_name}"
}
rule {
description = "allow ICMP"
direction = "in"
protocol = "icmp"
source_ips = [
"0.0.0.0/0",
]
}
rule {
description = "allow all TCP inside cluster"
direction = "in"
protocol = "tcp"
port = "any"
source_ips = [
var.ip_range,
]
}
rule {
description = "allow all UDP inside cluster"
direction = "in"
protocol = "udp"
port = "any"
source_ips = [
var.ip_range,
]
}
rule {
description = "allow SSH from any"
direction = "in"
protocol = "tcp"
port = "22"
source_ips = [
"0.0.0.0/0",
]
}
rule {
description = "allow NodePorts from any"
direction = "in"
protocol = "tcp"
port = "30000-32767"
source_ips = [
"0.0.0.0/0",
]
}
}
resource "hcloud_network_subnet" "kubeone" {
network_id = hcloud_network.net.id
type = "server"
network_zone = var.network_zone
ip_range = var.ip_range
}
resource "hcloud_server_network" "control_plane" {
count = var.control_plane_vm_count
server_id = element(hcloud_server.control_plane.*.id, count.index)
subnet_id = hcloud_network_subnet.kubeone.id
}
resource "hcloud_placement_group" "control_plane" {
name = var.cluster_name
type = "spread"
labels = {
"kubeone_cluster_name" = var.cluster_name
}
}
resource "hcloud_server" "control_plane" {
count = var.control_plane_vm_count
name = "${var.cluster_name}-control-plane-${count.index + 1}"
server_type = var.control_plane_type
image = local.image
location = var.datacenter
placement_group_id = hcloud_placement_group.control_plane.id
ssh_keys = [
hcloud_ssh_key.kubeone.id,
]
labels = {
"kubeone_cluster_name" = var.cluster_name
"role" = "api"
}
}
resource "hcloud_load_balancer_network" "load_balancer" {
count = local.loadbalancer_count
load_balancer_id = hcloud_load_balancer.load_balancer.0.id
subnet_id = hcloud_network_subnet.kubeone.id
}
resource "hcloud_load_balancer" "load_balancer" {
count = local.loadbalancer_count
name = "${var.cluster_name}-lb"
load_balancer_type = var.lb_type
location = var.datacenter
labels = {
"kubeone_cluster_name" = var.cluster_name
"role" = "lb"
}
}
resource "hcloud_load_balancer_target" "load_balancer_target" {
count = local.loadbalancer_count
type = "label_selector"
load_balancer_id = hcloud_load_balancer.load_balancer.0.id
label_selector = "kubeone_cluster_name=${var.cluster_name},role=api"
use_private_ip = true
depends_on = [
hcloud_server_network.control_plane,
hcloud_load_balancer_network.load_balancer
]
}
resource "hcloud_load_balancer_service" "load_balancer_service" {
count = local.loadbalancer_count
load_balancer_id = hcloud_load_balancer.load_balancer.0.id
protocol = "tcp"
listen_port = 6443
destination_port = 6443
}