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

Fix edge cases such as disable istio-inject for Tekton compiler #119

Merged
merged 3 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 2 additions & 29 deletions sdk/python/kfp_tekton/compiler/_op_to_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@
from .. import tekton_api_version


class literal_str(str):
"""Literal string class for pyyaml

Literal string class is used for converting string with newline into
yaml's literal string format with '|'. In pyyaml, literal string
conversion is not natively supported in the default dumper.
Therefore, we need to define this class as part of the dumper
before compiling it into yaml.
"""
pass


def _get_base_step(name: str):
"""Base image step for running bash commands.

Expand Down Expand Up @@ -329,12 +317,11 @@ def _process_output_artifacts(outputs_dict: Dict[Text, Any],
mounted_artifact_paths = []
for artifact in outputs_dict['artifacts']:
if artifact['name'] in replaced_param_list:
print(replaced_param_list)
copy_artifacts_step['script'] = copy_artifacts_step['script'] + \
'mc cp $(results.%s.path) storage/%s/runs/$PIPELINERUN/$PODNAME/%s' % (artifact_to_result_mapping[artifact['name']], bucket, artifact['path'].rsplit("/", 1)[1])
'mc cp $(results.%s.path) storage/%s/runs/$PIPELINERUN/$PODNAME/%s\n' % (artifact_to_result_mapping[artifact['name']], bucket, artifact['path'].rsplit("/", 1)[1])
else:
copy_artifacts_step['script'] = copy_artifacts_step['script'] + \
'mc cp %s storage/%s/runs/$PIPELINERUN/$PODNAME/%s' % (artifact['path'], bucket, artifact['path'].rsplit("/", 1)[1])
'mc cp %s storage/%s/runs/$PIPELINERUN/$PODNAME/%s\n' % (artifact['path'], bucket, artifact['path'].rsplit("/", 1)[1])
if artifact['path'].rsplit("/", 1)[0] not in mounted_artifact_paths:
volume_mount_step_template.append({'name': artifact['name'], 'mountPath': artifact['path'].rsplit("/", 1)[0]})
volume_template.append({'name': artifact['name'], 'emptyDir': {}})
Expand Down Expand Up @@ -445,17 +432,6 @@ def _op_to_template(op: BaseOp, enable_artifacts=False):
if processed_op.init_containers:
template['spec']['steps'] = _prepend_steps(processed_op.init_containers, template['spec']['steps'])

# initial base_step and volume setup
base_step = {
'image': 'busybox',
'name': 'copy-results',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see the copy-results step in the compiled YAML and in a comment, does that come from somewhere else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this into a different function, but somehow it get put back after the merge. We no longer need this over here because it is initiated at the beginning of this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, thanks :-)

'script': '#!/bin/sh\nset -exo pipefail\n'
}
volume_mount_step_template = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't those still being used in the code below?

volume_template = []
mounted_param_paths = []
replaced_param_list = []

# inputs
input_artifact_paths = processed_op.input_artifact_paths if isinstance(processed_op, dsl.ContainerOp) else None
artifact_arguments = processed_op.artifact_arguments if isinstance(processed_op, dsl.ContainerOp) else None
Expand All @@ -475,7 +451,6 @@ def _op_to_template(op: BaseOp, enable_artifacts=False):
mount_path = artifact['path'].rsplit("/", 1)[0]
if mount_path not in mounted_param_paths:
_add_mount_path(artifact['name'], artifact['path'], mount_path, volume_mount_step_template, volume_template, mounted_param_paths)
copy_inputs_step['script'] = literal_str(copy_inputs_step['script'])
template['spec']['steps'] = _prepend_steps([copy_inputs_step], template['spec']['steps'])
_update_volumes(template, volume_mount_step_template, volume_template)

Expand All @@ -502,11 +477,9 @@ def _op_to_template(op: BaseOp, enable_artifacts=False):
replaced_param_list,
artifact_to_result_mapping)
if mounted_param_paths:
copy_results_step['script'] = literal_str(copy_results_step['script'])
template['spec']['steps'].append(copy_results_step)
_update_volumes(template, volume_mount_step_template, volume_template)
if copy_artifacts_step:
copy_artifacts_step['script'] = literal_str(copy_artifacts_step['script'])
template['spec']['steps'].append(copy_artifacts_step)

# **********************************************************
Expand Down
22 changes: 7 additions & 15 deletions sdk/python/kfp_tekton/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import textwrap
from typing import Callable, Set, List, Text, Dict, Tuple, Any, Union, Optional

from ._op_to_template import _op_to_template, literal_str
from ._op_to_template import _op_to_template

from kfp import dsl
from kfp.compiler._default_transformers import add_pod_env
Expand All @@ -36,18 +36,6 @@
from .. import tekton_api_version


def _literal_str_representer(dumper, data):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot the history of this, can you quickly explain why we had to add this and why it's no longer needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add literal style to make the script part looks nicer. But there's some edge cases with pyyaml when using it on integer since it's designed with yaml 1.1

Here's the definition of literal style in yaml
https://yaml.org/spec/1.2/spec.html#id2795688

"""pyyaml representer for literal yaml string dumper

