-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcli.py
325 lines (286 loc) · 8.32 KB
/
cli.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import logging
import os
import webbrowser
from pathlib import Path
import click
from .auth import AuthHandler
from .config import PluginConfig
from .context_helper import ContextHelper
LOG = logging.getLogger(__name__)
def format_params(params: list):
return dict((p[: p.find(":")], p[p.find(":") + 1 :]) for p in params)
@click.group("Kubeflow")
def commands():
"""Kedro plugin adding support for Kubeflow Pipelines"""
pass
@commands.group(
name="kubeflow", context_settings=dict(help_option_names=["-h", "--help"])
)
@click.option(
"-e",
"--env",
"env",
type=str,
default=lambda: os.environ.get("KEDRO_ENV", "local"),
help="Environment to use.",
)
@click.pass_obj
@click.pass_context
def kubeflow_group(ctx, metadata, env):
"""Interact with Kubeflow Pipelines"""
ctx.ensure_object(dict)
ctx.obj["context_helper"] = ContextHelper.init(
metadata,
env,
)
@kubeflow_group.command()
@click.pass_context
def list_pipelines(ctx):
"""List deployed pipeline definitions"""
context_helper = ctx.obj["context_helper"]
click.echo(context_helper.kfp_client.list_pipelines())
@kubeflow_group.command()
@click.option(
"-i",
"--image",
type=str,
help="Docker image to use for pipeline execution.",
)
@click.option(
"-p",
"--pipeline",
"pipeline",
type=str,
help="Name of pipeline to run",
default="__default__",
)
@click.option(
"-en",
"--experiment-namespace",
"experiment_namespace",
type=str,
default=None,
help="Namespace where pipeline experiment run should be deployed to. Not needed "
"if provided experiment name already exists.",
)
@click.option(
"--param",
"params",
type=str,
multiple=True,
help="Parameters override in form of `key=value`",
)
@click.pass_context
def run_once(
ctx, image: str, pipeline: str, experiment_namespace: str, params: list
):
"""Deploy pipeline as a single run within given experiment.
Config can be specified in kubeflow.yml as well."""
context_helper = ctx.obj["context_helper"]
config = context_helper.config.run_config
context_helper.kfp_client.run_once(
pipeline=pipeline,
image=image if image else config.image,
experiment_name=config.experiment_name,
experiment_namespace=experiment_namespace,
run_name=config.run_name,
wait=config.wait_for_completion,
image_pull_policy=config.image_pull_policy,
parameters=format_params(params),
)
@kubeflow_group.command()
@click.pass_context
def ui(ctx) -> None:
"""Open Kubeflow Pipelines UI in new browser tab"""
host = ctx.obj["context_helper"].config.host
webbrowser.open_new_tab(host)
@kubeflow_group.command()
@click.option(
"-i",
"--image",
type=str,
help="Docker image to use for pipeline execution.",
)
@click.option(
"-p",
"--pipeline",
"pipeline",
type=str,
help="Name of pipeline to run",
default="__default__",
)
@click.option(
"-o",
"--output",
type=str,
default="pipeline.yml",
help="Pipeline YAML definition file.",
)
@click.pass_context
def compile(ctx, image, pipeline, output) -> None:
"""Translates Kedro pipeline into YAML file with Kubeflow Pipeline definition"""
context_helper = ctx.obj["context_helper"]
config = context_helper.config.run_config
context_helper.kfp_client.compile(
pipeline=pipeline,
image_pull_policy=config.image_pull_policy,
image=image if image else config.image,
output=output,
)
@kubeflow_group.command()
@click.option(
"-i",
"--image",
type=str,
help="Docker image to use for pipeline execution.",
)
@click.option(
"-p",
"--pipeline",
"pipeline",
type=str,
help="Name of pipeline to upload",
default="__default__",
)
@click.pass_context
def upload_pipeline(ctx, image, pipeline) -> None:
"""Uploads pipeline to Kubeflow server"""
context_helper = ctx.obj["context_helper"]
config = context_helper.config.run_config
context_helper.kfp_client.upload(
pipeline_name=pipeline,
image=image if image else config.image,
image_pull_policy=config.image_pull_policy,
)
@kubeflow_group.command()
@click.option(
"-p",
"--pipeline",
"pipeline",
type=str,
help="Name of pipeline to run",
default="__default__",
)
@click.option(
"-c",
"--cron-expression",
type=str,
help="Cron expression for recurring run",
required=True,
)
@click.option(
"-x",
"--experiment-name",
"experiment_name",
type=str,
help="Name of experiment associated with this run.",
)
@click.option(
"-en",
"--experiment-namespace",
"experiment_namespace",
type=str,
default=None,
help="Namespace where pipeline experiment run should be deployed to. Not needed "
"if provided experiment name already exists.",
)
@click.option(
"--param",
"params",
type=str,
multiple=True,
help="Parameters override in form of `key=value`",
)
@click.pass_context
def schedule(
ctx,
pipeline: str,
experiment_namespace: str,
experiment_name: str,
cron_expression: str,
params: list,
):
"""Schedules recurring execution of latest version of the pipeline"""
context_helper = ctx.obj["context_helper"]
config = context_helper.config.run_config
experiment = experiment_name if experiment_name else config.experiment_name
context_helper.kfp_client.schedule(
pipeline,
experiment,
experiment_namespace,
cron_expression,
run_name=config.scheduled_run_name,
parameters=format_params(params),
)
@kubeflow_group.command()
@click.argument("kfp_url", type=str)
@click.option("--with-github-actions", is_flag=True, default=False)
@click.pass_context
def init(ctx, kfp_url: str, with_github_actions: bool):
"""Initializes configuration for the plugin"""
context_helper = ctx.obj["context_helper"]
project_name = context_helper.context.project_path.name
if with_github_actions:
image = f"gcr.io/${{google_project_id}}/{project_name}:${{commit_id}}"
run_name = f"{project_name}:${{commit_id}}"
else:
image = project_name
run_name = project_name
sample_config = PluginConfig.sample_config(
url=kfp_url, image=image, project=project_name, run_name=run_name
)
config_path = Path.cwd().joinpath("conf/base/kubeflow.yaml")
with open(config_path, "w") as f:
f.write(sample_config)
click.echo(f"Configuration generated in {config_path}")
if with_github_actions:
PluginConfig.initialize_github_actions(
project_name,
where=Path.cwd(),
templates_dir=Path(__file__).parent / "templates",
)
@kubeflow_group.command(hidden=True)
@click.argument("kubeflow_run_id", type=str)
@click.option(
"--output",
type=str,
default="/tmp/mlflow_run_id",
)
@click.pass_context
def mlflow_start(ctx, kubeflow_run_id: str, output: str):
import mlflow
from kedro_mlflow.framework.context import get_mlflow_config
token = AuthHandler().obtain_id_token()
if token:
os.environ["MLFLOW_TRACKING_TOKEN"] = token
LOG.info("Configuring MLFLOW_TRACKING_TOKEN")
try:
kedro_context = ctx.obj["context_helper"].context
mlflow_conf = get_mlflow_config(kedro_context)
mlflow_conf.setup(kedro_context)
except AttributeError:
kedro_session = ctx.obj["context_helper"].session
with kedro_session:
mlflow_conf = get_mlflow_config(kedro_session)
mlflow_conf.setup()
run = mlflow.start_run(
experiment_id=mlflow_conf.experiment.experiment_id, nested=False
)
mlflow.set_tag("kubeflow_run_id", kubeflow_run_id)
with open(output, "w") as f:
f.write(run.info.run_id)
click.echo(f"Started run: {run.info.run_id}")
@kubeflow_group.command(hidden=True)
@click.argument("pvc_name", type=str)
def delete_pipeline_volume(pvc_name: str):
import kubernetes.client
import kubernetes.config
kubernetes.config.load_incluster_config()
current_namespace = open(
"/var/run/secrets/kubernetes.io/serviceaccount/namespace"
).read()
kubernetes.client.CoreV1Api().delete_namespaced_persistent_volume_claim(
pvc_name,
current_namespace,
)
click.echo(f"Volume removed: {pvc_name}")