forked from hcloud-k8s/terraform-hcloud-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoscaler.tf
More file actions
142 lines (134 loc) · 4.86 KB
/
Copy pathautoscaler.tf
File metadata and controls
142 lines (134 loc) · 4.86 KB
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
locals {
cluster_autoscaler_enabled = length(local.cluster_autoscaler_nodepools) > 0
cluster_autoscaler_hostname_pattern = "^${var.cluster_name}-(${join("|", distinct([for np in local.cluster_autoscaler_nodepools : np.name]))})-[0-9a-f]+$"
cluster_autoscaler_release_name = "cluster-autoscaler"
cluster_autoscaler_cloud_provider = "hetzner"
cluster_autoscaler_config_secret_name = "${local.cluster_autoscaler_release_name}-${local.cluster_autoscaler_cloud_provider}-config"
cluster_autoscaler_cluster_config_manifest = local.cluster_autoscaler_enabled ? {
apiVersion = "v1"
kind = "Secret"
type = "Opaque"
metadata = {
name = local.cluster_autoscaler_config_secret_name
namespace = "kube-system"
}
data = {
cluster-config = base64encode(jsonencode(
{
imagesForArch = {
arm64 = local.image_label_selector,
amd64 = local.image_label_selector
},
nodeConfigs = {
for nodepool in local.cluster_autoscaler_nodepools : "${var.cluster_name}-${nodepool.name}" => {
cloudInit = data.talos_machine_configuration.cluster_autoscaler[nodepool.name].machine_configuration,
labels = nodepool.labels
taints = nodepool.taints
}
}
}
))
}
} : null
}
data "helm_template" "cluster_autoscaler" {
count = local.cluster_autoscaler_enabled ? 1 : 0
name = local.cluster_autoscaler_release_name
namespace = "kube-system"
repository = var.cluster_autoscaler_helm_repository
chart = var.cluster_autoscaler_helm_chart
version = var.cluster_autoscaler_helm_version
kube_version = var.kubernetes_version
values = [
yamlencode({
cloudProvider = local.cluster_autoscaler_cloud_provider
replicaCount = local.control_plane_sum > 1 ? 2 : 1
podDisruptionBudget = {
minAvailable = null
maxUnavailable = 1
}
topologySpreadConstraints = [
{
topologyKey = "kubernetes.io/hostname"
maxSkew = 1
whenUnsatisfiable = "DoNotSchedule"
labelSelector = {
matchLabels = {
"app.kubernetes.io/instance" = local.cluster_autoscaler_release_name
"app.kubernetes.io/name" = "${local.cluster_autoscaler_cloud_provider}-${var.cluster_autoscaler_helm_chart}"
}
}
matchLabelKeys = ["pod-template-hash"]
},
{
topologyKey = "topology.kubernetes.io/zone"
maxSkew = 1
whenUnsatisfiable = "ScheduleAnyway"
labelSelector = {
matchLabels = {
"app.kubernetes.io/instance" = local.cluster_autoscaler_release_name
"app.kubernetes.io/name" = "${local.cluster_autoscaler_cloud_provider}-${var.cluster_autoscaler_helm_chart}"
}
}
matchLabelKeys = ["pod-template-hash"]
}
]
nodeSelector = { "node-role.kubernetes.io/control-plane" : "" }
tolerations = [
{
key = "node-role.kubernetes.io/control-plane"
effect = "NoSchedule"
operator = "Exists"
}
]
autoscalingGroups = [
for np in local.cluster_autoscaler_nodepools : {
name = "${var.cluster_name}-${np.name}"
minSize = np.min
maxSize = np.max
instanceType = np.server_type
region = np.location
}
]
image = {
tag = var.cluster_autoscaler_image_tag
}
extraEnv = {
HCLOUD_CLUSTER_CONFIG_FILE = "/config/cluster-config"
HCLOUD_SERVER_CREATION_TIMEOUT = "10"
HCLOUD_FIREWALL = tostring(local.firewall_id)
HCLOUD_SSH_KEY = tostring(hcloud_ssh_key.this.id)
HCLOUD_PUBLIC_IPV4 = tostring(var.talos_public_ipv4_enabled)
HCLOUD_PUBLIC_IPV6 = tostring(var.talos_public_ipv6_enabled)
HCLOUD_NETWORK = tostring(hcloud_network_subnet.autoscaler.network_id)
}
extraEnvSecrets = {
HCLOUD_TOKEN = {
name = "hcloud"
key = "token"
}
}
extraVolumeSecrets = {
"${local.cluster_autoscaler_config_secret_name}" = {
name = local.cluster_autoscaler_config_secret_name
mountPath = "/config"
}
}
}),
yamlencode(var.cluster_autoscaler_helm_values)
]
depends_on = [
terraform_data.amd64_image,
terraform_data.arm64_image,
]
}
locals {
cluster_autoscaler_manifest = local.cluster_autoscaler_enabled ? {
name = "cluster-autoscaler"
contents = <<-EOF
${data.helm_template.cluster_autoscaler[0].manifest}
---
${yamlencode(local.cluster_autoscaler_cluster_config_manifest)}
EOF
} : null
}