Skip to content

sarthak21-negi/Deployment-custom-operator

Repository files navigation

Description

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.


Features

  • 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

Tech Stack

  • Golang
  • Kubernetes
  • Kubebuilder
  • controller-runtime
  • Kind
  • Docker

Project Structure

deployment-custom-operator/
├── api/
│   └── v1alpha1/
├── cmd/
├── config/
│   ├── crd/
│   ├── rbac/
│   ├── manager/
│   └── samples/
├── internal/
│   └── controller/
├── Makefile
└── README.md

Architecture

The operator follows the Kubernetes Operator pattern:

  1. User creates a custom DeploymentCustomOperator resource
  2. Controller watches the resource
  3. Reconcile loop checks current time
  4. Controller compares desired state vs actual state
  5. Deployment replicas are updated automatically

Custom Resource Example

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

How It Works

  • 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

Prerequisites

Make sure the following are installed:

  • Go >= 1.24
  • Docker
  • kubectl
  • Kind
  • Kubebuilder

Setup Instructions

1. Clone Repository

git clone https://github.com/sarthak21-negi/deployment-custom-operator.git

cd deployment-custom-operator

2. Create Kind Cluster

kind create cluster --name dpscaler

3. Install CRD

make install

4. Run Controller

make run

5. Deploy Test Application

Create 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: 80

Apply deployment:

kubectl apply -f nginx-deployment.yaml

6. Apply Custom Resource

kubectl apply -f config/samples/dpscaler_v1alpha1_deploymentcustomoperator.yaml

Verify Scaling

Watch deployment replicas:

kubectl get deployment nginx-demo -w

Controller Logic

The controller:

  • Watches DeploymentCustomOperator resources
  • Reads scaling schedules
  • Checks current system hour
  • Fetches target Deployments
  • Updates replica counts dynamically
  • Updates CRD status fields

Status Fields

The operator updates status information:

status:
  active: true
  lastScaleTime: "2026-05-11T10:00:00Z"

Testing The Controller

Use the make test command to run tests

make test
make test

ScreenShots

Operator in action

Screenshot make run



Screenshot (16)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors