Skip to content

Commit

Permalink
feat(RELEASE-1327): update internal-request script to support resolvers
Browse files Browse the repository at this point in the history
This commit updates the utils/internal-request script to support calling
pipelines via resolvers. It keeps functionality for using request,
although the goal is to only keep it temporarily. I did want to keep
request support though in case a bug is found in the utils image before
the catalog task hitting the bug has a chance to be migrated to using
pipeline instead of request.

Signed-off-by: Johnny Bieren <[email protected]>
  • Loading branch information
johnbieren committed Jan 20, 2025
1 parent 0a58b6a commit f194439
Showing 1 changed file with 130 additions and 41 deletions.
171 changes: 130 additions & 41 deletions utils/internal-request
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@
# for status updates.
#
# Usage:
# ./internal-request.sh -r request [-p <key=value> ...] [-s sync] [-t timeout] [--service-account name]
# [--pipeline-timeout 1h0m0s] [--task-timeout 0h55m0s] [--finally-timeout 0h5m0s]
# ./internal-request.sh [-r request] [--pipeline pipeline-name] [-p <key=value> ...] [-s sync]
# [-t timeout] [--service-account name] [--pipeline-timeout 1h0m0s] [--task-timeout 0h55m0s]
# [--finally-timeout 0h5m0s] [--resolver git]
#
# Parameters:
# -r Request: the name of the request.
# -p Parameters: can be specified multiple times. Each '-p' flag represents a
# parameter that will be added to the 'parameters' field in the
# InternalRequest resource. The value of the parameter is treated as a string,
# and it can be a valid JSON object or array. When passing complex parameter
# values, make sure to enclose them in quotes.
# -l labels: can be specified multiple times. Each '-l' flag represents a
# label that will be added to the 'metadata.labels' field in the
# InternalRequest resource. The value of the parameter must be a string. Optional.
# -s Sync: a flag that indicates whether the script has to finish to complete
# the tasks or exit immediately after creating the resource. Default is true.
# -t Timeout: Defaults to 600 seconds.
# --service-account The name of the service account to be used in the pipelineRun execution. Optional.
# --pipeline-timeout The total timeout for the invoked pipelineRun. Defaults to 60mins
# --task-timeout The timeout for the tasks invoked in the pipelineRun. Defaults to 55mins
# --finally-timeout The timeout for the finally tasks invoked in the pipelineRun. Defaults to 5mins
# -h Display this help message.
# -r Request: the name of the request.
# --pipeline The name of the pipeline to execute in the internal/pipelines directory of
# release-service-catalog.
# -p Parameters: can be specified multiple times. Each '-p' flag represents a
# parameter that will be added to the 'parameters' field in the
# InternalRequest resource. The value of the parameter is treated as a string,
# and it can be a valid JSON object or array. When passing complex parameter
# values, make sure to enclose them in quotes.
# -l labels: can be specified multiple times. Each '-l' flag represents a
# label that will be added to the 'metadata.labels' field in the
# InternalRequest resource. The value of the parameter must be a string. Optional.
# -s Sync: a flag that indicates whether the script has to finish to complete
# the tasks or exit immediately after creating the resource. Default is true.
# -t Timeout: Defaults to 600 seconds.
# --task-git-url The url to the git repo where the release-service-catalog tasks to be used are stored.
# --task-git-revision The revision in the taskGitUrl repo to be used.
# --service-account The name of the service account to be used in the pipelineRun execution. Optional.
# --pipeline-timeout The total timeout for the invoked pipelineRun. Defaults to 60mins
# --task-timeout The timeout for the tasks invoked in the pipelineRun. Defaults to 55mins
# --finally-timeout The timeout for the finally tasks invoked in the pipelineRun. Defaults to 5mins
# --resolver The type of resolver to use (e.g. git, cluster).
# -h Display this help message.
#
# Prerequisites:
# - kubectl: The Kubernetes command line tool must be installed and properly
Expand All @@ -55,24 +61,30 @@ PARAMS=""
PIPELINE_TIMEOUT=1h0m0s
TASK_TIMEOUT=0h55m0s
FINALLY_TIMEOUT=0h5m0s
RESOLVER=git

