Skip to content

Commit

Permalink
Refactor resource op sample for sample test coverage (#2274)
Browse files Browse the repository at this point in the history
* Initial.

* add sample test coverage

* Try default namespace

* Remove the use of config map.

* Remove configmap

* simplify container command
  • Loading branch information
Jiaxiao Zheng authored and k8s-ci-robot committed Oct 2, 2019
1 parent bf81e63 commit 022c73d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
66 changes: 38 additions & 28 deletions samples/core/resource_ops/resource_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,55 @@
# 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


_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": ["/usr/bin/env"]
}],
"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():

secret = k8s_client.V1Volume(
name="my-secret",
secret=k8s_client.V1SecretVolumeSource(secret_name=rop.output)
# Start a container. Print out env vars.
op = dsl.ResourceOp(
name='test-step',
k8s_resource=json.loads(_CONTAINER_MANIFEST),
action='create'
)

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

0 comments on commit 022c73d

Please sign in to comment.