14
14
15
15
def run_argo_workflow (
16
16
kubernetes_namespace : str ,
17
- template_name : str ,
17
+ template_name : Optional [str ] = None ,
18
+ project_name : Optional [str ] = None ,
19
+ branch_name : Optional [str ] = None ,
20
+ template_name_prefix : Optional [str ] = None ,
18
21
parameters : Optional [dict ] = None ,
19
22
wait_timeout : Union [int , float , datetime .timedelta ] = 0 ,
20
23
** kwarg , # Other parameters for wait function
21
24
) -> Tuple [str , str ]:
25
+ """
26
+ Using template_name to trigger a workflow template name with exact match.
27
+
28
+ If no template_name is provided, the latest workflow template satisfying
29
+ the project_name, branch_name and template_name_prefix will be used.
30
+ All of these filters are optional. If not provided, the latest workflow
31
+ from the namespace will be used.
32
+ """
33
+ client = ArgoClient (namespace = kubernetes_namespace )
34
+
35
+ if not template_name :
36
+ template_name = get_latest_workflow (
37
+ kubernetes_namespace ,
38
+ project_name = project_name ,
39
+ branch_name = branch_name ,
40
+ template_name_prefix = template_name_prefix ,
41
+ )
42
+
22
43
try :
23
44
# TODO(talebz): add tag of origin-run-id to correlate parent flow
24
- workflow_manifest : Dict [str , Any ] = ArgoClient (
25
- namespace = kubernetes_namespace ,
26
- ).trigger_workflow_template (template_name , parameters )
45
+ logger .info (f"Triggering workflow template: { template_name } " )
46
+ workflow_manifest : Dict [str , Any ] = client .trigger_workflow_template (
47
+ template_name , parameters
48
+ )
27
49
except Exception as e :
28
50
raise AIPException (str (e ))
29
51
@@ -41,6 +63,52 @@ def run_argo_workflow(
41
63
return argo_run_id , argo_run_uid
42
64
43
65
66
+ def get_latest_workflow (
67
+ kubernetes_namespace : str ,
68
+ project_name : Optional [str ] = None ,
69
+ branch_name : Optional [str ] = None ,
70
+ template_name_prefix : Optional [str ] = None ,
71
+ ):
72
+ # TODO:
73
+ # - Add filter by project_id instead of project name - project_id is not added as a label yet.
74
+ # - Add filter by flow_name - flow_name is not added as a label yet.
75
+ client = ArgoClient (namespace = kubernetes_namespace )
76
+
77
+ templates = client .list_workflow_template ()["items" ]
78
+ templates = [
79
+ template
80
+ for template in templates
81
+ if (
82
+ not project_name
83
+ or template ["metadata" ]["labels" ]["gitlab.zgtools.net/project-name" ]
84
+ == project_name
85
+ )
86
+ and (
87
+ not branch_name
88
+ or template ["metadata" ]["labels" ]["gitlab.zgtools.net/branch-name" ]
89
+ == branch_name
90
+ )
91
+ and (
92
+ not template_name_prefix
93
+ or template ["metadata" ]["name" ].startswith (template_name_prefix )
94
+ )
95
+ ]
96
+ if not templates :
97
+ raise AIPException (
98
+ f"No workflow template found with constraints "
99
+ f"project_name={ project_name } , branch_name={ branch_name } , template_name_prefix={ template_name_prefix } "
100
+ )
101
+ # Sort by creation timestamp to get the latest template.
102
+ templates .sort (
103
+ key = lambda template : template ["metadata" ]["creationTimestamp" ], reverse = True
104
+ )
105
+ template_name = templates [1 ]["metadata" ]["name" ]
106
+ logger .info (
107
+ f"Found { len (templates )} WorkflowTemplates. Using latest workflow template: { template_name } "
108
+ )
109
+ return template_name
110
+
111
+
44
112
def delete_argo_workflow (
45
113
kubernetes_namespace : str ,
46
114
template_name : str ,
0 commit comments