function usage {
echo "Usage: $0 -r request [-p parameters] [-l labels] [-s sync] [-t timeout] [--service-account name] [--pipeline-timeout 1h0m0s] [--task-timeout 0h55m0s] [--finally-timeout 0h5m0s]"
echo "Usage: $0 [-r request] [--pipeline pipeline-name] [-p parameters] [-l labels] [-s sync] [-t timeout] [--service-account name] [--pipeline-timeout 1h0m0s] [--task-timeout 0h55m0s] [--finally-timeout 0h5m0s] [--resolver git]"
echo
echo " -r Request: the name of the request."
echo " -p Params: can be specified multiple times. Each '-p' flag represents a"
echo " parameter that will be added to the 'parameters' field in the"
echo " InternalRequest resource."
echo " -l Labels: can be specified multiple times. Each '-l' flag represents a"
echo " label that will be added to the 'metadata.labels' field in the"
echo " InternalRequest resource. Optional."
echo " -s Sync: a flag that indicates whether the script has to finish to complete the tasks or"
echo " exit immediately after creating the resource. Default is true."
echo " -t Timeout: Defaults to 600 seconds."
echo " --service-account The name of the service account to be used in the pipelineRun execution. Optional."
echo " --pipeline-timeout The total timeout for the invoked pipelineRun. Defaults to 60mins."
echo " --task-timeout The timeout for the tasks invoked in the pipelineRun. Defaults to 55mins."
echo " --finally-timeout The timeout for the finally tasks invoked in the pipelineRun. Defaults to 5mins."
echo " -r Request: the name of the request."
echo " --pipeline The name of the pipeline to execute in the internal/pipelines directory of"
echo " release-service-catalog."
echo " -p Params: can be specified multiple times. Each '-p' flag represents a"
echo " parameter that will be added to the 'parameters' field in the"
echo " InternalRequest resource."
echo " -l Labels: can be specified multiple times. Each '-l' flag represents a"
echo " label that will be added to the 'metadata.labels' field in the"
echo " InternalRequest resource. Optional."
echo " -s Sync: a flag that indicates whether the script has to finish to complete the tasks or"
echo " exit immediately after creating the resource. Default is true."
echo " -t Timeout: Defaults to 600 seconds."
echo " --task-git-url The url to the git repo where the release-service-catalog tasks to be used are stored."
echo " --task-git-revision The revision in the taskGitUrl repo to be used."
echo " --service-account The name of the service account to be used in the pipelineRun execution. Optional."
echo " --pipeline-timeout The total timeout for the invoked pipelineRun. Defaults to 60mins."
echo " --task-timeout The timeout for the tasks invoked in the pipelineRun. Defaults to 55mins."
echo " --finally-timeout The timeout for the finally tasks invoked in the pipelineRun. Defaults to 5mins."
echo " --resolver The type of resolver to use (e.g. git, cluster)."
echo " -h Display this help message."
exit 1
}
Expand All @@ -85,14 +97,18 @@ function convert_to_seconds {
# Parsing arguments
PARAMS=() # initialize PARAMS as an empty array
LABELS=() # initialize LABELS as an empty array
OPTIONS=$(getopt -l "service-account:,pipeline-timeout:,task-timeout:,finally-timeout:" -o "r:p:l:s:t:h" -a -- "$@")
OPTIONS=$(getopt -l "pipeline:,task-git-url:,task-git-revision:,service-account:,pipeline-timeout:,task-timeout:,finally-timeout:,resolver:" -o "r:p:l:s:t:h" -a -- "$@")
eval set -- "$OPTIONS"
while true; do
case "$1" in
-r)
shift
REQUEST=$1
;;
--pipeline)
shift
PIPELINE=$1
;;
-p)
shift
PARAMS+=("$1") # append each parameter to the PARAMS array
Expand All @@ -109,6 +125,14 @@ while true; do
shift
TIMEOUT=$1
;;
--task-git-url)
shift
TASKGITURL=$1
;;
--task-git-revision)
shift
TASKGITREVISION=$1
;;
--service-account)
shift
SERVICEACCOUNT=$1
Expand All @@ -125,6 +149,10 @@ while true; do
shift
FINALLY_TIMEOUT=$1
;;
--resolver)
shift
RESOLVER=$1
;;
-h)
shift
usage
Expand All @@ -138,11 +166,24 @@ while true; do
done

