Skip to content

Commit ef82b77

Browse files
authored
Merge pull request #83 from Cryptophobia/master
feat(deployment.py): add support fo annotations to deployments and carry forward
2 parents 3c6cce0 + 401644e commit ef82b77

File tree

6 files changed

+72
-9
lines changed

6 files changed

+72
-9
lines changed

rootfs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ morph==0.1.2
1515
ndg-httpsclient==0.4.2
1616
packaging==16.8
1717
pyasn1==0.3.2
18-
psycopg2==2.7.3
18+
psycopg2-binary==2.7.5
1919
pyldap==2.4.37
2020
pyOpenSSL==17.5.0
2121
pytz==2017.2

rootfs/scheduler/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def deploy(self, namespace, name, image, entrypoint, command, **kwargs): # noqa
232232
"""Deploy Deployment depending on what's requested"""
233233
app_type = kwargs.get('app_type')
234234
version = kwargs.get('version')
235+
spec_annotations = {}
235236

236237
# If an RC already exists then stop processing of the deploy
237238
try:
@@ -255,19 +256,24 @@ def deploy(self, namespace, name, image, entrypoint, command, **kwargs): # noqa
255256
}
256257
# this depends on the deployment object having the latest information
257258
deployment = self.deployment.get(namespace, name).json()
259+
# a hack to persist the spec annotations on the deployment object to next release
260+
# instantiate spec_annotations and set to blank to avoid errors
261+
if 'annotations' in deployment['spec']['template']['metadata'].keys():
262+
old_spec_annotations = deployment['spec']['template']['metadata']['annotations']
263+
spec_annotations = old_spec_annotations
258264
if deployment['spec']['template']['metadata']['labels'] == labels:
259265
self.log(namespace, 'Deployment {} with release {} already exists. Stopping deploy'.format(name, version)) # noqa
260266
return
261267
except KubeException:
262268
# create the initial deployment object (and the first revision)
263269
self.deployment.create(
264-
namespace, name, image, entrypoint, command, **kwargs
270+
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
265271
)
266272
else:
267273
try:
268274
# kick off a new revision of the deployment
269275
self.deployment.update(
270-
namespace, name, image, entrypoint, command, **kwargs
276+
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
271277
)
272278
except KubeException as e:
273279
raise KubeException(
@@ -283,7 +289,10 @@ def scale(self, namespace, name, image, entrypoint, command, **kwargs):
283289
if e.response.status_code == 404:
284290
# create missing deployment - deleted if it fails
285291
try:
286-
self.deployment.create(namespace, name, image, entrypoint, command, **kwargs)
292+
spec_annotations = {}
293+
self.deployment.create(
294+
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
295+
)
287296
except KubeException:
288297
# see if the deployment got created
289298
try:

rootfs/scheduler/resources/deployment.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get(self, namespace, name=None, **kwargs):
3030

3131
return response
3232

33-
def manifest(self, namespace, name, image, entrypoint, command, **kwargs):
33+
def manifest(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
3434
replicas = kwargs.get('replicas', 0)
3535
batches = kwargs.get('deploy_batches', None)
3636
tags = kwargs.get('tags', {})
@@ -104,11 +104,14 @@ def manifest(self, namespace, name, image, entrypoint, command, **kwargs):
104104
# pod manifest spec
105105
manifest['spec']['template'] = self.pod.manifest(namespace, name, image, **kwargs)
106106

107+
# set the old deployment spec annotations on this deployment
108+
manifest['spec']['template']['metadata']['annotations'] = spec_annotations
109+
107110
return manifest
108111

109-
def create(self, namespace, name, image, entrypoint, command, **kwargs):
112+
def create(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
110113
manifest = self.manifest(namespace, name, image,
111-
entrypoint, command, **kwargs)
114+
entrypoint, command, spec_annotations, **kwargs)
112115

113116
url = self.api("/namespaces/{}/deployments", namespace)
114117
response = self.http_post(url, json=manifest)
@@ -124,9 +127,9 @@ def create(self, namespace, name, image, entrypoint, command, **kwargs):
124127

125128
return response
126129

127-
def update(self, namespace, name, image, entrypoint, command, **kwargs):
130+
def update(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
128131
manifest = self.manifest(namespace, name, image,
129-
entrypoint, command, **kwargs)
132+
entrypoint, command, spec_annotations, **kwargs)
130133

131134
url = self.api("/namespaces/{}/deployments/{}", namespace, name)
132135
response = self.http_put(url, json=manifest)

rootfs/scheduler/tests/test_deployments.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
2525
'image': 'quay.io/fake/image',
2626
'entrypoint': 'sh',
2727
'command': 'start',
28+
'spec_annotations': kwargs.get('spec_annotations', {}),
2829
}
2930

3031
deployment = self.scheduler.deployment.create(namespace, name, **kwargs)
@@ -45,6 +46,7 @@ def update(self, namespace=None, name=generate_random_name(), **kwargs):
4546
'image': 'quay.io/fake/image',
4647
'entrypoint': 'sh',
4748
'command': 'start',
49+
'spec_annotations': kwargs.get('spec_annotations', {}),
4850
}
4951

