diff --git a/tasks/apply-mapping/README.md b/tasks/apply-mapping/README.md index 7e6f33d7c..0a2124106 100644 --- a/tasks/apply-mapping/README.md +++ b/tasks/apply-mapping/README.md @@ -31,6 +31,9 @@ You can also expand image labels, e.g. "{{ labels.mylabel }}" -> The value of im | dataPath | Path to the JSON string of the merged data to use in the data workspace | No | - | | failOnEmptyResult | Fail the task if the resulting snapshot contains zero components | Yes | false | +## Changes in 1.9.0 +* support defaults for `contentGateway` + ## Changes in 1.8.1 * Fixed substitution logic to prevent incomplete replacements in `translate_tags`. diff --git a/tasks/apply-mapping/apply-mapping.yaml b/tasks/apply-mapping/apply-mapping.yaml index de03166ce..743d8a18c 100644 --- a/tasks/apply-mapping/apply-mapping.yaml +++ b/tasks/apply-mapping/apply-mapping.yaml @@ -4,7 +4,7 @@ kind: Task metadata: name: apply-mapping labels: - app.kubernetes.io/version: "1.8.1" + app.kubernetes.io/version: "1.9.0" annotations: tekton.dev/pipelines.minVersion: "0.12.1" tekton.dev/tags: release @@ -252,6 +252,7 @@ spec: defaultTags=$(jq '.defaults.tags // []' <<< "$MAPPING") defaultTimestampFormat=$(jq -r '.defaults.timestampFormat // "%s"' <<< "$MAPPING") currentTimestamp="$(date "+%Y%m%d %T")" + defaultCGWSettings=$(jq '.defaults.contentGateway // {}' <<< "$MAPPING") NUM_MAPPED_COMPONENTS=$(jq '.components | length' "${SNAPSHOT_SPEC_FILE}") for ((i = 0; i < NUM_MAPPED_COMPONENTS; i++)) ; do component=$(jq -c --argjson i "$i" '.components[$i]' "${SNAPSHOT_SPEC_FILE}") @@ -314,6 +315,17 @@ spec: && mv /tmp/temp "${SNAPSHOT_SPEC_FILE}" done + # apply defaults for contentGateway + componentCGWSettings=$(jq '.contentGateway // {}' <<< "$component") + updatedComponentCGWSettings=$(merge-json "$defaultCGWSettings" "$componentCGWSettings") + componentCGWSettingsSize=$(jq '. | length' <<< "${updatedComponentCGWSettings}") + + if [ "${componentCGWSettingsSize}" -gt "0" ]; then + jq --argjson i "$i" --argjson componentCGWSettings "$updatedComponentCGWSettings" \ + '.components[$i].contentGateway = $componentCGWSettings' \ + "${SNAPSHOT_SPEC_FILE}" > /tmp/temp && mv /tmp/temp "${SNAPSHOT_SPEC_FILE}" + fi + # Determine the format of the original repository and update keys accordingly repository=$(jq -r '.repository' <<< "$component") echo "Processing component: $NAME" diff --git a/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-disabled.yaml b/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-disabled.yaml new file mode 100644 index 000000000..6de48debd --- /dev/null +++ b/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-disabled.yaml @@ -0,0 +1,92 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: test-apply-mapping-content-gateway-disabled +spec: + description: | + Run the apply-mapping task with a snapshot.spec json and no default configuration for content gateway and no + configuration data for any of the components. + workspaces: + - name: tests-workspace + tasks: + - name: setup + workspaces: + - name: config + workspace: tests-workspace + taskSpec: + workspaces: + - name: config + steps: + - name: setup-values + image: quay.io/konflux-ci/release-service-utils:d320c36f3d707cd5bfe55fe783f70236c06cc2e5 + script: | + #!/usr/bin/env sh + set -eux + + cat > "$(workspaces.config.path)/test_data.json" << EOF + { + "mapping": { + "components": [ + { + "name": "comp1" + } + ] + } + } + EOF + + cat > "$(workspaces.config.path)/test_snapshot_spec.json" << EOF + { + "application": "myapp", + "components": [ + { + "name": "comp1", + "containerImage": "registry.io/image1@sha256:123456", + "source": { + "git": { + "revision": "testrevision", + "url": "myurl" + } + } + } + ] + } + EOF + - name: run-task + taskRef: + name: apply-mapping + params: + - name: snapshotPath + value: test_snapshot_spec.json + - name: dataPath + value: test_data.json + runAfter: + - setup + workspaces: + - name: config + workspace: tests-workspace + - name: check-result + workspaces: + - name: config + workspace: tests-workspace + taskSpec: + workspaces: + - name: config + steps: + - name: check-result + image: quay.io/konflux-ci/release-service-utils:e633d51cd41d73e4b3310face21bb980af7a662f + script: | + #!/usr/bin/env bash + set -eux + + cat "$(workspaces.config.path)/test_snapshot_spec.json" + + echo Test that comp1 has NO contentGateway data + test "$( + jq -c '.components[] | select(.name=="comp1") | .contentGateway // {}' \ + < "$(workspaces.config.path)/test_snapshot_spec.json" + )" == '{}' + + runAfter: + - run-task diff --git a/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-with-defaults.yaml b/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-with-defaults.yaml new file mode 100644 index 000000000..642236246 --- /dev/null +++ b/tasks/apply-mapping/tests/test-apply-mapping-content-gateway-with-defaults.yaml @@ -0,0 +1,174 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: test-apply-mapping-content-gateway-with-defaults +spec: + description: | + Run the apply-mapping task with a snapshot.spec json and a default configuration for content gateway and + validate that the values are overridden if defined in the component or if not defined then the defaults are used. + workspaces: + - name: tests-workspace + tasks: + - name: setup + workspaces: + - name: config + workspace: tests-workspace + taskSpec: + workspaces: + - name: config + steps: + - name: setup-values + image: quay.io/konflux-ci/release-service-utils:d320c36f3d707cd5bfe55fe783f70236c06cc2e5 + script: | + #!/usr/bin/env sh + set -eux + + cat > "$(workspaces.config.path)/test_data.json" << EOF + { + "mapping": { + "defaults": { + "contentGateway": { + "productName": "myapp", + "productCode": "MYAPP", + "productVersionName": "1.0", + "filePrefix": "myapp-1.3" + } + }, + "components": [ + { + "name": "comp1", + "contentGateway": { + "productVersionName": "comp1-1" + }, + "staged": { + "destination": "ai-1-for-isos", + "files": [ + { + "source": "one.qcow2", + "filename": "ai-{{git_sha}}" + }, + { + "source": "one.raw", + "filename": "ai-{{git_short_sha}}" + } + ] + } + }, + { + "name": "comp2", + "contentGateway": { + "filePrefix": "myapp-comp1-1" + }, + "staged": { + "destination": "ai-2-for-isos", + "files": [ + { + "source": "two.qcow2", + "filename": "ai-{{digest_sha}}" + } + ] + } + }, + { + "name": "comp3", + "staged": { + "destination": "ai-2-for-isos", + "files": [ + { + "source": "three.qcow2", + "filename": "ai-{{digest_sha}}" + } + ] + } + } + ] + } + } + EOF + + cat > "$(workspaces.config.path)/test_snapshot_spec.json" << EOF + { + "application": "myapp", + "components": [ + { + "name": "comp1", + "containerImage": "registry.io/image1@sha256:123456", + "source": { + "git": { + "revision": "testrevision", + "url": "myurl" + } + } + }, + { + "name": "comp2", + "containerImage": "registry.io/image2@sha256:123456", + "source": { + "git": { + "revision": "testrevision2", + "url": "myurl2" + } + } + }, + { + "name": "comp3", + "containerImage": "registry.io/image3@sha256:78910", + "source": { + "git": { + "revision": "testrevision3", + "url": "myurl3" + } + } + } + ] + } + EOF + - name: run-task + taskRef: + name: apply-mapping + params: + - name: snapshotPath + value: test_snapshot_spec.json + - name: dataPath + value: test_data.json + runAfter: + - setup + workspaces: + - name: config + workspace: tests-workspace + - name: check-result + workspaces: + - name: config + workspace: tests-workspace + taskSpec: + workspaces: + - name: config + steps: + - name: check-result + image: quay.io/konflux-ci/release-service-utils:e633d51cd41d73e4b3310face21bb980af7a662f + script: | + #!/usr/bin/env bash + set -eux + + cat "$(workspaces.config.path)/test_snapshot_spec.json" + + echo Test that comp1 has the correct contentGateway.productVersionName + test "$( + jq -r '.components[] | select(.name=="comp1") | .contentGateway.productVersionName' \ + < "$(workspaces.config.path)/test_snapshot_spec.json" + )" == "comp1-1" + + echo Test that comp2 has the correct contentGateway.filePrefix + test "$( + jq -r '.components[] | select(.name=="comp2") | .contentGateway.filePrefix' \ + < "$(workspaces.config.path)/test_snapshot_spec.json" + )" == "myapp-comp1-1" + + echo Test that comp3 has the correct contentGateway defaults + test "$( + jq -c '.components[] | select(.name=="comp3") | .contentGateway' \ + < "$(workspaces.config.path)/test_snapshot_spec.json" + )" == '{"productName":"myapp","productCode":"MYAPP","productVersionName":"1.0","filePrefix":"myapp-1.3"}' + runAfter: + - run-task