2022-11-30 11:13:57 +00:00
|
|
|
package internal
|
2022-10-03 07:55:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-18 10:55:33 +00:00
|
|
|
"sync"
|
2022-10-03 07:55:59 +00:00
|
|
|
|
2022-10-06 10:38:35 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-10-03 07:55:59 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/controllers"
|
|
|
|
)
|
|
|
|
|
2022-11-30 11:13:57 +00:00
|
|
|
type Controller interface {
|
|
|
|
Run(context.Context, logr.Logger, *sync.WaitGroup)
|
|
|
|
}
|
|
|
|
|
2022-10-03 07:55:59 +00:00
|
|
|
type controller struct {
|
2022-10-06 10:38:35 +00:00
|
|
|
name string
|
2022-10-03 07:55:59 +00:00
|
|
|
controller controllers.Controller
|
|
|
|
workers int
|
|
|
|
}
|
|
|
|
|
2022-11-30 11:13:57 +00:00
|
|
|
func NewController(name string, c controllers.Controller, w int) Controller {
|
2022-10-06 10:38:35 +00:00
|
|
|
return controller{
|
|
|
|
name: name,
|
|
|
|
controller: c,
|
|
|
|
workers: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 11:13:57 +00:00
|
|
|
func (c controller) Run(ctx context.Context, logger logr.Logger, wg *sync.WaitGroup) {
|
2022-10-18 10:55:33 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(logger logr.Logger) {
|
|
|
|
logger.Info("starting controller", "workers", c.workers)
|
|
|
|
defer logger.Info("controller stopped")
|
|
|
|
defer wg.Done()
|
|
|
|
c.controller.Run(ctx, c.workers)
|
|
|
|
}(logger.WithValues("name", c.name))
|
2022-10-03 07:55:59 +00:00
|
|
|
}
|