A Kubernetes custom controller for managing CronHorizontalPodAutoscaler
(CronHPA) Custom Resource Definitions (CRDs). This controller allows you to schedule automatic scaling of HorizontalPodAutoscalers (HPAs), Deployments, or StatefulSets based on cron expressions, supporting timezones and one-time executions.
Built with Kubebuilder, using gocron for scheduling and Uber FX for dependency injection.
- Scheduled Scaling: Define jobs with cron schedules to update minReplicas on HPAs or replicas on Deployments/StatefulSets.
- Timezone Support: Schedules are interpreted in specified timezones (defaults to "Asia/Bangkok"), converted internally to UTC.
- Run Once Option: Jobs can be set to run only once and self-remove after execution.
- Target Flexibility: Scales HPAs (minReplicas), Deployments, or StatefulSets (replicas) via unstructured updates.
- Logging and Verification: Detailed info/debug logs for reconciliation, job registration, and scaling actions, including post-update verification.
- Finalizer Handling: Ensures cleanup of scheduled jobs on CR deletion.
- Kubernetes cluster (v1.20+ recommended)
- kubectl
- Go (v1.22+)
- Kubebuilder (v3.15+ for development)
Apply the generated manifests from Kubebuilder:
kubectl apply -k config/manager
Apply a YAML manifest like the following:
apiVersion: autoscaling.dmakeroam.com/v1
kind: CronHorizontalPodAutoscaler
metadata:
name: cronhpa-sample
namespace: default
spec:
scaleTargetRef:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
name: hpa-sample
jobs:
- name: "scale-up-9am"
schedule: "0 0 9 * * *"
timezone: "Asia/Bangkok"
minReplicas: 20
runOnce: false
- name: "scale-down-5pm"
schedule: "0 0 17 * * *"
timezone: "Asia/Bangkok"
minReplicas: 3
runOnce: false
scaleTargetRef
: References the target HPA, Deployment, or StatefulSet.jobs
: List of scaling jobs with cron schedule, optional timezone, minReplicas, and runOnce flag.
Check controller logs for reconciliation and scaling events:
kubectl logs -f deployment/cronhpa-controller-manager -c manager
Example log output during scaling:
INFO Executing scheduled job {"cr": "cronhpa-sample", "job": "scale-up-9am", "target": "hpa-sample", "minReplicas": 20}
INFO Updating resource {"kind": "HorizontalPodAutoscaler", "name": "hpa-sample", "namespace": "default", "replicas": 20}
INFO Current minReplicas {"current": 3}
INFO HPA minReplicas updated successfully {"from": 3, "to": 20}
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/my-feature
). - Commit your changes (
git commit -am 'Add my feature'
). - Push to the branch (
git push origin feature/my-feature
). - Open a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues, open a GitHub issue.