Skip to content

About

Custom Kubernetes operator (Go + kubebuilder) managing a simple ScheduledBackup CRD.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

kubernetes-operator-example

CI Go Version License: MIT

Custom Kubernetes operator (Go + kubebuilder pattern) managing a ScheduledBackup CRD — a namespaced resource that drives periodic backup Jobs against any target, storing artifacts in S3, GCS, or a local path.


Architecture

┌─────────────────────────────────────────────────────────────────┐
│  Kubernetes Cluster                                              │
│                                                                  │
│  ┌──────────────────────┐        ┌──────────────────────────┐   │
│  │  ScheduledBackup CR  │        │  scheduled-backup-       │   │
│  │  (backup.example.com)│◄──────►│  operator (Deployment)  │   │
│  │                      │  watch │                          │   │
│  │  spec:               │        │  Reconciler loop:        │   │
│  │    schedule: "0 2 * *│        │  1. Parse cron schedule  │   │
│  │    storageBackend: s3│        │  2. Compare now vs next  │   │
│  │    storagePath: s3://│        │  3. Create batch/v1 Job  │   │
│  │    retentionDays: 30 │        │  4. Update .status       │   │
│  │                      │        │  5. Requeue at nextRun   │   │
│  └──────────────────────┘        └──────────┬───────────────┘   │
│                                             │ creates            │
│                                    ┌────────▼─────────┐         │
│                                    │  batch/v1 Job    │         │
│                                    │  (backup-<ts>)   │         │
│                                    │                  │         │
│                                    │  Container runs: │         │
│                                    │  backup command  │         │
│                                    └──────────────────┘         │
└─────────────────────────────────────────────────────────────────┘

CRD — ScheduledBackup

apiVersion: backup.example.com/v1alpha1
kind: ScheduledBackup
metadata:
  name: postgres-daily
  namespace: default
spec:
  schedule: "0 2 * * *"       # cron or shorthand (@daily, @hourly, @every 6h)
  retentionDays: 30            # how long to keep artifacts
  storageBackend: s3           # s3 | gcs | local
  storagePath: s3://my-bucket/postgres/daily
  image: alpine:3.18           # optional; defaults to alpine:3.18
  targetNamespace: ""          # optional; defaults to operator namespace

Status fields

Field Description
lastBackupTime Timestamp of most recent backup
nextScheduledTime When the next backup will run
lastBackupJobName Name of the most recently created Job
conditions[].type=Ready True = healthy, False = invalid schedule
kubectl get sb                         # list all ScheduledBackups (short name)
kubectl describe sb postgres-daily     # full status + events
kubectl get jobs -l backup.example.com/owner=postgres-daily

Tech Stack

Component Choice
Language Go 1.22
Operator framework controller-runtime v0.17
CRD generation kubebuilder markers (manually maintained CRD YAML in config/)
Cron parsing robfig/cron v3
Tests go test with controller-runtime fake client
CI GitHub Actions
Image gcr.io/distroless/static:nonroot (scratch-like, non-root)

Local Development

# Prerequisites: Go 1.22, kubectl, a running cluster (kind/k3d/minikube)

# Run tests (no cluster needed)
make test

# Build binary
make build

# Install CRD into cluster
make install

# Run operator locally (out-of-cluster, uses KUBECONFIG)
./bin/manager

# Apply a sample ScheduledBackup
make sample

# Watch status
kubectl get sb -w
kubectl get jobs -w

Using kind (local cluster)

kind create cluster
make install
make build
./bin/manager &
make sample
kubectl get sb

Project Layout

.
├── api/v1alpha1/
│   ├── groupversion_info.go          # Group/Version/SchemeBuilder
│   ├── scheduledbackup_types.go      # CRD struct definitions + kubebuilder markers
│   └── zz_generated.deepcopy.go     # DeepCopy implementations
├── controllers/
│   ├── scheduledbackup_controller.go # Reconciler: cron → Job creation → status
│   └── scheduledbackup_controller_test.go
├── config/
│   ├── crd/bases/                    # CRD YAML
│   ├── manager/                      # Deployment YAML
│   ├── rbac/                         # ClusterRole + binding
│   └── samples/                      # Example CRs
├── main.go                           # Manager setup, scheme registration
├── Dockerfile                        # Multi-stage distroless image
└── Makefile

CI

The GitHub Actions workflow (.github/workflows/ci.yml) runs on every push:

  1. go mod verify — dependency integrity
  2. go vet ./... — static analysis
  3. go test -race ./... — unit tests with data-race detector
  4. go build — binary compilation
  5. docker build — image build

License

MIT

About

Custom Kubernetes operator (Go + kubebuilder) managing a simple ScheduledBackup CRD.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages