2023-05-09 14:25:55 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-09 14:25:55 +00:00
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-09 14:25:55 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2023-04-25 11:52:35 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2023-05-09 14:25:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/consul/controller/queue"
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-04-25 11:52:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// controllerRunner contains the actual implementation of running a controller
|
|
|
|
// including creating watches, calling the reconciler, handling retries, etc.
|
|
|
|
type controllerRunner struct {
|
|
|
|
ctrl Controller
|
2023-05-09 14:25:55 +00:00
|
|
|
client pbresource.ResourceServiceClient
|
2023-04-25 11:52:35 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) run(ctx context.Context) error {
|
|
|
|
c.logger.Debug("controller running")
|
|
|
|
defer c.logger.Debug("controller stopping")
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
group, groupCtx := errgroup.WithContext(ctx)
|
|
|
|
recQueue := runQueue[Request](groupCtx, c.ctrl)
|
|
|
|
|
|
|
|
// Managed Type Events → Reconciliation Queue
|
|
|
|
group.Go(func() error {
|
|
|
|
return c.watch(groupCtx, c.ctrl.managedType, func(res *pbresource.Resource) {
|
|
|
|
recQueue.Add(Request{ID: res.Id})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
for _, w := range c.ctrl.watches {
|
2023-05-09 17:14:20 +00:00
|
|
|
mapQueue := runQueue[mapperRequest](groupCtx, c.ctrl)
|
2023-08-17 20:34:18 +00:00
|
|
|
watcher := w
|
2023-05-09 14:25:55 +00:00
|
|
|
// Watched Type Events → Mapper Queue
|
|
|
|
group.Go(func() error {
|
2023-08-17 20:34:18 +00:00
|
|
|
return c.watch(groupCtx, watcher.watchedType, func(res *pbresource.Resource) {
|
2023-05-09 17:14:20 +00:00
|
|
|
mapQueue.Add(mapperRequest{res: res})
|
|
|
|
})
|
2023-05-09 14:25:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Mapper Queue → Mapper → Reconciliation Queue
|
|
|
|
group.Go(func() error {
|
2023-08-17 20:34:18 +00:00
|
|
|
return c.runMapper(groupCtx, watcher, mapQueue, recQueue, func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error) {
|
|
|
|
return watcher.mapper(ctx, runtime, itemType.(mapperRequest).res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cw := range c.ctrl.customWatches {
|
|
|
|
customMapQueue := runQueue[Event](groupCtx, c.ctrl)
|
|
|
|
watcher := cw
|
|
|
|
// Custom Events → Mapper Queue
|
|
|
|
group.Go(func() error {
|
|
|
|
return watcher.source.Watch(groupCtx, func(e Event) {
|
|
|
|
customMapQueue.Add(e)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Mapper Queue → Mapper → Reconciliation Queue
|
|
|
|
group.Go(func() error {
|
|
|
|
return c.runCustomMapper(groupCtx, watcher, customMapQueue, recQueue, func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error) {
|
|
|
|
return watcher.mapper(ctx, runtime, itemType.(Event))
|
|
|
|
})
|
2023-05-09 14:25:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconciliation Queue → Reconciler
|
|
|
|
group.Go(func() error {
|
|
|
|
return c.runReconciler(groupCtx, recQueue)
|
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runQueue[T queue.ItemType](ctx context.Context, ctrl Controller) queue.WorkQueue[T] {
|
|
|
|
base, max := ctrl.backoff()
|
|
|
|
return queue.RunWorkQueue[T](ctx, base, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) watch(ctx context.Context, typ *pbresource.Type, add func(*pbresource.Resource)) error {
|
2023-08-17 20:34:18 +00:00
|
|
|
wl, err := c.client.WatchList(ctx, &pbresource.WatchListRequest{
|
2023-05-09 14:25:55 +00:00
|
|
|
Type: typ,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: storage.Wildcard,
|
|
|
|
PeerName: storage.Wildcard,
|
|
|
|
Namespace: storage.Wildcard,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("failed to create watch", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2023-08-17 20:34:18 +00:00
|
|
|
event, err := wl.Recv()
|
2023-05-09 14:25:55 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn("error received from watch", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
add(event.Resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) runMapper(
|
|
|
|
ctx context.Context,
|
|
|
|
w watch,
|
2023-05-09 17:14:20 +00:00
|
|
|
from queue.WorkQueue[mapperRequest],
|
2023-05-09 14:25:55 +00:00
|
|
|
to queue.WorkQueue[Request],
|
2023-08-17 20:34:18 +00:00
|
|
|
mapper func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error),
|
2023-05-09 14:25:55 +00:00
|
|
|
) error {
|
|
|
|
logger := c.logger.With("watched_resource_type", resource.ToGVK(w.watchedType))
|
|
|
|
|
|
|
|
for {
|
2023-05-09 17:14:20 +00:00
|
|
|
item, shutdown := from.Get()
|
2023-05-09 14:25:55 +00:00
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
if err := c.doMap(ctx, mapper, to, item, logger); err != nil {
|
2023-05-09 17:14:20 +00:00
|
|
|
from.AddRateLimited(item)
|
|
|
|
from.Done(item)
|
2023-05-09 14:25:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
from.Forget(item)
|
|
|
|
from.Done(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) runCustomMapper(
|
|
|
|
ctx context.Context,
|
|
|
|
cw customWatch,
|
|
|
|
from queue.WorkQueue[Event],
|
|
|
|
to queue.WorkQueue[Request],
|
|
|
|
mapper func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error),
|
|
|
|
) error {
|
|
|
|
logger := c.logger.With("watched_event", cw.source)
|
|
|
|
|
|
|
|
for {
|
|
|
|
item, shutdown := from.Get()
|
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.doMap(ctx, mapper, to, item, logger); err != nil {
|
|
|
|
from.AddRateLimited(item)
|
|
|
|
from.Done(item)
|
|
|
|
continue
|
2023-05-09 14:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:14:20 +00:00
|
|
|
from.Forget(item)
|
|
|
|
from.Done(item)
|
2023-05-09 14:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 20:34:18 +00:00
|
|
|
func (c *controllerRunner) doMap(ctx context.Context, mapper func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error), to queue.WorkQueue[Request], item queue.ItemType, logger hclog.Logger) error {
|
|
|
|
var reqs []Request
|
|
|
|
if err := c.handlePanic(func() error {
|
|
|
|
var err error
|
|
|
|
reqs, err = mapper(ctx, c.runtime(), item)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range reqs {
|
|
|
|
if !resource.EqualType(r.ID.Type, c.ctrl.managedType) {
|
|
|
|
logger.Error("dependency mapper returned request for a resource of the wrong type",
|
|
|
|
"type_expected", resource.ToGVK(c.ctrl.managedType),
|
|
|
|
"type_got", resource.ToGVK(r.ID.Type),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
to.Add(r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:25:55 +00:00
|
|
|
func (c *controllerRunner) runReconciler(ctx context.Context, queue queue.WorkQueue[Request]) error {
|
|
|
|
for {
|
|
|
|
req, shutdown := queue.Get()
|
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Trace("handling request", "request", req)
|
|
|
|
err := c.handlePanic(func() error {
|
|
|
|
return c.ctrl.reconciler.Reconcile(ctx, c.runtime(), req)
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
queue.Forget(req)
|
|
|
|
} else {
|
|
|
|
var requeueAfter RequeueAfterError
|
|
|
|
if errors.As(err, &requeueAfter) {
|
|
|
|
queue.Forget(req)
|
|
|
|
queue.AddAfter(req, time.Duration(requeueAfter))
|
|
|
|
} else {
|
|
|
|
queue.AddRateLimited(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
queue.Done(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) handlePanic(fn func() error) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
stack := hclog.Stacktrace()
|
|
|
|
c.logger.Error("controller panic",
|
|
|
|
"panic", r,
|
|
|
|
"stack", stack,
|
|
|
|
)
|
|
|
|
err = fmt.Errorf("panic [recovered]: %v", r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controllerRunner) runtime() Runtime {
|
|
|
|
return Runtime{
|
|
|
|
Client: c.client,
|
|
|
|
Logger: c.logger,
|
|
|
|
}
|
2023-04-25 11:52:35 +00:00
|
|
|
}
|
2023-05-09 17:14:20 +00:00
|
|
|
|
|
|
|
type mapperRequest struct{ res *pbresource.Resource }
|
|
|
|
|
|
|
|
// Key satisfies the queue.ItemType interface. It returns a string which will be
|
|
|
|
// used to de-duplicate requests in the queue.
|
|
|
|
func (i mapperRequest) Key() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"type=%q,part=%q,peer=%q,ns=%q,name=%q,uid=%q",
|
|
|
|
resource.ToGVK(i.res.Id.Type),
|
|
|
|
i.res.Id.Tenancy.Partition,
|
|
|
|
i.res.Id.Tenancy.PeerName,
|
|
|
|
i.res.Id.Tenancy.Namespace,
|
|
|
|
i.res.Id.Name,
|
|
|
|
i.res.Id.Uid,
|
|
|
|
)
|
|
|
|
}
|