Added timeout for checking the deployment

If the time limit is hit, the checking is aborted and we return with
return-code None.
This commit is contained in:
Jochen Breuer 2017-08-28 08:53:52 +02:00
parent 32d7d34fe5
commit 767af9bb4f
No known key found for this signature in database
GPG key ID: 29ACE79F4D5EEE69

View file

@ -40,7 +40,9 @@ import base64
import logging
import yaml
import tempfile
import signal
from time import sleep
from contextlib import contextmanager
from salt.exceptions import CommandExecutionError
from salt.ext.six import iteritems
@ -68,6 +70,9 @@ log = logging.getLogger(__name__)
__virtualname__ = 'kubernetes'
_polling_time_limit = 20
def __virtual__():
'''
@ -79,6 +84,21 @@ def __virtual__():
return False, 'python kubernetes library not found'
class TimeoutException(Exception):
pass
@contextmanager
def _time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutException, "Timed out!"
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
# pylint: disable=no-member
def _setup_conn(**kwargs):
'''
@ -694,10 +714,14 @@ def delete_deployment(name, namespace='default', **kwargs):
namespace=namespace,
body=body)
mutable_api_response = api_response.to_dict()
while show_deployment(name, namespace) is not None:
sleep(0.5)
else:
mutable_api_response['code'] = 200
try:
with _time_limit(_polling_time_limit):
while show_deployment(name, namespace) is not None:
sleep(1)
else:
mutable_api_response['code'] = 200
except TimeoutException:
pass
return mutable_api_response
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404: