Skip to content

Instantly share code, notes, and snippets.

@oradwell
Last active March 9, 2022 13:27
Show Gist options
  • Select an option

  • Save oradwell/8d3262326dd2b7d51c63708a698e861d to your computer and use it in GitHub Desktop.

Select an option

Save oradwell/8d3262326dd2b7d51c63708a698e861d to your computer and use it in GitHub Desktop.

Revisions

  1. oradwell revised this gist Feb 1, 2022. 1 changed file with 25 additions and 18 deletions.
    43 changes: 25 additions & 18 deletions cronjob-bug-active-job-cleanup.sh
    Original 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
    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
    echo Cannot authenticate with API server
    exit 1
    fi

    jsonpath_template='{.items[*].status.active[*].name}'
    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)
    | grep " 1 " \
    | cut -d' ' -f1)

    if [ -z "${active_cronjobs}" ]
    then
    echo No active CronJobs
    exit 1
    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}")
    --context="${KUBE_CONTEXT}" get cronjob -ojsonpath \
    --template="${jsonpath_template}")

    if [ -z "${active_cronjob_jobs}" ]
    then
    echo CronJobs have no active Jobs
    exit 1
    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)
    | grep 1/1 \
    | cut -d' ' -f1)

    if [ -z "${completed_jobs_marked_active}" ]
    then
    echo Found jobs are actually active. Nothing to do.
    exit 0
    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
    | xargs kubectl --context="${KUBE_CONTEXT}" delete job

    exit 0
  2. oradwell created this gist Jan 24, 2022.
    55 changes: 55 additions & 0 deletions cronjob-bug-active-job-cleanup.sh
    Original 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