From f55c6b7c6160c833226a550f843cd5ff359faa81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo=20Maida?= Date: Sun, 2 Jun 2024 16:15:57 -0300 Subject: [PATCH] Tekton Task JQ v0.1 --- task/jq/0.1/README.md | 31 ++++++++ task/jq/0.1/jq.yaml | 62 +++++++++++++++ task/jq/0.1/tests/pipeline.yaml | 124 +++++++++++++++++++++++++++++ task/jq/0.1/tests/pipelinerun.yaml | 13 +++ 4 files changed, 230 insertions(+) create mode 100644 task/jq/0.1/README.md create mode 100644 task/jq/0.1/jq.yaml create mode 100644 task/jq/0.1/tests/pipeline.yaml create mode 100644 task/jq/0.1/tests/pipelinerun.yaml diff --git a/task/jq/0.1/README.md b/task/jq/0.1/README.md new file mode 100644 index 00000000..149173e0 --- /dev/null +++ b/task/jq/0.1/README.md @@ -0,0 +1,31 @@ +# JQ Task + +The following task is used to execute a given JQ script and expose its result. It is able to work with both JSON string and file as input to the script. + +## Installing the Task + +```bash +kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/jq/0.1/raw +``` + +## Parameters + +- **options**: JQ options that will be used. If none is provided, the default value "" will be used." +- **filter**: JQ filter that will be executed. If none is provided, the the filter '.' will be executed by default. The filter does not need to be wrapped with ''. +- **image**: The jq image to use. +- **input**: The input to be passed to the JQ command. It can be a JSON string or a file name. In case of file pass the entire path where the file is mounted. +- **stringOrFile**: Flag used to determine whether the input passed will be a JSON string or file. Use **string** or **file**. + +## Workspaces + +- **source** : The workspace containing files on which we want to handle. This workspace can be a persistent volume or configmap. + +## Platforms + +The Task can be run on `linux/amd64` platform. + +## Usage + +For an example of real-world usage, check the [**tests**](https://github.com/tektoncd/catalog/tree/main/task/jq/0.1/tests) directory for an example of how it can be used. + +For cases where it is necessary to pass large JSON sections as input parameter, it is recommended to enter the full path of a file as the parameter's value. Therefore, the **stringOrFile** parameter must be properly configured. \ No newline at end of file diff --git a/task/jq/0.1/jq.yaml b/task/jq/0.1/jq.yaml new file mode 100644 index 00000000..7c550635 --- /dev/null +++ b/task/jq/0.1/jq.yaml @@ -0,0 +1,62 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: jq + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.56.1" + tekton.dev/categories: Developer Tools + tekton.dev/tags: jq + tekton.dev/displayName: "JQ" + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + Task built for using the JQ utility in pipelines. + workspaces: + - name: source + description: A workspace that contains the file which needs to be altered. It can take the form of a persistent volume (PV and PVC) or configmap. + optional: true + params: + - name: options + type: string + description: JQ options that will be used. If none is provided, the default value '-M' will be used." + default: "-M" + - name: filter + type: string + description: "JQ filter that will be executed. If none is provided, the the filter '.' will be executed by default." + default: "." + - name: image + type: string + description: "The jq image to use." + default: quay.io/jpmaida1/jq@sha256:f90645c017ba8b1a2fff44309231cb7f8fd3a441690e17c15a53e60997f309c0 + - name: input + type: string + description: >- + The input to be passed to the JQ command. It can be a JSON string or a file name. It is recommended for large JSON to use file mode. If this parameter is not provided, the default value '{\"hello\":\"world\"}' will be used. If file mode is used the full file's path needs to be informed. + default: '{\"hello\":\"world\"}' + - name: stringOrFile + type: string + default: "file" + description: >- + Flag used to determine whether the input passed will be a JSON string or file. Use the value 'string' for JSON string or 'file' for file. In order to use the enum type the parameter 'enable-param-enum' needs to be enabled. + results: + - name: jq-script-outcome + description: "The result of the JQ script passed as a parameter" + steps: + - name: jq-script + image: $(params.image) + workingDir: $(workspaces.source.path) + script: | + echo "You submitted as input: $(params.input)" + echo "JQ script result:" + if [ "$(params.stringOrFile)" != "string" ] && [ "$(params.stringOrFile)" != "file" ]; then + echo "You must provide the following values 'string' or 'file' to the stringOrFile parameter." + exit 1 + fi + if [ "string" = "$(params.stringOrFile)" ]; then + printf "%s" "$(params.input)" > tmp.json + jq "$(params.options)" '$(params.filter)' tmp.json | tee "$(results.jq-script-outcome.path)" + else + jq "$(params.options)" '$(params.filter)' "$(params.input)" | tee "$(results.jq-script-outcome.path)" + fi \ No newline at end of file diff --git a/task/jq/0.1/tests/pipeline.yaml b/task/jq/0.1/tests/pipeline.yaml new file mode 100644 index 00000000..e2311896 --- /dev/null +++ b/task/jq/0.1/tests/pipeline.yaml @@ -0,0 +1,124 @@ +kind: ConfigMap +apiVersion: v1 +metadata: + name: test-json +immutable: false +data: + test.json: |- + { + "company": { + "name": "Acme Corporation", + "industry": "Technology", + "location": { + "city": "Metropolis", + "country": "USA" + }, + "employees": [ + { + "name": "Alice", + "age": 35, + "position": "Software Engineer" + }, + { + "name": "Bob", + "age": 42, + "position": "Data Analyst" + }, + { + "name": "Charlie", + "age": 28, + "position": "Product Manager" + } + ], + "products": [ + { + "name": "Widget", + "price": 19.99, + "color": "blue" + }, + { + "name": "Gizmo", + "price": 29.99, + "color": "red" + }, + { + "name": "Thingamajig", + "price": 39.99, + "color": "green" + } + ] + } + } + +--- + +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: print-result +spec: + params: + - name: input + steps: + - name: print + image: registry.access.redhat.com/ubi8:8.9-1160 + script: | + echo "$(params.input)" + +--- + +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: new-pipeline +spec: + tasks: + - name: jq-using-string-as-input + params: + - name: filter + value: "." + - name: input + value: '{ \"product\": \"Widget\", \"price\": 24.99, \"color\": \"blue\", \"quantity_available\": 50, \"manufacturer\": { \"name\": \"ABC Manufacturing\", \"location\": \"Cityville, USA\" }, \"features\": [\"durable\", \"easy to use\", \"versatile\"], \"dimensions\": { \"length\": 10, \"width\": 5, \"height\": 3 }, \"tags\": [\"gadget\", \"technology\", \"home\"] }' + - name: stringOrFile + value: "string" + taskRef: + kind: Task + name: jq + - name: jq-using-file-as-input + params: + - name: filter + value: ".company.employees[].name" + - name: input + value: 'test.json' + - name: stringOrFile + value: "file" + taskRef: + kind: Task + name: jq + workspaces: + - name: source + workspace: shared-workspace + - name: jq-using-string-as-input-with-wild-filter + params: + - name: filter + # value: "map(length) | max_by(.) as $max_length | .[] | select(length == $max_length)" + value: map_values(if type == "string" then . | split("") | reverse | join("") | ascii_upcase else . end) + - name: input + value: '[\"apple\", \"banana\", \"orange\", \"grape\", \"kiwi\", \"siriguela\", \"mamão\"]' + - name: stringOrFile + value: "string" + taskRef: + kind: Task + name: jq + - name: print-result + runAfter: + - "jq-using-string-as-input" + - "jq-using-file-as-input" + taskRef: + kind: Task + name: print-result + params: + - name: input + value: $(tasks.jq-using-string-as-input.results.jq-script-outcome) + workspaces: + - name: shared-workspace diff --git a/task/jq/0.1/tests/pipelinerun.yaml b/task/jq/0.1/tests/pipelinerun.yaml new file mode 100644 index 00000000..e5ad6e97 --- /dev/null +++ b/task/jq/0.1/tests/pipelinerun.yaml @@ -0,0 +1,13 @@ +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + name: new-pipeline +spec: + pipelineRef: + name: new-pipeline + timeouts: + pipeline: 1h0m0s + workspaces: + - configMap: + name: test-json + name: shared-workspace \ No newline at end of file