Skip to content

Commit 71f1cc1

Browse files
authored
sdk(feat): add metadata support for loop (kubeflow#1028)
1 parent 364d10f commit 71f1cc1

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

sdk/python/kfp_tekton/compiler/compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ def process_pipelineparam(s):
461461
"kind": "PipelineLoop",
462462
"name": group_name
463463
}
464+
# Handle sub-pipeline metadata
465+
if hasattr(sub_group, 'pod_annotations') and sub_group.pod_annotations:
466+
self.loops_pipeline[group_name]['spec']['taskRef']['metadata'] = \
467+
self.loops_pipeline[group_name]['spec']['taskRef'].setdefault('metadata', {'annotations': {}})
468+
self.loops_pipeline[group_name]['spec']['taskRef']['metadata']['annotations'] = sub_group.pod_annotations
469+
if hasattr(sub_group, 'pod_labels') and sub_group.pod_annotations:
470+
self.loops_pipeline[group_name]['spec']['taskRef']['metadata'] = \
471+
self.loops_pipeline[group_name]['spec']['taskRef'].setdefault('metadata', {'labels': {}})
472+
self.loops_pipeline[group_name]['spec']['taskRef']['metadata']['labels'] = sub_group.pod_labels
464473
if sub_group.items_is_pipeline_param:
465474
# these loop args are a 'dynamic param' rather than 'static param'.
466475
# i.e., rather than a static list, they are either the output of another task or were input

sdk/python/kfp_tekton/tekton.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ def range(cls,
257257
parallelism: Optional[int] = None):
258258
return cls(start=start, step=step, end=end, parallelism=parallelism)
259259

260+
def add_pod_annotation(self, name: str, value: str):
261+
"""Adds a pod's metadata annotation.
262+
Args:
263+
name: The name of the annotation.
264+
value: The value of the annotation.
265+
"""
266+
267+
self.pod_annotations[name] = value
268+
return self
269+
270+
def add_pod_label(self, name: str, value: str):
271+
"""Adds a pod's metadata label.
272+
Args:
273+
name: The name of the label.
274+
value: The value of the label.
275+
"""
276+
277+
self.pod_labels[name] = value
278+
return self
279+
260280
def _next_id(self):
261281
return str(_pipeline.Pipeline.get_default_pipeline().get_next_group_id())
262282

@@ -274,6 +294,8 @@ def __init__(self,
274294
self.step = None
275295
self.call_enumerate = False
276296
self.iteration_number = None
297+
self.pod_annotations = {}
298+
self.pod_labels = {}
277299
if start and end:
278300
super().__init__(loop_args=["iteration"], parallelism=parallelism)
279301
self.start = start

sdk/python/tests/compiler/testdata/loop_with_step.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def empty(self):
4747
def pipeline(my_pipe_param: int = 10, start: int = 1, end: int = 5, step: int = 3):
4848
start_2 = 1
4949
end_2 = 2
50-
with Loop.range(start=start, end=end, step=step) as item:
50+
range_object = Loop.range(start=start, end=end, step=step)\
51+
.add_pod_annotation('test_anno', 'test').add_pod_label('test_label', 'test')
52+
with range_object as item:
5153
op1_template = components.load_component_from_text(op1_yaml)
5254
op1_template(item, my_pipe_param)
5355
with Loop.range(start=start_2, end=end_2) as item2:

sdk/python/tests/compiler/testdata/loop_with_step_noninlined.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ spec:
8989
apiVersion: custom.tekton.dev/v1alpha1
9090
kind: PipelineLoop
9191
name: my-pipeline-for-loop-2
92+
metadata:
93+
annotations:
94+
test_anno: test
95+
labels:
96+
test_label: test
9297
params:
9398
- name: from
9499
value: $(params.start)

0 commit comments

Comments
 (0)