Skip to content

Commit 8e3b5ac

Browse files
authored
Merge pull request #1283 from rappertomate/gitlab-paths
Gitlab registry support
2 parents 938324b + f73791a commit 8e3b5ac

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

binderhub/builder.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from http.client import responses
88
import json
99
import string
10+
import re
1011
import time
1112
import escapism
1213

@@ -55,6 +56,30 @@
5556
LAUNCHES_INPROGRESS = Gauge('binderhub_inprogress_launches', 'Launches currently in progress')
5657

5758

59+
def _get_image_basename_and_tag(full_name):
60+
"""Get a supposed image name and tag without the registry part
61+
:param full_name: full image specification, e.g. "gitlab.com/user/project:tag"
62+
:return: tuple of image name and tag, e.g. ("user/project", "tag")
63+
"""
64+
# the tag is either after the last (and only) colon, or not given at all,
65+
# in which case "latest" is implied
66+
tag_splits = full_name.rsplit(':', 1)
67+
if len(tag_splits) == 2:
68+
image_name = tag_splits[0]
69+
tag = tag_splits[1]
70+
else:
71+
image_name = full_name
72+
tag = 'latest'
73+
74+
if re.fullmatch('[a-z0-9]{4,40}/[a-z0-9._-]{2,255}', image_name):
75+
# if it looks like a Docker Hub image name, we're done
76+
return image_name, tag
77+
# if the image isn't implied to origin at Docker Hub,
78+
# the first part has to be a registry
79+
image_basename = '/'.join(image_name.split('/')[1:])
80+
return image_basename, tag
81+
82+
5883
def _generate_build_name(build_slug, ref, prefix='', limit=63, ref_length=6):
5984
"""Generate a unique build name with a limited character length.
6085
@@ -315,7 +340,7 @@ async def get(self, provider_prefix, _unescaped_spec):
315340
if self.settings['use_registry']:
316341
for _ in range(3):
317342
try:
318-
image_manifest = await self.registry.get_image_manifest(*'/'.join(image_name.split('/')[-2:]).split(':', 1))
343+
image_manifest = await self.registry.get_image_manifest(*_get_image_basename_and_tag(image_name))
319344
image_found = bool(image_manifest)
320345
break
321346
except HTTPClientError:

binderhub/registry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ async def get_image_manifest(self, image, tag):
191191
# first, get a token to perform the manifest request
192192
if self.token_url:
193193
auth_req = httpclient.HTTPRequest(
194-
url_concat(self.token_url, {"scope": "repository:{}:pull".format(image)}),
194+
url_concat(self.token_url, {
195+
"scope": "repository:{}:pull".format(image),
196+
"service": "container_registry"
197+
}),
195198
auth_username=self.username,
196199
auth_password=self.password,
197200
)

binderhub/tests/test_builder.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import pytest
22

3-
from binderhub.builder import _generate_build_name
3+
from binderhub.builder import _generate_build_name, _get_image_basename_and_tag
44

55

6+
@pytest.mark.parametrize("fullname,basename,tag", [
7+
("jupyterhub/k8s-binderhub:0.2.0-a2079a5", "jupyterhub/k8s-binderhub", "0.2.0-a2079a5"),
8+
("jupyterhub/jupyterhub", "jupyterhub/jupyterhub", "latest"),
9+
("gcr.io/project/image:tag", "project/image", "tag"),
10+
("weirdregistry.com/image:tag", "image", "tag"),
11+
("gitlab-registry.example.com/group/project:some-tag", "group/project", "some-tag"),
12+
("gitlab-registry.example.com/group/project/image:latest", "group/project/image", "latest"),
13+
("gitlab-registry.example.com/group/project/my/image:rc1", "group/project/my/image", "rc1")
14+
])
15+
def test_image_basename_resolution(fullname, basename, tag):
16+
result_basename, result_tag = _get_image_basename_and_tag(fullname)
17+
assert result_basename == basename
18+
assert result_tag == tag
19+
620
@pytest.mark.parametrize('ref,build_slug', [
721
# a long ref, no special characters at critical positions
822
('3035124.v3.0', 'dataverse-dvn-2ftjclkp'),

0 commit comments

Comments
 (0)