Skip to content

Commit

Permalink
feat(CVP-4395,CVP-4396): add get package and channel functions in utils
Browse files Browse the repository at this point in the history
This commit adds two functions:
*get_package_from_catalog:
Given an output of opm render fbcFragment, it returns a package.
*get_channel_from_catalog:
Given an output of opm render fbcFragment and a olm.package,
returns the 'defaultChannel' value from the olm.package entry
Both functions will be used in the task of deploy-fbc pipeline
in tekton-integration-catalog

Signed-off-by: Anna Rania <[email protected]>
  • Loading branch information
Anna Rania committed Jan 28, 2025
1 parent 6ec3651 commit 8a0b759
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
74 changes: 74 additions & 0 deletions test/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,77 @@ replace_image_pullspec() {
exit 2
fi
}

# This function will be used by tasks in tekton-integration-catalog
# Given the output of 'opm render $fbc_fragment' command, this function returns a package name
# If there is only one 'olm.package', it returns it's name
# If there are multiple 'olm.package' entries, it returns the one with the highest bundle version in the package’s 'defaultChannel'
get_package_from_catalog() {
local RENDER_OUT_FBC="$1"
local package_count
local package_name

if [ -z "$RENDER_OUT_FBC" ]; then
echo "get_package_from_catalog: Missing 'opm render' output for the image" >&2
exit 2
fi

# Count 'olm.package' entries in the rendered FBC output
package_count=$(echo "$RENDER_OUT_FBC" | tr -d '\000-\031' | jq -s '[.[] | select(.schema == "olm.package")] | length')

if [[ "$package_count" -eq 1 ]]; then
# Return the single package name
package_name=$(echo "$RENDER_OUT_FBC" | tr -d '\000-\031' | jq -r 'select(.schema == "olm.package") | .name')
echo "$package_name"
else
# Handle multiple packages
# Find the highest bundle version for each package based on the entries in their respective defaulChannel
package_name=$(echo "$RENDER_OUT_FBC" | tr -d '\000-\031' | jq -r '
reduce inputs as $obj (
{
packages: [],
channels: []
};
if $obj.schema == "olm.package" then
.packages += [$obj]
elif $obj.schema == "olm.channel" then
.channels += [$obj]
else . end
) |
.packages[] as $pkg |
(.channels[] | select(.package == $pkg.name and .name == $pkg.defaultChannel)) as $channel |
($channel.entries | map({
name: .name,
version: (.name | split(".") | map(try tonumber // 0))
}) | max_by(.version)) as $highest |
if $highest.name then $pkg.name else empty end
')
echo "$package_name"
fi
}

# This function will be used by tasks in tekton-integration-catalog
# Given the output of 'opm render $fbc_fragment' command and a package name, this function returns the defaultChannel value specified in the olm.package entry
get_channel_from_catalog() {
local RENDER_OUT_FBC="$1"
local PACKAGE_NAME="$2"
local default_channel

if [[ -z "$RENDER_OUT_FBC" || -z "$PACKAGE_NAME" ]]; then
echo "get_channel_from_catalog: Invalid input. Usage: get_channel_from_catalog <RENDER_OUT_FBC> <PACKAGE_NAME>" >&2
exit 2
fi

# Extract the defaultChannel for a given package
default_channel=$(echo "$RENDER_OUT_FBC" | tr -d '\000-\031' | jq -r --arg PACKAGE_NAME "$PACKAGE_NAME" '
select(.schema == "olm.package" and .name == $PACKAGE_NAME) | .defaultChannel
')

# Handle case when PACKAGE_NAME is not found in RENDER_OUT_FBC
if [[ -z "$default_channel" || "$default_channel" == "null" ]]; then
echo "get_channel_from_catalog: Package name $PACKAGE_NAME not found in the rendered FBC" >&2
exit 1
fi

echo "$default_channel"
}
121 changes: 121 additions & 0 deletions unittests_bash/test_utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,124 @@ EOF
EXPECTED_RESPONSE="Invalid pullspec format: registry.io/unavailable/pullspec@sha256:short-sha"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 2 ]]
}

@test "Get package from catalog: success single package" {
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged",
"defaultChannel": "stable"
}
EOF
)
run get_package_from_catalog "${RENDER_OUT_FBC}"
EXPECTED_RESPONSE="kubevirt-hyperconverged"
echo "${output}"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 0 ]]
}

@test "Get package from catalog: success multiple packages" {
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged-v1",
"defaultChannel": "stable"
}
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged-v2",
"defaultChannel": "dev-preview"
}
{
"schema": "olm.channel",
"name": "dev-preview",
"package": "kubevirt-hyperconverged-v2",
"entries": [
{
"name": "kubevirt-hyperconverged-operator.v4.99.0-0.1723448771"
}
]
}
{
"schema": "olm.channel",
"name": "stable",
"package": "kubevirt-hyperconverged-v1",
"entries": [
{
"name": "kubevirt-hyperconverged-operator.v4.17.3"
},
{
"name": "kubevirt-hyperconverged-operator.v4.17.4"
}
]
}
{
"schema": "olm.channel",
"name": "dev-preview-2",
"package": "kubevirt-hyperconverged-v2",
"entries": [
{
"name": "kubevirt-hyperconverged-operator.v5.00.0-0.1723448771"
}
]
}
EOF
)
run get_package_from_catalog "${RENDER_OUT_FBC}"
EXPECTED_RESPONSE="kubevirt-hyperconverged-v2"
echo "${output}"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 0 ]]
}

@test "Get package from catalog: missing image" {
run get_package_from_catalog
EXPECTED_RESPONSE="get_package_from_catalog: Missing 'opm render' output for the image"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 2 ]]
}

@test "Get channel from catalog: success" {
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged",
"defaultChannel": "stable"
}
EOF
)
PACKAGE_NAME="kubevirt-hyperconverged"
run get_channel_from_catalog "${RENDER_OUT_FBC}" "${PACKAGE_NAME}"
EXPECTED_RESPONSE="stable"
echo "${output}"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 0 ]]
}

@test "Get channel from catalog: failure, package not found" {
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged",
"defaultChannel": "stable"
}
EOF
)
PACKAGE_NAME="kubevirt"
run get_channel_from_catalog "${RENDER_OUT_FBC}" "${PACKAGE_NAME}"
EXPECTED_RESPONSE="get_channel_from_catalog: Package name kubevirt not found in the rendered FBC"
echo "${output}"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 1 ]]
}

@test "Get channel from catalog: failure, invalid input" {
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.package",
"name": "kubevirt-hyperconverged",
"defaultChannel": "stable"
}
EOF
)
run get_channel_from_catalog "${RENDER_OUT_FBC}"
EXPECTED_RESPONSE="get_channel_from_catalog: Invalid input. Usage: get_channel_from_catalog <RENDER_OUT_FBC> <PACKAGE_NAME>"
echo "${output}"
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 2 ]]
}

0 comments on commit 8a0b759

Please sign in to comment.