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 source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-13 20:57:03 +00:00
|
|
|
"errors"
|
2020-10-21 05:49:41 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
2024-05-13 20:57:03 +00:00
|
|
|
"k8s.io/utils/ptr"
|
2021-06-25 05:02:01 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/handler"
|
2023-06-01 17:01:19 +00:00
|
|
|
internal "sigs.k8s.io/controller-runtime/pkg/internal/source"
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/cache"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
|
|
|
)
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// Source is a source of events (e.g. Create, Update, Delete operations on Kubernetes Objects, Webhook callbacks, etc)
|
2020-10-21 05:49:41 +00:00
|
|
|
// which should be processed by event.EventHandlers to enqueue reconcile.Requests.
|
|
|
|
//
|
|
|
|
// * Use Kind for events originating in the cluster (e.g. Pod Create, Pod Update, Deployment Update).
|
|
|
|
//
|
2024-05-13 20:57:03 +00:00
|
|
|
// * Use Channel for events originating outside the cluster (e.g. GitHub Webhook callback, Polling external urls).
|
2020-10-21 05:49:41 +00:00
|
|
|
//
|
2023-06-01 17:01:19 +00:00
|
|
|
// Users may build their own Source implementations.
|
2020-10-21 05:49:41 +00:00
|
|
|
type Source interface {
|
|
|
|
// Start is internal and should be called only by the Controller to register an EventHandler with the Informer
|
|
|
|
// to enqueue reconcile.Requests.
|
2024-05-13 20:57:03 +00:00
|
|
|
Start(context.Context, workqueue.RateLimitingInterface) error
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncingSource is a source that needs syncing prior to being usable. The controller
|
|
|
|
// will call its WaitForSync prior to starting workers.
|
|
|
|
type SyncingSource interface {
|
|
|
|
Source
|
|
|
|
WaitForSync(ctx context.Context) error
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Kind creates a KindSource with the given cache provider.
|
2024-05-13 20:57:03 +00:00
|
|
|
func Kind[T client.Object](cache cache.Cache, object T, handler handler.TypedEventHandler[T], predicates ...predicate.TypedPredicate[T]) SyncingSource {
|
|
|
|
return &internal.Kind[T]{
|
|
|
|
Type: object,
|
|
|
|
Cache: cache,
|
|
|
|
Handler: handler,
|
|
|
|
Predicates: predicates,
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
var _ Source = &channel[string]{}
|
|
|
|
|
|
|
|
// ChannelOpt allows to configure a source.Channel.
|
|
|
|
type ChannelOpt[T any] func(*channel[T])
|
|
|
|
|
|
|
|
// WithPredicates adds the configured predicates to a source.Channel.
|
|
|
|
func WithPredicates[T any](p ...predicate.TypedPredicate[T]) ChannelOpt[T] {
|
|
|
|
return func(c *channel[T]) {
|
|
|
|
c.predicates = append(c.predicates, p...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBufferSize configures the buffer size for a source.Channel. By
|
|
|
|
// default, the buffer size is 1024.
|
|
|
|
func WithBufferSize[T any](bufferSize int) ChannelOpt[T] {
|
|
|
|
return func(c *channel[T]) {
|
|
|
|
c.bufferSize = &bufferSize
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// Channel is used to provide a source of events originating outside the cluster
|
|
|
|
// (e.g. GitHub Webhook callback). Channel requires the user to wire the external
|
2024-05-13 20:57:03 +00:00
|
|
|
// source (e.g. http handler) to write GenericEvents to the underlying channel.
|
|
|
|
func Channel[T any](source <-chan event.TypedGenericEvent[T], handler handler.TypedEventHandler[T], opts ...ChannelOpt[T]) Source {
|
|
|
|
c := &channel[T]{
|
|
|
|
source: source,
|
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type channel[T any] struct {
|
2020-10-21 05:49:41 +00:00
|
|
|
// once ensures the event distribution goroutine will be performed only once
|
|
|
|
once sync.Once
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// source is the source channel to fetch GenericEvents
|
|
|
|
source <-chan event.TypedGenericEvent[T]
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
handler handler.TypedEventHandler[T]
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
predicates []predicate.TypedPredicate[T]
|
|
|
|
|
|
|
|
bufferSize *int
|
|
|
|
|
|
|
|
// dest is the destination channels of the added event handlers
|
|
|
|
dest []chan event.TypedGenericEvent[T]
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// destLock is to ensure the destination channels are safely added/removed
|
|
|
|
destLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
func (cs *channel[T]) String() string {
|
2020-10-21 05:49:41 +00:00
|
|
|
return fmt.Sprintf("channel source: %p", cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements Source and should only be called by the Controller.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (cs *channel[T]) Start(
|
2021-06-25 05:02:01 +00:00
|
|
|
ctx context.Context,
|
2020-10-21 05:49:41 +00:00
|
|
|
queue workqueue.RateLimitingInterface,
|
2024-05-13 20:57:03 +00:00
|
|
|
) error {
|
2020-10-21 05:49:41 +00:00
|
|
|
// Source should have been specified by the user.
|
2024-05-13 20:57:03 +00:00
|
|
|
if cs.source == nil {
|
2020-10-21 05:49:41 +00:00
|
|
|
return fmt.Errorf("must specify Channel.Source")
|
|
|
|
}
|
2024-05-13 20:57:03 +00:00
|
|
|
if cs.handler == nil {
|
|
|
|
return errors.New("must specify Channel.Handler")
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
if cs.bufferSize == nil {
|
|
|
|
cs.bufferSize = ptr.To(1024)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
dst := make(chan event.TypedGenericEvent[T], *cs.bufferSize)
|
2021-06-25 05:02:01 +00:00
|
|
|
|
|
|
|
cs.destLock.Lock()
|
|
|
|
cs.dest = append(cs.dest, dst)
|
|
|
|
cs.destLock.Unlock()
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
cs.once.Do(func() {
|
|
|
|
// Distribute GenericEvents to all EventHandler / Queue pairs Watching this source
|
2021-06-25 05:02:01 +00:00
|
|
|
go cs.syncLoop(ctx)
|
2020-10-21 05:49:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for evt := range dst {
|
|
|
|
shouldHandle := true
|
2024-05-13 20:57:03 +00:00
|
|
|
for _, p := range cs.predicates {
|
2020-10-21 05:49:41 +00:00
|
|
|
if !p.Generic(evt) {
|
|
|
|
shouldHandle = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldHandle {
|
2023-06-01 17:01:19 +00:00
|
|
|
func() {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2024-05-13 20:57:03 +00:00
|
|
|
cs.handler.Generic(ctx, evt, queue)
|
2023-06-01 17:01:19 +00:00
|
|
|
}()
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
func (cs *channel[T]) doStop() {
|
2020-10-21 05:49:41 +00:00
|
|
|
cs.destLock.Lock()
|
|
|
|
defer cs.destLock.Unlock()
|
|
|
|
|
|
|
|
for _, dst := range cs.dest {
|
|
|
|
close(dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
func (cs *channel[T]) distribute(evt event.TypedGenericEvent[T]) {
|
2020-10-21 05:49:41 +00:00
|
|
|
cs.destLock.Lock()
|
|
|
|
defer cs.destLock.Unlock()
|
|
|
|
|
|
|
|
for _, dst := range cs.dest {
|
|
|
|
// We cannot make it under goroutine here, or we'll meet the
|
|
|
|
// race condition of writing message to closed channels.
|
|
|
|
// To avoid blocking, the dest channels are expected to be of
|
|
|
|
// proper buffer size. If we still see it blocked, then
|
|
|
|
// the controller is thought to be in an abnormal state.
|
|
|
|
dst <- evt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
func (cs *channel[T]) syncLoop(ctx context.Context) {
|
2020-10-21 05:49:41 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-06-25 05:02:01 +00:00
|
|
|
case <-ctx.Done():
|
2020-10-21 05:49:41 +00:00
|
|
|
// Close destination channels
|
|
|
|
cs.doStop()
|
|
|
|
return
|
2024-05-13 20:57:03 +00:00
|
|
|
case evt, stillOpen := <-cs.source:
|
2021-06-25 05:02:01 +00:00
|
|
|
if !stillOpen {
|
|
|
|
// if the source channel is closed, we're never gonna get
|
|
|
|
// anything more on it, so stop & bail
|
|
|
|
cs.doStop()
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
cs.distribute(evt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create).
|
2020-10-21 05:49:41 +00:00
|
|
|
type Informer struct {
|
|
|
|
// Informer is the controller-runtime Informer
|
2024-05-13 20:57:03 +00:00
|
|
|
Informer cache.Informer
|
|
|
|
Handler handler.EventHandler
|
|
|
|
Predicates []predicate.Predicate
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Source = &Informer{}
|
|
|
|
|
|
|
|
// Start is internal and should be called only by the Controller to register an EventHandler with the Informer
|
|
|
|
// to enqueue reconcile.Requests.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (is *Informer) Start(ctx context.Context, queue workqueue.RateLimitingInterface) error {
|
2020-10-21 05:49:41 +00:00
|
|
|
// Informer should have been specified by the user.
|
|
|
|
if is.Informer == nil {
|
|
|
|
return fmt.Errorf("must specify Informer.Informer")
|
|
|
|
}
|
2024-05-13 20:57:03 +00:00
|
|
|
if is.Handler == nil {
|
|
|
|
return errors.New("must specify Informer.Handler")
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
_, err := is.Informer.AddEventHandler(internal.NewEventHandler(ctx, queue, is.Handler, is.Predicates).HandlerFuncs())
|
2023-02-01 17:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *Informer) String() string {
|
|
|
|
return fmt.Sprintf("informer source: %p", is.Informer)
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
var _ Source = Func(nil)
|
|
|
|
|
|
|
|
// Func is a function that implements Source.
|
2024-05-13 20:57:03 +00:00
|
|
|
type Func func(context.Context, workqueue.RateLimitingInterface) error
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Start implements Source.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (f Func) Start(ctx context.Context, queue workqueue.RateLimitingInterface) error {
|
|
|
|
return f(ctx, queue)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f Func) String() string {
|
|
|
|
return fmt.Sprintf("func source: %p", f)
|
|
|
|
}
|