Argo Events sensor

回顾一下之前的内容

This image has an empty alt attribute; its file name is image-137-1024x268.png

sensor-controller 运行在 argo events 命名空间中,它需要通过trigger创建一个workflows/argoproj.io/v1alpha1资源。workspace的资源规范没有指定在哪个命名空间中创建资源,之前我们将 workflow 安装在了 argo 命名空间中。
sensor需要具有在argo命名空间中创建工作流/argoproj.io/v1alpha1资源权限的服务帐户。

创建需要的SA

apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: argo
  name: operate-workflow-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: operate-workflow-role
  namespace: argo
rules:
  - apiGroups:
      - argoproj.io
    verbs:
      - "*"
    resources:
      - workflows
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: operate-workflow-role-binding
  namespace: argo
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: operate-workflow-role
subjects:
  - kind: ServiceAccount
    name: operate-workflow-sa
    namespace: argo

sensor.yaml

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: webhook
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: test-dep
      eventSourceName: webhook
      eventName: example
  triggers:
    - template:
        name: webhook-workflow-trigger
        k8s:
          group: argoproj.io
          version: v1alpha1
          resource: workflows
          operation: create
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: webhook-
              spec:
                entrypoint: whalesay
                arguments:
                  parameters:
                  - name: message
                    # the value will get overridden by event payload from test-dep
                    value: hello world
                templates:
                - name: whalesay
                  inputs:
                    parameters:
                    - name: message
                  container:
                    image: docker/whalesay:latest
                    command: [cowsay]
                    args: ["{{inputs.parameters.message}}"]
          parameters:
            - src:
                dependencyName: test-dep
              dest: spec.arguments.parameters.0.value
kubectl -n argo apply -f sensor.yaml

通过webhook来触发这个workflow

kubectl -n argo port-forward $(kubectl -n argo get pod -l eventsource-name=webhook -o name) 12000:12000
curl -d '{"message":"hello argo"}' -H "Content-Type: application/json" -X POST http://localhost:12000/deploy

Send a Message