github.com/argoproj/argo-events@v1.9.1/docs/sensors/triggers/k8s-object-trigger.md (about)

     1  # Kubernetes Object Trigger
     2  
     3  Apart from Argo workflow objects, the sensor lets you trigger any Kubernetes objects including Custom Resources
     4  such as Pod, Deployment, Job, CronJob, etc.
     5  Having the ability to trigger Kubernetes objects is quite powerful as providing an avenue to
     6  set up event-driven pipelines for existing workloads.
     7  
     8  <br/>
     9  <br/>
    10  
    11  <p align="center">
    12    <img src="https://github.com/argoproj/argo-events/blob/master/docs/assets/k8s-trigger.png?raw=true" alt="K8s Trigger"/>
    13  </p>
    14  
    15  <br/>
    16  <br/>
    17  
    18  ## Trigger a K8s Pod
    19  
    20  1. We will use webhook event-source and sensor to trigger a K8s pod.
    21  
    22  1. Lets set up a webhook event source to process incoming requests.
    23  
    24          kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/event-sources/webhook.yaml
    25  
    26  1. To trigger a pod, we need to create a sensor as defined below.
    27  
    28          apiVersion: argoproj.io/v1alpha1
    29          kind: Sensor
    30          metadata:
    31            name: webhook
    32          spec:
    33            template:
    34              serviceAccountName: create-pod-sa # A service account has privileges to create a Pod
    35            dependencies:
    36              - name: test-dep
    37                eventSourceName: webhook
    38                eventName: example
    39            triggers:
    40              - template:
    41                  name: webhook-pod-trigger
    42                  k8s:
    43                    operation: create
    44                    source:
    45                      resource:
    46                        apiVersion: v1
    47                        kind: Pod
    48                        metadata:
    49                          generateName: hello-world-
    50                        spec:
    51                          containers:
    52                            - name: hello-container
    53                              args:
    54                                - "hello-world"
    55                              command:
    56                                - cowsay
    57                              image: "docker/whalesay:latest"
    58                    parameters:
    59                      - src:
    60                          dependencyName: test-dep
    61                        dest: spec.containers.0.args.0
    62  
    63  1. Create the sensor.
    64  
    65          kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/trigger-standard-k8s-resource.yaml
    66  
    67  1. Lets expose the webhook event-source pod using `port-forward` so that we can make a request to it.
    68    
    69          kubectl -n argo-events port-forward <name-of-event-source-pod> 12000:12000   
    70  
    71  1. Use either Curl or Postman to send a post request to the `http://localhost:12000/example`.
    72  
    73          curl -d '{"message":"ok"}' -H "Content-Type: application/json" -X POST http://localhost:12000/example
    74  
    75  1. After the pod was completed, inspect the logs of the pod, you will see something similar as below.
    76  
    77          _________________________________________ 
    78          / {"context":{"type":"webhook","specVersi \
    79          | on":"0.3","source":"webhook","e |
    80          | ventID":"30306463666539362d346666642d34 |
    81          | 3336332d383861312d336538363333613564313 |
    82          | 932","time":"2020-01-11T21:23:07.682961 |
    83          | Z","dataContentType":"application/json" |
    84          | ,"subject":"example"},"data":"eyJoZWFkZ |
    85          | XIiOnsiQWNjZXB0IjpbIiovKiJdLCJDb250ZW50 |
    86          | LUxlbmd0aCI6WyIxOSJdLCJDb250ZW50LVR5cGU |
    87          | iOlsiYXBwbGljYXRpb24vanNvbiJdLCJVc2VyLU |
    88          | FnZW50IjpbImN1cmwvNy41NC4wIl19LCJib2R5I |
    89          \ jp7Im1lc3NhZ2UiOiJoZXkhISJ9fQ=="}       /
    90          ----------------------------------------- 
    91            \
    92             \
    93              \     
    94                            ##        .            
    95                      ## ## ##       ==            
    96                   ## ## ## ##      ===            
    97               /""""""""""""""""___/ ===        
    98          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
    99               \______ o          __/            
   100                \    \        __/             
   101                  \____\______/   
   102  
   103  ## Operation
   104  
   105  You can specify the operation for the trigger using the `operation` key under triggers->template->k8s.
   106  
   107  Operation can be either.
   108  
   109  1. `create`: Creates the object if not available in K8s cluster.
   110  2. `update`: Updates the object.
   111  3. `patch`: Patches the object using given patch strategy.
   112  4. `delete`: Deletes the object if it exists.
   113  
   114  More info available at [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#argoproj.io/v1alpha1.StandardK8sTrigger).
   115  
   116  ## Parameterization
   117  
   118  Similar to other type of triggers, sensor offers parameterization for the K8s trigger. Parameterization is specially useful when
   119  you want to define a generic trigger template in the sensor and populate the K8s object values on the fly.
   120  
   121  You can learn more about trigger parameterization [here](https://argoproj.github.io/argo-events/tutorials/02-parameterization/).
   122  
   123  ## Policy
   124  
   125  Trigger policy helps you determine the status of the triggered K8s object and decide whether to stop or continue sensor.
   126  
   127  To determine whether the K8s object was successful or not, the K8s trigger provides a `Resource Labels` policy.
   128  The `Resource Labels` holds a list of labels which are checked against the triggered K8s object to determine the status of the object.
   129  
   130                  # Policy to configure backoff and execution criteria for the trigger
   131                  # Because the sensor is able to trigger any K8s resource, it determines the resource state by looking at the resource's labels.
   132                  policy:
   133                    k8s:
   134                      # Backoff before checking the resource labels
   135                      backoff:
   136                        # Duration is the duration in nanoseconds
   137                        duration: 1000000000 # 1 second
   138                        # Duration is multiplied by factor each iteration
   139                        factor: 2
   140                        # The amount of jitter applied each iteration
   141                        jitter: 0.1
   142                        # Exit with error after these many steps
   143                        steps: 5
   144                      # labels set on the resource decide if the resource has transitioned into the success state.
   145                      labels:
   146                        workflows.argoproj.io/phase: Succeeded
   147                      # Determines whether trigger should be marked as failed if the backoff times out and sensor is still unable to decide the state of the trigger.
   148                      # defaults to false
   149                      errorOnBackoffTimeout: true
   150  
   151  Complete example is available [here](https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/trigger-with-policy.yaml).