Skip to content

Create k8s_failed_cronjobs.py - #49

Open
kkellerlbl wants to merge 1 commit into
masterfrom
DEVOPS-2524
Open

Create k8s_failed_cronjobs.py#49
kkellerlbl wants to merge 1 commit into
masterfrom
DEVOPS-2524

Conversation

@kkellerlbl

Copy link
Copy Markdown
Member

Initial commit of script checking Jobs spawned by CronJobs. Suitable for use as an MRPE check.

Initial commit of script checking Jobs spawned by CronJobs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Python MRPE/Nagios-style check that queries Kubernetes for CronJobs and their child Jobs (via ownerReferences) and reports failures via exit status.

Changes:

  • Introduces lakehouse/k8s_failed_cronjobs.py to enumerate CronJobs/Jobs cluster-wide and compute an overall status.
  • Emits human-readable summary output intended for MRPE consumption.
Suppressed comments (3)

lakehouse/k8s_failed_cronjobs.py:72

  • len(failed_children) > 0 is a global list across all CronJobs, so a failure in one CronJob can incorrectly downgrade a different CronJob to WARNING as soon as it has a successful Job. Track failures per CronJob when deciding whether to return WARNING vs OK for that CronJob.
            elif any(c.type == "SuccessCriteriaMet" and c.status == "True" for c in conditions):
                if (len(failed_children) > 0):
                    cronjobstatus=1

lakehouse/k8s_failed_cronjobs.py:87

  • The script can print "all CronJobs OK" while returning exit status 3 (UNKNOWN), e.g. when there are no CronJobs or no child Jobs and status remains 3. Also, the "failed CronJobs" message is currently listing failed Job names. Make the output consistent with the exit code and label the list accurately.
    if failed_children:
        print(f"failed CronJobs:  {', '.join(failed_children)} ; CronJobs checked: {', '.join(cronjob_names)}")
    else:
#        print(str(status) + f" ok      {ns}/{name}  ({len(children)} job(s))")
        print(f"all CronJobs OK, CronJobs checked: {', '.join(cronjob_names)}")

lakehouse/k8s_failed_cronjobs.py:93

  • Unhandled Kubernetes API/config exceptions will currently bubble up as a Python traceback, which is noisy for MRPE/local checks and may result in a non-Nagios exit status. Catch exceptions at the top-level and exit with UNKNOWN (3) and a single-line message.
if __name__ == "__main__":
    sys.exit(main())    

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

uid = cj.metadata.uid

cronjob_names.append(cj.metadata.namespace+'/'+cj.metadata.name)
children = jobs_by_cronjob_uid.get(uid, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants