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

Move imagepullsecrets sample to samples/core #1767

Merged
merged 15 commits into from
Aug 9, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add image pull secret sample.
  • Loading branch information
numerology committed Aug 2, 2019
commit aa0c3b6a40710136ee0bfa7abbc8a86022a42b06
176 changes: 176 additions & 0 deletions samples/notebooks/imagepullsecret.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sample of using image pull secrets\n",
"ImagePullSecrets is a way provided by kubernetes to access hidden/protected images. It is a list of references to secrets. Please refer to [here](https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod) and [Argo reference](https://github.com/argoproj/argo/blob/47eba519107c229edf61dbe024a6a5e0f1618a8d/pkg/apis/workflow/v1alpha1/types.go#L115) for more details.\n",
"\n",
"In KFP, we can simply set the ImagePullSecrets for each pipeline to grant it the ability to access the images in other registries when needed. Please see the example below for a simple demonstration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip3 install kfp --upgrade"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import kfp\n",
"import kfp.dsl as dsl\n",
"from kfp import compiler\n",
"from kubernetes import client as k8s_client"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class GetFrequentWordOp(dsl.ContainerOp):\n",
" \"\"\"A get frequent word class representing a component in ML Pipelines.\n",
" \"\"\"\n",
" def __init__(self, name, message):\n",
" \"\"\"Args:\n",
" name: An identifier of the step which needs to be unique within a pipeline.\n",
" message: a dsl.PipelineParam object representing an input message.\n",
" \"\"\"\n",
" super(GetFrequentWordOp, self).__init__(\n",
" name=name,\n",
" image='python:3.5-jessie',\n",
" command=['sh', '-c'],\n",
" arguments=['python -c \"from collections import Counter; '\n",
" 'words = Counter(\\'%s\\'.split()); print(max(words, key=words.get))\" '\n",
" '| tee /tmp/message.txt' % message],\n",
" file_outputs={'word': '/tmp/message.txt'})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we defined a KFP component by directly specifying the base image and the python command it runs. Note that we're using a public image so actually there is no need to use imagepullsecrets. However, if the image lives within a private/protected container registry, an imagepullsecret might be necessary to authenticate the pipeline in order to pull the image."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"@dsl.pipeline(\n",
" name='Save Most Frequent',\n",
" description='Get Most Frequent Word and Save to GCS'\n",
")\n",
"def save_most_frequent_word(message: str):\n",
" \"\"\"A pipeline function describing the orchestration of the workflow.\"\"\"\n",
"\n",
" counter = GetFrequentWordOp(\n",
" name='get-Frequent',\n",
" message=message)\n",
" dsl.get_pipeline_conf().set_image_pull_secrets([k8s_client.V1ObjectReference(name=\"secretA\")])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Compile the pipeline.\n",
"compiler.Compiler().compile(save_most_frequent_word, 'save_most_frequent.yaml')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above code should generate following `save_most_frequent.yaml` pipeline definition.\n",
"```yaml\n",
"apiVersion: argoproj.io/v1alpha1\n",
"kind: Workflow\n",
"metadata:\n",
" generateName: save-most-frequent-\n",
"spec:\n",
" arguments:\n",
" parameters:\n",
" - name: message\n",
" entrypoint: save-most-frequent\n",
" imagePullSecrets:\n",
" - name: secretA\n",
" serviceAccountName: pipeline-runner\n",
" templates:\n",
" - container:\n",
" args:\n",
" - python -c \"from collections import Counter; words = Counter('{{inputs.parameters.message}}'.split());\n",
" print(max(words, key=words.get))\" | tee /tmp/message.txt\n",
" command:\n",
" - sh\n",
" - -c\n",
" image: python:3.5-jessie\n",
" inputs:\n",
" parameters:\n",
" - name: message\n",
" name: get-frequent\n",
" outputs:\n",
" artifacts:\n",
" - name: mlpipeline-ui-metadata\n",
" optional: true\n",
" path: /mlpipeline-ui-metadata.json\n",
" - name: mlpipeline-metrics\n",
" optional: true\n",
" path: /mlpipeline-metrics.json\n",
" parameters:\n",
" - name: get-frequent-word\n",
" valueFrom:\n",
" path: /tmp/message.txt\n",
" - dag:\n",
" tasks:\n",
" - arguments:\n",
" parameters:\n",
" - name: message\n",
" value: '{{inputs.parameters.message}}'\n",
" name: get-frequent\n",
" template: get-frequent\n",
" inputs:\n",
" parameters:\n",
" - name: message\n",
" name: save-most-frequent\n",
"```\n",
"\n",
"When looking into the `save_most_frequent.yaml` generated, one can find that the generated workflow is associated with an imagePullSecrets specified via its name."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}