2023-12-13 15:06:39 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-02-13 16:51:54 +00:00
|
|
|
"math/rand"
|
2023-12-13 15:06:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
2024-02-06 16:23:06 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-12-13 15:06:39 +00:00
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/controller/queue"
|
|
|
|
"github.com/hashicorp/consul/internal/controller/cache"
|
2024-01-12 16:54:07 +00:00
|
|
|
"github.com/hashicorp/consul/internal/protoutil"
|
2023-12-13 15:06:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runtime contains the dependencies required by reconcilers.
|
|
|
|
type Runtime struct {
|
|
|
|
Client pbresource.ResourceServiceClient
|
|
|
|
Logger hclog.Logger
|
|
|
|
Cache cache.ReadOnlyCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// controllerRunner contains the actual implementation of running a controller
|
|
|
|
// including creating watches, calling the reconciler, handling retries, etc.
|
|
|
|
type controllerRunner struct {
|
2024-01-12 16:54:07 +00:00
|
|
|
ctrl *Controller
|
|
|
|
// watchClient will be used by the controller infrastructure internally to
|
|
|
|
// perform watches and maintain the cache. On servers, this client will use
|
|
|
|
// the in-memory gRPC clients which DO NOT cause cloning of data returned by
|
|
|
|
// the resource service. This is desirable so that our cache doesn't incur
|
|
|
|
// the overhead of duplicating all resources that are watched. Generally
|
|
|
|
// dependency mappers and reconcilers should not be given this client so
|
|
|
|
// that they can be free to modify the data they are returned.
|
|
|
|
watchClient pbresource.ResourceServiceClient
|
|
|
|
// runtimeClient will be used by dependency mappers and reconcilers to
|
|
|
|
// access the resource service. On servers, this client will use the in-memory
|
|
|
|
// gRPC client wrapped with the cloning client to force cloning of protobuf
|
|
|
|
// messages as they pass through the client. This is desirable so that
|
|
|
|
// controllers and their mappers can be free to modify the data returned
|
|
|
|
// to them without having to think about the fact that the data should
|
|
|
|
// be immutable as it is shared with the controllers cache as well as the
|
|
|
|
// resource service itself.
|
|
|
|
runtimeClient pbresource.ResourceServiceClient
|
|
|
|
logger hclog.Logger
|
|
|
|
cache cache.Cache
|
2023-12-13 15:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newControllerRunner(c *Controller, client pbresource.ResourceServiceClient, defaultLogger hclog.Logger) *controllerRunner {
|
|
|
|
return &controllerRunner{
|
2024-01-12 16:54:07 +00:00
|
|
|
ctrl: c,
|
|
|
|
watchClient: client,
|
|
|
|
runtimeClient: pbresource.NewCloningResourceServiceClient(client),
|
|
|
|
logger: c.buildLogger(defaultLogger),
|
2023-12-13 15:06:39 +00:00
|
|
|
// Do not build the cache here. If we build/set it when the controller runs
|
|
|
|
// then if a controller is restarted it will invalidate the previous cache automatically.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) run(ctx context.Context) error {
|
|
|
|
cr.logger.Debug("controller running")
|
|
|
|
defer cr.logger.Debug("controller stopping")
|
2023-12-13 15:06:39 +00:00
|
|
|
|
2024-01-19 17:47:48 +00:00
|
|
|
// Initialize the controller if required
|
2024-02-13 16:51:54 +00:00
|
|
|
if cr.ctrl.initializer != nil {
|
|
|
|
cr.logger.Debug("controller initializing")
|
|
|
|
err := cr.ctrl.initializer.Initialize(ctx, cr.runtime(cr.logger))
|
2024-01-19 17:47:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Debug("controller initialized")
|
2024-01-19 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache = cr.ctrl.buildCache()
|
2024-01-12 16:54:07 +00:00
|
|
|
defer func() {
|
|
|
|
// once no longer running we should nil out the cache
|
|
|
|
// so that we don't hold pointers to resources which may
|
|
|
|
// become out of date in the future.
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache = nil
|
2024-01-12 16:54:07 +00:00
|
|
|
}()
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
if cr.ctrl.startCb != nil {
|
|
|
|
cr.ctrl.startCb(ctx, cr.runtime(cr.logger))
|
2024-01-12 16:54:07 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
if cr.ctrl.stopCb != nil {
|
|
|
|
defer cr.ctrl.stopCb(ctx, cr.runtime(cr.logger))
|
2024-01-12 16:54:07 +00:00
|
|
|
}
|
2023-12-13 15:06:39 +00:00
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
// Before we launch the reconciler or the dependency mappers, ensure the
|
|
|
|
// cache is properly primed to avoid errant reconciles.
|
|
|
|
//
|
|
|
|
// Without doing this the cache is unsafe for general use without causing
|
|
|
|
// reconcile regressions in certain cases.
|
|
|
|
{
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Debug("priming caches")
|
2024-02-02 21:11:05 +00:00
|
|
|
primeGroup, primeGroupCtx := errgroup.WithContext(ctx)
|
|
|
|
// Managed Type Events
|
|
|
|
primeGroup.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.primeCache(primeGroupCtx, cr.ctrl.managedTypeWatch.watchedType)
|
2024-02-02 21:11:05 +00:00
|
|
|
})
|
2024-02-13 16:51:54 +00:00
|
|
|
for _, w := range cr.ctrl.watches {
|
2024-02-02 21:11:05 +00:00
|
|
|
watcher := w
|
|
|
|
// Watched Type Events
|
|
|
|
primeGroup.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.primeCache(primeGroupCtx, watcher.watchedType)
|
2024-02-02 21:11:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := primeGroup.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Debug("priming caches complete")
|
2024-02-02 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 15:06:39 +00:00
|
|
|
group, groupCtx := errgroup.WithContext(ctx)
|
2024-02-13 16:51:54 +00:00
|
|
|
recQueue := runQueue[Request](groupCtx, cr.ctrl)
|
2023-12-13 15:06:39 +00:00
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// Managed Type Events → Managed Type Reconciliation Queue
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.watch(groupCtx, cr.ctrl.managedTypeWatch.watchedType, func(res *pbresource.Resource) {
|
2023-12-13 15:06:39 +00:00
|
|
|
recQueue.Add(Request{ID: res.Id})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
for _, w := range cr.ctrl.watches {
|
|
|
|
mapQueue := runQueue[mapperRequest](groupCtx, cr.ctrl)
|
2023-12-13 15:06:39 +00:00
|
|
|
watcher := w
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// Watched Type Events → Watched Type Mapper Queue
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.watch(groupCtx, watcher.watchedType, func(res *pbresource.Resource) {
|
2023-12-13 15:06:39 +00:00
|
|
|
mapQueue.Add(mapperRequest{res: res})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// Watched Type Mapper Queue → Watched Type Mapper → Managed Type Reconciliation Queue
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.runMapper(groupCtx, watcher, mapQueue, recQueue, func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error) {
|
2023-12-13 15:06:39 +00:00
|
|
|
return watcher.mapper(ctx, runtime, itemType.(mapperRequest).res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
for _, cw := range cr.ctrl.customWatches {
|
|
|
|
customMapQueue := runQueue[Event](groupCtx, cr.ctrl)
|
2023-12-13 15:06:39 +00:00
|
|
|
watcher := cw
|
2024-02-13 16:51:54 +00:00
|
|
|
// Custom Events → Custom Mapper Queue
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
return watcher.source.Watch(groupCtx, func(e Event) {
|
|
|
|
customMapQueue.Add(e)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// Custom Mapper Queue → Custom Dependency Mapper → Managed Type Reconciliation Queue
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.runCustomMapper(groupCtx, watcher, customMapQueue, recQueue, func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error) {
|
2023-12-13 15:06:39 +00:00
|
|
|
return watcher.mapper(ctx, runtime, itemType.(Event))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// Managed Type Reconciliation Queue → Reconciler
|
2023-12-13 15:06:39 +00:00
|
|
|
group.Go(func() error {
|
2024-02-13 16:51:54 +00:00
|
|
|
return cr.runReconciler(groupCtx, recQueue)
|
2023-12-13 15:06:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) primeCache(ctx context.Context, typ *pbresource.Type) error {
|
|
|
|
wl, err := cr.watchClient.WatchList(ctx, &pbresource.WatchListRequest{
|
2024-02-02 21:11:05 +00:00
|
|
|
Type: typ,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: storage.Wildcard,
|
|
|
|
Namespace: storage.Wildcard,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.handleInvalidControllerWatch(err)
|
|
|
|
cr.logger.Error("failed to create cache priming watch", "error", err)
|
2024-02-02 21:11:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := wl.Recv()
|
|
|
|
if err != nil {
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.handleInvalidControllerWatch(err)
|
|
|
|
cr.logger.Warn("error received from cache priming watch", "error", err)
|
2024-02-02 21:11:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case event.GetUpsert() != nil:
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache.Insert(event.GetUpsert().Resource)
|
2024-02-02 21:11:05 +00:00
|
|
|
case event.GetDelete() != nil:
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache.Delete(event.GetDelete().Resource)
|
2024-02-02 21:11:05 +00:00
|
|
|
case event.GetEndOfSnapshot() != nil:
|
|
|
|
// This concludes the initial snapshot. The cache is primed.
|
|
|
|
return nil
|
|
|
|
default:
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Warn("skipping unexpected event type", "type", hclog.Fmt("%T", event.GetEvent()))
|
2024-02-02 21:11:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) watch(ctx context.Context, typ *pbresource.Type, add func(*pbresource.Resource)) error {
|
|
|
|
wl, err := cr.watchClient.WatchList(ctx, &pbresource.WatchListRequest{
|
2023-12-13 15:06:39 +00:00
|
|
|
Type: typ,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Partition: storage.Wildcard,
|
|
|
|
Namespace: storage.Wildcard,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.handleInvalidControllerWatch(err)
|
|
|
|
cr.logger.Error("failed to create watch", "error", err)
|
2023-12-13 15:06:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := wl.Recv()
|
|
|
|
if err != nil {
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.handleInvalidControllerWatch(err)
|
|
|
|
cr.logger.Warn("error received from watch", "error", err)
|
2023-12-13 15:06:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the cache up to date. There main reason to do this here is
|
2024-02-13 16:51:54 +00:00
|
|
|
// to ensure that any mapper/reconciliation queue deduping won't
|
2023-12-13 15:06:39 +00:00
|
|
|
// hide events from being observed and updating the cache state.
|
|
|
|
// Therefore we should do this before any queueing.
|
2024-02-02 21:11:05 +00:00
|
|
|
var resource *pbresource.Resource
|
|
|
|
switch {
|
|
|
|
case event.GetUpsert() != nil:
|
|
|
|
resource = event.GetUpsert().GetResource()
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache.Insert(resource)
|
2024-02-02 21:11:05 +00:00
|
|
|
case event.GetDelete() != nil:
|
|
|
|
resource = event.GetDelete().GetResource()
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.cache.Delete(resource)
|
2024-02-02 21:11:05 +00:00
|
|
|
case event.GetEndOfSnapshot() != nil:
|
|
|
|
continue // ignore
|
|
|
|
default:
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Warn("skipping unexpected event type", "type", hclog.Fmt("%T", event.GetEvent()))
|
2024-02-02 21:11:05 +00:00
|
|
|
continue
|
2023-12-13 15:06:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:54:07 +00:00
|
|
|
// Before adding the resource into the queue we must clone it.
|
|
|
|
// While we want the cache to not have duplicate copies of all the
|
|
|
|
// data, we do want downstream consumers like dependency mappers and
|
|
|
|
// controllers to be able to freely modify the data they are given.
|
|
|
|
// Therefore we clone the resource here to prevent any accidental
|
|
|
|
// mutation of data held by the cache (and presumably by the resource
|
|
|
|
// service assuming that the regular client we were given is the inmem
|
|
|
|
// variant)
|
2024-02-02 21:11:05 +00:00
|
|
|
add(protoutil.Clone(resource))
|
2023-12-13 15:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) runMapper(
|
2023-12-13 15:06:39 +00:00
|
|
|
ctx context.Context,
|
|
|
|
w *watch,
|
|
|
|
from queue.WorkQueue[mapperRequest],
|
|
|
|
to queue.WorkQueue[Request],
|
|
|
|
mapper func(ctx context.Context, runtime Runtime, itemType queue.ItemType) ([]Request, error),
|
|
|
|
) error {
|
2024-02-13 16:51:54 +00:00
|
|
|
logger := cr.logger.With("watched_resource_type", resource.ToGVK(w.watchedType))
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
item, shutdown := from.Get()
|
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
if err := cr.doMap(ctx, mapper, to, item, logger); err != nil {
|
2023-12-13 15:06:39 +00:00
|
|
|
from.AddRateLimited(item)
|
|
|
|
from.Done(item)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
from.Forget(item)
|
|
|
|
from.Done(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) runCustomMapper(
|
2023-12-13 15:06:39 +00:00
|
|
|
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 {
|
2024-02-13 16:51:54 +00:00
|
|
|
logger := cr.logger.With("watched_event", cw.source)
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
item, shutdown := from.Get()
|
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
if err := cr.doMap(ctx, mapper, to, item, logger); err != nil {
|
2023-12-13 15:06:39 +00:00
|
|
|
from.AddRateLimited(item)
|
|
|
|
from.Done(item)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
from.Forget(item)
|
|
|
|
from.Done(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *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 {
|
2023-12-13 15:06:39 +00:00
|
|
|
var reqs []Request
|
2024-02-13 16:51:54 +00:00
|
|
|
if err := cr.handlePanic(func() error {
|
2023-12-13 15:06:39 +00:00
|
|
|
var err error
|
2024-02-13 16:51:54 +00:00
|
|
|
reqs, err = mapper(ctx, cr.runtime(logger.With("map-request-key", item.Key())), item)
|
2023-12-13 15:06:39 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range reqs {
|
2024-02-13 16:51:54 +00:00
|
|
|
if !resource.EqualType(r.ID.Type, cr.ctrl.managedTypeWatch.watchedType) {
|
2023-12-13 15:06:39 +00:00
|
|
|
logger.Error("dependency mapper returned request for a resource of the wrong type",
|
2024-02-13 16:51:54 +00:00
|
|
|
"type_expected", resource.ToGVK(cr.ctrl.managedTypeWatch.watchedType),
|
2023-12-13 15:06:39 +00:00
|
|
|
"type_got", resource.ToGVK(r.ID.Type),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
to.Add(r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
// maybeScheduleForcedReconcile makes sure that a "reconcile the world" happens periodically for the
|
|
|
|
// controller's managed type.
|
|
|
|
func (cr *controllerRunner) maybeScheduleForcedReconcile(queue queue.WorkQueue[Request], req Request) {
|
|
|
|
// In order to periodically "reconcile the world", we schedule a deferred reconcile request
|
|
|
|
// (aka forced reconcile) minus a sizeable random jitter to avoid a thundering herd.
|
|
|
|
//
|
|
|
|
// A few notes on how this integrates with existing non-"reconcile the world" requests:
|
|
|
|
//
|
|
|
|
// 1. Successful reconciles result in a new deferred "reconcile the world" request being scheduled.
|
|
|
|
// The net effect is that the managed type will be continually reconciled regardless of any updates.
|
|
|
|
// 2. Failed reconciles are re-queued with a rate limit and get added to the deferred reconcile queue.
|
|
|
|
// Any existing deferred "reconcile the world" request will be replaced by the rate-limited deferred
|
|
|
|
// request.
|
|
|
|
// 3. An existing deferred "reconcile the world" request can't be removed on the successful reconcile
|
|
|
|
// of a delete operation. We rely on controller idempotency to eventually process the deferred request
|
|
|
|
// as a no-op.
|
|
|
|
_, err := cr.runtimeClient.Read(context.Background(), &pbresource.ReadRequest{Id: req.ID})
|
|
|
|
switch {
|
|
|
|
case err != nil && status.Code(err) == codes.NotFound:
|
|
|
|
// Resource was deleted -> nothing to force reconcile so do nothing
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Reconcile of resource upsert was successful or we had an unexpected
|
|
|
|
// error. In either case, we should schedule a forced reconcile for completeness.
|
|
|
|
scheduleAt := reduceByRandomJitter(cr.ctrl.forceReconcileEvery)
|
|
|
|
queue.AddAfter(req, scheduleAt, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduceByRandomJitter returns a duration reduced by a random amount up to 20%.
|
|
|
|
func reduceByRandomJitter(d time.Duration) time.Duration {
|
|
|
|
percent := rand.Float64() * 0.2
|
|
|
|
reduction := time.Duration(float64(d) * percent)
|
|
|
|
return d - reduction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *controllerRunner) runReconciler(ctx context.Context, queue queue.WorkQueue[Request]) error {
|
2023-12-13 15:06:39 +00:00
|
|
|
for {
|
|
|
|
req, shutdown := queue.Get()
|
|
|
|
if shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Trace("handling request", "request", req)
|
|
|
|
err := cr.handlePanic(func() error {
|
|
|
|
return cr.ctrl.reconciler.Reconcile(ctx, cr.runtime(cr.logger.With("resource-id", req.ID.String())), req)
|
2023-12-13 15:06:39 +00:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
queue.Forget(req)
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.maybeScheduleForcedReconcile(queue, req)
|
2023-12-13 15:06:39 +00:00
|
|
|
} else {
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Trace("post-processing reconcile failure")
|
2023-12-13 15:06:39 +00:00
|
|
|
var requeueAfter RequeueAfterError
|
|
|
|
if errors.As(err, &requeueAfter) {
|
|
|
|
queue.Forget(req)
|
2024-02-13 16:51:54 +00:00
|
|
|
queue.AddAfter(req, time.Duration(requeueAfter), false)
|
2023-12-13 15:06:39 +00:00
|
|
|
} else {
|
|
|
|
queue.AddRateLimited(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
queue.Done(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) handlePanic(fn func() error) (err error) {
|
2023-12-13 15:06:39 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
stack := hclog.Stacktrace()
|
2024-02-13 16:51:54 +00:00
|
|
|
cr.logger.Error("controller panic",
|
2023-12-13 15:06:39 +00:00
|
|
|
"panic", r,
|
|
|
|
"stack", stack,
|
|
|
|
)
|
|
|
|
err = fmt.Errorf("panic [recovered]: %v", r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) runtime(logger hclog.Logger) Runtime {
|
2023-12-13 15:06:39 +00:00
|
|
|
return Runtime{
|
2024-01-12 16:54:07 +00:00
|
|
|
// dependency mappers and controllers are always given the cloning client
|
|
|
|
// so that they do not have to care about mutating values that they read
|
|
|
|
// through the client.
|
2024-02-13 16:51:54 +00:00
|
|
|
Client: cr.runtimeClient,
|
2023-12-13 15:06:39 +00:00
|
|
|
Logger: logger,
|
2024-01-12 16:54:07 +00:00
|
|
|
// ensure that resources queried via the cache get cloned so that the
|
|
|
|
// dependency mapper or reconciler is free to modify them.
|
2024-02-13 16:51:54 +00:00
|
|
|
Cache: cache.NewCloningReadOnlyCache(cr.cache),
|
2023-12-13 15:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
func (cr *controllerRunner) handleInvalidControllerWatch(err error) {
|
2024-02-06 16:23:06 +00:00
|
|
|
st, ok := status.FromError(err)
|
|
|
|
if ok && st.Code() == codes.InvalidArgument {
|
2024-02-13 16:51:54 +00:00
|
|
|
panic(fmt.Sprintf("controller %s attempted to initiate an invalid watch: %q. This is a bug within the controller.", cr.ctrl.name, err.Error()))
|
2024-02-06 16:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:51:54 +00:00
|
|
|
type mapperRequest struct {
|
|
|
|
// res is the resource that was watched and is being mapped.
|
|
|
|
res *pbresource.Resource
|
|
|
|
}
|
2023-12-13 15:06:39 +00:00
|
|
|
|
|
|
|
// 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(
|
2024-01-29 20:08:31 +00:00
|
|
|
"type=%q,part=%q,ns=%q,name=%q,uid=%q",
|
2023-12-13 15:06:39 +00:00
|
|
|
resource.ToGVK(i.res.Id.Type),
|
|
|
|
i.res.Id.Tenancy.Partition,
|
|
|
|
i.res.Id.Tenancy.Namespace,
|
|
|
|
i.res.Id.Name,
|
|
|
|
i.res.Id.Uid,
|
|
|
|
)
|
|
|
|
}
|