Skip to content

Commit 636dceb

Browse files
authored
chore(controller): allow re attach to update and some error (#181)
* fix(timeouts): timeouts set error * fix(tls): tls force enable error * chore(certs): allow re attach to update * chore(controller): https_enforced when has domains * chore(controller): pod pending has not containerStatuses
1 parent edfeef9 commit 636dceb

5 files changed

Lines changed: 27 additions & 13 deletions

File tree

rootfs/api/models/certificate.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.contrib.auth import get_user_model
1414
from rest_framework.exceptions import ValidationError
1515
from api.utils import validate_label
16-
from api.exceptions import AlreadyExists, ServiceUnavailable
16+
from api.exceptions import ServiceUnavailable
1717
from scheduler import KubeException
1818
from .base import AuditedModel
1919
from .domain import Domain
@@ -192,8 +192,6 @@ def delete(self, *args, **kwargs):
192192
def attach(self, *args, **kwargs):
193193
# add the certificate to the domain
194194
domain = get_object_or_404(Domain, domain=kwargs['domain'])
195-
if domain.certificate is not None:
196-
raise AlreadyExists("Domain already has a certificate attached to it")
197195
# create in kubernetes
198196
self.attach_in_kubernetes(domain)
199197
domain.certificate = self

rootfs/api/models/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,18 @@ def _update_limits(self, previous_config, replace_ptypes=[]):
265265
"the %s has already been used and cannot be deleted" % ptype)
266266
self._merge_data('limits', data, new_data)
267267
setattr(self, 'limits', data)
268+
269+
def _update_termination_grace_period(self, previous_config, replace_ptypes=[]):
270+
data = {
271+
k: v for k, v in getattr(previous_config, 'termination_grace_period', {}).copy().items() # noqa
272+
if k not in replace_ptypes
273+
}
274+
new_data = getattr(self, 'termination_grace_period', {}).copy()
275+
# check procfile
276+
for ptype, value in new_data.items():
277+
if value is None:
278+
if ptype in self.app.ptypes:
279+
raise UnprocessableEntity(
280+
"the %s has already been used and cannot be deleted" % ptype)
281+
self._merge_data('termination_grace_period', data, new_data)
282+
setattr(self, 'termination_grace_period', data)

rootfs/api/models/gateway.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def cleaned_rules(self):
222222
@property
223223
def tls_force_hostnames(self):
224224
tls = self.app.tls_set.latest()
225-
q = Q(ptype__int=[s.ptype for s in self.services])
225+
q = Q(ptype__in=[s.ptype for s in self.services])
226226
if not tls.certs_auto_enabled:
227227
q &= Q(certificate__isnull=False)
228228
domains = self.app.domain_set.filter(q)
@@ -260,7 +260,8 @@ def refresh_to_k8s(self):
260260
if self.routable:
261261
parent_refs, http_parent_refs = self._get_all_parent_refs()
262262
tls = self.app.tls_set.latest()
263-
if tls.https_enforced and self.kind == "HTTPRoute":
263+
# requestRedirect only when has tls or certs
264+
if tls.https_enforced and self.kind == "HTTPRoute" and self.tls_force_hostnames:
264265
self._https_enforced_to_k8s(http_parent_refs)
265266
elif self.kind == "HTTPRoute":
266267
parent_refs.extend(http_parent_refs)

rootfs/api/tests/test_certificate_use_case_4.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def test_certificate_attach_overwrite(self):
168168
'{}/{}/domain/'.format(self.url, 'bar-com'),
169169
{'domain': 'foo.com'}
170170
)
171-
# Should be a 409 Conflict since it already existed
172-
self.assertEqual(response.status_code, 409)
171+
# allow re attach to update
172+
self.assertEqual(response.status_code, 201)
173173

174174
# Assert that domain and cert are still the original
175175
response = self.client.get(
@@ -180,7 +180,7 @@ def test_certificate_attach_overwrite(self):
180180
expected = {
181181
'name': 'foo-com',
182182
'common_name': 'foo.com',
183-
'domains': ['foo.com']
183+
'domains': []
184184
}
185185
for key, value in list(expected.items()):
186186
self.assertEqual(
@@ -197,7 +197,7 @@ def test_certificate_attach_overwrite(self):
197197
expected = {
198198
'name': 'bar-com',
199199
'common_name': 'bar.com',
200-
'domains': []
200+
'domains': ['foo.com']
201201
}
202202
for key, value in list(expected.items()):
203203
self.assertEqual(

rootfs/scheduler/resources/pod.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def readiness_status(self, pod):
413413
"""Check if the pod container have passed the readiness probes"""
414414
name = '{}-{}'.format(pod['metadata']['labels']['app'], pod['metadata']['labels']['type'])
415415
# find the right container in case there are many on the pod
416-
container = self.find_container(name, pod['status']['containerStatuses'])
416+
container = self.find_container(name, pod['status'].get('containerStatuses', []))
417417
if container is None:
418418
# Seems like the most sensible default
419419
return 'Unknown'
@@ -466,7 +466,7 @@ def pending_status(self, pod):
466466

467467
name = '{}-{}'.format(pod['metadata']['labels']['app'], pod['metadata']['labels']['type'])
468468
# find the right container in case there are many on the pod
469-
container = self.pod.find_container(name, pod['status']['containerStatuses'])
469+
container = self.find_container(name, pod['status'].get('containerStatuses', []))
470470
if container is None:
471471
# Return Pending if nothing else can be found
472472
return 'Pending', ''
@@ -596,7 +596,7 @@ def _handle_pending_pods(self, namespace, labels):
596596
phase = pod['status']['phase']
597597
name = '{}-{}'.format(pod['metadata']['labels']['app'],
598598
pod['metadata']['labels']['type'])
599-
container = self.find_container(name, pod['status']['containerStatuses'])
599+
container = self.find_container(name, pod['status'].get('containerStatuses', []))
600600
# phase is Running, but state is waiting in CrashLoopBackOff
601601
if phase not in ['Pending', 'ContainerCreating'] and \
602602
(phase == 'Running' and 'waiting' not in container['state'].keys()):
@@ -748,7 +748,7 @@ def _handle_not_ready_pods(self, namespace, labels):
748748

749749
name = '{}-{}'.format(pod['metadata']['labels']['app'], pod['metadata']['labels']['type']) # noqa
750750
# find the right container in case there are many on the pod
751-
container = self.find_container(name, pod['status']['containerStatuses'])
751+
container = self.find_container(name, pod['status'].get('containerStatuses', []))
752752
if container is None or container['ready'] == 'true':
753753
continue
754754

0 commit comments

Comments
 (0)