Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor resource op sample for sample test coverage #2274

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 64 additions & 27 deletions samples/core/resource_ops/resource_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,82 @@
# limitations under the License.


"""Note that this sample is just to show the ResourceOp's usage.

It is not a good practice to put password as a pipeline argument, since it will
be visible on KFP UI.
"""
This example demonstrates how to use ResourceOp to specify the value of env var.
"""

import json
import kfp
import kfp.dsl as dsl
from kubernetes import client as k8s_client
from string import Template


_ENV_CONFIG = """
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "sample-env-config"
},
"data": {
"SPECIAL_LEVEL": "level",
"SPECIAL_TYPE": "type"
}
}
"""

_CONTAINER_MANIFEST = """
{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"generateName": "resourceop-basic-job-"
},
"spec": {
"template": {
"metadata": {
"name": "resource-basic"
},
"spec": {
"containers": [{
"name": "sample-container",
"image": "k8s.gcr.io/busybox",
"command": ["/bin/sh", "-c", "env"],
"envFrom": [{
"configMapRef": {
"name": "sample-env-config"
}
}]
}],
"restartPolicy": "Never"
}
},
"backoffLimit": 4
}
}
"""


@dsl.pipeline(
name="ResourceOp Basic",
description="A Basic Example on ResourceOp Usage."
)
def resourceop_basic(username, password):
secret_resource = k8s_client.V1Secret(
api_version="v1",
kind="Secret",
metadata=k8s_client.V1ObjectMeta(generate_name="my-secret-"),
type="Opaque",
data={"username": username, "password": password}
)
rop = dsl.ResourceOp(
name="create-my-secret",
k8s_resource=secret_resource,
attribute_outputs={"name": "{.metadata.name}"}
def resourceop_basic():
# Create a ConfigMap.
env_config_map_op = dsl.ResourceOp(
name='environment-config',
k8s_resource=json.loads(_ENV_CONFIG),
action='create'
)

secret = k8s_client.V1Volume(
name="my-secret",
secret=k8s_client.V1SecretVolumeSource(secret_name=rop.output)
)
# Referring the ConfigMap in a Container.
cop = dsl.ResourceOp(
name='test-step',
k8s_resource=json.loads(_CONTAINER_MANIFEST),
action='create'
).after(env_config_map_op)

cop = dsl.ContainerOp(
name="cop",
image="library/bash:4.4.23",
command=["sh", "-c"],
arguments=["ls /etc/secret-volume"],
pvolumes={"/etc/secret-volume": secret}
)

if __name__ == '__main__':
kfp.compiler.Compiler().compile(resourceop_basic, __file__ + '.zip')
1 change: 1 addition & 0 deletions test/sample_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ spec:
- loop_output
- loop_parameter
- loop_static
- resource_ops
# Build and push image
- name: build-image-by-dockerfile
inputs:
Expand Down