From fc82712f24c5fd155078c07dc6494d4f45e0f52a Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Sat, 22 Feb 2025 11:26:55 +0100
Subject: [PATCH] feat: add unofficial oc extra options support

---
 templates/gitlab-ci-openshift.yml | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/templates/gitlab-ci-openshift.yml b/templates/gitlab-ci-openshift.yml
index d606805..19ff9dd 100644
--- a/templates/gitlab-ci-openshift.yml
+++ b/templates/gitlab-ci-openshift.yml
@@ -485,7 +485,8 @@ stages:
   function build_template_param_args() {
     global_env="$1"
     spec_env="$2"
-    oc process --parameters -f - | awk 'NR > 1{print $1}' | while read -r param_name
+    # shellcheck disable=SC2086
+    oc $OS_OC_EXTRA_OPTS process --parameters -f - | awk 'NR > 1{print $1}' | while read -r param_name
     do
       # 1: look for param into specific dotenv file
       if grep -e "^$param_name=" "$spec_env" >/dev/null 2>&1
@@ -521,7 +522,8 @@ stages:
 
     # set label 'app' and 'env' on all created objects ($OS_STAGE_LABEL for backwards compatibility)
     echo "oc process --labels \"${OS_ENV_LABEL:-${OS_STAGE_LABEL:-env}}=$environment_type,${OS_APP_LABEL:-app}=$environment_name\" $param_args -f $templatefile"
-    eval oc process --labels "${OS_ENV_LABEL:-${OS_STAGE_LABEL:-env}}=$environment_type,${OS_APP_LABEL:-app}=$environment_name" "$param_args" -f "$templatefile" | oc ${TRACE+--loglevel=6} apply -f -
+    # shellcheck disable=SC2086
+    eval oc $OS_OC_EXTRA_OPTS process --labels "${OS_ENV_LABEL:-${OS_STAGE_LABEL:-env}}=$environment_type,${OS_APP_LABEL:-app}=$environment_name" "$param_args" -f "$templatefile" | oc $OS_OC_EXTRA_OPTS ${TRACE+--loglevel=6} apply -f -
   }
 
   function check_readiness() {
@@ -552,7 +554,8 @@ stages:
 
   function os_deploy() {
      # export project as it may be usefull to build image name based on internal registry (ex: docker-registry.default.svc:5000/${project}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_LABEL} )
-    project=$(oc project -q)
+    # shellcheck disable=SC2086
+    project=$(oc $OS_OC_EXTRA_OPTS project -q)
     export project
     export environment_type=$ENV_TYPE
     export environment_name=${ENV_APP_NAME:-${OS_BASE_APP_NAME}${ENV_APP_SUFFIX}}
@@ -626,7 +629,8 @@ stages:
 
   # $1 deployment name
   function last_rollout_status() {
-    oc rollout history "dc/$1" | sed  -e '1,2d' -e '$d' | tail -1 | awk -F" " '{print tolower($2)}'
+    # shellcheck disable=SC2086
+    oc $OS_OC_EXTRA_OPTS rollout history "dc/$1" | sed  -e '1,2d' -e '$d' | tail -1 | awk -F" " '{print tolower($2)}'
   }
 
   # $1 deployment name
@@ -635,7 +639,8 @@ stages:
     if [ "$last_status" != "running" ] && [ "$last_status" != "pending" ]
     then
       log_info "Force rollout as last status is $last_status"
-      oc rollout latest "$1"
+      # shellcheck disable=SC2086
+      oc $OS_OC_EXTRA_OPTS rollout latest "$1"
     else
       log_info "A rollout is currently processing with status $last_status"
     fi
@@ -727,7 +732,8 @@ stages:
       # delete app
       log_info "--- \\e[32moc delete\\e[0m"
       # delete all objects with label 'app=$environment_name'
-      oc ${TRACE+--loglevel=6} delete "${OS_CLEANUP_OBJECT_TYPES}" --selector "${OS_APP_LABEL:-app}=$environment_name"
+      # shellcheck disable=SC2086
+      oc $OS_OC_EXTRA_OPTS ${TRACE+--loglevel=6} delete "${OS_CLEANUP_OBJECT_TYPES}" --selector "${OS_APP_LABEL:-app}=$environment_name"
 
       # maybe execute post cleanup script
       postscript="$OS_SCRIPTS_DIR/os-post-cleanup.sh"
@@ -748,7 +754,8 @@ stages:
     # make environment_name regex by replacing $CI_COMMIT_REF_SLUG with .*
     environment_nameregex=$(echo "$environment_nameproto" | sed -r "s/$CI_COMMIT_REF_SLUG/.*/g")
     # list services | remove 1st line | pick 7th column (selector) | filter services with label "app=$regex"
-    matchingselectors=$(oc get svc -o wide | tail -n +2 | awk '{print $7}' | awk "/${OS_APP_LABEL:-app}=$environment_nameregex/ {print \$1}")
+    # shellcheck disable=SC2086
+    matchingselectors=$(oc $OS_OC_EXTRA_OPTS get svc -o wide | tail -n +2 | awk '{print $7}' | awk "/${OS_APP_LABEL:-app}=$environment_nameregex/ {print \$1}")
     matchcount=$(echo "$matchingselectors" | wc -w)
 
     log_info "--- \\e[32mdelete all"
@@ -781,10 +788,12 @@ stages:
     # $1 image name
     # $2 number of image tags to keep
     log_info "Keeping last $2 images"
-    oc get -o json "is/$1" | extract_oldest_tags_from_image_stream_definition "$2" | while read -r tag
+    # shellcheck disable=SC2086
+    oc $OS_OC_EXTRA_OPTS get -o json "is/$1" | extract_oldest_tags_from_image_stream_definition "$2" | while read -r tag
     do
       log_info "Removing image stream tag $1:$tag"
-      oc delete "istag/$1:$tag"
+      # shellcheck disable=SC2086
+      oc $OS_OC_EXTRA_OPTS delete "istag/$1:$tag"
     done
   }
 
@@ -808,7 +817,8 @@ stages:
     - assert_defined "${ENV_API_URL:-$OS_URL}" 'Missing required OpenShift url'
     - assert_defined "${ENV_TOKEN:-$OS_TOKEN}" 'Missing required OpenShift token'
     - assert_defined "$ENV_PROJECT" 'Missing required OpenShift project'
-    - oc login "${ENV_API_URL:-$OS_URL}" --token="${ENV_TOKEN:-$OS_TOKEN}" -n "$ENV_PROJECT"
+    # use of unofficial $OC_LOGIN_EXTRA_OPTS variable to pass extra options to oc login
+    - oc $OS_OC_EXTRA_OPTS login ${OC_LOGIN_EXTRA_OPTS} "${ENV_API_URL:-$OS_URL}" --token="${ENV_TOKEN:-$OS_TOKEN}" -n "$ENV_PROJECT"
 
 # Deploy job prototype
 # Can be extended to define a concrete environment
-- 
GitLab