A custom Kubernetes Operator built using Golang, Kubebuilder, and controller-runtime that automatically scales Kubernetes Deployments based on configurable time schedules.
The operator watches custom resources and dynamically updates Deployment replica counts during active and inactive hours to optimize cluster resource utilization.
- Custom Kubernetes Operator built with Kubebuilder
- Time-based autoscaling for Kubernetes Deployments
- Custom Resource Definition (CRD) for scaling schedules
- Declarative scaling configuration
- Automatic scale-up and scale-down
- Reconciliation loop using controller-runtime
- Status updates for active schedules
- RBAC-enabled controller permissions
- Tested locally using Kind cluster
- Golang
- Kubernetes
- Kubebuilder
- controller-runtime
- Kind
- Docker
deployment-custom-operator/
├── api/
│ └── v1alpha1/
├── cmd/
├── config/
│ ├── crd/
│ ├── rbac/
│ ├── manager/
│ └── samples/
├── internal/
│ └── controller/
├── Makefile
└── README.mdThe operator follows the Kubernetes Operator pattern:
- User creates a custom DeploymentCustomOperator resource
- Controller watches the resource
- Reconcile loop checks current time
- Controller compares desired state vs actual state
- Deployment replicas are updated automatically
apiVersion: dpscaler.sarthak.dev/v1alpha1
kind: DeploymentCustomOperator
metadata:
name: deploymentcustomoperator-sample
namespace: default
spec:
targets:
- name: nginx-demo
namespace: default
replicas: 5
schedule:
startHour: 9
endHour: 18
defaultReplicas: 1- During active hours (9 AM – 6 PM), the Deployment scales to 5 replicas
- Outside active hours, the Deployment scales back to 1 replica
- The controller continuously monitors the cluster state and reconciles resources every minute
Make sure the following are installed:
- Go >= 1.24
- Docker
- kubectl
- Kind
- Kubebuilder
git clone https://github.com/sarthak21-negi/deployment-custom-operator.git
cd deployment-custom-operatorkind create cluster --name dpscalermake installmake runCreate deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-demo
spec:
replicas: 1
selector:
matchLabels:
app: nginx-demo
template:
metadata:
labels:
app: nginx-demo
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80Apply deployment:
kubectl apply -f nginx-deployment.yamlkubectl apply -f config/samples/dpscaler_v1alpha1_deploymentcustomoperator.yamlWatch deployment replicas:
kubectl get deployment nginx-demo -wThe controller:
- Watches DeploymentCustomOperator resources
- Reads scaling schedules
- Checks current system hour
- Fetches target Deployments
- Updates replica counts dynamically
- Updates CRD status fields
The operator updates status information:
status:
active: true
lastScaleTime: "2026-05-11T10:00:00Z"Use the make test command to run tests
make test
Operator in action