Skip to content

Commit 50a1592

Browse files
author
v-reyder
committed
feat(config.py): Add ability to setup default tags
Set default tags in controller cause set nodeSelector for each application deployment
1 parent 0685227 commit 50a1592

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

rootfs/api/models/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.conf import settings
22
from django.db import models
33
from jsonfield import JSONField
4+
import json
45

56
from api.models.release import Release
67
from api.models import UuidAuditedModel
@@ -91,7 +92,14 @@ def set_registry(self):
9192
def set_tags(self, previous_config):
9293
"""verify the tags exist on any nodes as labels"""
9394
if not self.tags:
94-
return
95+
if settings.DEIS_DEFAULT_CONFIG_TAGS != '':
96+
try:
97+
tags = json.loads(settings.DEIS_DEFAULT_CONFIG_TAGS)
98+
self.tags = tags
99+
except json.JSONDecodeError as e:
100+
return
101+
else:
102+
return
95103

96104
# Get all nodes with label selectors
97105
nodes = self._scheduler.node.get(labels=self.tags).json()
@@ -174,6 +182,6 @@ def save(self, **kwargs):
174182
self.set_registry()
175183
self.set_tags(previous_config)
176184
except Config.DoesNotExist:
177-
pass
185+
self.set_tags({'tags': {}})
178186

179187
return super(Config, self).save(**kwargs)

rootfs/api/settings/production.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@
309309

310310
KUBERNETES_DEPLOYMENTS_REVISION_HISTORY_LIMIT = os.environ.get('KUBERNETES_DEPLOYMENTS_REVISION_HISTORY_LIMIT', None) # noqa
311311

312+
DEIS_DEFAULT_CONFIG_TAGS = os.environ.get('DEIS_DEFAULT_CONFIG_TAGS', '')
313+
312314
# How long k8s waits for a pod to finish work after a SIGTERM before sending SIGKILL
313315
KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS = int(os.environ.get('KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS', 30)) # noqa
314316

rootfs/api/settings/testing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22
import string
3+
import os
34

45
from api.settings.production import DATABASES
56
from api.settings.production import * # noqa
@@ -37,3 +38,5 @@
3738
# How long k8s waits for a pod to finish work after a SIGTERM before sending SIGKILL
3839
KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS = int(os.environ.get('KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS', 2)) # noqa
3940
KUBERNETES_NAMESPACE_DEFAULT_QUOTA_SPEC = '{"spec":{"hard":{"pods":"10"}}}'
41+
42+
DEIS_DEFAULT_CONFIG_TAGS = str(os.environ.get('DEIS_DEFAULT_CONFIG_TAGS', ''))

rootfs/api/tests/test_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from django.contrib.auth.models import User
1010
from django.core.cache import cache
11+
from django.conf import settings
1112
from unittest import mock
1213
from rest_framework.authtoken.models import Token
1314

@@ -36,6 +37,8 @@ def setUp(self):
3637
self.app = App.objects.all()[0]
3738

3839
def tearDown(self):
40+
# Restore default tags to empty string
41+
settings.DEIS_DEFAULT_CONIFG_TAGS = ''
3942
# make sure every test has a clean slate for k8s mocking
4043
cache.clear()
4144

@@ -120,6 +123,37 @@ def test_config(self, mock_requests):
120123
self.assertEqual(response.status_code, 405, response.data)
121124
return config5
122125

126+
def test_default_tags(self, mock_requests):
127+
settings.DEIS_DEFAULT_CONFIG_TAGS = '{"ssd": "true"}'
128+
app_id = self.create_app()
129+
url = "/v2/apps/{app_id}/config".format(**locals())
130+
131+
response = self.client.get(url)
132+
expected = {
133+
'owner': self.user.username,
134+
'app': app_id,
135+
'values': {},
136+
'memory': {},
137+
'cpu': {},
138+
'tags': {'ssd': 'true'},
139+
'registry': {}
140+
}
141+
self.assertDictContainsSubset(expected, response.data)
142+
143+
# make sure changes not drop tags
144+
body = {'values': json.dumps({'PORT': '5001'})}
145+
response = self.client.post(url, body)
146+
expected = {
147+
'owner': self.user.username,
148+
'app': app_id,
149+
'values': {'PORT': '5001'},
150+
'memory': {},
151+
'cpu': {},
152+
'tags': {'ssd': 'true'},
153+
'registry': {}
154+
}
155+
self.assertDictContainsSubset(expected, response.data)
156+
123157
def test_response_data(self, mock_requests):
124158
"""Test that the serialized response contains only relevant data."""
125159
app_id = self.create_app()

0 commit comments

Comments
 (0)