Last active
March 9, 2022 13:27
-
-
Save oradwell/8d3262326dd2b7d51c63708a698e861d to your computer and use it in GitHub Desktop.
Revisions
-
oradwell revised this gist
Feb 1, 2022 . 1 changed file with 25 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,54 +2,61 @@ if [ -z "${KUBE_CONTEXT}" ] then echo Please provide KUBE_CONTEXT exit 1 fi kubectl --context="${KUBE_CONTEXT}" version > /dev/null 2>&1 if [ $? -ne 0 ] then echo Cannot authenticate with API server exit 1 fi jsonpath_template_multi='{.items[*].status.active[*].name}' jsonpath_template_single='{.status.active[*].name}' active_cronjobs=$(kubectl --context="${KUBE_CONTEXT}" get cronjob \ | grep " 1 " \ | cut -d' ' -f1) if [ -z "${active_cronjobs}" ] then echo No active CronJobs exit 1 fi jsonpath_template="${jsonpath_template_multi}" if [ "$(echo "${active_cronjobs}" | wc -l)" = "1" ] then jsonpath_template="${jsonpath_template_single}" fi active_cronjob_jobs=$(echo "${active_cronjobs}" | xargs kubectl \ --context="${KUBE_CONTEXT}" get cronjob -ojsonpath \ --template="${jsonpath_template}") if [ -z "${active_cronjob_jobs}" ] then echo CronJobs have no active Jobs exit 1 fi completed_jobs_marked_active=$(echo "${active_cronjob_jobs}" | xargs kubectl --context="${KUBE_CONTEXT}" get job \ | grep 1/1 \ | cut -d' ' -f1) if [ -z "${completed_jobs_marked_active}" ] then echo Found jobs are actually active. Nothing to do. exit 0 fi echo Deleting the following completed jobs: echo "${completed_jobs_marked_active}" sleep 3 echo "${completed_jobs_marked_active}" \ | xargs kubectl --context="${KUBE_CONTEXT}" delete job exit 0 -
oradwell created this gist
Jan 24, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ #!/usr/bin/env bash if [ -z "${KUBE_CONTEXT}" ] then echo Please provide KUBE_CONTEXT exit 1 fi kubectl --context="${KUBE_CONTEXT}" version > /dev/null 2>&1 if [ $? -ne 0 ] then echo Cannot authenticate with API server exit 1 fi jsonpath_template='{.items[*].status.active[*].name}' active_cronjobs=$(kubectl --context="${KUBE_CONTEXT}" get cronjob \ | grep " 1 " \ | cut -d' ' -f1) if [ -z "${active_cronjobs}" ] then echo No active CronJobs exit 1 fi active_cronjob_jobs=$(echo "${active_cronjobs}" | xargs kubectl \ --context="${KUBE_CONTEXT}" get cronjob -ojsonpath \ --template="${jsonpath_template}") if [ -z "${active_cronjob_jobs}" ] then echo CronJobs have no active Jobs exit 1 fi completed_jobs_marked_active=$(echo "${active_cronjob_jobs}" | xargs kubectl --context="${KUBE_CONTEXT}" get job \ | grep 1/1 \ | cut -d' ' -f1) if [ -z "${completed_jobs_marked_active}" ] then echo Found jobs are actually active. Nothing to do. exit 0 fi echo Deleting the following completed jobs: echo "${completed_jobs_marked_active}" sleep 3 echo "${completed_jobs_marked_active}" \ | xargs kubectl --context="${KUBE_CONTEXT}" delete job exit 0