-
Notifications
You must be signed in to change notification settings - Fork 752
/
enable.kubeflow.sh
executable file
·273 lines (233 loc) · 7.66 KB
/
enable.kubeflow.sh
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
#!/usr/bin/env python3
import json
import os
import random
import string
import subprocess
import sys
import tempfile
import textwrap
import time
from itertools import count
from distutils.util import strtobool
def run(*args, die=True, debug=False, stdout=True):
# Add wrappers to $PATH
env = os.environ.copy()
env["PATH"] += ":%s" % os.environ["SNAP"]
if debug and stdout:
print("\033[;1;32m+ %s\033[;0;0m" % " ".join(args))
result = subprocess.run(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
try:
result.check_returncode()
except subprocess.CalledProcessError as err:
if die:
print("Kubeflow could not be enabled:")
if result.stderr:
print(result.stderr.decode("utf-8"))
print(err)
sys.exit(1)
else:
raise
result_stdout = result.stdout.decode('utf-8')
if debug and stdout:
print(result_stdout)
if result.stderr:
print(result.stderr.decode('utf-8'))
return result_stdout
def get_random_pass():
return "".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(30)
)
def juju(*args, **kwargs):
if strtobool(os.environ.get("KUBEFLOW_DEBUG") or "false"):
return run('microk8s-juju.wrapper', "--debug", *args, debug=True, **kwargs)
else:
return run('microk8s-juju.wrapper', *args, **kwargs)
def get_hostname():
"""Gets the hostname that Ambassador will respond to."""
# Check if we've manually set a hostname on the ingress
try:
output = run(
"microk8s-kubectl.wrapper",
"get",
"--namespace=kubeflow",
"ingress/ambassador",
"-ojson",
stdout=False,
die=False,
)
return json.loads(output)["spec"]["rules"][0]["host"]
except (KeyError, subprocess.CalledProcessError) as err:
pass
# Otherwise, see if we've set up metallb with a custom service
try:
output = run(
"microk8s-kubectl.wrapper",
"get",
"--namespace=kubeflow",
"svc/ambassador",
"-ojson",
stdout=False,
die=False,
)
pub_ip = json.loads(output)["status"]["loadBalancer"]["ingress"][0]["ip"]
return "%s.xip.io" % pub_ip
except (KeyError, subprocess.CalledProcessError) as err:
pass
# If all else fails, just use localhost
return "localhost"
def main():
password = os.environ.get("KUBEFLOW_AUTH_PASSWORD") or get_random_pass()
channel = os.environ.get("KUBEFLOW_CHANNEL") or "stable"
no_proxy = os.environ.get("KUBEFLOW_NO_PROXY") or None
hostname = os.environ.get("KUBEFLOW_HOSTNAME") or None
password_overlay = {
"applications": {
"dex-auth": {
"options": {"static-username": "admin", "static-password": password}
},
"katib-db": {"options": {"root_password": get_random_pass()}},
"modeldb-db": {"options": {"root_password": get_random_pass()}},
"oidc-gatekeeper": {"options": {"client-secret": get_random_pass()}},
"pipelines-api": {"options": {"minio-secret-key": "minio123"}},
"pipelines-db": {"options": {"root_password": get_random_pass()}},
}
}
for service in [
"dns",
"storage",
"rbac",
"dashboard",
"ingress",
"metallb:10.64.140.43-10.64.140.49",
]:
print("Enabling %s..." % service)
run("microk8s-enable.wrapper", service)
run("microk8s-status.wrapper", '--wait-ready')
try:
juju("show-controller", "uk8s", die=False, stdout=False)
except subprocess.CalledProcessError:
pass
else:
print("Kubeflow has already been enabled.")
sys.exit(1)
print("Deploying Kubeflow...")
if no_proxy is not None:
juju("bootstrap", "microk8s", "uk8s", "--config=juju-no-proxy=%s" % no_proxy)
juju("add-model", "kubeflow", "microk8s")
juju("model-config", "-m", "kubeflow", "juju-no-proxy=%s" % no_proxy)
else:
juju("bootstrap", "microk8s", "uk8s")
juju("add-model", "kubeflow", "microk8s")
with tempfile.NamedTemporaryFile("w+") as f:
json.dump(password_overlay, f)
f.flush()
juju("deploy", "cs:kubeflow", "--channel", channel, "--overlay", f.name)
for _ in range(240):
try:
run(
"microk8s-kubectl.wrapper",
"wait",
"--namespace=kubeflow",
"--for=condition=ready",
"pod/cert-manager-webhook-operator-0",
die=False,
)
break
except subprocess.CalledProcessError:
time.sleep(5)
else:
print("Waited too long for cert manager webhook operator pod to appear.")
sys.exit(1)
run(
"microk8s-kubectl.wrapper",
"patch",
"role",
"--namespace=kubeflow",
"cert-manager-webhook-operator",
"-p",
json.dumps(
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "Role",
"metadata": {"name": "cert-manager-webhook-operator"},
"rules": [
{
"apiGroups": [""],
"resources": ["pods"],
"verbs": ["get", "list"],
},
{
"apiGroups": [""],
"resources": ["pods/exec"],
"verbs": ["create"],
},
{
"apiGroups": [""],
"resources": ["secrets"],
"verbs": ["get", "list"],
},
],
}
),
)
print("Kubeflow deployed.")
print("Waiting for operator pods to become ready.")
wait_seconds = 15
for i in count():
status = json.loads(
juju("status", "-m", "uk8s:kubeflow", "--format=json", stdout=False)
)
unready_apps = [
name
for name, app in status["applications"].items()
if "message" in app["application-status"]
]
if unready_apps:
print(
"Waited %ss for operator pods to come up, %s remaining."
% (wait_seconds * i, len(unready_apps))
)
time.sleep(wait_seconds)
else:
break
print("Operator pods ready.")
print("Waiting for service pods to become ready.")
run(
"microk8s-kubectl.wrapper",
"wait",
"--namespace=kubeflow",
"--for=condition=Ready",
"pod",
"--timeout=-1s",
"--all",
)
hostname = hostname or get_hostname()
juju("config", "dex-auth", "public-url=http://%s:80" % hostname)
juju("config", "oidc-gatekeeper", "public-url=http://%s:80" % hostname)
juju("config", "ambassador", "juju-external-hostname=%s" % hostname)
juju("expose", "ambassador")
print(
textwrap.dedent(
"""
Congratulations, Kubeflow is now available.
The dashboard is available at http://%s/
Username: admin
Password: %s
To see these values again, run:
microk8s juju config dex-auth static-username
microk8s juju config dex-auth static-password
To tear down Kubeflow and associated infrastructure, run:
microk8s disable kubeflow
"""
% (hostname, password)
)
)
if __name__ == "__main__":
main()