Skip to content

[chore] reduce coupling to global vars #862

Merged
ldmonster merged 5 commits intomainfrom
chore/refactor
Apr 8, 2026
Merged

[chore] reduce coupling to global vars #862
ldmonster merged 5 commits intomainfrom
chore/refactor

Conversation

@ldmonster
Copy link
Copy Markdown
Collaborator

@ldmonster ldmonster commented Apr 7, 2026

Overview

What this PR does / why we need it

This PR implements shell-operator refactoring. None of the changes alter runtime behaviour; they are all internal decoupling improvements that reduce coupling to global app.* state, remove shared mutable globals that block test isolation, and tighten a previously-silent failure path.


1.1 — Replace app.* reads in business logic with injected configuration

Files: pkg/hook/hook_manager.go, pkg/shell-operator/kube_client.go, pkg/shell-operator/bootstrap.go, test/hook/context/generator.go

hook_manager.go — Three app.* reads (app.DebugKeepTmpFiles, app.LogProxyHookJSON, app.ProxyJsonLogKey) are replaced with fields stored on Manager (keepTemporaryHookFiles, logProxyHookJSON, logProxyHookJSONKey). The values are supplied through three new ManagerConfig fields. Bootstrap sets them from app.* at the composition root; the pkg/app import is removed from hook_manager.go.

kube_client.go — Introduces KubeClientConfig struct with explicit fields (Context, Config, QPS, Burst, Timeout). defaultMainKubeClient and defaultObjectPatcherKubeClient now accept a KubeClientConfig instead of reaching into app.* directly. The init* wrapper functions remain the only point that reads app.*, preserving clean separation between the composition root and infrastructure code. WithTimeout is now guarded with cfg.Timeout > 0 to avoid setting an unnecessary zero timeout on the main client.

test/hook/context/generator.go — Removes the pkg/app import; passes explicit zero/false values to hook.NewHook.


1.2 — Remove DefaultFactoryStore global; inject via constructor

Files: pkg/kube_events_manager/factory.go, pkg/kube_events_manager/resource_informer.go, pkg/kube_events_manager/monitor.go, pkg/kube_events_manager/kube_events_manager.go, pkg/kube_events_manager/monitor_test.go, test/hook/context/generator.go

The DefaultFactoryStore package-level variable and its init() initialiser are removed. Instead:

  • NewKubeEventsManager creates its own *FactoryStore per instance.
  • monitor receives the store via its constructor (NewMonitor) and threads it through CreateInformersForNamespaceresourceInformerConfig.
  • resourceInformer stores and uses ei.factoryStore in start() and wait() instead of the global.

Each test that creates a KubeEventsManager or a Monitor now gets an isolated FactoryStore, eliminating the shared-state race that could cause non-deterministic failures in parallel test runs. The manual DefaultFactoryStore.Reset() call in test/hook/context/generator.go is also removed as it is no longer needed.


1.3 — Merge DebugKeepTmpFilesVar (string) into DebugKeepTmpFiles (bool)

Files: pkg/app/debug.go, pkg/hook/hook.go

Two variables represented the same concept: DebugKeepTmpFilesVar string (CLI flag sink, compared via != "yes") and DebugKeepTmpFiles bool (used at hook construction time). The string variable is removed; the CLI flag is re-wired directly to BoolVar(&DebugKeepTmpFiles). The app.DebugKeepTmpFilesVar != "yes" guard in hook.Run() is replaced with the h.KeepTemporaryHookFiles bool field that was already set at construction time, allowing the pkg/app import to be dropped from pkg/hook/hook.go.


1.4 — Fix HookMetadataAccessor to return (HookMetadata, bool)

Files: pkg/hook/task_metadata/task_metadata.go, pkg/shell-operator/operator.go, pkg/shell-operator/operator_test.go, pkg/hook/task_metadata/task_metadata_test.go, pkg/task/queue/task_queue_compaction_test.go

HookMetadataAccessor previously returned a zero HookMetadata{} on both the nil-metadata and wrong-type-assertion paths, while only logging an error. Callers would continue with empty hook names and silently dispatch no-op work.

The function signature changes to (HookMetadata, bool). All three call sites in operator.go (taskHandler, taskHandleEnableKubernetesBindings, taskHandleHookRun) now explicitly short-circuit with Status: "Fail" when ok == false, preventing silent data corruption.


1.5 — Rename ManagerConfig abbreviated fields (Kmgr, Smgr, Wmgr, Cmgr)

Files: pkg/hook/hook_manager.go, pkg/shell-operator/bootstrap.go, pkg/hook/hook_manager_test.go

The four fields in ManagerConfig used non-self-documenting abbreviations. They are renamed to match the types they carry:

Before After
Kmgr KubeEventsManager
Smgr ScheduleManager
Wmgr AdmissionWebhookManager
Cmgr ConversionWebhookManager

All construction sites updated accordingly.

Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
@ldmonster ldmonster self-assigned this Apr 7, 2026
@ldmonster ldmonster added the enhancement New feature or request label Apr 7, 2026
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
@ldmonster ldmonster changed the title [chore] refactor [chore] reduce coupling to global vars Apr 8, 2026
@ldmonster ldmonster merged commit 50e0d1c into main Apr 8, 2026
9 checks passed
@ldmonster ldmonster deleted the chore/refactor branch April 8, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant