|
11 | 11 | from django.core.exceptions import ValidationError |
12 | 12 | from jsonfield import JSONField |
13 | 13 |
|
14 | | -from api.models import UuidAuditedModel, log_event |
| 14 | +from api.models import UuidAuditedModel, log_event, AlreadyExists |
15 | 15 | from api.utils import generate_app_name, app_build_type |
16 | 16 | from api.models.release import Release |
17 | 17 | from api.models.config import Config |
@@ -71,6 +71,20 @@ def save(self, *args, **kwargs): |
71 | 71 | while App.objects.filter(id=self.id).exists(): |
72 | 72 | self.id = generate_app_name() |
73 | 73 |
|
| 74 | + # verify the application name doesn't exist as a k8s namespace |
| 75 | + # only check for it if there have been on releases |
| 76 | + try: |
| 77 | + self.release_set.latest() |
| 78 | + except Release.DoesNotExist: |
| 79 | + try: |
| 80 | + if self._scheduler._get_namespace(self.id).status_code == 200: |
| 81 | + # Namespace already exists |
| 82 | + err = "{} already exists as a namespace in this kuberenetes setup".format(self.id) # noqa |
| 83 | + log_event(self, err, logging.INFO) |
| 84 | + raise AlreadyExists(err) |
| 85 | + except KubeHTTPException: |
| 86 | + pass |
| 87 | + |
74 | 88 | application = super(App, self).save(**kwargs) |
75 | 89 |
|
76 | 90 | # create all the required resources |
@@ -310,12 +324,12 @@ def _scale_pods(self, scale_types): |
310 | 324 |
|
311 | 325 | def deploy(self, user, release): |
312 | 326 | """Deploy a new release to this application""" |
313 | | - # use create to make sure minimum resources are created |
314 | | - self.create() |
315 | | - |
316 | 327 | if release.build is None: |
317 | 328 | raise EnvironmentError('No build associated with this release') |
318 | 329 |
|
| 330 | + # use create to make sure minimum resources are created |
| 331 | + self.create() |
| 332 | + |
319 | 333 | if self.structure == {}: |
320 | 334 | self.structure = self._default_structure(release) |
321 | 335 | self.save() |
|
0 commit comments