Receiving custom events

Learn how to generate custom events using the curl command-line tool and then send those events to a service in Cloud Run for Anthos on Google Cloud.

Before you begin

To complete this task, you must have an events broker and know in which namespace it's running. Learn how to configure Events for Cloud Run for Anthos and create an events broker.

If you have an events broker running, you can view the Kubernetes namespace by running:

kubectl get brokers -n NAMESPACE

Deploying a Cloud Run for Anthos on Google Cloud event receiver service

Deploy a Cloud Run for Anthos on Google Cloud service that receives events from the event broker.

To deploy the Cloud Run for Anthos on Google Cloud service:

  1. Clone the repository:

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    cd python-docs-samples/eventarc/audit-storage
    

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    cd java-docs-samples/eventarc/audit-storage
    

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/eventarc/audit_storage
    

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    cd nodejs-docs-samples/eventarc/audit-storage
    

    C#

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
    cd dotnet-docs-samples/eventarc/audit-storage
    
  2. Build the container and upload it to Cloud Build:

    gcloud builds submit \
      --tag gcr.io/$(gcloud config get-value project)/CLOUD_RUN_CONTAINER_NAME
    
  3. Deploy the container image to Cloud Run for Anthos on Google Cloud:

    gcloud run deploy CLOUD_RUN_SERVICE_NAME \
        --namespace=NAMESPACE \
        --image gcr.io/$(gcloud config get-value project)/CLOUD_RUN_CONTAINER_NAME
    

When you see the service's URL, it has been successfully deployed.

Generating events using cURL

You can use curl running in a Google Kubernetes Engine pod to generate events:

  1. Create a Google Kubernetes Engine pod in the namespace of your events broker:

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        run: curl
      name: curl
      namespace: NAMESPACE
    spec:
      containers:
      - image: radial/busyboxplus:curl
        imagePullPolicy: IfNotPresent
        name: curl
        stdin: true
        tty: true
    EOF
    

    where NAMESPACE is the same namespace as your events broker.

  2. Verify the pod is working correctly by running the following command:

    kubectl get pod curl -n NAMESPACE
    

    where NAMESPACE is the same namespace as your events broker.

    You will see a pod called curl up and running (Status=Running).

Creating a trigger for your events broker

  1. Create a trigger with a filter alpha-type events:

    gcloud beta events triggers create TRIGGER_NAME \
        --namespace NAMESPACE \
        --target-service=CLOUD_RUN_SERVICE_NAME \
        --type=alpha-type \
        --custom-type
    

    where NAMESPACE is the same namespace as your events broker.

    If you don’t specify a filter, all events are sent to the Cloud Run for Anthos on Google Cloud service. Note that exact match filtering on any number of CloudEvents attributes and extensions are supported. If your filter sets multiple attributes, an event must match all of the attributes.

  2. Verify that the trigger is set up:

    gcloud beta events triggers list \
        --target-service 'CLOUD_RUN_SERVICE_NAME' \
        --namespace NAMESPACE
    

    where NAMESPACE is the same namespace as your events broker.

    The output is similar to the following:

    TRIGGER                  EVENT TYPE           TARGET
    trigger-name           alpha-type           cloud-run-service
    

You will see trigger [TRIGGER-NAME] up and running.

Creating an event

To generate an event, send a HTTP request to the event broker from the Google Kubernetes Engine pod.

  1. Get the URL of the event broker that the HTTP request will be made to:

    kubectl get brokers default \
        --namespace NAMESPACE \
        --output jsonpath="{.status.address.url}"
    

    Note the URL of the broker, you will use it to send the HTTP request.

  2. SSH into the pod:

    kubectl --namespace NAMESPACE attach curl -it
    
  3. Make a HTTP request using curl with a HTTP header of Ce-Type: alpha-type to match the trigger's filter to the event broker. In this example, the message is send-cloudevents-to-broker:

    curl -v "EVENT_BROKER_URL" \
        -X POST \
        -H "Ce-Id: my-id" \
        -H "Ce-Specversion: 1.0" \
        -H "Ce-Type: alpha-type" \
        -H "Ce-Subject: custom event" \
        -H "Ce-Source: my-source" \
        -H "Content-Type: application/json" \
        -d '{"msg":"send-cloudevents-to-broker"}'
    

    This generates an event, and the broker then responds with an HTTP 202 response.

Viewing the event

View the event by looking at the logs of the Cloud Run for Anthos on Google Cloud receiver service:

  1. To view the logs, type:

    gcloud logging read "resource.type=k8s_container \
        resource.labels.namespace_name=events \
        resource.labels.container_name=user-container"
    

Clean up

Delete the resources created in this tutorial to avoid recurring charges.

  1. Delete the pod:

    kubectl delete pod curl --namespace >NAMESPACE
    
  2. Delete the trigger:

    gcloud beta events triggers delete TRIGGER_NAME \
        --namespace >NAMESPACE