Skip to content

Commit 4af252d

Browse files
authored
fix(registry): tell a user they need PORT when using off-cluster native iaas registry (#988)
registry:set does the same kind of warning but since this is a whole platform setting then we need to warn on deploy as well Fixes #986
1 parent f8d28a2 commit 4af252d

3 files changed

Lines changed: 44 additions & 13 deletions

File tree

rootfs/api/models/release.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ def get_port(self):
152152
# application has registry auth - $PORT is required
153153
if (creds is not None) or (settings.REGISTRY_LOCATION != 'on-cluster'):
154154
if envs.get('PORT', None) is None:
155-
self.app.log('Private registry detected but no $PORT defined. Defaulting to $PORT 5000', logging.WARNING) # noqa
156-
return 5000
155+
raise DeisException(
156+
'PORT needs to be set in the config '
157+
'when using a private registry'
158+
)
157159

158160
# User provided PORT
159161
return int(envs.get('PORT'))

rootfs/api/tests/test_release.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
from django.contrib.auth.models import User
1212
from django.core.cache import cache
13-
from django.conf import settings
13+
from django.test.utils import override_settings
1414
from unittest import mock
1515
from rest_framework.authtoken.models import Token
1616

1717
from api.models import App, Release
1818
from scheduler import KubeHTTPException
19+
from api.exceptions import DeisException
1920
from api.tests import adapter, mock_port, DeisTransactionTestCase
2021
import requests_mock
2122

@@ -380,20 +381,19 @@ def test_release_get_port(self, mock_requests):
380381

381382
# TODO(bacongobbler): test dockerfile ports
382383

384+
@override_settings(REGISTRY_LOCATION="off-cluster")
383385
def test_release_external_registry(self, mock_requests):
384386
"""
385387
Test that get_port always returns the proper value.
386388
"""
387-
app_id = "test"
388-
body = {'id': app_id}
389-
response = self.client.post('/v2/apps', body,)
390-
self.assertEqual(response.status_code, 201, response.data)
389+
app_id = self.create_app()
390+
391+
# set the required port for external registries
391392
body = {'values': json.dumps({'PORT': '3000'})}
392-
config_response = self.client.post('/v2/apps/test/config', body)
393+
config_response = self.client.post('/v2/apps/{}/config'.format(app_id), body)
393394
self.assertEqual(config_response.status_code, 201, config_response.data)
394395

395396
app = App.objects.get(id=app_id)
396-
settings.REGISTRY_LOCATION = "off-cluster"
397397
url = '/v2/apps/{app_id}/builds'.format(**locals())
398398
body = {'image': 'test/autotest/example'}
399399
response = self.client.post(url, body)
@@ -403,3 +403,24 @@ def test_release_external_registry(self, mock_requests):
403403
self.assertEqual(release.get_port(), 3000)
404404

405405
self.assertEqual(release.image, 'test/autotest/example')
406+
407+
@override_settings(REGISTRY_LOCATION="off-cluster")
408+
def test_release_external_registry_no_port(self, mock_requests):
409+
"""
410+
Test that an exception is raised when registry if off-cluster but
411+
no port is provided.
412+
"""
413+
app_id = self.create_app()
414+
415+
app = App.objects.get(id=app_id)
416+
url = '/v2/apps/{app_id}/builds'.format(**locals())
417+
body = {'image': 'test/autotest/example'}
418+
response = self.client.post(url, body)
419+
self.assertEqual(response.status_code, 400, response.data)
420+
421+
with self.assertRaises(
422+
DeisException,
423+
msg='PORT needs to be set in the config when using a private registry'
424+
):
425+
release = app.release_set.latest()
426+
release.get_port()

rootfs/scheduler/tests/test_scheduler.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
from django.core.cache import cache
77
from django.test import TestCase
8-
from django.conf import settings
8+
from django.test.utils import override_settings
99

1010
from scheduler import mock
1111
import base64
@@ -143,15 +143,21 @@ def test_get_private_registry_config(self):
143143
self.assertEqual(secret_name, "private-registry")
144144
self.assertEqual(secret_create, True)
145145

146-
settings.REGISTRY_LOCATION = "ecr"
146+
@override_settings(REGISTRY_LOCATION="ecr")
147+
def test_get_private_registry_config_ecr(self):
147148
registry = {}
148149
image = "test.com/test/test"
149150
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
150151
self.assertEqual(docker_config, None)
151152
self.assertEqual(secret_name, "private-registry-ecr")
152153
self.assertEqual(secret_create, False)
153154

154-
settings.REGISTRY_LOCATION = "off-cluster"
155+
@override_settings(REGISTRY_LOCATION="off-cluster")
156+
def test_get_private_registry_config_off_cluster(self):
157+
registry = {}
158+
auth = bytes('{}:{}'.format("test", "test"), 'UTF-8')
159+
encAuth = base64.b64encode(auth).decode(encoding='UTF-8')
160+
image = "test.com/test/test"
155161
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
156162
dockerConfig = json.loads(docker_config)
157163
expected = {"https://index.docker.io/v1/": {
@@ -161,7 +167,9 @@ def test_get_private_registry_config(self):
161167
self.assertEqual(secret_name, "private-registry-off-cluster")
162168
self.assertEqual(secret_create, True)
163169

164-
settings.REGISTRY_LOCATION = "ecra"
170+
@override_settings(REGISTRY_LOCATION="ecra")
171+
def test_get_private_registry_config_bad_registry_location(self):
172+
registry = {}
165173
image = "test.com/test/test"
166174
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
167175
self.assertEqual(docker_config, None)

0 commit comments

Comments
 (0)