-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy path_pipeline.py
220 lines (173 loc) · 6.89 KB
/
_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright 2018-2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from . import _container_op
from . import _resource_op
from . import _ops_group
from ..components._naming import _make_name_unique_by_adding_index
import sys
# This handler is called whenever the @pipeline decorator is applied.
# It can be used by command-line DSL compiler to inject code that runs for every pipeline definition.
_pipeline_decorator_handler = None
def pipeline(name : str = None, description : str = None):
"""Decorator of pipeline functions.
Usage:
```python
@pipeline(
name='my awesome pipeline',
description='Is it really awesome?'
)
def my_pipeline(a: PipelineParam, b: PipelineParam):
...
```
"""
def _pipeline(func):
if name:
func._pipeline_name = name
if description:
func._pipeline_description = description
if _pipeline_decorator_handler:
return _pipeline_decorator_handler(func) or func
else:
return func
return _pipeline
class PipelineConf():
"""PipelineConf contains pipeline level settings
"""
def __init__(self):
self.image_pull_secrets = []
self.artifact_location = None
def set_image_pull_secrets(self, image_pull_secrets):
"""Configures the pipeline level imagepullsecret
Args:
image_pull_secrets: a list of Kubernetes V1LocalObjectReference
For detailed description, check Kubernetes V1LocalObjectReference definition
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1LocalObjectReference.md
"""
self.image_pull_secrets = image_pull_secrets
return self
def set_artifact_location(self, artifact_location):
"""Configures the pipeline level artifact location.
Example::
from kfp.dsl import ArtifactLocation, get_pipeline_conf, pipeline
from kubernetes.client.models import V1SecretKeySelector
@pipeline(name='foo', description='hello world')
def foo_pipeline(tag: str, pull_image_policy: str):
'''A demo pipeline'''
# create artifact location object
artifact_location = ArtifactLocation.s3(
bucket="foo",
endpoint="minio-service:9000",
insecure=True,
access_key_secret=V1SecretKeySelector(name="minio", key="accesskey"),
secret_key_secret=V1SecretKeySelector(name="minio", key="secretkey"))
# config pipeline level artifact location
conf = get_pipeline_conf().set_artifact_location(artifact_location)
# rest of codes
...
Args:
artifact_location: V1alpha1ArtifactLocation object
For detailed description, check Argo V1alpha1ArtifactLocation definition
https://github.com/e2fyi/argo-models/blob/release-2.2/argo/models/v1alpha1_artifact_location.py
https://github.com/argoproj/argo/blob/release-2.2/api/openapi-spec/swagger.json
"""
self.artifact_location = artifact_location
return self
def get_pipeline_conf():
"""Configure the pipeline level setting to the current pipeline
Note: call the function inside the user defined pipeline function.
"""
return Pipeline.get_default_pipeline().conf
#TODO: Pipeline is in fact an opsgroup, refactor the code.
class Pipeline():
"""A pipeline contains a list of operators.
This class is not supposed to be used by pipeline authors since pipeline authors can use
pipeline functions (decorated with @pipeline) to reference their pipelines. This class
is useful for implementing a compiler. For example, the compiler can use the following
to get the pipeline object and its ops:
```python
with Pipeline() as p:
pipeline_func(*args_list)
traverse(p.ops)
```
"""
# _default_pipeline is set when it (usually a compiler) runs "with Pipeline()"
_default_pipeline = None
@staticmethod
def get_default_pipeline():
"""Get default pipeline. """
return Pipeline._default_pipeline
@staticmethod
def add_pipeline(name, description, func):
"""Add a pipeline function with the specified name and description."""
# Applying the @pipeline decorator to the pipeline function
func = pipeline(name=name, description=description)(func)
def __init__(self, name: str):
"""Create a new instance of Pipeline.
Args:
name: the name of the pipeline. Once deployed, the name will show up in Pipeline System UI.
"""
self.name = name
self.ops = {}
# Add the root group.
self.groups = [_ops_group.OpsGroup('pipeline', name=name)]
self.group_id = 0
self.conf = PipelineConf()
self._metadata = None
def __enter__(self):
if Pipeline._default_pipeline:
raise Exception('Nested pipelines are not allowed.')
Pipeline._default_pipeline = self
def register_op_and_generate_id(op):
return self.add_op(op, op.is_exit_handler)
self._old__register_op_handler = _container_op._register_op_handler
_container_op._register_op_handler = register_op_and_generate_id
return self
def __exit__(self, *args):
Pipeline._default_pipeline = None
_container_op._register_op_handler = self._old__register_op_handler
def add_op(self, op: _container_op.BaseOp, define_only: bool):
"""Add a new operator.
Args:
op: An operator of ContainerOp, ResourceOp or their inherited types.
Returns
op_name: a unique op name.
"""
#If there is an existing op with this name then generate a new name.
op_name = _make_name_unique_by_adding_index(op.human_name, list(self.ops.keys()), ' ')
self.ops[op_name] = op
if not define_only:
self.groups[-1].ops.append(op)
return op_name
def push_ops_group(self, group: _ops_group.OpsGroup):
"""Push an OpsGroup into the stack.
Args:
group: An OpsGroup. Typically it is one of ExitHandler, Branch, and Loop.
"""
self.groups[-1].groups.append(group)
self.groups.append(group)
def pop_ops_group(self):
"""Remove the current OpsGroup from the stack."""
del self.groups[-1]
def get_next_group_id(self):
"""Get next id for a new group. """
self.group_id += 1
return self.group_id
def _set_metadata(self, metadata):
'''_set_metadata passes the containerop the metadata information
Args:
metadata (ComponentMeta): component metadata
'''
if not isinstance(metadata, PipelineMeta):
raise ValueError('_set_medata is expecting PipelineMeta.')
self._metadata = metadata