# Check if mandatory parameters are set
if [ -z "$REQUEST" ]
if [ -z "$REQUEST" ] && [ -z "$PIPELINE" ]
then
usage
fi

# Make sure resolver is a supported value
# The -n PIPELINE can be dropped once PIPELINE is mandatory
if [[ -n "$PIPELINE" ]]; then
if [[ "$RESOLVER" != "git" ]] && [[ "$RESOLVER" != "cluster" ]] ; then
echo "Only git and cluster resolvers are supported at this time. Please use one of those."
exit 1
fi
if [[ "$RESOLVER" == "git" ]] && ([ -z "$TASKGITURL" ] || [ -z "$TASKGITREVISION" ]) ; then
echo "You must pass --task-git-url and --task-git-revision when using a git resolver."
exit 1
fi
fi

# Make sure all timeouts are passing in XhYmZs format
PIPELINE_VALUES=$(echo $PIPELINE_TIMEOUT | awk -F[h,m,s] '{print $3}')
TASK_VALUES=$(echo $TASK_TIMEOUT | awk -F[h,m,s] '{print $3}')
Expand Down Expand Up @@ -199,22 +240,70 @@ LABEL_JSON=$(echo "${LABEL_JSON_ARRAY[@]}" | jq -s 'add')

# Create JSON payload for the InternalRequest
PAYLOAD=$(jq -n \
--arg request "$REQUEST" \
--argjson parameters "$PARAM_JSON" \
--argjson timeouts "$TIMEOUTS_JSON" \
'{
"apiVersion": "appstudio.redhat.com/v1alpha1",
"kind": "InternalRequest",
"metadata": {
"generateName": ($request + "-")
},
"spec": {
"request": $request,
"params": $parameters,
"timeouts": $timeouts
}
}'
)
# We will inject the pipeline stuff as we did request (without the if)
# when we drop support for request
if [[ -n "${REQUEST}" ]]; then
PAYLOAD=$(jq \
--arg request "$REQUEST" \
'.spec.request = $request' <<< $PAYLOAD)
PAYLOAD=$(jq \
--arg request "$REQUEST" \
'.metadata.generateName = $request + "-"' <<< $PAYLOAD)
fi
# generateName and resolver will be injected when PAYLOAD is initialized once REQUEST support is dropped
if [[ -n "${PIPELINE}" ]]; then
PAYLOAD=$(jq \
--arg pipeline "$PIPELINE" \
'.metadata.generateName = $pipeline + "-"' <<< $PAYLOAD)
PAYLOAD=$(jq \
--arg resolver "$RESOLVER" \
'.spec.pipeline = {
"pipelineRef": {
"resolver": $resolver
}
}' <<< $PAYLOAD)
if [ "$RESOLVER" == "cluster" ] ; then
PAYLOAD=$(jq \
--arg pipeline "${PIPELINE}" \
'.spec.pipeline.pipelineRef.params = [
{
"name": "name",
"value": $pipeline
}
]' <<< $PAYLOAD)
fi
if [ "$RESOLVER" == "git" ] ; then
PAYLOAD=$(jq \
--arg url "$TASKGITURL" \
--arg revision "$TASKGITREVISION" \
--arg pipeline "pipelines/internal/${PIPELINE}/${PIPELINE}.yaml" \
'.spec.pipeline.pipelineRef.params = [
{
"name": "url",
"value": $url
},
{
"name": "revision",
"value": $revision
},
{
"name": "pathInRepo",
"value": $pipeline
}
]' <<< $PAYLOAD)
fi
fi
if [[ -n ${LABELS[@]} ]]; then
PAYLOAD=$(jq \
--argjson labels "$LABEL_JSON" \
Expand Down

0 comments on commit f194439

Please sign in to comment.