Skip to content

Commit 7b69a62

Browse files
committed
fix(k8s): recreate basic k8s resource required for apps if they are missing
gunicorn reload will fix things up, as will a deploy and scale operation.
1 parent 56deb5f commit 7b69a62

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

rootfs/api/models/app.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ def select_app_name(self):
7575

7676
return name
7777

78-
def save(self, **kwargs):
78+
def save(self, *args, **kwargs):
7979
if not self.id:
8080
self.id = generate_app_name()
8181
while App.objects.filter(id=self.id).exists():
8282
self.id = generate_app_name()
8383

84-
return super(App, self).save(**kwargs)
84+
application = super(App, self).save(**kwargs)
85+
86+
# create all the required resources
87+
self.create(*args, **kwargs)
88+
89+
return application
8590

8691
def __str__(self):
8792
return self.id
@@ -119,15 +124,31 @@ def log(self, message, level=logging.INFO):
119124
logger.log(level, "[{}]: {}".format(self.id, message))
120125

121126
def create(self, *args, **kwargs):
122-
"""Create a new application with an initial config and release"""
123-
config = Config.objects.create(owner=self.owner, app=self)
124-
Release.objects.create(version=1, owner=self.owner, app=self, config=config, build=None)
127+
"""
128+
Create a application with an initial config, release, domain
129+
and k8s resource if needed
130+
"""
131+
try:
132+
cfg = self.config_set.latest()
133+
except Config.DoesNotExist:
134+
cfg = Config.objects.create(owner=self.owner, app=self)
135+
136+
# Only create if no release can be found
137+
try:
138+
rel = self.release_set.latest()
139+
except Release.DoesNotExist:
140+
rel = Release.objects.create(
141+
version=1, owner=self.owner, app=self,
142+
config=cfg, build=None
143+
)
125144

126145
# create required minimum resources in k8s for the application
127146
self._scheduler.create(self.id)
128147

129148
# Attach the platform specific application sub domain to the k8s service
130-
Domain(owner=self.owner, app=self, domain=self.id).save()
149+
# Only attach it on first release in case a customer has remove the app domain
150+
if rel.version == 1 and not Domain.objects.filter(domain=self.id).exists():
151+
Domain(owner=self.owner, app=self, domain=self.id).save()
131152

132153
def delete(self, *args, **kwargs):
133154
"""Delete this application including all containers"""
@@ -231,6 +252,9 @@ def _clean_app_logs(self):
231252

232253
def scale(self, user, structure): # noqa
233254
"""Scale containers up or down to match requested structure."""
255+
# use create to make sure minimum resources are created
256+
self.create()
257+
234258
if self.release_set.latest().build is None:
235259
raise EnvironmentError('No build associated with this release')
236260

@@ -392,6 +416,9 @@ def _destroy_containers(self, to_destroy):
392416

393417
def deploy(self, user, release):
394418
"""Deploy a new release to this application"""
419+
# use create to make sure minimum resources are created
420+
self.create()
421+
395422
existing = self.container_set.exclude(type='run')
396423
new = []
397424
scale_types = set()

rootfs/api/views.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ def list(self, request, *args, **kwargs):
220220

221221
return Response(serializer.data)
222222

223-
def post_save(self, app):
224-
app.create()
225-
226223
def scale(self, request, **kwargs):
227224
new_structure = {}
228225
app = self.get_object()

rootfs/scheduler/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,22 @@ def create(self, namespace, **kwargs):
414414
logger.debug('create {}'.format(namespace))
415415
try:
416416
# Create essential resources
417-
self._create_namespace(namespace)
418-
self._create_minio_secret(namespace)
419-
self._create_service(namespace, namespace)
417+
try:
418+
self._get_namespace(namespace)
419+
except KubeException:
420+
self._create_namespace(namespace)
421+
422+
try:
423+
self._get_secret(namespace, 'minio-user')
424+
except KubeException:
425+
self._create_minio_secret(namespace)
426+
427+
try:
428+
self._get_service(namespace, namespace)
429+
except KubeException:
430+
self._create_service(namespace, namespace)
420431
except KubeException as e:
432+
# Blow it all away only if something horrible happens
421433
logger.debug(e)
422434
self._delete_namespace(namespace)
423435
raise

0 commit comments

Comments
 (0)