WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d4ae2de

Browse files
authored
Merge pull request #209 from whole-tale/env_api_key
Propagate girder API key to instances
2 parents 76b045c + 0248fba commit d4ae2de

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

gwvolman/tasks_kubernetes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
_get_user_and_instance,
1818
new_user,
1919
_get_container_config,
20+
_get_api_key,
2021
)
2122
from .utils_k8s import tale_deployment, tale_service, tale_ingress
2223

@@ -172,6 +173,7 @@ def launch_container(self, task, payload):
172173
"claimName": K8SDeployment.existing_claim,
173174
"claimSubPath": K8SDeployment.existing_claim_subpath,
174175
"girderApiUrl": girder_api_url,
176+
"girderApiKey": _get_api_key(task.girder_client),
175177
"mounterImage": self.deployment.mounter_image,
176178
"instanceId": instanceId,
177179
"girderToken": task.girder_client.token,
@@ -197,6 +199,7 @@ def launch_container(self, task, payload):
197199
template_params["girderfsDef"] = {
198200
"mounts": payload["mounts"],
199201
"girderApiUrl": girder_api_url,
202+
"girderApiKey": _get_api_key(task.girder_client),
200203
"girderToken": task.girder_client.token,
201204
"taleId": tale["_id"],
202205
"userId": user["_id"],

gwvolman/utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class K8SDeployment(object):
8787

8888
__name__ = "K8SDeployment"
8989
dashboard_url = f"https://dashboard.{DOMAIN}"
90-
#girder_url = f"http://{os.environ.get('GIRDER_SERVICE_HOST')}:8080"
90+
# girder_url = f"http://{os.environ.get('GIRDER_SERVICE_HOST')}:8080"
9191
girder_url = f"https://girder.{DOMAIN}"
9292
registry_url = f"https://registry.{DOMAIN}"
9393
builder_url = os.environ.get("BUILDER_URL", "https://builder.{DOMAIN}")
@@ -107,6 +107,7 @@ class DockerDeployment(object):
107107
This class allows to read and store configuration of services in a WT
108108
deployment. It's meant to be used as a singleton across gwvolman.
109109
"""
110+
110111
__name__ = "DockerDeployment"
111112
_dashboard_url = None
112113
_girder_url = None
@@ -296,19 +297,24 @@ def _launch_container(volume_info, container_config, gc):
296297

297298
# inject Girder token into the container
298299
environment = container_config.environment or []
299-
environment += [f"GIRDER_TOKEN={gc.token}", f"GIRDER_API_URL={gc.urlBase}"]
300+
environment += [
301+
f"GIRDER_TOKEN={gc.token}",
302+
f"GIRDER_API_URL={gc.urlBase}",
303+
f"GIRDER_API_KEY={_get_api_key(gc)}",
304+
]
300305

301306
source_mount = os.path.join(VOLUMES_ROOT, "mountpoints", volume_info["volumeName"])
302307
mounts = []
303308
volumes = _get_container_volumes(source_mount, container_config, MOUNTPOINTS)
304309
user = gc.get("/user/me")
305310
volumes[os.path.join(VOLUMES_ROOT, f"homes/{user['login'][0]}/{user['login']}")] = {
306-
"bind": os.path.join(container_config.target_mount, "home"), "mode": "rw"
311+
"bind": os.path.join(container_config.target_mount, "home"),
312+
"mode": "rw",
307313
}
308314
tale = gc.get("/tale/%s" % volume_info["taleId"])
309-
volumes[os.path.join(VOLUMES_ROOT, f"workspaces/{tale['_id'][0]}/{tale['_id']}")] = {
310-
"bind": os.path.join(container_config.target_mount, "workspace"), "mode": "rw"
311-
}
315+
volumes[
316+
os.path.join(VOLUMES_ROOT, f"workspaces/{tale['_id'][0]}/{tale['_id']}")
317+
] = {"bind": os.path.join(container_config.target_mount, "workspace"), "mode": "rw"}
312318

313319
for source in volumes:
314320
mounts.append(
@@ -317,7 +323,7 @@ def _launch_container(volume_info, container_config, gc):
317323
)
318324
)
319325

320-
host = 'tmp-{}'.format(new_user(12).lower())
326+
host = "tmp-{}".format(new_user(12).lower())
321327
environment.append(f"TMP_URL={host}.{DOMAIN}")
322328

323329
# https://github.com/containous/traefik/issues/2582#issuecomment-354107053

gwvolman/utils_k8s.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import json
22
import os
3+
import re
34

45
from kubernetes import client, config
56

67
from .constants import NFS_PATH, NFS_SERVER, VOLUMES_ROOT
78

89

10+
def split_and_clean_quotes(input_string):
11+
"""
12+
Splits a string by whitespace, ignoring whitespace within single quotes,
13+
and removes the surrounding single quotes from the resulting quoted tokens.
14+
"""
15+
# 1. Find all tokens (quoted or unquoted)
16+
pattern = r"'[^']+'|\S+"
17+
tokens = re.findall(pattern, input_string)
18+
19+
# 2. Clean the quotes from the found tokens
20+
cleaned_tokens = []
21+
for token in tokens:
22+
# Check if the token starts and ends with a single quote
23+
if token.startswith("'") and token.endswith("'"):
24+
# Remove the first and last character (the quotes)
25+
cleaned_tokens.append(token[1:-1])
26+
else:
27+
# Keep unquoted tokens as they are
28+
cleaned_tokens.append(token)
29+
30+
return cleaned_tokens
31+
32+
933
def tale_ingress(params: dict) -> None:
1034
host = f"{params['host']}.{params['domain']}"
1135
annotations = {
@@ -193,7 +217,9 @@ def tale_deployment(params):
193217
client.V1VolumeMount(
194218
mount_path=os.path.join(params["mountPoint"], "workspace"),
195219
name=params["claimName"],
196-
sub_path=os.path.join(params["claimSubPath"], params["workspaceSubPath"]),
220+
sub_path=os.path.join(
221+
params["claimSubPath"], params["workspaceSubPath"]
222+
),
197223
),
198224
client.V1VolumeMount(
199225
mount_path=os.path.join(params["mountPoint"], "home"),
@@ -214,6 +240,7 @@ def tale_deployment(params):
214240
]
215241

216242
# Apply the deployment
243+
command_array = split_and_clean_quotes(params["command"])
217244
api_instance = client.AppsV1Api()
218245
api_instance.create_namespaced_deployment(
219246
body=client.V1Deployment(
@@ -248,7 +275,8 @@ def tale_deployment(params):
248275
containers=[
249276
client.V1Container(
250277
name="instance",
251-
command=params["command"].split(" "),
278+
command=[command_array[0]],
279+
args=command_array[1:],
252280
image=params["instanceImage"],
253281
ports=[
254282
client.V1ContainerPort(
@@ -266,6 +294,20 @@ def tale_deployment(params):
266294
"cpu": "1",
267295
},
268296
),
297+
env=[
298+
client.V1EnvVar(
299+
name="GIRDER_API_KEY",
300+
value=params["girderApiKey"],
301+
),
302+
client.V1EnvVar(
303+
name="GIRDER_API_URL",
304+
value=params["girderApiUrl"],
305+
),
306+
client.V1EnvVar(
307+
name="TMP_URL",
308+
value=f"{params['host']}.{params['domain']}",
309+
),
310+
],
269311
),
270312
client.V1Container(
271313
name="mounter",

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="gwvolman",
5-
version="2.1.2",
5+
version="2.1.3",
66
description="WholeTale Girder Volume Manager",
77
author="Kacper Kowalik",
88
author_email="[email protected]",

tests/test_tasks_kubernetes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def mock_gc_get(path, parameters=None):
3838
}
3939
elif path == "/run/123abc":
4040
return {"_id": "123abc", "name": "run1", "runVersionId": "xyz234"}
41+
elif path == "/api_key":
42+
return [{"name": "tmpnb", "active": True, "key": "girderApiKey"}]
4143

4244

4345
@pytest.fixture

0 commit comments

Comments
 (0)