Skip to content

hive: Support supplying a custom Lifecycle via Options - #81

Merged
joamaki merged 1 commit into
cilium:mainfrom
KR-Ravindra:feature/custom-lifecycle-option
Sep 22, 2026
Merged

joamaki merged 1 commit into
cilium:mainfrom
KR-Ravindra:feature/custom-lifecycle-option

Conversation

@KR-Ravindra

@KR-Ravindra KR-Ravindra commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

hive.NewWithOptions always constructs a cell.DefaultLifecycle, so an application embedding hive cannot supply its own cell.Lifecycle (nested lifecycles, hook instrumentation, test harnesses) even though cell.Lifecycle is an exported interface and DefaultLifecycle is exported for that purpose.

Root cause

hive.go:135 (NewWithOptions) hard-codes lifecycle: &cell.DefaultLifecycle{LogThreshold: opts.LogThreshold} and Options has no way to override it.

Fix

Add an optional Lifecycle cell.Lifecycle field to hive.Options, as proposed in the issue. When set, the hive provides it to the cells and drives Start/Stop through it; when nil, a DefaultLifecycle with the configured LogThreshold is created as before, so existing callers are unaffected. cell.Module already passes non-DefaultLifecycle implementations through unchanged.

How tested

Added TestCustomLifecycle (a wrapper embedding *cell.DefaultLifecycle that counts Append/Start/Stop, passed via opts.Lifecycle, asserting it is the lifecycle injected into cells and that each counter is 1 after h.Start/h.Stop) and TestDefaultLifecycle (nil opts.Lifecycle still yields a *cell.DefaultLifecycle carrying opts.LogThreshold).

Before the fix:

./hive_test.go:729:7: opts.Lifecycle undefined (type hive.Options has no field or method Lifecycle)
FAIL	github.com/cilium/hive [build failed]

After the fix:

--- PASS: TestCustomLifecycle (0.00s)
--- PASS: TestDefaultLifecycle (0.00s)
ok  	github.com/cilium/hive	0.016s

go build ./..., go vet ./..., gofmt -l . and go test ./... (plus -race on the root package) pass.

Links

This change was prepared with an AI agent operated by KR-Ravindra, who reviewed and tested it.

NewWithOptions always constructs a cell.DefaultLifecycle, so an
application embedding hive cannot swap in its own implementation even
though cell.Lifecycle is an exported interface and DefaultLifecycle is
exported precisely for applications with nested lifecycles. Wrapping the
lifecycle is useful to run a hive inside an outer lifecycle, to
instrument hooks or to build test harnesses around a hive.

Add an optional Lifecycle field to Options. When set, the hive provides
it to the cells and drives Start and Stop through it. When nil, a
DefaultLifecycle is created with the configured LogThreshold as before,
so existing users are unaffected.

Fixes: cilium#39
Signed-off-by: KR Ravindra <42912207+KR-Ravindra@users.noreply.github.com>
@KR-Ravindra
KR-Ravindra force-pushed the feature/custom-lifecycle-option branch from 3bb0a22 to 130ca59 Compare September 13, 2026 06:17
@KR-Ravindra
KR-Ravindra marked this pull request as ready for review September 19, 2026 23:06
@KR-Ravindra
KR-Ravindra requested a review from a team as a code owner September 19, 2026 23:06
@KR-Ravindra
KR-Ravindra requested review from derailed and removed request for a team September 19, 2026 23:06
@joamaki
joamaki merged commit ef2e6d5 into cilium:main Sep 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support custom lifecycle implementation

3 participants