2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-28 15:59:09 +00:00
|
|
|
|
|
|
|
package controllerlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-11-10 15:22:16 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-08-28 15:59:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Manager interface {
|
|
|
|
Start(ctx context.Context)
|
|
|
|
WithController(controller Controller, workers int) Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() Manager {
|
|
|
|
return &controllerManager{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runnableController represents single controller runnable configuration.
|
|
|
|
type runnableController struct {
|
|
|
|
controller Controller
|
|
|
|
workers int
|
|
|
|
}
|
|
|
|
|
|
|
|
type controllerManager struct {
|
|
|
|
controllers []runnableController
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Manager = &controllerManager{}
|
|
|
|
|
|
|
|
func (c *controllerManager) WithController(controller Controller, workers int) Manager {
|
|
|
|
c.controllers = append(c.controllers, runnableController{
|
|
|
|
controller: controller,
|
|
|
|
workers: workers,
|
|
|
|
})
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will run all managed controllers and block until all controllers shutdown.
|
|
|
|
// When the context passed is cancelled, all controllers are signalled to shutdown.
|
|
|
|
func (c *controllerManager) Start(ctx context.Context) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(c.controllers))
|
|
|
|
for i := range c.controllers {
|
|
|
|
idx := i
|
|
|
|
go func() {
|
|
|
|
r := c.controllers[idx]
|
2020-11-10 15:22:16 +00:00
|
|
|
defer plog.Debug("controller terminated", "controller", r.controller.Name())
|
2020-08-28 15:59:09 +00:00
|
|
|
defer wg.Done()
|
|
|
|
r.controller.Run(ctx, r.workers)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|