Skip to content

Commit

Permalink
Merge pull request #193 from Mirantis/cluster-timeout
Browse files Browse the repository at this point in the history
Configurable timeout for Provisioner
  • Loading branch information
tomkukral authored Jan 13, 2018
2 parents 52f962a + a0a3020 commit 32f874b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kqueen/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class BaseConfig:

PROVISIONER_ENGINE_WHITELIST = None

# Timeout for cluster operations (in seconds)
PROVISIONER_TIMEOUT = 3600
PROMETHEUS_WHITELIST = '127.0.0.0/8'

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion kqueen/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def cluster():
'provisioner': prov,
'state': config.get('CLUSTER_UNKNOWN_STATE'),
'kubeconfig': yaml.load(open('kubeconfig_localhost', 'r').read()),
'created_at': datetime.datetime(2017, 11, 15, 13, 36, 24),
'created_at': datetime.datetime.utcnow().replace(microsecond=0),
'owner': _user
}

Expand Down
6 changes: 6 additions & 0 deletions kqueen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from kqueen.storages.etcd import RelationField
from kqueen.storages.etcd import StringField
from tempfile import mkstemp
from datetime import datetime, timedelta

import logging
import os
Expand Down Expand Up @@ -51,6 +52,11 @@ def get_state(self):
self.state = config.get('CLUSTER_UNKNOWN_STATE')
self.save()

# check for stale clusters
max_age = timedelta(seconds=config.get('PROVISIONER_TIMEOUT'))
if self.state == config.get('CLUSTER_PROVISIONING_STATE') and datetime.utcnow() - self.created_at > max_age:
self.state = config.get('CLUSTER_ERROR_STATE')

return self.state

@property
Expand Down
2 changes: 2 additions & 0 deletions kqueen/tests/test_manual_cluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import url_for
from kqueen.conftest import auth_header
from kqueen.models import User
from datetime import datetime

import json
import pytest
Expand Down Expand Up @@ -58,6 +59,7 @@ def create_cluster(self):
'provisioner': 'Provisioner:{}'.format(self.provisioner_id),
'kubeconfig': yaml.load(open('kubeconfig_localhost', 'r').read()),
'owner': 'User:{}'.format(self.user.id),
'created_at': str(datetime.utcnow()),
}

response = self.client.post(
Expand Down
26 changes: 26 additions & 0 deletions kqueen/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from datetime import datetime
from datetime import timedelta
from kqueen.config import current_config
from kqueen.engines import __all__ as all_engines
from kqueen.engines import ManualEngine
from kqueen.models import Cluster
from kqueen.models import Provisioner
from kqueen.storages.etcd import Field
Expand All @@ -9,6 +12,8 @@
import subprocess
import yaml

config = current_config()


class TestModelMethods:
@pytest.mark.parametrize('model_class,req', [
Expand Down Expand Up @@ -200,3 +205,24 @@ def test_load_provisioner(self, user):
loaded = Provisioner.load(user.namespace, provisioner.id)

assert loaded.get_dict(True) == provisioner.get_dict(True)


class TestClusterState:
@pytest.fixture(autouse=True)
def prepare(self, cluster, monkeypatch):
def fake_cluster_get(self):
return {'state': config.get('CLUSTER_PROVISIONING_STATE')}

monkeypatch.setattr(ManualEngine, 'cluster_get', fake_cluster_get)

stale_date = datetime.utcnow() - timedelta(days=365)
cluster.created_at = stale_date
cluster.save()

self.cluster = cluster

def test_stale_cluster(self):
cluster_state = self.cluster.get_state()
print(self.cluster.get_state())

assert cluster_state == config.get('CLUSTER_ERROR_STATE')

0 comments on commit 32f874b

Please sign in to comment.