From 6d0edce59632df56172859f1ffdf003d05ec8a69 Mon Sep 17 00:00:00 2001 From: Luigi Molinaro Date: Fri, 18 Oct 2024 15:58:46 +0000 Subject: [PATCH] Fix project assignment and improve project existence checks in subscription listing script --- scripts/cp4d/cp4d-olm-fix.sh | 65 +++++++++++++++++++-------------- scripts/cp4d/cp4d-olm-status.sh | 31 ++++++++++------ 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/scripts/cp4d/cp4d-olm-fix.sh b/scripts/cp4d/cp4d-olm-fix.sh index a5af5e1b3..7a02f34a2 100755 --- a/scripts/cp4d/cp4d-olm-fix.sh +++ b/scripts/cp4d/cp4d-olm-fix.sh @@ -13,6 +13,29 @@ log() { printf "[${LOG_TIME}] ${1}\n" | tee -a ${temp_dir}/$fs_project-olm-fix.log } +# +# Checks +# + +# Check if jq is installed; handle apt and dnf systems (Ubuntu and Red Hat) +if ! command -v jq &> /dev/null; then + if command -v apt &> /dev/null; then + echo "Error: jq is not installed. You can install it by running: sudo apt install jq" + elif command -v dnf &> /dev/null; then + echo "Error: jq is not installed. You can install it by running: sudo dnf install jq" + else + echo "Error: jq is not installed, and your package manager is not recognized. Please install jq manually." + fi + exit 1 +fi + +# Check if PROJECT_CPD_INST_OPERATORS and PROJECT_CPD_INST_OPERANDS are set, otherwise throw an error +if [ -z "${PROJECT_CPD_INST_OPERATORS}" ] || [ -z "${PROJECT_CPD_INST_OPERANDS}" ]; then + echo "Error: Environment variables PROJECT_CPD_INST_OPERATORS or PROJECT_CPD_INST_OPERANDS are not set." + echo "Please source the cpd_vars file or define them" + exit 1 +fi + # # Initialization # @@ -22,29 +45,15 @@ csv_file=${temp_dir}/csv.csv ip_file=${temp_dir}/ip.csv -# -# Checks -# -# Checking for command jq to exist. This is required to parse the json output to recreate the subscriptions. -if ! command -v jq &> /dev/null;then - echo "The jq command is required for this script. Please install first." -fi - -# Determine the project that runs the operators -oc get project ibm-common-services > /dev/null 2>&1 -if [ $? -eq 0 ];then - fs_project="ibm-common-services" -else - fs_project=cpd-operators" -fi +#Operators Project +fs_project=${PROJECT_CPD_INST_OPERATORS} # # Body # echo "Temporary directory with logs and output files: ${temp_dir}" -while true;do - +while true; do current_ts=$(date +%s) log "Collecting OLM information into ${temp_dir}" @@ -69,36 +78,36 @@ while true;do cat ${sub_file} # Check if any subscriptions older than 2 minutes still don't have a CSV - while IFS=, read -r sub sub_ts csv sub_state;do + while IFS=, read -r sub sub_ts csv sub_state; do sub_ts_epoch=$(date -d ${sub_ts} +%s) - if [[ "${csv}" == "" ]] && (( current_ts > (sub_ts_epoch + 120) ));then - log "WARNING: Subscription ${sub} does not have a valid CSV, will try to remediate." + if [[ "${csv}" == "" ]] && (( current_ts > (sub_ts_epoch + 120) )); then + log "WARNING: Subscription ${sub} does not have a valid CSV, will try to remediate." remediate=true fi done < ${sub_file} # Start remediation for all subscriptions - if ${remediate};then + if ${remediate}; then cat ${sub_file} log "WARNING: Not all operators are correctly installed. All entries should have a CSV and have state AtLatestKnown" # Delete all subscriptions that don't have a CSV oc get sub -n ${fs_project} -o json | jq 'del(.items[] .metadata.annotations, .items[] .metadata.resourceVersion, .items[] .metadata.creationTimestamp, .items[] .metadata.generation, .items[] .metadata.uid, .items[] .status)' > ${temp_dir}/${fs_project}-subcriptions.json - while IFS=, read -r sub sub_ts csv sub_state;do - if [[ "${csv}" == "" ]];then + while IFS=, read -r sub sub_ts csv sub_state; do + if [[ "${csv}" == "" ]]; then log "DIAG: Deleting subscription ${sub}" oc delete sub -n ${fs_project} ${sub} fi done < ${sub_file} # Delete orphaned CSVs and their install plans - while IFS=, read -r csv csv_ts;do - if ! grep ${csv} ${sub_file};then - for ip in $(grep ${csv} ${ip_file} | awk '{print $1}');do + while IFS=, read -r csv csv_ts; do + if ! grep ${csv} ${sub_file}; then + for ip in $(grep ${csv} ${ip_file} | awk '{print $1}'); do log "REMEDIATE: Deleting install plan $ip, associated with CSV ${csv}" oc delete ip -n ${fs_project} $ip --ignore-not-found done log "REMEDIATE: Deleting orphaned CSV ${csv}" oc delete csv -n ${fs_project} ${csv} --ignore-not-found - fi + fi done < ${csv_file} log "REMEDIATE: Re-creating deleted subscriptions" oc apply -f ${temp_dir}/${fs_project}-subcriptions.json @@ -113,4 +122,4 @@ while true;do sleep 60 done -exit 0 \ No newline at end of file +exit 0 diff --git a/scripts/cp4d/cp4d-olm-status.sh b/scripts/cp4d/cp4d-olm-status.sh index 65b3517ee..d2eee37ae 100755 --- a/scripts/cp4d/cp4d-olm-status.sh +++ b/scripts/cp4d/cp4d-olm-status.sh @@ -1,16 +1,25 @@ #!/bin/bash SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) -# Determine the project that runs the operators -oc get project ibm-common-services > /dev/null 2>&1 -if [ $? -eq 0 ];then - fs_project="ibm-common-services" -else - fs_project=cpd-operators" + +# Check if PROJECT_CPD_INST_OPERATORS and PROJECT_CPD_INST_OPERANDS are set, otherwise throw an error +if [ -z "${PROJECT_CPD_INST_OPERATORS}" ] || [ -z "${PROJECT_CPD_INST_OPERANDS}" ]; then + echo "Error: Environment variables PROJECT_CPD_INST_OPERATORS or PROJECT_CPD_INST_OPERANDS are not set." + echo "Please source the cpd_vars file or define them" + exit 1 fi -echo "Listing subscriptions and their CSVs" -oc get sub -n ${fs_project} \ - --sort-by=.metadata.creationTimestamp \ - --no-headers \ - -o jsonpath='{range .items[*]}{.metadata.name}{","}{.metadata.creationTimestamp}{","}{.status.installedCSV}{","}{.status.state}{"\n"}{end}' \ No newline at end of file +#Operators Project +fs_project=${PROJECT_CPD_INST_OPERATORS} + +# List subscriptions and their CSVs +if oc get project ${fs_project} > /dev/null 2>&1; then + echo "Listing subscriptions and their CSVs" + oc get sub -n ${fs_project} \ + --sort-by=.metadata.creationTimestamp \ + --no-headers \ + -o jsonpath='{range .items[*]}{.metadata.name}{","}{.metadata.creationTimestamp}{","}{.status.installedCSV}{","}{.status.state}{"\n"}{end}' +else + echo "Error: Project ${fs_project} does not exist or cannot be accessed." + exit 1 +fi \ No newline at end of file