Skip to content

Commit

Permalink
Merge pull request #83 from Cryptophobia/master
Browse files Browse the repository at this point in the history
feat(deployment.py): add support fo annotations to deployments and carry forward
  • Loading branch information
Cryptophobia authored Nov 9, 2018
2 parents 3c6cce0 + 401644e commit ef82b77
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rootfs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ morph==0.1.2
ndg-httpsclient==0.4.2
packaging==16.8
pyasn1==0.3.2
psycopg2==2.7.3
psycopg2-binary==2.7.5
pyldap==2.4.37
pyOpenSSL==17.5.0
pytz==2017.2
Expand Down
15 changes: 12 additions & 3 deletions rootfs/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def deploy(self, namespace, name, image, entrypoint, command, **kwargs): # noqa
"""Deploy Deployment depending on what's requested"""
app_type = kwargs.get('app_type')
version = kwargs.get('version')
spec_annotations = {}

# If an RC already exists then stop processing of the deploy
try:
Expand All @@ -255,19 +256,24 @@ def deploy(self, namespace, name, image, entrypoint, command, **kwargs): # noqa
}
# this depends on the deployment object having the latest information
deployment = self.deployment.get(namespace, name).json()
# a hack to persist the spec annotations on the deployment object to next release
# instantiate spec_annotations and set to blank to avoid errors
if 'annotations' in deployment['spec']['template']['metadata'].keys():
old_spec_annotations = deployment['spec']['template']['metadata']['annotations']
spec_annotations = old_spec_annotations
if deployment['spec']['template']['metadata']['labels'] == labels:
self.log(namespace, 'Deployment {} with release {} already exists. Stopping deploy'.format(name, version)) # noqa
return
except KubeException:
# create the initial deployment object (and the first revision)
self.deployment.create(
namespace, name, image, entrypoint, command, **kwargs
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
)
else:
try:
# kick off a new revision of the deployment
self.deployment.update(
namespace, name, image, entrypoint, command, **kwargs
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
)
except KubeException as e:
raise KubeException(
Expand All @@ -283,7 +289,10 @@ def scale(self, namespace, name, image, entrypoint, command, **kwargs):
if e.response.status_code == 404:
# create missing deployment - deleted if it fails
try:
self.deployment.create(namespace, name, image, entrypoint, command, **kwargs)
spec_annotations = {}
self.deployment.create(
namespace, name, image, entrypoint, command, spec_annotations, **kwargs
)
except KubeException:
# see if the deployment got created
try:
Expand Down
13 changes: 8 additions & 5 deletions rootfs/scheduler/resources/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get(self, namespace, name=None, **kwargs):

return response

def manifest(self, namespace, name, image, entrypoint, command, **kwargs):
def manifest(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
replicas = kwargs.get('replicas', 0)
batches = kwargs.get('deploy_batches', None)
tags = kwargs.get('tags', {})
Expand Down Expand Up @@ -104,11 +104,14 @@ def manifest(self, namespace, name, image, entrypoint, command, **kwargs):
# pod manifest spec
manifest['spec']['template'] = self.pod.manifest(namespace, name, image, **kwargs)

# set the old deployment spec annotations on this deployment
manifest['spec']['template']['metadata']['annotations'] = spec_annotations

return manifest

def create(self, namespace, name, image, entrypoint, command, **kwargs):
def create(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
manifest = self.manifest(namespace, name, image,
entrypoint, command, **kwargs)
entrypoint, command, spec_annotations, **kwargs)

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

return response

def update(self, namespace, name, image, entrypoint, command, **kwargs):
def update(self, namespace, name, image, entrypoint, command, spec_annotations, **kwargs):
manifest = self.manifest(namespace, name, image,
entrypoint, command, **kwargs)
entrypoint, command, spec_annotations, **kwargs)

url = self.api("/namespaces/{}/deployments/{}", namespace, name)
response = self.http_put(url, json=manifest)
Expand Down
47 changes: 47 additions & 0 deletions rootfs/scheduler/tests/test_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
'image': 'quay.io/fake/image',
'entrypoint': 'sh',
'command': 'start',
'spec_annotations': kwargs.get('spec_annotations', {}),
}

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

deployment = self.scheduler.deployment.update(namespace, name, **kwargs)
Expand Down Expand Up @@ -241,6 +243,51 @@ def test_get_deployment_replicaset(self):
data
)

def test_get_deployment_annotations(self):
"""
Look at the annotations on the Deployment object
"""
# test success
kwargs = {
'spec_annotations': {'iam.amazonaws.com/role': 'role-arn'},
}
deployment = self.create(**kwargs)
data = self.scheduler.deployment.get(self.namespace, deployment).json()

self.assertDictContainsSubset(
{
'iam.amazonaws.com/role': 'role-arn'
},
data['spec']['template']['metadata']['annotations']
)

def test_get_pod_annotations(self):
"""
Look at the Pod annotations that the Deployment created
"""
kwargs = {
'spec_annotations': {
'iam.amazonaws.com/role': 'role-arn-pods',
'nginx.ingress.kubernetes.io/app-root': '/rootfs',
'sidecar.istio.io/inject': 'true'
},
}
deployment = self.create(**kwargs)
data = self.scheduler.deployment.get(self.namespace, deployment).json()
self.assertEqual(data['kind'], 'Deployment')
self.assertEqual(data['metadata']['name'], deployment)

labels = {'app': self.namespace, 'version': 'v99', 'type': 'web'}
pods = self.scheduler.pod.get(self.namespace, labels=labels).json()
self.assertDictEqual(
{
'iam.amazonaws.com/role': 'role-arn-pods',
'nginx.ingress.kubernetes.io/app-root': '/rootfs',
'sidecar.istio.io/inject': 'true'
},
pods['items'][0]['metadata']['annotations']
)

def test_check_for_failed_events(self):
deploy_name = self.create(self.namespace)
deployment = self.scheduler.deployment.get(self.namespace, deploy_name).json()
Expand Down
2 changes: 2 additions & 0 deletions rootfs/scheduler/tests/test_horizontalpodautoscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
'image': 'quay.io/fake/image',
'entrypoint': 'sh',
'command': 'start',
'spec_annotations': kwargs.get('spec_annotations', {}),
}

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

deployment = self.scheduler.deployment.update(namespace, name, **d_kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def create(self, namespace=None, name=generate_random_name(), **kwargs):
'image': 'quay.io/fake/image',
'entrypoint': 'sh',
'command': 'start',
'spec_annotations': kwargs.get('spec_annotations', {}),
}

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

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

0 comments on commit ef82b77

Please sign in to comment.