SaaS Job Cleaner
The mas-devops-saas-job-cleaner tool cleans up completed jobs in SaaS environments.
Usage
Description
This tool automatically cleans up completed Kubernetes jobs in MAS SaaS environments. It helps maintain a clean cluster by removing old job resources that are no longer needed, preventing resource accumulation and improving cluster performance.
Options
--namespace: Kubernetes namespace to clean jobs from--max-age-hours: Maximum age in hours for completed jobs (default: 24)--dry-run: Preview what would be deleted without actually deleting--log-level: Logging level (DEBUG, INFO, WARNING, ERROR)
Examples
Clean Jobs Older Than 24 Hours
Clean Jobs Older Than 48 Hours
Dry Run (Preview Only)
mas-devops-saas-job-cleaner \
--namespace mas-myinstance-core \
--max-age-hours 24 \
--dry-run \
--log-level DEBUG
Clean Multiple Namespaces
# Clean jobs in multiple namespaces
for ns in mas-inst1-core mas-inst2-core mas-inst3-core; do
mas-devops-saas-job-cleaner \
--namespace $ns \
--max-age-hours 24 \
--log-level INFO
done
What It Does
The tool performs the following operations:
- Scans Namespace: Lists all jobs in the specified namespace
- Filters Completed Jobs: Identifies jobs that have completed (succeeded or failed)
- Checks Age: Determines which jobs are older than the specified age
- Deletes Jobs: Removes jobs that meet the criteria
- Reports Results: Provides summary of deleted jobs
Job Selection Criteria
Jobs are selected for deletion if they meet ALL of the following criteria:
- Job status is "Completed" (either succeeded or failed)
- Job completion time is older than
--max-age-hours - Job is in the specified namespace
Prerequisites
- Access to the OpenShift/Kubernetes cluster
- Valid kubeconfig configured
- Appropriate permissions to list and delete jobs in the namespace
- Must be logged into the cluster
Safety Features
Dry Run Mode
Use --dry-run to preview what would be deleted:
This will show you which jobs would be deleted without actually deleting them.
Age Protection
The tool only deletes jobs older than the specified age, protecting recent jobs from accidental deletion.
Automation
Cron Job Setup
You can automate job cleanup using a Kubernetes CronJob:
apiVersion: batch/v1
kind: CronJob
metadata:
name: job-cleaner
namespace: mas-myinstance-core
spec:
schedule: "0 2 * * *" # Run daily at 2 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: job-cleaner
image: your-registry/mas-devops:latest
command:
- mas-devops-saas-job-cleaner
- --namespace
- mas-myinstance-core
- --max-age-hours
- "24"
- --log-level
- INFO
restartPolicy: OnFailure
Script Automation
Create a script to clean multiple namespaces:
#!/bin/bash
# cleanup-jobs.sh
NAMESPACES=(
"mas-inst1-core"
"mas-inst2-core"
"mas-inst3-core"
)
MAX_AGE=24
LOG_LEVEL="INFO"
for ns in "${NAMESPACES[@]}"; do
echo "Cleaning jobs in namespace: $ns"
mas-devops-saas-job-cleaner \
--namespace "$ns" \
--max-age-hours "$MAX_AGE" \
--log-level "$LOG_LEVEL"
done
Output
The tool provides detailed output including:
- Number of jobs scanned
- Number of jobs eligible for deletion
- List of deleted jobs
- Any errors encountered
Example output:
INFO: Scanning namespace mas-myinstance-core for completed jobs
INFO: Found 15 completed jobs
INFO: 8 jobs are older than 24 hours
INFO: Deleting job: data-import-job-20231201
INFO: Deleting job: backup-job-20231202
...
INFO: Successfully deleted 8 jobs
Exit Codes
0: Cleanup completed successfully1: Error occurred during cleanup
Best Practices
- Start with Dry Run: Always test with
--dry-runfirst - Conservative Age: Start with a longer age (e.g., 48 hours) and adjust as needed
- Regular Schedule: Run regularly (e.g., daily) to prevent accumulation
- Monitor Logs: Review logs to ensure expected behavior
- Namespace Specific: Run separately for each namespace to maintain control
Troubleshooting
Permission Denied
If you get permission errors:
No Jobs Deleted
If no jobs are deleted:
- Check that jobs exist:
oc get jobs -n mas-myinstance-core - Verify job completion status
- Check job age against
--max-age-hours - Use
--dry-runto see what would be deleted