[shell-operator] refactor for using as a lib#898
Merged
Conversation
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
… calls (#899) Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes
shell-operatorimportable as a Go library while keeping thestandalone binary fully functional. The two key entry points — construction and
lifecycle — are now clean, composable API surfaces rather than side-effectful
init-style calls.What changed
Unified constructor:
NewShellOperator(ctx, cfg, ...Option)Init(ctx, cfg, logger)+NewShellOperator(ctx, ms, hms, opts...)pair into a single function that owns the full assembly sequence (directories, metrics, debug server, kube clients, webhook managers, hook discovery).Initis kept as a one-line compatibility shim (Deprecatedgodoc, will be removed in a future release).NewBareShellOperator(ctx, opts...)is exposed for tests and downstream tooling that need to assemble a partial operator by hand.WithMetricStorageandWithHookMetricStorage, let callers share a Prometheus registry with an outer program.Lifecycle:
Start,Run,ShutdownStart(ctx) erroris now non-blocking and idempotent (guarded bysync.Once); repeated calls return the cached error.Shutdown(ctx) errortears down every subsystem in reverse order — schedule manager, manager events handler, kube informers, task queues, admission/conversion webhooks, debug server, API server, and tracked goroutines — respecting the provided context deadline. Also idempotent.Run(ctx) erroris a convenience wrapper:Start → block on ctx → Shutdown. It is the recommended entry point formain().DefaultShutdownTimeout = 30sreplaces the oldWaitQueuesTimeout = 10spackage variable.DebugShutdownerinterface decouplesoperator.gofrompkg/debug, keeping the debug server optional.Goroutine hygiene
runLiveTicks,runQueueLengthMetric) are tracked viasync.WaitGroupand useselect-on-channel instead oftime.Sleep, so they exit cleanly when the operator context is canceled.baseHTTPServergains aShutdown(ctx) errormethod backed byhttp.Server.Shutdown;doneChlets the oldErrServerClosedlog-fatal path become a structured error log.Webhook settings: no more package-level singletons
admission.InitFromSettings/conversion.InitFromSettingscalls are replaced withadmissionSettingsFromConfig/conversionSettingsFromConfighelpers that build settings structs directly from*app.Config. This removes the package-level globals that made parallel tests flaky.Misc cleanup
app.AppStartMessageglobal removed; callers compose their own banner fromapp.AppName+app.Version.taskFactoryfield onShellOperatorreplaces the package-levelglobalHookTaskFactory.RateLimitWaitnow receives the task handler'sctxinstead ofcontext.Background(), so in-flight rate-limit waits are interrupted at shutdown.MetricNames *metrics.Namesfield onShellOperatorgives library consumers a stable handle to the resolved metric names without touching package globals.