Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflows and volumes enhancements #507

Merged
merged 7 commits into from
Jul 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
#512 add auto affinity to workflows
filippomc committed Jul 8, 2022
commit 3ce0e357bb5b08e2057d33d3fcc101b4506212ba
31 changes: 16 additions & 15 deletions libraries/cloudharness-common/cloudharness/workflows/operations.py
Original file line number Diff line number Diff line change
@@ -68,7 +68,13 @@ def __init__(self, basename: str, pod_context: Union[PodExecutionContext, list,
:param shared_directory: bool|str|list
"""
super(ContainerizedOperation, self).__init__(basename, *args, **kwargs)
self.pod_context = pod_context
if type(pod_context) == PodExecutionContext:
self.pod_contexts = [pod_context]
elif pod_context is None:
self.pod_contexts = []
else:
self.pod_contexts = list(pod_context)

self.persisted = None
shared_path = None
if shared_directory:
@@ -88,6 +94,8 @@ def __init__(self, basename: str, pod_context: Union[PodExecutionContext, list,
task.add_env('shared_directory', shared_path)
else:
self.volumes = tuple()

self.pod_contexts += [PodExecutionContext('usesvolume', v.split(':')[0], True) for v in self.volumes if ':' in v]

def task_list(self):
raise NotImplementedError()
@@ -126,7 +134,7 @@ def spec(self):
if self.on_exit_notify:
spec = self.add_on_exit_notify_handler(spec)

if self.pod_context:
if self.pod_contexts:
spec['affinity'] = self.affinity_spec()
if self.volumes:
spec['volumeClaimTemplates'] = [self.spec_volumeclaim(volume) for volume in self.volumes if
@@ -135,11 +143,10 @@ def spec(self):
':' in volume] # with PVC prefix (e.g. pvc-001:/location)
return spec



def affinity_spec(self):
if type(self.pod_context) in (list, tuple):
contexts = self.pod_context
else:
contexts = (self.pod_context,)
contexts=self.pod_contexts
PREFERRED = 'preferredDuringSchedulingIgnoredDuringExecution'
REQUIRED = 'requiredDuringSchedulingIgnoredDuringExecution'

@@ -195,15 +202,9 @@ def add_on_exit_notify_handler(self, spec):

def modify_template(self, template):
"""Hook to modify templates (e.g. add volumes)"""
if self.pod_context:
if 'metadata' not in template:
template['metadata'] = {}
if 'labels' not in template['metadata']:
template['metadata']['labels'] = {}
contexts = self.pod_context if type(self.pod_context) in (
list, tuple) else [self.pod_context]
for context in contexts:
template['metadata']['labels'][context.key] = context.value

template["metadata"] = {"labels": {c.key:c.value for c in self.pod_contexts}}

if self.volumes:
if 'container' in template:
template['container']['volumeMounts'] += [
9 changes: 9 additions & 0 deletions libraries/cloudharness-common/tests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -126,6 +126,14 @@ def test_single_task_shared_multiple():
assert len(wf['spec']['templates'][0]['container']['volumeMounts']) == 3

assert wf['spec']['templates'][0]['container']['volumeMounts'][2]['readonly'] == True

assert wf['spec']['templates'][0]['metadata']['labels']['usesvolume']

assert 'affinity' in wf['spec']
assert len(wf['spec']['affinity']['podAffinity']['requiredDuringSchedulingIgnoredDuringExecution']) == 2, "A pod affinity for each volume is expected"
affinity_expr = wf['spec']['affinity']['podAffinity']['requiredDuringSchedulingIgnoredDuringExecution'][0]['labelSelector']['matchExpressions'][0]
assert affinity_expr['key'] == 'usesvolume'
assert affinity_expr['values'][0] == 'myclaim'
if execute:
print(op.execute())

@@ -141,6 +149,7 @@ def test_single_task_shared_script():
assert len(wf['spec']['volumes']) == 2
assert wf['spec']['volumes'][1]['persistentVolumeClaim']['claimName'] == 'myclaim'
assert len(wf['spec']['templates'][0]['script']['volumeMounts']) == 2

if execute:
print(op.execute())