如何参数化 workflowTemplateRef?
How to parameterize a workflowTempateRef?
我有一个 WorkflowTemplate“nyc-test-template”,我通过 Argo Events 和 PubSub 触发它。因此,如果我将消息 {}
发布到 PubSub 主题“argo-events-nyc”,则会启动通过 workflowTempateRef
指定的模板。那确实工作得很好。现在我想参数化要启动的模板。
我的无效草稿如下所示:
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
name: pubsub-event-source-nyc
spec:
template:
serviceAccountName: argo-events
pubSub:
examplex:
jsonBody: true
topic: argo-events-nyc
subscriptionID: argo-events-nyc-sub
---
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: pubsub-sensor-nyc
spec:
template:
serviceAccountName: argo-events-sa
dependencies:
- name: pubsub-event-source-dep
eventSourceName: pubsub-event-source-nyc
eventName: examplex
triggers:
- template:
name: argo-workflow-trigger
argoWorkflow:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: nyc-test-template-
namespace: argo
spec:
arguments:
parameters:
- name: wft
value: nyc-test-template
workflowTemplateRef:
# I'm pretty sure this inputs block is useless. But leaving it out
# and instead referencing arguments.parameters.wft won't work either.
inputs:
parameters:
- name: wft
name: "{{inputs.parameters.wft}}"
parameters:
- src:
dependencyName: pubsub-event-source-dep
dataKey: body.wft
dest: spec.arguments.parameters.0.value
我希望发生的事情是这样的:
- 空消息
{}
会触发“nyc-test-template”
- 消息
{"wft": "my-template"}
将触发“我的模板”
相反,发布空消息将导致传感器抛出错误:
2021-03-29T15:31:16.386441528Z2021/03/29 15:31:16 Failed to parse workflow: error unmarshaling JSON: while decoding JSON: json: unknown field "inputs"
坦率地说 - 上面的 yaml 从 this example 中获得了粗略的灵感。这并不是有根据的猜测的结果,因为我仍然不了解参数、参数和输入如何相互作用的机制。
您可以使用 when
根据参数切换要使用的模板。
假设我有两个像这样的简单工作流模板:
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: t1
spec:
templates:
- name: whalesay-template
container:
image: docker/whalesay
command: [cowsay]
args: [t1]
---
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: t2
spec:
templates:
- name: whalesay-template
container:
image: docker/whalesay
command: [cowsay]
args: [t2]
我可以根据传递给工作流的参数(手动或通过 argo-events 设置)选择从 WorkflowTemplates 执行一个或另一个模板。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: switch-
spec:
entrypoint: pick
arguments:
parameters:
- name: which
templates:
- name: pick
steps:
- - name: t1
when: "{{workflow.parameters.which}} == t1"
templateRef:
name: t1
template: whalesay-template
- name: t2
when: "{{workflow.parameters.which}} == t2"
templateRef:
name: t2
template: whalesay-template
对于工作流的顶级参数,您可以使用 workflow.parameters.SOMETHING
。
在上述基础上,您可以使用 JSON 解析工具,例如 jq
来检索切换值,然后根据该值选择您的模板。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: switch-
spec:
entrypoint: pick
arguments:
parameters:
- name: json
templates:
- name: pick
steps:
- - name: parse
template: parse
- - name: t1
when: "{{steps.parse.outputs.result}} == a"
templateRef:
name: t1
template: whalesay-template
- name: t2
when: "{{steps.parse.outputs.result}} == b"
templateRef:
name: t2
template: whalesay-template
- name: parse
container:
image: jorgeandrada/alpine-jq
command: [sh, -c]
env:
- name: JSON
value: "{{workflow.parameters.json}}"
args: [echo "$JSON" | jq -j '.test']
我应该提一下,使用 jq
有点笨拙。在 Argo (3.1+) 的未来版本中,将有工具可以更直接地检查 JSON。但是这个解决方案很好地反向兼容。
致谢名单 Derek Wang。
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: pubsub-sensor-nyc
spec:
template:
serviceAccountName: argo-events-sa
dependencies:
- name: pubsub-event-source-dep
eventSourceName: pubsub-event-source-nyc
eventName: examplex
triggers:
- template:
name: argo-workflow-trigger
argoWorkflow:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: nyc-test-template-
namespace: argo
spec:
workflowTemplateRef:
name: nyc-test-template
parameters:
- src:
dependencyName: pubsub-event-source-dep
dataKey: body.wft
value: nyc-test-template # default value
dest: spec.workflowTemplateRef.name # <- this
我有一个 WorkflowTemplate“nyc-test-template”,我通过 Argo Events 和 PubSub 触发它。因此,如果我将消息 {}
发布到 PubSub 主题“argo-events-nyc”,则会启动通过 workflowTempateRef
指定的模板。那确实工作得很好。现在我想参数化要启动的模板。
我的无效草稿如下所示:
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
name: pubsub-event-source-nyc
spec:
template:
serviceAccountName: argo-events
pubSub:
examplex:
jsonBody: true
topic: argo-events-nyc
subscriptionID: argo-events-nyc-sub
---
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: pubsub-sensor-nyc
spec:
template:
serviceAccountName: argo-events-sa
dependencies:
- name: pubsub-event-source-dep
eventSourceName: pubsub-event-source-nyc
eventName: examplex
triggers:
- template:
name: argo-workflow-trigger
argoWorkflow:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: nyc-test-template-
namespace: argo
spec:
arguments:
parameters:
- name: wft
value: nyc-test-template
workflowTemplateRef:
# I'm pretty sure this inputs block is useless. But leaving it out
# and instead referencing arguments.parameters.wft won't work either.
inputs:
parameters:
- name: wft
name: "{{inputs.parameters.wft}}"
parameters:
- src:
dependencyName: pubsub-event-source-dep
dataKey: body.wft
dest: spec.arguments.parameters.0.value
我希望发生的事情是这样的:
- 空消息
{}
会触发“nyc-test-template” - 消息
{"wft": "my-template"}
将触发“我的模板”
相反,发布空消息将导致传感器抛出错误:
2021-03-29T15:31:16.386441528Z2021/03/29 15:31:16 Failed to parse workflow: error unmarshaling JSON: while decoding JSON: json: unknown field "inputs"
坦率地说 - 上面的 yaml 从 this example 中获得了粗略的灵感。这并不是有根据的猜测的结果,因为我仍然不了解参数、参数和输入如何相互作用的机制。
您可以使用 when
根据参数切换要使用的模板。
假设我有两个像这样的简单工作流模板:
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: t1
spec:
templates:
- name: whalesay-template
container:
image: docker/whalesay
command: [cowsay]
args: [t1]
---
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: t2
spec:
templates:
- name: whalesay-template
container:
image: docker/whalesay
command: [cowsay]
args: [t2]
我可以根据传递给工作流的参数(手动或通过 argo-events 设置)选择从 WorkflowTemplates 执行一个或另一个模板。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: switch-
spec:
entrypoint: pick
arguments:
parameters:
- name: which
templates:
- name: pick
steps:
- - name: t1
when: "{{workflow.parameters.which}} == t1"
templateRef:
name: t1
template: whalesay-template
- name: t2
when: "{{workflow.parameters.which}} == t2"
templateRef:
name: t2
template: whalesay-template
对于工作流的顶级参数,您可以使用 workflow.parameters.SOMETHING
。
在上述基础上,您可以使用 JSON 解析工具,例如 jq
来检索切换值,然后根据该值选择您的模板。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: switch-
spec:
entrypoint: pick
arguments:
parameters:
- name: json
templates:
- name: pick
steps:
- - name: parse
template: parse
- - name: t1
when: "{{steps.parse.outputs.result}} == a"
templateRef:
name: t1
template: whalesay-template
- name: t2
when: "{{steps.parse.outputs.result}} == b"
templateRef:
name: t2
template: whalesay-template
- name: parse
container:
image: jorgeandrada/alpine-jq
command: [sh, -c]
env:
- name: JSON
value: "{{workflow.parameters.json}}"
args: [echo "$JSON" | jq -j '.test']
我应该提一下,使用 jq
有点笨拙。在 Argo (3.1+) 的未来版本中,将有工具可以更直接地检查 JSON。但是这个解决方案很好地反向兼容。
致谢名单 Derek Wang。
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: pubsub-sensor-nyc
spec:
template:
serviceAccountName: argo-events-sa
dependencies:
- name: pubsub-event-source-dep
eventSourceName: pubsub-event-source-nyc
eventName: examplex
triggers:
- template:
name: argo-workflow-trigger
argoWorkflow:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: nyc-test-template-
namespace: argo
spec:
workflowTemplateRef:
name: nyc-test-template
parameters:
- src:
dependencyName: pubsub-event-source-dep
dataKey: body.wft
value: nyc-test-template # default value
dest: spec.workflowTemplateRef.name # <- this