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.
┌─────────────────────────────────────────────────────────────────┐
│ 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 │ │
│ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
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| 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| 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) |
# 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 -wkind create cluster
make install
make build
./bin/manager &
make sample
kubectl get sb.
├── 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
The GitHub Actions workflow (.github/workflows/ci.yml) runs on every push:
go mod verify— dependency integritygo vet ./...— static analysisgo test -race ./...— unit tests with data-race detectorgo build— binary compilationdocker build— image build