cronjob-scale-down-operator

by Z4ck404

Helm chartv0.5.3App 0.5.1MITVerified publisher

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

ParameterDescriptionDefault
replicaCountNumber of operator replicas1
image.repositoryContainer image repositoryghcr.io/cronschedules/cronjob-scale-down-operator
image.tagContainer image tag0.5.1
image.pullPolicyImage pull policyIfNotPresent
serviceAccount.createCreate service accounttrue
rbac.createCreate RBAC resourcestrue
resources.limits.cpuCPU limit500m
resources.limits.memoryMemory limit128Mi
resources.requests.cpuCPU request10m
resources.requests.memoryMemory request64Mi
metrics.enabledEnable metrics servicetrue
webui.enabledExpose 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-forwardfalse
webui.networkPolicy.enabledCreate a NetworkPolicy that only lets webui.networkPolicy.from peers reach port 8082false
webui.networkPolicy.fromNetworkPolicy peers allowed to reach the web UI[]
security.allowCrossNamespaceLet a CronJobScaleDown target or clean up other namespaces (--allow-cross-namespace, operator >= 0.5.0)false
security.allowClusterScopedCleanupAllow ClusterRole/ClusterRoleBinding cleanup (--allow-cluster-scoped-cleanup, operator >= 0.5.0)false
leaderElection.enabledEnable leader election for HAtrue

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 tracking
  • cronjob_scaledown_scale_operations_total - Total scaling operations (up/down) with target resource details
  • cronjob_scaledown_cleanup_operations_total - Total cleanup operations by resource type and label
  • cronjob_scaledown_orphan_cleanup_total - Orphan resource cleanup tracking
  • cronjob_scaledown_active_resources - Active resource counts and replica status
  • cronjob_scaledown_schedule_executions - Schedule execution timestamps
  • cronjob_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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. 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.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcronjob-scale-down-operator is pinned at v0.5.3 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. 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