-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathclient.tf
More file actions
217 lines (180 loc) · 6.34 KB
/
Copy pathclient.tf
File metadata and controls
217 lines (180 loc) · 6.34 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
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
locals {
kubeconfig = replace(
talos_cluster_kubeconfig.this.kubeconfig_raw,
"/(\\s+server:).*/",
"$1 ${local.kube_api_url_external}"
)
talosconfig = data.talos_client_configuration.this.talos_config
kubeconfig_data = {
name = var.cluster_name
server = local.kube_api_url_external
ca = base64decode(talos_cluster_kubeconfig.this.kubernetes_client_configuration.ca_certificate)
cert = base64decode(talos_cluster_kubeconfig.this.kubernetes_client_configuration.client_certificate)
key = base64decode(talos_cluster_kubeconfig.this.kubernetes_client_configuration.client_key)
}
talosconfig_data = {
name = data.talos_client_configuration.this.cluster_name
endpoints = data.talos_client_configuration.this.endpoints
ca = base64decode(data.talos_client_configuration.this.client_configuration.ca_certificate)
cert = base64decode(data.talos_client_configuration.this.client_configuration.client_certificate)
key = base64decode(data.talos_client_configuration.this.client_configuration.client_key)
}
}
data "talos_client_configuration" "this" {
cluster_name = var.cluster_name
client_configuration = talos_machine_secrets.this.client_configuration
endpoints = local.talos_endpoints
nodes = [local.talos_primary_node_private_ipv4]
}
resource "talos_cluster_kubeconfig" "this" {
client_configuration = talos_machine_secrets.this.client_configuration
node = local.talos_primary_endpoint
depends_on = [talos_machine_configuration_apply.control_plane]
}
resource "terraform_data" "create_talosconfig" {
count = var.cluster_talosconfig_path != null ? 1 : 0
triggers_replace = [
nonsensitive(sha1(local.talosconfig)),
var.cluster_talosconfig_path
]
input = {
cluster_talosconfig_path = var.cluster_talosconfig_path
}
provisioner "local-exec" {
when = create
quiet = true
command = <<-EOT
set -eu
printf '%s' "$TALOSCONFIG_CONTENT" > "$CLUSTER_TALOSCONFIG_PATH"
EOT
environment = {
TALOSCONFIG_CONTENT = local.talosconfig
CLUSTER_TALOSCONFIG_PATH = var.cluster_talosconfig_path
}
}
provisioner "local-exec" {
when = destroy
quiet = true
on_failure = continue
command = <<-EOT
set -eu
if [ -f "$CLUSTER_TALOSCONFIG_PATH" ]; then
cp -f "$CLUSTER_TALOSCONFIG_PATH" "$CLUSTER_TALOSCONFIG_PATH.bak"
fi
EOT
environment = {
CLUSTER_TALOSCONFIG_PATH = self.input.cluster_talosconfig_path
}
}
depends_on = [talos_machine_configuration_apply.control_plane]
}
resource "terraform_data" "create_kubeconfig" {
count = var.cluster_kubeconfig_path != null ? 1 : 0
triggers_replace = [
nonsensitive(sha1(local.kubeconfig)),
var.cluster_kubeconfig_path
]
input = {
cluster_kubeconfig_path = var.cluster_kubeconfig_path
}
provisioner "local-exec" {
when = create
quiet = true
command = <<-EOT
set -eu
printf '%s' "$KUBECONFIG_CONTENT" > "$CLUSTER_KUBECONFIG_PATH"
EOT
environment = {
KUBECONFIG_CONTENT = local.kubeconfig
CLUSTER_KUBECONFIG_PATH = var.cluster_kubeconfig_path
}
}
provisioner "local-exec" {
when = destroy
quiet = true
on_failure = continue
command = <<-EOT
set -eu
if [ -f "$CLUSTER_KUBECONFIG_PATH" ]; then
cp -f "$CLUSTER_KUBECONFIG_PATH" "$CLUSTER_KUBECONFIG_PATH.bak"
fi
EOT
environment = {
CLUSTER_KUBECONFIG_PATH = self.input.cluster_kubeconfig_path
}
}
depends_on = [talos_machine_configuration_apply.control_plane]
}
data "external" "client_prerequisites_check" {
count = var.client_prerequisites_check_enabled ? 1 : 0
program = [
"sh", "-c", <<-EOT
set -eu
missing=0
if [ "${local.talos_cloud_image_build_required}" = "true" ] && ! command -v packer >/dev/null 2>&1; then
printf '\n%s' ' - packer is not installed or not in PATH. Install it at https://developer.hashicorp.com/packer/install' >&2
missing=1
fi
if ! command -v curl >/dev/null 2>&1; then
printf '\n%s' ' - curl is not installed or not in PATH. Install it at https://curl.se/download.html' >&2
missing=1
fi
if ! command -v jq >/dev/null 2>&1; then
printf '\n%s' ' - jq is not installed or not in PATH. Install it at https://jqlang.org/download/' >&2
missing=1
fi
if ! command -v talosctl >/dev/null 2>&1; then
printf '\n%s' ' - talosctl is not installed or not in PATH. Install it at https://www.talos.dev/latest/talos-guides/install/talosctl' >&2
missing=1
fi
printf '%s' '{}'
exit "$missing"
EOT
]
}
data "external" "talosctl_version_check" {
count = var.talosctl_version_check_enabled ? 1 : 0
program = [
"sh", "-c", <<-EOT
set -eu
parse() {
case $1 in
*[vV][0-9]*.[0-9]*.[0-9]*)
v=$${1##*[vV]}
maj=$${v%%.*}
r=$${v#*.}
min=$${r%%.*}
patch=$${r#*.}
patch=$${patch%%[!0-9]*}
printf '%s %s %s\n' "$maj" "$min" "$patch"
return 0
;;
esac
return 1
}
parsed_version=$(
talosctl version --client --short |
while IFS= read -r line; do
if out=$(parse "$line"); then
printf '%s\n' "$out"
break
fi
done
)
if [ -z "$parsed_version" ]; then
printf '%s\n' "Could not parse talosctl client version" >&2
exit 1
fi
set -- $parsed_version; major=$1; minor=$2; patch=$3
if [ "$major" -lt "${local.talos_version_major}" ] ||
{ [ "$major" -eq "${local.talos_version_major}" ] && [ "$minor" -lt "${local.talos_version_minor}" ]; } ||
{ [ "$major" -eq "${local.talos_version_major}" ] && [ "$minor" -eq "${local.talos_version_minor}" ] && [ "$patch" -lt "${local.talos_version_patch}" ]; }
then
printf '%s\n' "talosctl version ($major.$minor.$patch) is lower than Talos target version: ${local.talos_version_major}.${local.talos_version_minor}.${local.talos_version_patch}" >&2
exit 1
fi
printf '%s' "{\"talosctl_version\": \"$major.$minor.$patch\"}"
EOT
]
depends_on = [data.external.client_prerequisites_check]
}