forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend + SDK): Add backend and SDK support to use Kubernetes Fi…
…eldPath as env (kubeflow#10496) Signed-off-by: Tommy Li <[email protected]>
1 parent
bb36d7a
commit 8412015
Showing
8 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 The Kubeflow Authors | ||
# | ||
# 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 google.protobuf import json_format | ||
from kfp.dsl import PipelineTask | ||
from kfp.kubernetes import common | ||
from kfp.kubernetes import kubernetes_executor_config_pb2 as pb | ||
|
||
|
||
def use_field_path_as_env( | ||
task: PipelineTask, | ||
env_name: str, | ||
field_path: str, | ||
) -> PipelineTask: | ||
"""Use a Kubernetes Field Path as an environment variable as described in | ||
https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information | ||
Args: | ||
task: Pipeline task. | ||
env_name: Name of the enviornment variable. | ||
field_path: Kubernetes field path to expose as the enviornment variable. | ||
Returns: | ||
Task object with updated field path as the enviornment variable. | ||
""" | ||
|
||
msg = common.get_existing_kubernetes_config_as_message(task) | ||
field_path_as_env = pb.FieldPathAsEnv( | ||
name=env_name, | ||
field_path=field_path, | ||
) | ||
msg.field_path_as_env.append(field_path_as_env) | ||
task.platform_config['kubernetes'] = json_format.MessageToDict(msg) | ||
|
||
return task |
36 changes: 36 additions & 0 deletions
36
kubernetes_platform/python/test/snapshot/data/field_path_as_env.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2023 The Kubeflow Authors | ||
# | ||
# 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 kfp import dsl | ||
from kfp import kubernetes | ||
|
||
|
||
@dsl.component | ||
def comp(): | ||
pass | ||
|
||
|
||
@dsl.pipeline | ||
def my_pipeline(): | ||
task = comp() | ||
kubernetes.use_field_path_as_env( | ||
task, | ||
env_name='KFP_RUN_NAME', | ||
field_path="metadata.annotations['pipelines.kubeflow.org/run_name']" | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
from kfp import compiler | ||
compiler.Compiler().compile(my_pipeline, __file__.replace('.py', '.yaml')) |
58 changes: 58 additions & 0 deletions
58
kubernetes_platform/python/test/snapshot/data/field_path_as_env.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# PIPELINE DEFINITION | ||
# Name: my-pipeline | ||
components: | ||
comp-comp: | ||
executorLabel: exec-comp | ||
deploymentSpec: | ||
executors: | ||
exec-comp: | ||
container: | ||
args: | ||
- --executor_input | ||
- '{{$}}' | ||
- --function_to_execute | ||
- comp | ||
command: | ||
- sh | ||
- -c | ||
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\ | ||
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\ | ||
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.6.0'\ | ||
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' && \"\ | ||
$0\" \"$@\"\n" | ||
- sh | ||
- -ec | ||
- 'program_path=$(mktemp -d) | ||
printf "%s" "$0" > "$program_path/ephemeral_component.py" | ||
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@" | ||
' | ||
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\ | ||
\ *\n\ndef comp():\n pass\n\n" | ||
image: python:3.7 | ||
pipelineInfo: | ||
name: my-pipeline | ||
root: | ||
dag: | ||
tasks: | ||
comp: | ||
cachingOptions: | ||
enableCache: true | ||
componentRef: | ||
name: comp-comp | ||
taskInfo: | ||
name: comp | ||
schemaVersion: 2.1.0 | ||
sdkVersion: kfp-2.6.0 | ||
--- | ||
platforms: | ||
kubernetes: | ||
deploymentSpec: | ||
executors: | ||
exec-comp: | ||
fieldPathAsEnv: | ||
- fieldPath: metadata.annotations['pipelines.kubeflow.org/run_name'] | ||
name: KFP_RUN_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2024 The Kubeflow Authors | ||
# | ||
# 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 google.protobuf import json_format | ||
from kfp import dsl | ||
from kfp import kubernetes | ||
|
||
|
||
class TestUseFieldPathAsEnv: | ||
|
||
def test_use_one(self): | ||
|
||
@dsl.pipeline | ||
def my_pipeline(): | ||
task = comp() | ||
kubernetes.use_field_path_as_env( | ||
task, | ||
env_name="KFP_RUN_NAME", | ||
field_path="metadata.annotations['pipelines.kubeflow.org/run_name']" | ||
) | ||
|
||
assert json_format.MessageToDict(my_pipeline.platform_spec) == { | ||
'platforms': { | ||
'kubernetes': { | ||
'deploymentSpec': { | ||
'executors': { | ||
'exec-comp': { | ||
'fieldPathAsEnv': [{ | ||
'name': | ||
'KFP_RUN_NAME', | ||
'fieldPath': | ||
'metadata.annotations[\'pipelines.kubeflow.org/run_name\']' | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def test_use_two(self): | ||
|
||
@dsl.pipeline | ||
def my_pipeline(): | ||
task = comp() | ||
kubernetes.use_field_path_as_env( | ||
task, | ||
env_name="KFP_RUN_NAME", | ||
field_path="metadata.annotations['pipelines.kubeflow.org/run_name']" | ||
) | ||
kubernetes.use_field_path_as_env( | ||
task, | ||
env_name="POD_NAME", | ||
field_path="metadata.name" | ||
) | ||
|
||
assert json_format.MessageToDict(my_pipeline.platform_spec) == { | ||
'platforms': { | ||
'kubernetes': { | ||
'deploymentSpec': { | ||
'executors': { | ||
'exec-comp': { | ||
'fieldPathAsEnv': [{ | ||
'name': | ||
'KFP_RUN_NAME', | ||
'fieldPath': | ||
'metadata.annotations[\'pipelines.kubeflow.org/run_name\']' | ||
}, | ||
{ | ||
'name': | ||
'POD_NAME', | ||
'fieldPath': | ||
'metadata.name' | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@dsl.component | ||
def comp(): | ||
pass |