refactor: split controller.go into focused files by concern#69
refactor: split controller.go into focused files by concern#69
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the Kubernetes deployment-tracker controller by splitting the previous monolithic internal/controller/controller.go into smaller files organized by responsibility, aiming to keep behavior unchanged while improving readability and maintainability.
Changes:
- Extracted event handler registration and worker lifecycle logic into
handlers.go. - Extracted reconciliation / posting logic into
reconcile.go. - Extracted pod/informer helper utilities into
pod.goand simplifiedcontroller.goto core wiring (New,Run, API client construction).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/controller/controller.go | Slimmed down to core types + constructor + API client creation + Run(), delegating handlers/workers elsewhere. |
| internal/controller/handlers.go | New file containing informer event handlers and queue worker processing logic. |
| internal/controller/reconcile.go | New file containing processEvent/recordContainer and caching logic for posts. |
| internal/controller/pod.go | New file containing informer factory creation + pod/container helper functions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Heads up, I'm probably about to merge this PR: #72 It adds/updates several functions/methods in |
Split the 995-line controller.go into four files organized by responsibility: - controller.go (~195 lines): types, interfaces, Controller struct, New(), Run() - handlers.go (~205 lines): event handler registration, worker loop - reconcile.go (~450 lines): event processing, deployment recording, workload resolution, cache existence checks - pod.go (~195 lines): pod utility functions, informer factory Extract registerEventHandlers() from New() as a Controller method and newAPIClient() as a package-level helper to simplify the constructor. Fix a pre-existing bug in DeleteFunc where MetaNamespaceKeyFunc(obj) was called on the original obj which may be a DeletedFinalStateUnknown tombstone (causing silent event drops). Now uses the extracted pod instead. No other behavioral changes — pure structural refactoring. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
8130fd7 to
b94bde3
Compare
piceri
left a comment
There was a problem hiding this comment.
After the changes to support different "workloads", I wonder if having a workload.go file would make sense with the following:
controller.go
New()Run()newAPIClient()createInformerFactory()
handlers.go
registerEventHandlers()runWorker()startWorkers()processNextItem()isTerminalPhase()
workload.go
getWorkloadRef()resolveJobWorkload()workloadActive()deploymentExists()jobExists()daemonSetExists()statefulSetExists()cronJobExists()getDeploymentName()getJobOwnerName()getDaemonSetName()getStatefulSetName()
event.go
processEvent()recordContainer()getCacheKey()
pod.go
isNumeric()podToPartialMetadata()getARDeploymentName()getContainerDigest()hasSupportedOwner()
Also wondering about event.go instead of reconcile.go. Very open to push back/other opinions
Summary
Splits the 995-line
internal/controller/controller.gointo four focused files organized by responsibility. Includes one bug fix; otherwise pure structural refactoring.New file layout
controller.goControllerstruct,New(),newAPIClient(),Run()handlers.goregisterEventHandlers(),runWorker(),processNextItem(),startWorkers()reconcile.goprocessEvent(),recordContainer(),getWorkloadRef(),resolveJobWorkload(),workloadActive(), workload existence checks,getCacheKey()pod.gocreateInformerFactory(),getARDeploymentName(),getContainerDigest(),getDeploymentName(),hasSupportedOwner(),isTerminalPhase(),getJobOwnerName(),getDaemonSetName(),getStatefulSetName(),isNumeric(),podToPartialMetadata()config.goKey changes
registerEventHandlers()extracted fromNew()as aControllermethod — the event handler closures now usec.workqueueinstead of closing over a localqueuevariable.newAPIClient()extracted as a package-level helper to isolate API client construction from the controller constructor.startWorkers()extracted fromRun()for clarity.Bug fix
Fixed a pre-existing bug in
DeleteFuncwherecache.MetaNamespaceKeyFunc(obj)was called on the originalobj, which may be aDeletedFinalStateUnknowntombstone.MetaNamespaceKeyFunccannot extract the key from a tombstone, so delete events were silently dropped in that case. Now derives the key from the extractedpodinstead.Testing
All existing unit and integration tests pass without modification. No test changes needed since all files remain in the same package.