Create a representer for the literal string class that converts the string
object with newline into yaml's literal string '|' style.
"""
return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')

# Add the _literal_str_representer as part of the yaml dumper.
yaml.add_representer(literal_str, _literal_str_representer)


class TektonCompiler(Compiler) :
"""DSL Compiler to generate Tekton YAML.

Expand Down Expand Up @@ -296,7 +284,7 @@ def _process_resourceOp(self, task_refs, pipeline):
valueFrom: '%s'
""" % (value[0], value[1]))
output_values += output_value
tp['value'] = literal_str(output_values)
tp['value'] = output_values


def _workflow_with_pipelinerun(self, task_refs, pipeline, pipeline_template, workflow):
Expand Down Expand Up @@ -497,7 +485,11 @@ def _create_pipeline_workflow(self, args, pipeline, op_transformers=None, pipeli
'apiVersion': tekton_api_version,
'kind': 'Pipeline',
'metadata': {
'name': pipeline.name or 'Pipeline'
'name': pipeline.name or 'Pipeline',
'annotations': {
# Disable Istio inject since Tekton cannot run with Istio sidecar
'sidecar.istio.io/inject': 'false'
}
},
'spec': {
'params': params,
Expand Down
21 changes: 21 additions & 0 deletions sdk/python/tests/compiler/compiler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import unittest
import yaml
import re
import textwrap

from kfp_tekton import compiler

Expand All @@ -26,6 +27,24 @@
# in order to generate new "golden" YAML files
GENERATE_GOLDEN_YAML = False

# License header for Kubeflow project
LICENSE_HEADER = textwrap.dedent("""\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks :-)

# Copyright 2020 kubeflow.org
#
# 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.

