Skip to content

Commit 7992df9

Browse files
committed
ref(scheduler): move a few high level scheduling functions to the App model
1 parent 79cd529 commit 7992df9

4 files changed

Lines changed: 38 additions & 45 deletions

File tree

rootfs/api/models/app.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def log(self, message, level=logging.INFO):
151151
"""
152152
logger.log(level, "[{}]: {}".format(self.id, message))
153153

154-
def create(self, *args, **kwargs):
154+
def create(self, *args, **kwargs): # noqa
155155
"""
156156
Create a application with an initial config, release, domain
157157
and k8s resource if needed
@@ -171,9 +171,28 @@ def create(self, *args, **kwargs):
171171
)
172172

173173
# create required minimum resources in k8s for the application
174+
namespace = self.id
175+
service = self.id
174176
try:
175-
self._scheduler.create(self.id)
177+
self.log('creating Namespace {} and services'.format(namespace), level=logging.DEBUG)
178+
# Create essential resources
179+
try:
180+
self._scheduler.get_namespace(namespace)
181+
except KubeException:
182+
self._scheduler.create_namespace(namespace)
183+
184+
try:
185+
self._scheduler.get_service(namespace, service)
186+
except KubeException:
187+
self._scheduler.create_service(namespace, service)
176188
except KubeException as e:
189+
# Blow it all away only if something horrible happens
190+
try:
191+
self._scheduler.delete_namespace(namespace)
192+
except KubeException as e:
193+
# Just feed into the item below
194+
pass
195+
177196
raise ServiceUnavailable('Kubernetes resources could not be created') from e
178197

179198
# Attach the platform specific application sub domain to the k8s service
@@ -185,8 +204,14 @@ def delete(self, *args, **kwargs):
185204
"""Delete this application including all containers"""
186205
self.log("deleting environment")
187206
try:
188-
# attempt to remove application from kubernetes
189-
self._scheduler.destroy(self.id)
207+
self._scheduler.delete_namespace(self.id)
208+
209+
# wait 30 seconds for termination
210+
for _ in range(30):
211+
try:
212+
self._scheduler.get_namespace(self.id)
213+
except KubeException:
214+
break
190215
except KubeException as e:
191216
raise ServiceUnavailable('Could not delete Kubernetes Namespace {}'.format(self.id)) from e # noqa
192217

rootfs/api/tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SilentDjangoTestSuiteRunner(DiscoverRunner):
4545

4646
def run_tests(self, test_labels, extra_tests=None, **kwargs):
4747
"""Run tests with all but critical log messages disabled."""
48-
# hide any log messages less than critical
49-
logging.disable(logging.ERROR)
48+
# hide any log messages less than error (critical and error are shown)
49+
logging.disable(logging.WARNING)
5050
return super(SilentDjangoTestSuiteRunner, self).run_tests(
5151
test_labels, extra_tests, **kwargs)

rootfs/api/tests/test_app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,23 +406,23 @@ def test_app_exists_in_kubernetes(self, mock_requests):
406406

407407
def test_app_create_failure_kubernetes_create(self, mock_requests):
408408
"""
409-
Create an app but have scheduler.create fail with an exception
409+
Create an app but have scheduler.create_service fail with an exception
410410
"""
411-
with mock.patch('scheduler.KubeHTTPClient.create') as mock_kube:
411+
with mock.patch('scheduler.KubeHTTPClient.create_service') as mock_kube:
412412
mock_kube.side_effect = KubeException('Boom!')
413413
response = self.client.post('/v2/apps', {'id': 'test-kube'})
414414
self.assertEqual(response.status_code, 503, response.data)
415415

416416
def test_app_delete_failure_kubernetes_destroy(self, mock_requests):
417417
"""
418-
Create an app and then delete but have scheduler.destroy
418+
Create an app and then delete but have scheduler.delete_namespace
419419
fail with an exception
420420
"""
421421
# create
422422
response = self.client.post('/v2/apps', {'id': 'test'})
423423
self.assertEqual(response.status_code, 201, response.data)
424424

425-
with mock.patch('scheduler.KubeHTTPClient.destroy') as mock_kube:
425+
with mock.patch('scheduler.KubeHTTPClient.delete_namespace') as mock_kube:
426426
# delete
427427
mock_kube.side_effect = KubeException('Boom!')
428428
response = self.client.delete('/v2/apps/test')

rootfs/scheduler/__init__.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,10 @@ def cleanup_release(self, namespace, controller):
352352

353353
def _update_application_service(self, namespace, name, app_type, port, routable=False):
354354
"""Update application service with all the various required information"""
355-
try:
356-
# Fetch service
357-
service = self.get_service(namespace, namespace).json()
358-
old_service = service.copy() # in case anything fails for rollback
355+
service = self.get_service(namespace, namespace).json()
356+
old_service = service.copy() # in case anything fails for rollback
359357

358+
try:
360359
# Update service information
361360
if routable:
362361
service['metadata']['labels']['router.deis.io/routable'] = 'true'
@@ -399,37 +398,6 @@ def scale(self, namespace, name, image, command, **kwargs):
399398
self._scale_rc(namespace, name, old['spec']['replicas'])
400399
raise
401400

402-
def create(self, namespace, **kwargs):
403-
"""Create a basic structure for an application in k8s"""
404-
logger.debug('creating Namespace {} and services'.format(namespace))
405-
try:
406-
# Create essential resources
407-
try:
408-
self.get_namespace(namespace)
409-
except KubeException:
410-
self.create_namespace(namespace)
411-
412-
try:
413-
self.get_service(namespace, namespace)
414-
except KubeException:
415-
self.create_service(namespace, namespace)
416-
except KubeException:
417-
# Blow it all away only if something horrible happens
418-
self.delete_namespace(namespace)
419-
raise
420-
421-
def destroy(self, namespace):
422-
"""Destroy a application by deleting its namespace."""
423-
logger.debug("destroying Namespace {}".format(namespace))
424-
self.delete_namespace(namespace)
425-
426-
# wait 30 seconds for termination
427-
for _ in range(30):
428-
try:
429-
self.get_namespace(namespace).json()
430-
except KubeException:
431-
break
432-
433401
def run(self, namespace, name, image, entrypoint, command, **kwargs):
434402
"""Run a one-off command."""
435403
logger.info('run {}, img {}, entrypoint {}, cmd "{}"'.format(

0 commit comments

Comments
 (0)