|
| 1 | +# Copyright 2022 kubeflow.org |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from attr import dataclass, asdict, fields |
| 17 | + |
| 18 | +from kfp import dsl |
| 19 | +from kfp.components import load_component_from_text |
| 20 | +from kfp_tekton.compiler import TektonCompiler |
| 21 | +from kfp_tekton.tekton import AddOnGroup |
| 22 | + |
| 23 | +CEL_TASK_IMAGE_NAME = "veryunique/image:latest" |
| 24 | +from kfp_tekton.tekton import TEKTON_CUSTOM_TASK_IMAGES |
| 25 | +TEKTON_CUSTOM_TASK_IMAGES = TEKTON_CUSTOM_TASK_IMAGES.append(CEL_TASK_IMAGE_NAME) |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class ExitHandlerFields: |
| 30 | + status: dsl.PipelineParam |
| 31 | + status_message: dsl.PipelineParam |
| 32 | + |
| 33 | + def __getitem__(self, item: str) -> dsl.PipelineParam: |
| 34 | + di = asdict(self) |
| 35 | + return di.get(item) or di.get(item.replace('-', '_')) |
| 36 | + |
| 37 | + |
| 38 | +class ExitHandler(AddOnGroup): |
| 39 | + """A custom OpsGroup which maps to a custom task""" |
| 40 | + def __init__(self): |
| 41 | + labels = { |
| 42 | + 'pipelines.kubeflow.org/cache_enabled': 'false', |
| 43 | + } |
| 44 | + annotations = { |
| 45 | + 'ws-pipelines.ibm.com/pipeline-cache-enabled': 'false', |
| 46 | + } |
| 47 | + |
| 48 | + super().__init__( |
| 49 | + kind='Exception', |
| 50 | + api_version='custom.tekton.dev/v1alpha1', |
| 51 | + params={}, |
| 52 | + is_finally=True, |
| 53 | + labels=labels, |
| 54 | + annotations=annotations, |
| 55 | + ) |
| 56 | + |
| 57 | + internal_params = { |
| 58 | + field.name: AddOnGroup.create_internal_param(field.name) |
| 59 | + for field in fields(ExitHandlerFields) |
| 60 | + } |
| 61 | + self._internal_params = internal_params |
| 62 | + self.fields = ExitHandlerFields( |
| 63 | + status=internal_params['status'], |
| 64 | + status_message=internal_params['status_message'], |
| 65 | + ) |
| 66 | + |
| 67 | + def __enter__(self) -> ExitHandlerFields: |
| 68 | + super().__enter__() |
| 69 | + return self.fields |
| 70 | + |
| 71 | + def post_params(self, params: list) -> list: |
| 72 | + params_map = { |
| 73 | + param['name']: param for param in params |
| 74 | + } |
| 75 | + internal_params_names = set(self._internal_params.keys()) |
| 76 | + params_map = { |
| 77 | + k: v for k, v in params_map.items() |
| 78 | + if k not in internal_params_names |
| 79 | + } |
| 80 | + params = list(params_map.values()) |
| 81 | + params.append({ |
| 82 | + 'name': 'pipelinerun_name', |
| 83 | + 'value': '$(context.pipelineRun.name)', |
| 84 | + }) |
| 85 | + return params |
| 86 | + |
| 87 | + def post_task_spec(self, task_spec: dict) -> dict: |
| 88 | + spec = task_spec.get('spec') or {} |
| 89 | + |
| 90 | + pod_template = spec.setdefault('podTemplate', {}) |
| 91 | + pod_template['imagePullSecrets'] = [{'name': 'ai-lifecycle'}] |
| 92 | + pod_template['automountServiceAccountToken'] = 'false' |
| 93 | + |
| 94 | + pipeline_spec = spec.get('pipelineSpec') or {} |
| 95 | + params = pipeline_spec.get('params') or [] |
| 96 | + params_map = { |
| 97 | + param['name']: param for param in params |
| 98 | + } |
| 99 | + params_map.update({ |
| 100 | + k: { |
| 101 | + 'name': k, |
| 102 | + 'type': 'string', |
| 103 | + } for k in self._internal_params.keys() |
| 104 | + }) |
| 105 | + params = list(params_map.values()) |
| 106 | + spec = task_spec.setdefault('spec', {}) |
| 107 | + spec.setdefault('pipelineSpec', {})['params'] = params |
| 108 | + task_spec['spec'] = spec |
| 109 | + return task_spec |
| 110 | + |
| 111 | + |
| 112 | +def PrintOp(name: str, msg: str = None): |
| 113 | + if msg is None: |
| 114 | + msg = name |
| 115 | + print_op = load_component_from_text( |
| 116 | + """ |
| 117 | + name: %s |
| 118 | + inputs: |
| 119 | + - {name: input_text, type: String, description: 'Represents an input parameter.'} |
| 120 | + outputs: |
| 121 | + - {name: output_value, type: String, description: 'Represents an output paramter.'} |
| 122 | + implementation: |
| 123 | + container: |
| 124 | + image: alpine:3.6 |
| 125 | + command: |
| 126 | + - sh |
| 127 | + - -c |
| 128 | + - | |
| 129 | + set -e |
| 130 | + echo $0 > $1 |
| 131 | + - {inputValue: input_text} |
| 132 | + - {outputPath: output_value} |
| 133 | + """ % name |
| 134 | + ) |
| 135 | + return print_op(msg) |
| 136 | + |
| 137 | + |
| 138 | +@dsl.pipeline("test pipeline") |
| 139 | +def test_pipeline(): |
| 140 | + with ExitHandler() as it: |
| 141 | + # PrintOp("print-err-status", it.status) |
| 142 | + cel = load_component_from_text(r""" |
| 143 | + name: cel |
| 144 | + inputs: |
| 145 | + - {name: cel-input} |
| 146 | + outputs: |
| 147 | + - {name: cel-output} |
| 148 | + implementation: |
| 149 | + container: |
| 150 | + image: veryunique/image:latest |
| 151 | + command: [cel] |
| 152 | + args: |
| 153 | + - --apiVersion |
| 154 | + - custom.tekton.dev/v1alpha1 |
| 155 | + - --kind |
| 156 | + - Cel |
| 157 | + - --name |
| 158 | + - cel_123 |
| 159 | + - --status |
| 160 | + - {inputValue: cel-input} |
| 161 | + - --taskSpec |
| 162 | + - '{}' |
| 163 | + """)(it.status) |
| 164 | + cel.add_pod_annotation("valid_container", "false") |
| 165 | + |
| 166 | + PrintOp("print", "some-message") |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + TektonCompiler().compile(test_pipeline, __file__.replace('.py', '.yaml')) |
0 commit comments