如何从组件中获取 运行 的 ID?

How to get the id of the run from within a component?

我正在对 Kubeflow Pipelines 进行一些实验,我有兴趣检索 运行 id 以与有关管道执行的一些元数据一起保存。有什么办法可以从像 ContainerOp?

这样的组件做到这一点

我尝试使用 Python's DSL 来做到这一点,但现在似乎不可能。

我找到的唯一选择是使用他们在 this sample code 中使用的方法。您基本上声明了一个包含 {{workflow.uid}} 的字符串。它将在执行期间替换为实际值。

您也可以这样做以获得 pod 名称,它将是 {{pod.name}}

您可以使用 kfp.dsl.EXECUTION_ID_PLACEHOLDERkfp.dsl.RUN_ID_PLACEHOLDER 作为组件的参数。在运行时,它们将被替换为实际值。

您的组件的容器应该有一个名为 HOSTNAME 的环境变量,该变量设置为其唯一的 pod 名称,您可以从中获取所有必要的元数据。

create_run_from_pipeline_func 其中 returns RunPipelineResult,并且有 run_id attribute

client = kfp.Client(host)
result = client.create_run_from_pipeline_func(…) 
result.run_id

由于kubeflow管道依赖于argo,你可以使用argo变量来获取你想要的。

例如,

@func_to_container_op
def dummy(run_id, run_name) -> str:
    return run_id, run_name

@dsl.pipeline(
    name='test_pipeline',
)
def test_pipeline():
  dummy('{{workflow.labels.pipeline/runid}}', '{{workflow.annotations.pipelines.kubeflow.org/run_name}}')

您会发现占位符将被替换为正确的 run_id 和 run_name。

更多 argo 变量:https://github.com/argoproj/argo-workflows/blob/master/docs/variables.md

要知道kubeflow pipeline中的labels和annotation都记录了什么运行,从k8s中获取对应的workflow即可。

kubectl get workflow/XXX -oyaml