forked from 2i2c-org/infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.py
More file actions
337 lines (297 loc) · 12.5 KB
/
Copy pathcluster.py
File metadata and controls
337 lines (297 loc) · 12.5 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import json
import os
import subprocess
import tempfile
from contextlib import ExitStack, contextmanager
from pathlib import Path
from deployer.infra_components.hub import Hub
from deployer.utils.env_vars_management import unset_env_vars
from deployer.utils.file_acquisition import (
HELM_CHARTS_DIR,
get_decrypted_file,
get_decrypted_files,
)
from deployer.utils.helm import wait_for_deployments_daemonsets
from deployer.utils.jsonnet import render_jsonnet
from deployer.utils.rendering import print_colour
class Cluster:
"""
A single k8s cluster we can deploy to
"""
def __init__(self, spec, config_path):
self.spec = spec
self.config_path = config_path
self.hubs = [Hub(self, hub_spec) for hub_spec in self.spec.get("hubs", [])]
self.support = self.spec.get("support", {})
@contextmanager
def auth(self):
if self.spec["provider"] == "gcp":
yield from self.auth_gcp()
elif self.spec["provider"] == "aws":
yield from self.auth_aws()
elif self.spec["provider"] == "azure":
yield from self.auth_azure()
elif self.spec["provider"] == "kubeconfig":
yield from self.auth_kubeconfig()
else:
raise ValueError(f'Provider {self.spec["provider"]} not supported')
def deploy_support(self, cert_manager_version, debug):
cert_manager_url = "https://charts.jetstack.io"
print_colour("Provisioning cert-manager...")
subprocess.check_call(
[
"kubectl",
"apply",
"-f",
f"https://github.com/cert-manager/cert-manager/releases/download/{cert_manager_version}/cert-manager.crds.yaml",
]
)
subprocess.check_call(
[
"helm",
"upgrade",
"cert-manager", # given release name (aka. installation name)
"cert-manager", # helm chart to install
f"--repo={cert_manager_url}",
"--install",
"--create-namespace",
"--namespace=cert-manager",
f"--version={cert_manager_version}",
]
)
print_colour("Done!")
if self.spec["provider"] == "aws":
print_colour("Provisioning tigera operator...")
# Hardcoded here, as we want to upgrade everywhere together
# Ideally this would be a subchart of our support chart,
# but helm has made some unfortunate architectural choices
# with respect to CRDs and they seem super unreliable when
# used as subcharts. So we install it here directly from the
# manifests.
# We unconditionally install this on all AWS clusters - however,
# that doesn't actually turn NetworkPolicy enforcement on. That
# requires setting `calico.enabled` to True in `support` so a
# calico `Installation` object can be set up.
# I deeply loathe the operator *singleton* pattern.
tigera_operator_version = "v3.29.3"
subprocess.check_call(
[
"kubectl",
"apply",
"--force-conflicts", # Remove after https://github.com/2i2c-org/infrastructure/issues/5961
"--server-side", # https://github.com/projectcalico/calico/issues/7826
"-f",
f"https://raw.githubusercontent.com/projectcalico/calico/{tigera_operator_version}/manifests/tigera-operator.yaml",
]
)
print_colour("Done!")
# Patch the tigera operator to remove the NoSchedule toleration
# otherwise it will schedule on tainted nodes
print_colour("Patching tigera operator...")
patch_tolerations = {
"spec": {
"template": {
"spec": {
"tolerations": [
{"effect": "NoExecute", "operator": "Exists"},
],
}
}
}
}
patch_tolerations_json = json.dumps(patch_tolerations)
subprocess.check_call(
[
"kubectl",
"--namespace",
"tigera-operator",
"patch",
"deployment",
"tigera-operator",
"--patch",
patch_tolerations_json,
],
)
print_colour("Done!")
print_colour("Provisioning support charts...")
support_dir = HELM_CHARTS_DIR.joinpath("support")
subprocess.check_call(["helm", "dep", "up", support_dir])
# contains both encrypted and unencrypted values files
values_file_paths = [
support_dir.joinpath("enc-support.secret.values.yaml"),
support_dir.joinpath("enc-cryptnono.secret.values.yaml"),
support_dir.joinpath("support.values.jsonnet"),
] + [
self.config_path.joinpath(p)
for p in self.support["helm_chart_values_files"]
]
with get_decrypted_files(
values_file_paths
) as values_files, ExitStack() as jsonnet_stack:
cmd = [
"helm",
"upgrade",
"--install",
"--create-namespace",
"--namespace=support",
"support",
str(support_dir),
]
for values_file in values_files:
_, ext = os.path.splitext(values_file)
if ext == ".jsonnet":
rendered_path = jsonnet_stack.enter_context(
render_jsonnet(Path(values_file), [self.config_path])
)
cmd.append(f"--values={rendered_path}")
else:
cmd.append(f"--values={values_file}")
if debug:
cmd.append("--debug")
print_colour(f"Running {' '.join([str(c) for c in cmd])}")
subprocess.check_call(cmd)
wait_for_deployments_daemonsets("support")
print_colour("Done!")
def auth_kubeconfig(self):
"""
Context manager for authenticating with just a kubeconfig file
For the duration of the contextmanager, we:
1. Decrypt the file specified in kubeconfig.file with sops
2. Set `KUBECONFIG` env var to our decrypted file path, so applications
we call (primarily helm) will use that as config
"""
config = self.spec["kubeconfig"]
config_path = self.config_path.joinpath(config["file"])
with get_decrypted_file(config_path) as decrypted_key_path, unset_env_vars(
["KUBECONFIG"]
):
os.environ["KUBECONFIG"] = decrypted_key_path
yield
def auth_aws(self):
"""
Reads `aws` nested config and temporarily sets environment variables
like `KUBECONFIG`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`
before trying to authenticate with the `aws eks update-kubeconfig` command.
Finally get those environment variables to the original values to prevent
side-effects on existing local configuration.
"""
config = self.spec["aws"]
key_path = self.config_path.joinpath(config["key"])
cluster_name = config["clusterName"]
region = config["region"]
# Unset all env vars that start with AWS_, as that might affect the aws
# commandline we call. This could make some weird error messages.
unset_envs = ["KUBECONFIG"] + [k for k in os.environ if k.startswith("AWS_")]
with tempfile.NamedTemporaryFile() as kubeconfig, unset_env_vars(unset_envs):
with get_decrypted_file(key_path) as decrypted_key_path:
decrypted_key_abspath = os.path.abspath(decrypted_key_path)
if not os.path.isfile(decrypted_key_abspath):
raise FileNotFoundError("The decrypted key file does not exist")
with open(decrypted_key_abspath) as f:
creds = json.load(f)
os.environ["AWS_ACCESS_KEY_ID"] = creds["AccessKey"]["AccessKeyId"]
os.environ["AWS_SECRET_ACCESS_KEY"] = creds["AccessKey"][
"SecretAccessKey"
]
os.environ["KUBECONFIG"] = kubeconfig.name
subprocess.check_call(
[
"aws",
"eks",
"update-kubeconfig",
f"--name={cluster_name}",
f"--region={region}",
]
)
yield
def auth_azure(self):
"""
Read `azure` nested config, login to Azure with a Service Principal,
activate the appropriate subscription, then authenticate against the
cluster using `az aks get-credentials`.
"""
config = self.spect["azure"]
key_path = self.config_path.joinpath(config["key"])
cluster = config["cluster"]
resource_group = config["resource_group"]
with tempfile.NamedTemporaryFile() as kubeconfig, unset_env_vars(
["KUBECONFIG"]
):
os.environ["KUBECONFIG"] = kubeconfig.name
with get_decrypted_file(key_path) as decrypted_key_path:
decrypted_key_abspath = os.path.abspath(decrypted_key_path)
if not os.path.isfile(decrypted_key_abspath):
raise FileNotFoundError("The decrypted key file does not exist")
with open(decrypted_key_path) as f:
service_principal = json.load(f)
# Login to Azure
subprocess.check_call(
[
"az",
"login",
"--service-principal",
f"--username={service_principal['service_principal_id']}",
f"--password={service_principal['service_principal_password']}",
f"--tenant={service_principal['tenant_id']}",
]
)
# Set the Azure subscription
subprocess.check_call(
[
"az",
"account",
"set",
f"--subscription={service_principal['subscription_id']}",
]
)
# Get cluster creds
subprocess.check_call(
[
"az",
"aks",
"get-credentials",
f"--name={cluster}",
f"--resource-group={resource_group}",
]
)
yield
def auth_gcp(self):
config = self.spec["gcp"]
key_path = self.config_path.joinpath(config["key"])
project = config["project"]
# If cluster is regional, it'll have a `region` key set.
# Else, it'll just have a `zone` key set. Let's respect either.
location = config.get("zone", config.get("region"))
cluster = config["cluster"]
orig_file = os.environ.get("CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE")
orig_kubeconfig = os.environ.get("KUBECONFIG")
try:
with (
tempfile.NamedTemporaryFile() as kubeconfig,
get_decrypted_file(key_path) as decrypted_file,
):
os.environ["KUBECONFIG"] = kubeconfig.name
os.environ["CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE"] = decrypted_file
subprocess.check_call(
[
"gcloud",
"container",
"clusters",
# --zone works with regions too
f"--zone={location}",
f"--project={project}",
"get-credentials",
cluster,
]
)
yield
finally:
# restore modified environment variables to its previous state
if orig_kubeconfig is not None:
os.environ["KUBECONFIG"] = orig_kubeconfig
else:
os.environ.pop("KUBECONFIG")
if orig_file is not None:
os.environ["CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE"] = orig_file
else:
os.environ.pop("CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE")