Skip to content

[shell-operator] refactor for using as a lib#898

Merged
ldmonster merged 5 commits into
mainfrom
feat/use-shell-operator-as-lib
Jun 4, 2026
Merged

[shell-operator] refactor for using as a lib#898
ldmonster merged 5 commits into
mainfrom
feat/use-shell-operator-as-lib

Conversation

@ldmonster
Copy link
Copy Markdown
Collaborator

@ldmonster ldmonster commented May 26, 2026

Summary

This PR makes shell-operator importable as a Go library while keeping the
standalone 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)

  • Merges the old 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).
  • Init is kept as a one-line compatibility shim (Deprecated godoc, 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.
  • Two new option constructors, WithMetricStorage and WithHookMetricStorage, let callers share a Prometheus registry with an outer program.

Lifecycle: Start, Run, Shutdown

  • Start(ctx) error is now non-blocking and idempotent (guarded by sync.Once); repeated calls return the cached error.
  • Shutdown(ctx) error tears 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) error is a convenience wrapper: Start → block on ctx → Shutdown. It is the recommended entry point for main().
  • DefaultShutdownTimeout = 30s replaces the old WaitQueuesTimeout = 10s package variable.
  • DebugShutdowner interface decouples operator.go from pkg/debug, keeping the debug server optional.

Goroutine hygiene

  • Metric goroutines (runLiveTicks, runQueueLengthMetric) are tracked via sync.WaitGroup and use select-on-channel instead of time.Sleep, so they exit cleanly when the operator context is canceled.
  • baseHTTPServer gains a Shutdown(ctx) error method backed by http.Server.Shutdown; doneCh lets the old ErrServerClosed log-fatal path become a structured error log.

Webhook settings: no more package-level singletons

  • admission.InitFromSettings / conversion.InitFromSettings calls are replaced with admissionSettingsFromConfig / conversionSettingsFromConfig helpers that build settings structs directly from *app.Config. This removes the package-level globals that made parallel tests flaky.

Misc cleanup

  • app.AppStartMessage global removed; callers compose their own banner from app.AppName + app.Version.
  • taskFactory field on ShellOperator replaces the package-level globalHookTaskFactory.
  • RateLimitWait now receives the task handler's ctx instead of context.Background(), so in-flight rate-limit waits are interrupted at shutdown.
  • MetricNames *metrics.Names field on ShellOperator gives library consumers a stable handle to the resolved metric names without touching package globals.

Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
@ldmonster ldmonster self-assigned this May 26, 2026
@ldmonster ldmonster added the enhancement New feature or request label May 26, 2026
ldmonster and others added 4 commits May 26, 2026 16:15
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
)

Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Co-authored-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>
@ldmonster ldmonster merged commit afa1716 into main Jun 4, 2026
9 checks passed
@ldmonster ldmonster deleted the feat/use-shell-operator-as-lib branch June 4, 2026 13:12
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.

2 participants