5052
deployment = self.scheduler.deployment.update(namespace, name, **kwargs)
@@ -241,6 +243,51 @@ def test_get_deployment_replicaset(self):
241243
data
242244
)
243245

246+
def test_get_deployment_annotations(self):
247+
"""
248+
Look at the annotations on the Deployment object
249+
"""
250+
# test success
251+
kwargs = {
252+
'spec_annotations': {'iam.amazonaws.com/role': 'role-arn'},
253+
}
254+
deployment = self.create(**kwargs)
255+
data = self.scheduler.deployment.get(self.namespace, deployment).json()
256+
257+
self.assertDictContainsSubset(
258+
{
259+
'iam.amazonaws.com/role': 'role-arn'
260+
},
261+
data['spec']['template']['metadata']['annotations']
262+
)
263+
264+
def test_get_pod_annotations(self):
265+
"""
266+
Look at the Pod annotations that the Deployment created
267+
"""
268+
kwargs = {
269+
'spec_annotations': {
270+
'iam.amazonaws.com/role': 'role-arn-pods',
271+
'nginx.ingress.kubernetes.io/app-root': '/rootfs',
272+
'sidecar.istio.io/inject': 'true'
273+
},
274+
}
275+
deployment = self.create(**kwargs)
276+
data = self.scheduler.deployment.get(self.namespace, deployment).json()
277+
self.assertEqual(data['kind'], 'Deployment')
278+
self.assertEqual(data['metadata']['name'], deployment)
279+
280+
labels = {'app': self.namespace, 'version': 'v99', 'type': 'web'}
281+
pods = self.scheduler.pod.get(self.namespace, labels=labels).json()
282+
self.assertDictEqual(
283+
{
284+
'iam.amazonaws.com/role': 'role-arn-pods',
285+
'nginx.ingress.kubernetes.io/app-root': '/rootfs',
286+
'sidecar.istio.io/inject': 'true'
287+
},
288+
pods['items'][0]['metadata']['annotations']
289+
)
290+
244291
def test_check_for_failed_events(self):
245292
deploy_name = self.create(self.namespace)
246293
deployment = self.scheduler.deployment.get(self.namespace, deploy_name).json()

rootfs/scheduler/tests/test_horizontalpodautoscaler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
2727
'image': 'quay.io/fake/image',
2828
'entrypoint': 'sh',
2929
'command': 'start',
30+
'spec_annotations': kwargs.get('spec_annotations', {}),
3031
}
3132

3233
# create a Deployment to test HPA with
@@ -75,6 +76,7 @@ def update_deployment(self, namespace=None, name=generate_random_name(), **kwarg
7576
'image': 'quay.io/fake/image',
7677
'entrypoint': 'sh',
7778
'command': 'start',
79+
'spec_annotations': kwargs.get('spec_annotations', {}),
7880
}
7981

8082
deployment = self.scheduler.deployment.update(namespace, name, **d_kwargs)

rootfs/scheduler/tests/test_horizontalpodautoscaler_12_lower.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
3131
'image': 'quay.io/fake/image',
3232
'entrypoint': 'sh',
3333
'command': 'start',
34+
'spec_annotations': kwargs.get('spec_annotations', {}),
3435
}
3536

3637
# create a Deployment to test HPA with
@@ -79,6 +80,7 @@ def update_deployment(self, namespace=None, name=generate_random_name(), **kwarg
7980
'image': 'quay.io/fake/image',
8081
'entrypoint': 'sh',
8182
'command': 'start',
83+
'spec_annotations': kwargs.get('spec_annotations', {}),
8284
}
8385

8486
deployment = self.scheduler.deployment.update(namespace, name, **kwargs)

0 commit comments

Comments
 (0)