""")


class TestTektonCompiler(unittest.TestCase):

Expand Down Expand Up @@ -236,6 +255,8 @@ def _test_pipeline_workflow(self,
compiled = list(yaml.safe_load_all(f))
if GENERATE_GOLDEN_YAML:
with open(golden_yaml_file, 'w') as f:
f.write(LICENSE_HEADER)
with open(golden_yaml_file, 'a+') as f:
yaml.dump_all(compiled, f, default_flow_style=False)
else:
with open(golden_yaml_file, 'r') as f:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/affinity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"description": "A pipeline with affinity",
"name": "affinity"}'
sidecar.istio.io/inject: 'false'
name: affinity
spec:
params: []
Expand Down
11 changes: 8 additions & 3 deletions sdk/python/tests/compiler/testdata/artifact_location.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ spec:
name: $(inputs.params.secret_name)
image: minio/mc
name: copy-artifacts
script: |-
#!/usr/bin/env sh
mc config host add storage http://minio-service.$(inputs.params.namespace):9000 $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
script: '#!/usr/bin/env sh

mc config host add storage http://minio-service.$(inputs.params.namespace):9000
$AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY

mc cp $(results.output.path) storage/$(inputs.params.bucket)/runs/$PIPELINERUN/$PODNAME/output.txt

'
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
Expand All @@ -74,6 +78,7 @@ metadata:
true, "type": "String"}, {"default": "kubeflow", "name": "namespace", "optional":
true, "type": "String"}, {"default": "mlpipeline", "name": "bucket", "optional":
true, "type": "String"}], "name": "custom_artifact_location_pipeline"}'
sidecar.istio.io/inject: 'false'
name: custom-artifact-location-pipeline
spec:
params:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/condition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"description": "shows how to use dsl.Condition.",
"name": "pipeline flip coin"}'
sidecar.istio.io/inject: 'false'
name: pipeline-flip-coin
spec:
params: []
Expand Down
8 changes: 6 additions & 2 deletions sdk/python/tests/compiler/testdata/hidden_output_file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ spec:
name: download-file
- image: busybox
name: copy-results
script: |
#!/bin/sh
script: '#!/bin/sh

set -exo pipefail

cp /tmp/results.txt $(results.data.path);

'
volumes:
- emptyDir: {}
name: data
Expand Down Expand Up @@ -65,6 +68,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"description": "Run a script that passes
file to a non configurable path", "name": "Hidden output file pipeline"}'
sidecar.istio.io/inject: 'false'
name: hidden-output-file-pipeline
spec:
params: []
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/imagepullsecrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ metadata:
pipelines.kubeflow.org/pipeline_spec: '{"description": "Get Most Frequent Word
and Save to GCS", "inputs": [{"default": "This is testing", "name": "message",
"optional": true}], "name": "Save Most Frequent"}'
sidecar.istio.io/inject: 'false'
name: save-most-frequent
spec:
params:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/init_container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"description": "A pipeline with init container.",
"name": "InitContainer"}'
sidecar.istio.io/inject: 'false'
name: initcontainer
spec:
params: []
Expand Down
30 changes: 22 additions & 8 deletions sdk/python/tests/compiler/testdata/input_artifact_raw_value.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ spec:
steps:
- image: busybox
name: copy-inputs
script: |
#!/bin/sh
script: '#!/bin/sh

set -exo pipefail

echo -n "Constant artifact value" > /tmp/inputs/text/data

'
- command:
- cat
- /tmp/inputs/text/data
Expand All @@ -49,10 +52,13 @@ spec:
steps:
- image: busybox
name: copy-inputs
script: |
#!/bin/sh
script: '#!/bin/sh

set -exo pipefail

echo -n "Constant artifact value" > /tmp/inputs/text/data

'
- command:
- cat
- /tmp/inputs/text/data
Expand All @@ -74,10 +80,13 @@ spec:
steps:
- image: busybox
name: copy-inputs
script: |
#!/bin/sh
script: '#!/bin/sh

set -exo pipefail

echo -n "hard-coded artifact value" > /tmp/inputs/text/data

'
- command:
- cat
- /tmp/inputs/text/data
Expand All @@ -99,11 +108,15 @@ spec:
steps:
- image: busybox
name: copy-inputs
script: |
#!/bin/sh
script: '#!/bin/sh

set -exo pipefail

echo -n "Text from a file with hard-coded artifact value

" > /tmp/inputs/text/data

'
- command:
- cat
- /tmp/inputs/text/data
Expand All @@ -120,6 +133,7 @@ metadata:
pipelines.kubeflow.org/pipeline_spec: '{"description": "Pipeline shows how to
define artifact inputs and pass raw artifacts to them.", "name": "Pipeline with
artifact input raw argument value."}'
sidecar.istio.io/inject: 'false'
name: pipeline-with-artifact-input-raw-argument-value
spec:
params: []
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/katib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ metadata:
{"default": "60", "name": "experimentTimeoutMinutes", "optional": true}, {"default":
"True", "name": "deleteAfterDone", "optional": true}], "name": "Launch katib
experiment"}'
sidecar.istio.io/inject: 'false'
name: launch-katib-experiment
spec:
params:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/loop_static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"inputs": [{"default": "10", "name": "my_pipe_param",
"optional": true}], "name": "my-pipeline"}'
sidecar.istio.io/inject: 'false'
name: my-pipeline
spec:
params:
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/node_selector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ metadata:
annotations:
pipelines.kubeflow.org/pipeline_spec: '{"description": "A pipeline with Node Selector",
"name": "node_selector"}'
sidecar.istio.io/inject: 'false'
name: node-selector
spec:
params: []
Expand Down
1 change: 1 addition & 0 deletions sdk/python/tests/compiler/testdata/parallel_join.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ metadata:
in parallel and prints the concatenated result.", "inputs": [{"default": "gs://ml-pipeline-playground/shakespeare1.txt",
"name": "url1", "optional": true}, {"default": "gs://ml-pipeline-playground/shakespeare2.txt",
"name": "url2", "optional": true}], "name": "Parallel pipeline"}'
sidecar.istio.io/inject: 'false'
name: parallel-pipeline
spec:
params:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ metadata:
in parallel and prints the concatenated result.", "inputs": [{"default": "gs://ml-pipeline-playground/shakespeare1.txt",
"name": "url1", "optional": true}, {"default": "gs://ml-pipeline-playground/shakespeare2.txt",
"name": "url2", "optional": true}], "name": "Parallel pipeline"}'
sidecar.istio.io/inject: 'false'
name: parallel-pipeline
spec:
params:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2020 kubeflow.org
#
# 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.

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
Expand Down Expand Up @@ -68,6 +82,7 @@ metadata:
[{"default": "gs://ml-pipeline-playground/shakespeare1.txt", "name": "url1",
"optional": true}, {"default": "gs://ml-pipeline-playground/shakespeare2.txt",
"name": "url2", "optional": true}], "name": "Parallel pipeline with argo vars"}'
sidecar.istio.io/inject: 'false'
name: parallel-pipeline-with-argo-vars
spec:
params:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ metadata:
pipelines.kubeflow.org/pipeline_spec: '{"description": "The pipeline shows how
to apply functions to all ops in the pipeline by pipeline transformers", "name":
"Pipeline transformer"}'
sidecar.istio.io/inject: 'false'
name: pipeline-transformer
spec:
params: []
Expand Down
Loading