tektoncd / triggers

Event triggering with Tekton!
Apache License 2.0
558 stars 420 forks source link

TriggerTemplate unable to use JSON array as parameter #1562

Open jangel97 opened 1 year ago

jangel97 commented 1 year ago

Hi,

I am trying to trigger the creation of VMs using a tekton pipeline. The idea is that there is an eventlistener which receives a HTTP POST request with the data required for the provisioning.

This is an example of the JSON payload that I need to pass to the pipelinerun:

{
  "systems": [
    {
      "name": "vm1webserver",
      "os": "RHEL9",
      "vm size": "small",
      "network": "default",
      "namespace": "vm-webservers"
    },
    {
      "name": "vm2webserver",
      "os": "RHEL9",
      "vm size": "small",
      "network": "default",
      "namespace": "vm-webservers"
    },
  ]
}

I am unable to achieve to read an object parameter using TriggerTemplate

My tekton triggers look like this:

---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
  name: provisioning-pipelinebinding
  namespace: tekton-vm-provisioning-pipeline
spec:
  params:
    - name: systems
      value: $(body.systems)
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: provisioning-automation-tt
  namespace: tekton-vm-provisioning-pipeline
  annotations:
    triggers.tekton.dev/old-escape-quotes: "true"
spec:
  params:
    - name: systems
  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        generateName: tekton-vm-provisioning-pipeline-
        labels:
          tekton.dev/pipeline: tekton-vm-provisioning-pipeline
        namespace: tekton-vm-provisioning-pipeline
      spec:
        params:
          - name: systems
            value: $(tt.params.systems)
        pipelineRef:
          name: vm-provisioning
        workspaces:
          - name: shared-workspace
            volumeClaimTemplate:
              metadata:
                annotations:
                  kubernetes.io/reclaimPolicy: Delete
                labels: {}
                name: ws-pvc
              spec:
                accessModes:
                  - ReadWriteMany
                resources:
                  requests:
                    storage: 500Mi
                storageClassName: managed-nfs-storage

My tekton pipeline:

---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: vm-provisioning
spec:
  params:
    - name: systems
      type: array
  workspaces:
    - name: shared-workspace
  tasks:
    - name: get-cluster-info
      taskRef:
        name: get-cluster-info
      params:
        - name: names
          value: '$(params.systems[*])'
      workspaces:
        - name: source
          workspace: shared-workspace

When spawning pipeline I see following error:

PipelineRun tekton-vm-provisioning-pipeline/tekton-vm-provisioning-pipeline-jkv5k parameters have mismatching types with Pipeline tekton-vm-provisioning-pipeline/vm-provisioning's parameters: parameters have inconsistent types : [systems]

Is this supported in tekton?

Thanks,

jangel97 commented 1 year ago

I tried to parse JSON payload as string, and then use jq in the task to read json payload, I was not successful. Did anyone successfully achieved this?

savitaashture commented 1 year ago

Something similar ask here https://github.com/tektoncd/triggers/issues/893

savitaashture commented 1 year ago

Its not supported today But its a good feature to add

arjunsbabu43 commented 1 year ago

This is a much needed feature. Its very hard to fetch each parameter from a json payload. In Jenkins we had something like multi line parameter. Not sure why ChatGpt is saying there is a type json.

image
dibyom commented 1 year ago

hey @arjunsbabu43 Tekton pipelines does support object parameters as a beta feature but we have yet to build support for that in Triggers.

CPinhoK commented 1 year ago

+1 for this feature

aw185176 commented 1 year ago

Its not supported today But its a good feature to add

This is much more than a good feature to add... at this point the triggers project is not compatible with the pipelines project at a fundamental level.

dibyom commented 1 year ago

@khrm @savitaashture let's consider this for the next milestone?

arjunsbabu43 commented 1 year ago

Thank you, @dibyom. Do you have any information on the anticipated release date for Triggers v0.25?

khrm commented 1 year ago

A workaround for this is using CEL interceptor. Something like this:


        - ref:
            name: "cel"
          params:
            - name: "overlays"
              value:
                - key: list
                  expression: "body.list.marshalJSON()"
      bindings:
      - name: list
        value: $(extensions.list)
      template:
        spec:
          params:
            - name: list
          resourcetemplates:
            - apiVersion: tekton.dev/v1beta1
              kind: TaskRun
              metadata:
                generateName: cel-trig-
              spec:
                taskSpec:
                  steps:
                  - image: ubuntu
                    script: |
                      #!/usr/bin/env bash
                      echo "SHA is : $(tt.params.list)"
arjunsbabu43 commented 1 year ago

Thanks @khrm will have a try and will let you know the results

khrm commented 1 year ago

I will add the workaround to the docs in this release.

savitaashture commented 10 months ago

@khrm will create a PR with workaround in the doc for now as we are moving this issue to next milestone

Sylphe88 commented 6 days ago

Only commenting to say this would be very much needed (this and support for object data type both in pipelines and triggers!).

Also, tried the workaround and it turns out to be tricky and not completely functional because what you get in your taskrun/pipelinerun cannot be easily manipulated with jq or other JSON tools (basically you'd get [foo, bar] instead of ["foo", "bar"]. And using the old-escape annotation trick prevents the triggertemplate from unmarshalling the data.