Use Simple Message Queue (formerly MNS) to trigger a workflow

Updated at:

Workflow Clusters can integrate with Alibaba Cloud Simple Message Queue (formerly MNS). Use Simple Message Queue (formerly MNS) as a broker to connect to a variety of event sources and trigger workflow runs based on events. When a new event occurs, such as an Object Storage Service (OSS) event or an EventBridge event, Simple Message Queue (formerly MNS) receives the corresponding message. Argo automatically triggers the workflow run based on the trigger conditions, which automates workflow processing.

Prerequisites

Step 1: Create an event bus

An event bus can be shared by event-driven workflows in a namespace. You can create an event bus by using NATS or Simple Message Queue (formerly MNS). If an event bus already exists, proceed directly to Step 2: Create an event source.

Create by using NATS

  1. Create an event-bus.yaml file. Sample configuration:

    apiVersion: argoproj.io/v1alpha1
    kind: EventBus
    metadata:
      name: default
    spec:
      nats:
        native:
          replicas: 3
          auth: token
  2. Run the following command to create the EventBus:

    kubectl apply -f event-bus.yaml
    Note

    After the command succeeds, an EventBus pod is created in the default namespace. Subsequent operations must be performed in the same namespace.

  3. Run the following command to verify that the EventBus pod has started:

    kubectl get pod

Create by using Simple Message Queue (formerly MNS)

  1. Log on to the SMQ console.

  2. On the Topics page, create a topic named argoeventbus. Then, on the Topic Details page, obtain the endpoint from the Access Point section.

  3. Log on to the RAM console as a RAM administrator.

  4. Create a Resource Access Management (RAM) user, grant the AliyunMNSFullAccess permission to the RAM user, and obtain the RAM user's AccessKey ID and AccessKey secret.

    For more information, see Create a RAM user, Manage RAM user permissions, Create an AccessKey, and View RAM user AccessKey information.

  5. Run the following command to create a Secret for storing the AccessKey ID and AccessKey secret.

    kubectl create secret generic mns-secret\
      --from-literal=accesskey=*** \
      --from-literal=secretkey=***
  6. Create an event-bus-mns.yaml file and replace the parameters in the example with the values for your environment.

    • topic: Replace the value with the name of the topic created in step 2 of Simple Message Queue (formerly MNS).

    • endpoint: Replace the value with the endpoint obtained in step 2.

    apiVersion: argoproj.io/v1alpha1
    kind: EventBus
    metadata:
      name: default
    spec:
      alimns:
        accessKey:
          key: accesskey
          name: mns-secret
        secretKey:
          key: secretkey
          name: mns-secret
        topic: argoeventbus  # The name of the topic in Simple Message Queue (formerly MNS).
        endpoint: http://165***368.mns.<region>.aliyuncs.com  # The endpoint of Simple Message Queue (formerly MNS).
  7. Run the following command to apply the event-bus-mns.yaml file and create the event bus resource.

    kubectl apply -f event-bus-mns.yaml

Step 2: Create an event source

  1. Log on to the SMQ console.

  2. On the Queues page, create a queue named test-event-queue. Then, on the Queue Details page, obtain the endpoint from the Access Point section.

    Note

    If you created the event bus by using Simple Message Queue (formerly MNS), you can skip steps 3 through 5 and proceed directly to step 6.

  3. Log on to the RAM console as a RAM administrator.

  4. Create a Resource Access Management (RAM) user, grant the AliyunMNSFullAccess permission to the RAM user, and obtain the RAM user's AccessKey ID and AccessKey secret.

    For more information, see Create a RAM user, Manage RAM user permissions, Create an AccessKey, and View RAM user AccessKey information.

  5. Run the following command to create a Secret for storing the AccessKey ID and AccessKey secret.

    kubectl create secret generic mns-secret\
      --from-literal=accesskey=*** \
      --from-literal=secretkey=***
  6. Create an event-source.yaml file and replace the parameters in the example with the values for your environment.

    • topic: Replace the value with the name of the topic created in step 2 of Simple Message Queue (formerly MNS).

    • endpoint: Replace the value with the endpoint obtained in step 2.

    apiVersion: argoproj.io/v1alpha1
    kind: EventSource
    metadata:
      name: ali-mns
    spec:
      mns:
        example:
          jsonBody: true
          accessKey:
            key: accesskey
            name: mns-secret
          secretKey:
            key: secretkey
            name: mns-secret
          queue: test-event-queue # The name of the queue in Simple Message Queue (formerly MNS).
          waitTimeSeconds: 20
          endpoint: http://165***368.mns.<region>.aliyuncs.com # The endpoint of Simple Message Queue (formerly MNS).
  7. Run the following command to apply the event-source.yaml file and create the event source.

    kubectl apply -f event-source.yaml
  8. Run the following command to check whether the event source Pod starts as expected.

    kubectl get pod

