✨ Hooks by maxihafer · Pull Request #1 · mittwald/controller-runtime · GitHub
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pkg/manager/internal.go
22 changes: 22 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Manager interface {
// AddReadyzCheck allows you to add Readyz checker
AddReadyzCheck(name string, check healthz.Checker) error

// Hook allows to add Runnables as hooks to modify the behavior.
Hook(hook HookType, runnable Runnable) error

// Start starts all registered Controllers and blocks until the context is cancelled.
// Returns an error if there is an error starting any controller.
//
Expand Down Expand Up @@ -281,6 +284,10 @@ type Options struct {
// +optional
Controller config.Controller

// HookTimeout is the duration given to each hook to return successfully.
// To use hooks without timeout, set to a negative duration, e.g. time.Duration(-1)
HookTimeout *time.Duration

// makeBroadcaster allows deferring the creation of the broadcaster to
// avoid leaking goroutines if we never call Start on this manager. It also
// returns whether or not this is a "owned" broadcaster, and as such should be
Expand All @@ -295,6 +302,15 @@ type Options struct {
newPprofListener func(addr string) (net.Listener, error)
}

// HookType defines hooks for use with AddHook.
type HookType int

const (
// HookPrestartType defines a hook that is run after leader election and immediately before
// calling Start on the runnables that needed leader election.
HookPrestartType HookType = iota
)

// BaseContextFunc is a function used to provide a base Context to Runnables
// managed by a Manager.
type BaseContextFunc func() context.Context
Expand Down Expand Up @@ -465,6 +481,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
livenessEndpointName: options.LivenessEndpointName,
pprofListener: pprofListener,
gracefulShutdownTimeout: *options.GracefulShutdownTimeout,
hookTimeout: *options.HookTimeout,
internalProceduresStop: make(chan struct{}),
leaderElectionStopped: make(chan struct{}),
leaderElectionReleaseOnCancel: options.LeaderElectionReleaseOnCancel,
Expand Down Expand Up @@ -577,6 +594,11 @@ func setOptionsDefaults(config *rest.Config, options Options) (Options, error) {
options.GracefulShutdownTimeout = &gracefulShutdownTimeout
}

if options.HookTimeout == nil {
hookTimeout := defaultHookPeriod
options.HookTimeout = &hookTimeout
}

if options.Logger.GetSink() == nil {
options.Logger = log.Log
}
Expand Down
115 changes: 115 additions & 0 deletions pkg/manager/manager_test.go
Loading