Operator testing and least privilege
Choose a controller toolchain
An operator is a controller that captures operational knowledge for a workload, often including upgrades or recovery. A simple status reporter need not be called a full operator. client-go offers typed and dynamic clients plus informers and queues. controller-runtime assembles these into managers, caches, clients and reconcilers. Kubebuilder scaffolds Go controller projects and generates resources; Operator SDK builds on controller-runtime and supports operator workflows. Other language clients can use the API but may require different queue and lifecycle machinery. Select based on maintainability and supported versions rather than assuming these are interchangeable.
client-go: API and queue primitivescontroller-runtime: manager/reconciler abstractionsKubebuilder / Operator SDK: project and generation toolingPin dependencies before integration
The documentation candidates are client-go v0.37.2 and controller-runtime v0.25.3; compatibility of that combination, Go requirements, and Kubebuilder or Operator SDK releases have not been established here. Review upstream release dependencies, choose matching module versions and record go.mod, go.sum, generator versions and envtest binary versions before building a real controller. The provided model uses only Go standard library, tested with Go 1.23.2; no Kubernetes client modules are downloaded or linked.
Offline command (inside lab fixture):GO111MODULE=off GOPROXY=off GOSUMDB=off GOTOOLCHAIN=local CGO_ENABLED=0 go test -v .Build an evidence ladder
A pure unit test proves decision logic on its inputs. A fake client can help check object interactions but does not faithfully implement every API-server behavior, including validation or resourceVersion semantics. envtest launches an API server and etcd, enabling tests of CRD registration and actual API operations, but does not run the usual scheduler, kubelet or garbage-collection controller. A disposable cluster is needed for full controller-manager, ownership and workload behavior; explicitly check status persistence, retries, deletion, allowed and denied permissions, restart recovery and cleanup.
Pure model -> fake client -> envtest -> isolated clusterEach stage adds evidence; none substitutes for the next.Give the status reporter only its required permissions
The example Role permits reading BookRequests in book-lab and updating or patching their status subresource. A RoleBinding grants those permissions to a dedicated ServiceAccount. It has no grant to alter spec, create resources, read Secrets or manage cluster-wide CRDs. Real controllers that watch children or add finalizers need separate reviewed permissions; leader election requires separate Lease permissions. RBAC is additive, so inspect other bindings before asserting a denial. The following is an excerpt of the static Role; the full manifest is in status-reporter-rbac.yaml.
rules:- apiGroups: [books.example.com] resources: [bookrequests] verbs: [get, list, watch]- apiGroups: [books.example.com] resources: [bookrequests/status] verbs: [update, patch]Practice and next step
Write a test plan that includes a duplicate event, an optimistic conflict, a forbidden Secret read, a status write, and a restart. Identify which belongs to a pure test, fake, envtest or full cluster, and explain where real permissions must be tested. Inspect the RoleBinding and the test README; do not apply the manifest. Next: GitOps principles and Argo CD.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Operator testing and least privilege — knowledge check
Choose controller tooling, design an evidence ladder and restrict a status-only controller to least privilege.
Which test is appropriate for the pure Plan function?
Which components does normal envtest start?
Which resource should the status-only reporter be permitted to patch?
What must be checked before combining client-go and controller-runtime versions?
What evidence demonstrates actual workload reconciliation and permission enforcement?