Skip to content

Commit

Permalink
Merge pull request #816 from IBM/806-cpd-script-fix
Browse files Browse the repository at this point in the history
Fix project assignment and improve project existence checks in subscr…
  • Loading branch information
fketelaars authored Dec 3, 2024
2 parents 669ef56 + 6d0edce commit 8193fc0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
65 changes: 37 additions & 28 deletions scripts/cp4d/cp4d-olm-fix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -113,4 +122,4 @@ while true;do
sleep 60

done
exit 0
exit 0
31 changes: 20 additions & 11 deletions scripts/cp4d/cp4d-olm-status.sh
Original file line number Diff line number Diff line change
@@ -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}'
#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

0 comments on commit 8193fc0

Please sign in to comment.