Skip to content

perf: introduce DAG-based Initializer system, eliminate ServerFactory, and remove ViewerMode - #890

Merged
kyasbal merged 9 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-ytskynztpnlm
Aug 19, 2026
Merged

kyasbal merged 9 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-ytskynztpnlm

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 19, 2026

Copy link
Copy Markdown
Member

Summary

This PR refactors KHI's backend initialization and server lifecycle into a modular, DAG-based Initializer system, eliminates ServerFactory/ServerOption in favor of direct route registration, and completely removes ViewerMode across the backend and frontend.


Key Changes

1. Modular DAG-based Initializer & Lifecycle System (pkg/core/init)

  • coreinit.Engine & coreinit.InitContext:
    • Implemented a dependency injection context with type-safe ContextKey[T].
    • Added topological sort dependency resolution for coreinit.Initializer structs.
    • Added structured lifecycle hooks (OnRun for starting server/job runners and OnTerminate for graceful teardown).
  • Modularized Default Initializers (pkg/core/init/default):
    • Split the monolithic defaultextension.go into dedicated single-responsibility initializers:
      • ParametersInitializer: CLI flag binding and parsing.
      • LoggerInitializer: Slog initialization.
      • KubernetesClientInitializer: In-cluster / out-of-cluster k8s client setup.
      • InspectionServerInitializer: Task server and progress manager setup.
      • GinServerInitializer: Gin engine and middleware setup.
      • ServerInitializer: KHI HTTP routes registration.
      • ServerRunnerInitializer: HTTP server listener and graceful shutdown handling.
      • JobRunnerInitializer: Job mode execution handling.
      • OAuthInitializer, DebugInitializer: Auth callback routes and pprof debug endpoints.
  • Entrypoint Cleanup (cmd/kubernetes-history-inspector/main.go):
    • Simplified main() from ~250 lines of procedural setup to clean Engine.Init(), Engine.Run(), and Engine.Wait() lifecycle management with OS signal listening.

2. Elimination of ServerFactory and ServerOptions (pkg/server)

  • Replaced ServerFactory and functional options (pkg/server/option) with a direct route registration helper server.SetupKHIServerRoutes(engine, ...) integrated into ServerInitializer.

3. Complete Elimination of ViewerMode

  • Backend (pkg/):
    • Removed --viewer-mode CLI parameter from pkg/parameters.
    • Removed pkg/server/config package and /api/v3/config endpoint.
  • Frontend (web/):
    • Removed GetConfigResponse schema and BackendAPI.getConfig() method.
    • Removed Viewer Mode badge from HeaderComponent.
    • Removed viewer mode checks in StartupDialogComponent and InspectionListComponent (enabling "New Inspection" unconditionally).
    • Cleaned up related unit tests, mocks, and Storybook stories.

Verification

  • Go:
    • make test-go: All tests passed across all packages.
    • make lint-go: 0 issues reported by golangci-lint.
    • make build-go: Successfully built binary.
  • Web:
    • make test-web: All 728 unit tests passed in Karma ChromeHeadless.
    • make lint-web: 0 lint errors reported by Angular ESLint & Stylelint.
    • make build-storybook: Storybook build completed successfully.
    • make build-web: Frontend production build succeeded.
    • make pre-commit: Formatter and linters verified.
@kyasbal kyasbal added type:feature New feature or request area:backend-core Core backend frameworks, DAG task runner, and server API labels Aug 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the initialization and lifecycle management of the KHI application by introducing a DAG-based initialization engine (coreinit.Engine and Initializer) to replace the legacy InitExtension design. Additionally, it removes the ViewerMode configuration and its associated backend endpoints and frontend elements. The review feedback suggests making the Engine termination process thread-safe and idempotent using sync.Once, and adding os.O_TRUNC when opening the export destination file in job mode to prevent file corruption.

Comment thread pkg/core/init/engine.go Resolved
Comment thread pkg/core/init/engine.go Resolved
Comment thread pkg/core/init/default/job_runner.go Resolved
@kyasbal

kyasbal commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the KHI application's initialization and lifecycle management by replacing the legacy InitExtension mechanism with a new DAG-based Initializer and Engine system. It modularizes various setup steps (such as logging, parameter parsing, OAuth, and server execution) into discrete initializers, simplifies the main entry point, and removes the 'Viewer Mode' configuration across both the backend and frontend. The review feedback highlights two key areas for improvement: first, a high-severity race condition in main.go during signal-initiated termination, where the application may exit prematurely with status 0 instead of waiting for the graceful shutdown to complete and returning the correct signal-based exit code; second, a limitation in the Engine where OnRun hooks are executed sequentially, which could block subsequent hooks if any of them are blocking.

Comment thread pkg/core/init/engine.go Resolved
@kyasbal

kyasbal commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the initialization and lifecycle management of the KHI application by replacing the legacy InitExtension mechanism with a new DAG-based initialization engine (Engine, InitContext, Initializer). It also completely removes the 'Viewer Mode' feature from both the backend parameters/routes and the frontend Angular application. The review feedback highlights three key areas for improvement: executing OnRun hooks concurrently to prevent blocking other services, configuring timeouts on the http.Server to avoid resource exhaustion, and calling file.Sync() during job exports to ensure data integrity.

Comment thread pkg/core/init/engine.go Resolved
Comment thread pkg/core/init/default/server_runner.go Resolved
Comment thread pkg/core/init/default/job_runner.go Resolved
@kyasbal
kyasbal marked this pull request as ready for review August 19, 2026 08:27
@kyasbal
kyasbal merged commit 1374638 into GoogleCloudPlatform:main Aug 19, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-ytskynztpnlm branch August 19, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend-core Core backend frameworks, DAG task runner, and server API type:feature New feature or request

1 participant