refactor: split controller.go into focused files by concern#69
refactor: split controller.go into focused files by concern#69
Conversation
Split the 682-line controller.go into four files organized by responsibility: - controller.go (~160 lines): types, interfaces, Controller struct, New(), Run() - handlers.go (~170 lines): event handler registration, worker loop - reconcile.go (~270 lines): event processing, deployment recording, caching - pod.go (~130 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. No behavioral changes — pure structural refactoring. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
…tone support (#70) * Initial plan * fix: use DeletionHandlingMetaNamespaceKeyFunc in DeleteFunc for tombstone support Co-authored-by: bdehamer <398027+bdehamer@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/deployment-tracker/sessions/620a8570-c815-4b5f-95cd-25d107fcd82a --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bdehamer <398027+bdehamer@users.noreply.github.com>
| return | ||
| } | ||
|
|
||
| key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) |
There was a problem hiding this comment.
This was a nice bug fix picked-up by Copilot during this refactor. Even if we decide not to merge this PR, we should apply this change (using DeletionHandlingMetaNamespaceKeyFunc in place of MetaNamespaceKeyFunc)
|
Heads up, I'm probably about to merge this PR: #72 It adds/updates several functions/methods in |
Summary
Splits the 682-line
internal/controller/controller.gointo four focused files organized by responsibility. No behavioral changes — pure structural refactoring.New file layout
controller.goControllerstruct,New(),newAPIClient(),Run()handlers.goregisterEventHandlers(),runWorker(),processNextItem(),startWorkers()reconcile.goprocessEvent(),recordContainer(),deploymentExists(),getCacheKey()pod.gocreateInformerFactory(),getARDeploymentName(),getContainerDigest(),getDeploymentName(),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.Testing
All existing unit and integration tests pass without modification. No test changes needed since all files remain in the same package.