cronjob-scale-down-operator
by Z4ck404
A Kubernetes operator that automatically scales down Deployments and StatefulSets during specific time windows and provides comprehensive resource cleanup capabilities
About this chart
CronJob Scale Down Operator Helm Chart
A Kubernetes operator that automatically scales down Deployments and StatefulSets during specific time windows and provides comprehensive resource cleanup capabilities including orphan resource management.
Features
- ๐ Scheduled Scaling: Automatically scale down/up resources based on cron schedules
- ๐งน Resource Cleanup: Clean up test resources based on annotations and time-to-live
- ๐๏ธ Orphan Cleanup: Age-based cleanup for resources without annotations (v0.4.0+)
- ๐ RBAC Support: Extended cleanup for Roles, RoleBindings, and cluster resources (v0.4.0+)
- ๐ Timezone Support: Configure schedules with specific timezones
- ๐ Metrics & Monitoring: Comprehensive Prometheus metrics with Grafana dashboards (v0.4.1+)
- ๐ Security: Runs with minimal required permissions and security contexts
- ๐ฏ Flexible Targeting: Support for Deployments, StatefulSets, and comprehensive resource types
Prerequisites
- Kubernetes 1.16+
- Helm 3.0+
Installation
Add Helm Repository
helm repo add cronschedules https://cronschedules.github.io/charts
helm repo update
Install the Chart
# Create namespace (recommended)
kubectl create namespace cronschedules-system
# Install with default values
helm install cronjob-scale-down-operator cronschedules/cronjob-scale-down-operator \
--namespace cronschedules-system
# Install with custom values
helm install cronjob-scale-down-operator cronschedules/cronjob-scale-down-operator \
--namespace cronschedules-system \
--set image.tag=0.5.1 \
--set resources.limits.memory=256Mi
Configuration
| Parameter | Description | Default |
|---|---|---|
replicaCount | Number of operator replicas | 1 |
image.repository | Container image repository | ghcr.io/cronschedules/cronjob-scale-down-operator |
image.tag | Container image tag | 0.5.1 |
image.pullPolicy | Image pull policy | IfNotPresent |
serviceAccount.create | Create service account | true |
rbac.create | Create RBAC resources | true |
resources.limits.cpu | CPU limit | 500m |
resources.limits.memory | Memory limit | 128Mi |
resources.requests.cpu | CPU request | 10m |
resources.requests.memory | Memory request | 64Mi |
metrics.enabled | Enable metrics service | true |
webui.enabled | Expose the (unauthenticated) web UI on :8082 via a Service. When false, the UI only listens on pod loopback (--webui-addr=127.0.0.1:8082) for kubectl port-forward | false |
webui.networkPolicy.enabled | Create a NetworkPolicy that only lets webui.networkPolicy.from peers reach port 8082 | false |
webui.networkPolicy.from | NetworkPolicy peers allowed to reach the web UI | [] |
security.allowCrossNamespace | Let a CronJobScaleDown target or clean up other namespaces (--allow-cross-namespace, operator >= 0.5.0) | false |
security.allowClusterScopedCleanup | Allow ClusterRole/ClusterRoleBinding cleanup (--allow-cluster-scoped-cleanup, operator >= 0.5.0) | false |
leaderElection.enabled | Enable leader election for HA | true |
Security
The operator runs with a cluster-wide ClusterRole that can scale workloads and delete the resource types
supported by cleanup. Anyone who can create a CronJobScaleDown can direct that authority. Starting with
operator 0.5.0, each CronJobScaleDown is confined to its own namespace and cannot clean up cluster-scoped
RBAC unless security.allowCrossNamespace / security.allowClusterScopedCleanup are enabled. Only enable
them when everyone able to create CronJobScaleDown resources is trusted cluster-wide.
The web UI has no authentication and shows resources from all namespaces. It is not exposed by default:
kubectl -n cronschedules-system port-forward deploy/<release>-cronjob-scale-down-operator-controller-manager 8082:8082
If you set webui.enabled=true, put the Service/Ingress behind an authenticating proxy (e.g. oauth2-proxy)
and/or enable webui.networkPolicy.
Usage Examples
Scheduled Scaling
Scale down a deployment during non-business hours:
apiVersion: cronschedules.elbazi.co/v1
kind: CronJobScaleDown
metadata:
name: webapp-scaler
namespace: production
spec:
targetRef:
name: webapp-deployment
namespace: production
kind: Deployment
apiVersion: apps/v1
scaleDownSchedule: "0 18 * * 1-5" # 6 PM on weekdays
scaleUpSchedule: "0 8 * * 1-5" # 8 AM on weekdays
timeZone: "America/New_York"
Resource Cleanup
Clean up test resources older than 2 hours:
apiVersion: cronschedules.elbazi.co/v1
kind: CronJobScaleDown
metadata:
name: test-cleanup
namespace: testing
spec:
cleanupSchedule: "0 */2 * * *" # Every 2 hours
cleanupConfig:
annotationKey: "test.elbazi.co/cleanup-after"
resourceTypes:
- "Deployment"
- "Service"
- "ConfigMap"
- "Secret"
- "Pod"
- "Job"
labelSelector:
environment: "test"
dryRun: false
timeZone: "UTC"
Orphan Resource Cleanup (v0.4.0+)
Clean up forgotten resources without cleanup annotations:
apiVersion: cronschedules.elbazi.co/v1
kind: CronJobScaleDown
metadata:
name: orphan-cleanup
namespace: testing
spec:
cleanupSchedule: "0 0 3 * * *" # Daily at 3 AM
cleanupConfig:
annotationKey: "cleanup-after"
resourceTypes:
- "Pod"
- "Job"
- "ConfigMap"
- "Role"
- "RoleBinding"
# Orphan cleanup for resources older than 7 days
cleanupOrphanResources: true
orphanResourceMaxAge: "168h"
labelSelector:
app.kubernetes.io/managed-by: "test"
dryRun: false
timeZone: "UTC"
Monitoring
The operator exposes comprehensive Prometheus metrics on port 8080 at /metrics endpoint:
Core Metrics
cronjob_scaledown_reconciliations_total- Total reconciliation attempts with error trackingcronjob_scaledown_scale_operations_total- Total scaling operations (up/down) with target resource detailscronjob_scaledown_cleanup_operations_total- Total cleanup operations by resource type and labelcronjob_scaledown_orphan_cleanup_total- Orphan resource cleanup trackingcronjob_scaledown_active_resources- Active resource counts and replica statuscronjob_scaledown_schedule_executions- Schedule execution timestampscronjob_scaledown_config_validation_errors- Configuration validation error tracking
Grafana Dashboard
The operator includes example Grafana dashboards and PromQL queries for comprehensive monitoring and observability.
Troubleshooting
Check Operator Logs
kubectl logs -l control-plane=controller-manager -n cronschedules-system
Verify RBAC Permissions
kubectl auth can-i list deployments --as=system:serviceaccount:cronschedules-system:cronjob-scale-down-operator
Check CRD Status
kubectl get cronjobscaledowns -A
kubectl describe cronjobscaledown <name> -n <namespace>
Uninstallation
helm uninstall cronjob-scale-down-operator -n cronschedules-system
kubectl delete namespace cronschedules-system
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
Deploy cronjob-scale-down-operator with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cronjob-scale-down-operator on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartcronjob-scale-down-operator is pinned at v0.5.3 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply