Skip to content

Commit ae9c76c

Browse files
authored
Merge pull request #867 from helgi/rc_waits
ref(scheduler): simplify RC wait logic and container info around that
2 parents abdf9cb + 1ebd7d8 commit ae9c76c

1 file changed

Lines changed: 22 additions & 38 deletions

File tree

rootfs/scheduler/__init__.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,10 @@ def run(self, namespace, name, image, entrypoint, command, **kwargs):
370370
if unhealthy(response.status_code):
371371
raise KubeHTTPException(response, 'create Pod in Namespace "{}"', namespace)
372372

373-
labels = {
374-
'app': namespace,
375-
'type': kwargs.get('app_type'),
376-
'version': kwargs.get('version'),
377-
'heritage': 'deis',
378-
}
379373
# wait for run pod to start - use the same function as scale
380-
container_name = namespace + '-' + kwargs.get('app_type')
381-
container = self._find_container(container_name, manifest['spec']['containers'])
382-
self._wait_until_pods_are_ready(namespace, container, labels, desired=1)
374+
labels = manifest['metadata']['labels']
375+
containers = manifest['spec']['containers']
376+
self._wait_until_pods_are_ready(namespace, containers, labels, desired=1)
383377

384378
try:
385379
# give pod 20 minutes to execute (after it got into ready state)
@@ -766,7 +760,7 @@ def _wait_until_pods_terminate(self, namespace, labels, current, desired):
766760

767761
logger.info("{} pods in namespace {} are terminated".format(delta, namespace))
768762

769-
def _wait_until_pods_are_ready(self, namespace, container, labels, desired): # noqa
763+
def _wait_until_pods_are_ready(self, namespace, containers, labels, desired): # noqa
770764
# If desired is 0 then there is no ready state to check on
771765
if desired == 0:
772766
return
@@ -777,6 +771,10 @@ def _wait_until_pods_are_ready(self, namespace, container, labels, desired): #
777771
# this is to account for kubernetes having readiness check report as failure until
778772
# the initial delay period is up
779773
delay = 0
774+
775+
container_name = '{}-{}'.format(labels['app'], labels['type'])
776+
container = self._find_container(container_name, containers)
777+
780778
# get health info from container
781779
if 'readinessProbe' in container:
782780
delay = int(container['readinessProbe'].get('initialDelaySeconds', 50))
@@ -829,15 +827,7 @@ def _wait_until_pods_are_ready(self, namespace, container, labels, desired): #
829827
def _scale_rc(self, namespace, name, desired):
830828
rc = self.get_rc(namespace, name).json()
831829

832-
# get the current replica count by querying for pods instead of introspecting RC
833-
labels = {
834-
'app': rc['metadata']['labels']['app'],
835-
'type': rc['metadata']['labels']['type'],
836-
'version': rc['metadata']['labels']['version']
837-
}
838-
839830
current = int(rc['spec']['replicas'])
840-
841831
if desired == current:
842832
logger.info("Not scaling RC {} in Namespace {} to {} replicas. Already at desired replicas".format(name, namespace, desired)) # noqa
843833
return
@@ -848,18 +838,12 @@ def _scale_rc(self, namespace, name, desired):
848838
logger.info("scaling RC {} in Namespace {} from {} to {} replicas".format(name, namespace, current, desired)) # noqa
849839

850840
self.update_rc(namespace, name, rc)
851-
self._wait_for_rc_ready(namespace, name)
852-
853-
# Get application container
854-
container_name = '{}-{}'.format(
855-
rc['metadata']['labels']['app'],
856-
rc['metadata']['labels']['type']
857-
)
858-
# get health info from spec
859-
container = self._find_container(container_name, rc['spec']['template']['spec']['containers']) # noqa
841+
self._wait_until_rc_is_updated(namespace, name)
860842

861843
# Double check enough pods are in the required state to service the application
862-
self._wait_until_pods_are_ready(namespace, container, labels, desired)
844+
labels = rc['metadata']['labels']
845+
containers = rc['spec']['template']['spec']['containers']
846+
self._wait_until_pods_are_ready(namespace, containers, labels, desired)
863847

864848
# if it was a scale down operation, wait until terminating pods are done
865849
if int(desired) < int(current):
@@ -880,16 +864,16 @@ def create_rc(self, namespace, name, image, command, **kwargs):
880864
'kind': 'ReplicationController',
881865
'apiVersion': 'v1',
882866
'metadata': {
883-
'name': name,
884-
'labels': {
885-
'app': namespace,
886-
'version': kwargs.get("version"),
887-
'type': kwargs.get('app_type'),
888-
'heritage': 'deis',
889-
}
867+
'name': name,
868+
'labels': {
869+
'app': namespace,
870+
'version': kwargs.get('version'),
871+
'type': kwargs.get('app_type'),
872+
'heritage': 'deis',
873+
}
890874
},
891875
'spec': {
892-
'replicas': kwargs.get("replicas", 0)
876+
'replicas': kwargs.get('replicas', 0)
893877
}
894878
}
895879

@@ -908,11 +892,11 @@ def create_rc(self, namespace, name, image, command, **kwargs):
908892
)
909893
logger.debug('manifest used: {}'.format(ruamel.yaml.dump(manifest)))
910894

911-
self._wait_for_rc_ready(namespace, name)
895+
self._wait_until_rc_is_updated(namespace, name)
912896

913897
return resp
914898

915-
def _wait_for_rc_ready(self, namespace, name):
899+
def _wait_until_rc_is_updated(self, namespace, name):
916900
"""
917901
Looks at status/observedGeneration and metadata/generation and
918902
waits for observedGeneration >= generation to happen, indicates RC is ready

0 commit comments

Comments
 (0)