Step 3: Create an event sensor

  1. Create an event-sensor.yaml file and embed the workflow definition to run in the event sensor. The following sample code shows the event sensor configuration:

    View the sample code

    apiVersion: argoproj.io/v1alpha1
    kind: Sensor
    metadata:
      name: ali-mns
    spec:
      template:
        serviceAccountName: default
      dependencies:
        - name: test-dep
          eventSourceName: ali-mns    # Matches the event source name.
          eventName: example          # Matches the event name defined in the event source.
      triggers:
        - template:
            name: mns-workflow
            k8s:
              operation: create
              source:
                resource:
                  apiVersion: argoproj.io/v1alpha1    # Embeds the workflow definition.
                  kind: Workflow
                  metadata:
                    generateName: ali-mns-workflow-
                  spec:
                    entrypoint: whalesay
                    arguments:
                      parameters:        # Parameters pass the event content.
                        - name: message
                          # this is the value that should be overridden
                          value: hello world
                    templates:
                      - name: whalesay
                        inputs:
                          parameters:
                            - name: message
                        container:
                          image: docker/whalesay:latest
                          command: [cowsay]
                          args: ["{{inputs.parameters.message}}"]
              parameters:
                - src:        # Parses the event content and passes it to the workflow.
                    dependencyName: test-dep
                    dataKey: body
                  dest: spec.arguments.parameters.0.value
  2. Apply the event-sensor.yaml file to create the event sensor.

    kubectl apply -f event-sensor.yaml
  3. Check whether the event sensor Pod starts as expected.

    kubectl get pod
Note

When you create an event bus by using Simple Message Queue (formerly MNS), a corresponding Simple Message Queue (formerly MNS) queue is automatically created after the event sensor is created. The queue name uses the following format: ackone-argowf-<namespace>-<sensor-name>-<sensor-uid>.

Step 4: Verify that sending a message to Simple Message Queue (formerly MNS) triggers the workflow

  1. Log on to the SMQ console.

  2. On the Queues page, find the queue named test-event-queue. In the Actions column, click Send Messages.

  3. On the quick-start page for sending and receiving messages, enter test trigger argo workflow as the message content and then click Send Message.

  4. View the workflow run in the workflow cluster.

    argo list

    The expected output is as follows:

    NAME                     STATUS    AGE   DURATION   PRIORITY
    ali-mns-workflow-5prz7   Running   6s    6s         0
  5. Obtain the workflow logs to view the message content.

    argo logs ali-mns-workflow-5prz7
    Important
    • The workflow name in this command must match the name returned in the previous step. ali-mns-workflow-5prz7 is only an example value. Replace it with the value returned in your environment.

    • The message content is Base64-encoded.

    The expected output is as follows:

    ali-mns-workflow-5prz7-whalesay-2429203954: time="2023-12-14T08:33:37.964Z" level=info msg="capturing logs" argo=true
    ali-mns-workflow-5prz7-whalesay-2429203954:
    ali-mns-workflow-5prz7-whalesay-2429203954:  < dGVzdCB0cmlnZ2VyIGFyZ28gd29ya2Zsb3c= >
    ali-mns-workflow-5prz7-whalesay-2429203954:  -----------------------------------------
    ali-mns-workflow-5prz7-whalesay-2429203954:     \
    ali-mns-workflow-5prz7-whalesay-2429203954:      \
    ali-mns-workflow-5prz7-whalesay-2429203954:       \
    ali-mns-workflow-5prz7-whalesay-2429203954:                     ##        .
    ali-mns-workflow-5prz7-whalesay-2429203954:               ## ## ##       ==
    ali-mns-workflow-5prz7-whalesay-2429203954:            ## ## ## ##      ===
    ali-mns-workflow-5prz7-whalesay-2429203954:        /""""""""""""""""___/ ===
    ali-mns-workflow-5prz7-whalesay-2429203954:   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
    ali-mns-workflow-5prz7-whalesay-2429203954:        \______ o          __/
    ali-mns-workflow-5prz7-whalesay-2429203954:         \    \        __/
    ali-mns-workflow-5prz7-whalesay-2429203954:           \____\______/
    ali-mns-workflow-5prz7-whalesay-2429203954: time="2023-12-14T08:33:38.979Z" level=info msg="sub-process exited" argo=true error="<nil>"

Step 5: Delete event-related resources

  1. Delete the related resources.

    • Delete the event sensor.

      kubectl delete sensor ali-mns
    • Delete the event source.

      kubectl delete eventsource ali-mns
    • Delete the event bus.

      kubectl delete eventbus default
  2. Check the Pod status to confirm that all resources have been deleted.

    kubectl get pod