2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2021-06-25 05:02:01 +00:00
|
|
|
"context"
|
2020-10-21 05:49:41 +00:00
|
|
|
"fmt"
|
2021-06-25 05:02:01 +00:00
|
|
|
"time"
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2023-02-01 17:06:36 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/internal/controller"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
|
|
|
)
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Options are the arguments for creating a new Controller.
|
2020-10-21 05:49:41 +00:00
|
|
|
type Options struct {
|
|
|
|
// MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1.
|
|
|
|
MaxConcurrentReconciles int
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
|
|
|
|
// Defaults to 2 minutes if not set.
|
|
|
|
CacheSyncTimeout time.Duration
|
|
|
|
|
|
|
|
// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
|
|
|
|
// Defaults to the Controller.RecoverPanic setting from the Manager if unset.
|
|
|
|
RecoverPanic *bool
|
|
|
|
|
|
|
|
// NeedLeaderElection indicates whether the controller needs to use leader election.
|
|
|
|
// Defaults to true, which means the controller will use leader election.
|
|
|
|
NeedLeaderElection *bool
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Reconciler reconciles an object
|
|
|
|
Reconciler reconcile.Reconciler
|
|
|
|
|
|
|
|
// RateLimiter is used to limit how frequently requests may be queued.
|
|
|
|
// Defaults to MaxOfRateLimiter which has both overall and per-item rate limiting.
|
|
|
|
// The overall is a token bucket and the per-item is exponential.
|
|
|
|
RateLimiter ratelimiter.RateLimiter
|
2021-06-25 05:02:01 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// NewQueue constructs the queue for this controller once the controller is ready to start.
|
|
|
|
// With NewQueue a custom queue implementation can be used, e.g. a priority queue to prioritize with which
|
|
|
|
// priority/order objects are reconciled (e.g. to reconcile objects with changes first).
|
|
|
|
// This is a func because the standard Kubernetes work queues start themselves immediately, which
|
|
|
|
// leads to goroutine leaks if something calls controller.New repeatedly.
|
|
|
|
// The NewQueue func gets the controller name and the RateLimiter option (defaulted if necessary) passed in.
|
|
|
|
// NewQueue defaults to NewRateLimitingQueueWithConfig.
|
|
|
|
//
|
|
|
|
// NOTE: LOW LEVEL PRIMITIVE!
|
|
|
|
// Only use a custom NewQueue if you know what you are doing.
|
|
|
|
NewQueue func(controllerName string, rateLimiter ratelimiter.RateLimiter) workqueue.RateLimitingInterface
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// LogConstructor is used to construct a logger used for this controller and passed
|
|
|
|
// to each reconciliation via the context field.
|
|
|
|
LogConstructor func(request *reconcile.Request) logr.Logger
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Controller implements a Kubernetes API. A Controller manages a work queue fed reconcile.Requests
|
|
|
|
// from source.Sources. Work is performed through the reconcile.Reconciler for each enqueued item.
|
|
|
|
// Work typically is reads and writes Kubernetes objects to make the system state match the state specified
|
|
|
|
// in the object Spec.
|
|
|
|
type Controller interface {
|
|
|
|
// Reconciler is called to reconcile an object by Namespace/Name
|
|
|
|
reconcile.Reconciler
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// Watch watches the provided Source.
|
|
|
|
Watch(src source.Source) error
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Start starts the controller. Start blocks until the context is closed or a
|
2020-10-21 05:49:41 +00:00
|
|
|
// controller has an error starting.
|
2021-06-25 05:02:01 +00:00
|
|
|
Start(ctx context.Context) error
|
|
|
|
|
|
|
|
// GetLogger returns this controller logger prefilled with basic information.
|
|
|
|
GetLogger() logr.Logger
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Controller registered with the Manager. The Manager will ensure that shared Caches have
|
|
|
|
// been synced before the Controller is Started.
|
|
|
|
func New(name string, mgr manager.Manager, options Options) (Controller, error) {
|
|
|
|
c, err := NewUnmanaged(name, mgr, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the controller as a Manager components
|
|
|
|
return c, mgr.Add(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnmanaged returns a new controller without adding it to the manager. The
|
|
|
|
// caller is responsible for starting the returned controller.
|
|
|
|
func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller, error) {
|
|
|
|
if options.Reconciler == nil {
|
|
|
|
return nil, fmt.Errorf("must specify Reconciler")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, fmt.Errorf("must specify Name for Controller")
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
if options.LogConstructor == nil {
|
|
|
|
log := mgr.GetLogger().WithValues(
|
|
|
|
"controller", name,
|
|
|
|
)
|
|
|
|
options.LogConstructor = func(req *reconcile.Request) logr.Logger {
|
|
|
|
log := log
|
|
|
|
if req != nil {
|
|
|
|
log = log.WithValues(
|
|
|
|
"object", klog.KRef(req.Namespace, req.Name),
|
|
|
|
"namespace", req.Namespace, "name", req.Name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return log
|
|
|
|
}
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
if options.MaxConcurrentReconciles <= 0 {
|
2023-06-01 17:01:19 +00:00
|
|
|
if mgr.GetControllerOptions().MaxConcurrentReconciles > 0 {
|
|
|
|
options.MaxConcurrentReconciles = mgr.GetControllerOptions().MaxConcurrentReconciles
|
|
|
|
} else {
|
|
|
|
options.MaxConcurrentReconciles = 1
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
if options.CacheSyncTimeout == 0 {
|
2023-06-01 17:01:19 +00:00
|
|
|
if mgr.GetControllerOptions().CacheSyncTimeout != 0 {
|
|
|
|
options.CacheSyncTimeout = mgr.GetControllerOptions().CacheSyncTimeout
|
|
|
|
} else {
|
|
|
|
options.CacheSyncTimeout = 2 * time.Minute
|
|
|
|
}
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
if options.RateLimiter == nil {
|
|
|
|
options.RateLimiter = workqueue.DefaultControllerRateLimiter()
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
if options.NewQueue == nil {
|
|
|
|
options.NewQueue = func(controllerName string, rateLimiter ratelimiter.RateLimiter) workqueue.RateLimitingInterface {
|
|
|
|
return workqueue.NewRateLimitingQueueWithConfig(rateLimiter, workqueue.RateLimitingQueueConfig{
|
|
|
|
Name: controllerName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
if options.RecoverPanic == nil {
|
2023-04-18 08:08:00 +00:00
|
|
|
options.RecoverPanic = mgr.GetControllerOptions().RecoverPanic
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
if options.NeedLeaderElection == nil {
|
|
|
|
options.NeedLeaderElection = mgr.GetControllerOptions().NeedLeaderElection
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Create controller with dependencies set
|
2021-06-25 05:02:01 +00:00
|
|
|
return &controller.Controller{
|
2024-05-13 20:57:03 +00:00
|
|
|
Do: options.Reconciler,
|
|
|
|
RateLimiter: options.RateLimiter,
|
|
|
|
NewQueue: options.NewQueue,
|
2020-10-21 05:49:41 +00:00
|
|
|
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
|
2021-06-25 05:02:01 +00:00
|
|
|
CacheSyncTimeout: options.CacheSyncTimeout,
|
2020-10-21 05:49:41 +00:00
|
|
|
Name: name,
|
2023-02-01 17:06:36 +00:00
|
|
|
LogConstructor: options.LogConstructor,
|
2021-09-02 12:01:06 +00:00
|
|
|
RecoverPanic: options.RecoverPanic,
|
2023-06-01 17:01:19 +00:00
|
|
|
LeaderElected: options.NeedLeaderElection,
|
2021-06-25 05:02:01 +00:00
|
|
|
}, nil
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
|
|
|
|
// ReconcileIDFromContext gets the reconcileID from the current context.
|
|
|
|
var ReconcileIDFromContext = controller.ReconcileIDFromContext
|