Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠Only allow sources to start once #2686

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = Describe("controller.Controller", func() {

ctx, cancel := context.WithCancel(context.Background())
watchChan := make(chan event.GenericEvent, 1)
watch := source.Channel(watchChan, &handler.EnqueueRequestForObject{})
watch := source.Channel(source.NewChannelBroadcaster(watchChan), &handler.EnqueueRequestForObject{})
watchChan <- event.GenericEvent{Object: &corev1.Pod{}}

reconcileStarted := make(chan struct{})
Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ var _ = Describe("controller", func() {
}

ins := source.Channel(
ch,
source.NewChannelBroadcaster(ch),
handler.Funcs{
GenericFunc: func(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
defer GinkgoRecover()
Expand All @@ -248,7 +248,7 @@ var _ = Describe("controller", func() {
<-processed
})

It("should error when channel source is not specified", func() {
It("should error when ChannelBroadcaster is not specified", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -257,7 +257,7 @@ var _ = Describe("controller", func() {

e := ctrl.Start(ctx)
Expect(e).To(HaveOccurred())
Expect(e.Error()).To(ContainSubstring("must specify Channel.Source"))
Expect(e.Error()).To(ContainSubstring("must create Channel with a non-nil broadcaster"))
})

It("should call Start on sources with the appropriate EventHandler, Queue, and Predicates", func() {
Expand Down
31 changes: 29 additions & 2 deletions pkg/internal/source/kind.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/*
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 internal

import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"time"

"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -30,6 +47,9 @@ type Kind[T client.Object] struct {

Predicates []predicate.TypedPredicate[T]

mu sync.RWMutex
isStarted bool

// startedErr may contain an error if one was encountered during startup. If its closed and does not
// contain an error, startup and syncing finished.
startedErr chan error
Expand All @@ -40,14 +60,21 @@ type Kind[T client.Object] struct {
// to enqueue reconcile.Requests.
func (ks *Kind[T]) Start(ctx context.Context, queue workqueue.RateLimitingInterface) error {
if isNil(ks.Type) {
return fmt.Errorf("must create Kind with a non-nil object")
return fmt.Errorf("must create Kind with a non-nil type")
}
if isNil(ks.Cache) {
return fmt.Errorf("must create Kind with a non-nil cache")
}
if isNil(ks.Handler) {
return errors.New("must create Kind with non-nil handler")
return errors.New("must create Kind with a non-nil handler")
}

ks.mu.Lock()
defer ks.mu.Unlock()
if ks.isStarted {
return fmt.Errorf("cannot start an already started Kind source")
}
ks.isStarted = true

// cache.GetInformer will block until its context is cancelled if the cache was already started and it can not
// sync that informer (most commonly due to RBAC issues).
Expand Down
2 changes: 1 addition & 1 deletion pkg/source/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ExampleChannel() {

err := ctrl.Watch(
source.Channel(
events,
source.NewChannelBroadcaster(events),
&handler.EnqueueRequestForObject{},
),
)
Expand Down
Loading