Skip to content

Commit f7fc673

Browse files
committed
feat(cli): get image repository host on login
1 parent 2108f65 commit f7fc673

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

renku/core/login.py

+19
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ def login(endpoint: Optional[str], git_login: bool, yes: bool):
135135
else:
136136
raise errors.AuthenticationError(f"Invalid status code from server: {status_code} - {response.content}")
137137

138+
image_regsitry_host_req_url = _get_url(parsed_endpoint, path="/api/config/imageRegistries")
139+
image_registry_host_res = requests.get(image_regsitry_host_req_url)
140+
if image_registry_host_res.status_code != 200:
141+
raise errors.ConfigurationError(
142+
f"Cannot get the image registry host from {image_regsitry_host_req_url}, "
143+
f"got unexpected status code {image_registry_host_res.status_code}"
144+
)
145+
image_registry_host = image_registry_host_res.json().get("default")
146+
if not image_registry_host:
147+
raise errors.ConfigurationError(
148+
f"Cannot get the image registry host from the response {image_registry_host_res.text}"
149+
)
150+
set_value(
151+
section=CONFIG_SECTION,
152+
key=f"{parsed_endpoint.netloc}_image_registry_host",
153+
value=image_registry_host,
154+
global_only=True,
155+
)
156+
138157
access_token = response.json().get("access_token")
139158
_store_token(parsed_endpoint.netloc, access_token)
140159

renku/core/session/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import urllib
2020
from typing import Optional
2121

22+
from renku.core.config import get_value
2223
from renku.core.util.git import get_remote
2324
from renku.core.util.urls import parse_authentication_endpoint
2425
from renku.domain_model.project_context import project_context
@@ -48,11 +49,10 @@ def get_renku_url() -> Optional[str]:
4849

4950
def get_image_repository_host() -> Optional[str]:
5051
"""Derive the hostname for the gitlab container registry."""
51-
# NOTE: Used to circumvent cases where the registry URL is not guessed correctly
52-
# remove once #3301 is done
5352
if "RENKU_IMAGE_REGISTRY" in os.environ:
5453
return os.environ["RENKU_IMAGE_REGISTRY"]
55-
renku_url = get_renku_url()
54+
renku_url = parse_authentication_endpoint(use_remote=True)
5655
if not renku_url:
5756
return None
58-
return "registry." + urllib.parse.urlparse(renku_url).netloc
57+
renku_host = renku_url.netloc
58+
return get_value("http", f"{renku_host}_image_registry_host")

tests/cli/fixtures/cli_gateway.py

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
ENDPOINT = "renku.deployment.ch"
2727
ACCESS_TOKEN = "jwt-token"
2828
DEVICE_CODE = "valid-device-code"
29+
IMAGE_REGISTRY_HOST = "registry.renku.deployment.ch"
2930

3031

3132
@pytest.fixture(scope="module")
@@ -65,6 +66,12 @@ def token_callback(request):
6566

6667
return token_callback
6768

69+
def create_image_registry_host_callback(host):
70+
def image_registry_host_callback(_):
71+
return 200, {"Content-Type": "application/json"}, json.dumps({"default": host})
72+
73+
return image_registry_host_callback
74+
6875
requests_mock.add_passthru("https://pypi.org/")
6976

7077
class RequestMockWrapper:
@@ -82,6 +89,15 @@ def add_device_auth(endpoint, token):
8289
callback=create_token_callback(token),
8390
)
8491

92+
@staticmethod
93+
def add_registry_image_host(endpoint, host):
94+
requests_mock.add_callback(
95+
responses.GET,
96+
f"https://{endpoint}/api/config/imageRegistries",
97+
callback=create_image_registry_host_callback(host),
98+
)
99+
85100
RequestMockWrapper.add_device_auth(ENDPOINT, ACCESS_TOKEN)
101+
RequestMockWrapper.add_registry_image_host(ENDPOINT, IMAGE_REGISTRY_HOST)
86102

87103
yield RequestMockWrapper

tests/cli/test_login.py

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from renku.core import errors
2222
from renku.core.login import read_renku_token
23+
from renku.core.session.utils import get_image_repository_host
2324
from renku.core.util.contexts import chdir
2425
from renku.ui.cli import cli
2526
from tests.cli.fixtures.cli_gateway import ACCESS_TOKEN, ENDPOINT
@@ -144,7 +145,9 @@ def test_repeated_logout(runner, project, mock_login, with_injection):
144145
def test_login_to_multiple_endpoints(runner, project_with_remote, mock_login, with_injection):
145146
"""Test login to multiple endpoints changes project's remote to the first endpoint."""
146147
second_endpoint, second_token = "second.endpoint", "second-token"
148+
second_image_registry_host = "registry.second.endpoint"
147149
mock_login.add_device_auth(second_endpoint, second_token)
150+
mock_login.add_registry_image_host(second_endpoint, second_image_registry_host)
148151
assert 0 == runner.invoke(cli, ["login", "--yes", ENDPOINT]).exit_code
149152

150153
result = runner.invoke(cli, ["login", "--yes", second_endpoint])
@@ -157,6 +160,7 @@ def test_login_to_multiple_endpoints(runner, project_with_remote, mock_login, wi
157160
with with_injection():
158161
assert ACCESS_TOKEN == read_renku_token(ENDPOINT)
159162
assert second_token == read_renku_token(second_endpoint)
163+
assert second_image_registry_host == get_image_repository_host()
160164
assert project_with_remote.repository.remotes["origin"].url.startswith(f"https://{second_endpoint}/repo")
161165

162166

tests/core/models/test_git.py

+18
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@
296296
"owner": "renku-test",
297297
"env": "https://gitlab.example.com",
298298
},
299+
{
300+
"href": "https://dev.renku.ch/gitlab/group1/renku-test/test-2022-11-11-17-01-46.git",
301+
"scheme": "https",
302+
"hostname": "dev.renku.ch",
303+
"name": "test-2022-11-11-17-01-46",
304+
"path": "gitlab/group1/renku-test/test-2022-11-11-17-01-46.git",
305+
"owner": "group1/renku-test",
306+
"env": "https://dev.renku.ch/gitlab/",
307+
},
308+
{
309+
"href": "https://gitlab.example.com/group1/group2/renku-test/test-2022-11-11-17-01-46.git",
310+
"scheme": "https",
311+
"hostname": "gitlab.example.com",
312+
"name": "test-2022-11-11-17-01-46",
313+
"path": "group1/group2/renku-test/test-2022-11-11-17-01-46.git",
314+
"owner": "group1/group2/renku-test",
315+
"env": "https://gitlab.example.com",
316+
},
299317
],
300318
)
301319
def test_valid_href(fields):

0 commit comments

Comments
 (0)