-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ Allow removing individual informers from the cache (#935) #936
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
Closed
shomron
wants to merge
1
commit into
kubernetes-sigs:master
from
shomron:remove-informers-from-cache
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,9 @@ type MapEntry struct { | |
|
||
// CacheReader wraps Informer and implements the CacheReader interface for a single type | ||
Reader CacheReader | ||
|
||
// Stop can be used to stop this individual informer without stopping the entire specificInformersMap. | ||
stop chan struct{} | ||
} | ||
|
||
// specificInformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs. | ||
|
@@ -121,6 +124,17 @@ type specificInformersMap struct { | |
namespace string | ||
} | ||
|
||
// Start starts the informer managed by a MapEntry. | ||
// Blocks until the informer stops. The informer can be stopped | ||
// either individually (via the entry's stop channel) or globally | ||
// via the provided stop argument. | ||
func (e *MapEntry) Start(stop <-chan struct{}) { | ||
// Stop on either the whole map stopping or just this informer being removed. | ||
internalStop, cancel := anyOf(stop, e.stop) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #1116 should help here |
||
defer cancel() | ||
e.Informer.Run(internalStop) | ||
} | ||
|
||
// Start calls Run on each of the informers and sets started to true. Blocks on the stop channel. | ||
// It doesn't return start because it can't return an error, and it's not a runnable directly. | ||
func (ip *specificInformersMap) Start(stop <-chan struct{}) { | ||
|
@@ -132,8 +146,8 @@ func (ip *specificInformersMap) Start(stop <-chan struct{}) { | |
ip.stop = stop | ||
|
||
// Start each informer | ||
for _, informer := range ip.informersByGVK { | ||
go informer.Informer.Run(stop) | ||
for _, entry := range ip.informersByGVK { | ||
go entry.Start(stop) | ||
} | ||
|
||
// Set started to true so we immediately start any informers added later. | ||
|
@@ -183,8 +197,12 @@ func (ip *specificInformersMap) Get(ctx context.Context, gvk schema.GroupVersion | |
|
||
if started && !i.Informer.HasSynced() { | ||
// Wait for it to sync before returning the Informer so that folks don't read from a stale cache. | ||
if !cache.WaitForCacheSync(ctx.Done(), i.Informer.HasSynced) { | ||
return started, nil, apierrors.NewTimeoutError(fmt.Sprintf("failed waiting for %T Informer to sync", obj), 0) | ||
// Cancel for context, informer stopping, or entire map stopping. | ||
syncStop, cancel := mergeChan(ctx.Done(), i.stop, ip.stop) | ||
defer cancel() | ||
if !cache.WaitForCacheSync(syncStop, i.Informer.HasSynced) { | ||
// Return entry even on timeout - caller may have intended a non-blocking fetch. | ||
return started, i, apierrors.NewTimeoutError(fmt.Sprintf("failed waiting for %T Informer to sync", obj), 0) | ||
} | ||
} | ||
|
||
|
@@ -214,18 +232,32 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob | |
i := &MapEntry{ | ||
Informer: ni, | ||
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk}, | ||
stop: make(chan struct{}), | ||
} | ||
ip.informersByGVK[gvk] = i | ||
|
||
// Start the Informer if need by | ||
// TODO(seans): write thorough tests and document what happens here - can you add indexers? | ||
// can you add eventhandlers? | ||
if ip.started { | ||
go i.Informer.Run(ip.stop) | ||
go i.Start(ip.stop) | ||
} | ||
return i, ip.started, nil | ||
} | ||
|
||
// Remove removes an informer entry and stops it if it was running. | ||
func (ip *specificInformersMap) Remove(gvk schema.GroupVersionKind) { | ||
ip.mu.Lock() | ||
defer ip.mu.Unlock() | ||
|
||
entry, ok := ip.informersByGVK[gvk] | ||
if !ok { | ||
return | ||
} | ||
close(entry.stop) | ||
delete(ip.informersByGVK, gvk) | ||
} | ||
|
||
// newListWatch returns a new ListWatch object that can be used to create a SharedIndexInformer. | ||
func createStructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) { | ||
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/printer" | ||
) | ||
|
||
func TestCacheInternal(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecsWithDefaultAndCustomReporters(t, "Cache Internal Suite", []Reporter{printer.NewlineReporter{}}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// anyOf returns a "done" channel that is closed when any of its input channels | ||
// are closed or when the retuned cancel function is called, whichever comes first. | ||
// | ||
// The cancel function should always be called by the caller to ensure | ||
// resources are properly released. | ||
func anyOf(ch ...<-chan struct{}) (<-chan struct{}, context.CancelFunc) { | ||
var once sync.Once | ||
cancel := make(chan struct{}) | ||
cancelFunc := func() { | ||
once.Do(func() { | ||
close(cancel) | ||
}) | ||
} | ||
return anyInternal(append(ch, cancel)...), cancelFunc | ||
} | ||
|
||
func anyInternal(ch ...<-chan struct{}) <-chan struct{} { | ||
switch len(ch) { | ||
case 0: | ||
return nil | ||
case 1: | ||
return ch[0] | ||
} | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
|
||
switch len(ch) { | ||
case 2: | ||
// This case saves a recursion + goroutine when there are exactly 2 channels. | ||
select { | ||
case <-ch[0]: | ||
case <-ch[1]: | ||
} | ||
default: | ||
// >=3 channels to merge | ||
select { | ||
case <-ch[0]: | ||
case <-ch[1]: | ||
case <-ch[2]: | ||
case <-anyInternal(append(ch[3:], done)...): | ||
} | ||
} | ||
}() | ||
|
||
return done | ||
} | ||
|
||
// mergeChan returns a channel that is closed when any of the input channels are signaled. | ||
// The caller must call the returned CancelFunc to ensure no resources are leaked. | ||
func mergeChan(a, b, c <-chan struct{}) (<-chan struct{}, context.CancelFunc) { | ||
var once sync.Once | ||
out := make(chan struct{}) | ||
cancel := make(chan struct{}) | ||
cancelFunc := func() { | ||
once.Do(func() { | ||
close(cancel) | ||
}) | ||
} | ||
go func() { | ||
defer close(out) | ||
select { | ||
case <-a: | ||
case <-b: | ||
case <-c: | ||
case <-cancel: | ||
} | ||
}() | ||
|
||
return out, cancelFunc | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
) | ||
|
||
var _ = Describe("anyOf", func() { | ||
// Generate contexts for different number of input channels | ||
for n := 0; n < 4; n++ { | ||
n := n | ||
Context(fmt.Sprintf("with %d channels", n), func() { | ||
var ( | ||
channels []chan struct{} | ||
done <-chan struct{} | ||
cancel context.CancelFunc | ||
) | ||
BeforeEach(func() { | ||
channels = make([]chan struct{}, n) | ||
in := make([]<-chan struct{}, n) | ||
for i := 0; i < n; i++ { | ||
ch := make(chan struct{}) | ||
channels[i] = ch | ||
in[i] = ch | ||
} | ||
done, cancel = anyOf(in...) | ||
}) | ||
AfterEach(func() { | ||
cancel() | ||
}) | ||
|
||
It("isn't closed initially", func() { | ||
select { | ||
case <-done: | ||
Fail("done was closed before cancel") | ||
case <-time.After(5 * time.Millisecond): | ||
// Ok. | ||
} | ||
}) | ||
|
||
// Verify that done is closed when we call cancel explicitly. | ||
It("closes when cancelled", func() { | ||
cancel() | ||
select { | ||
case <-done: | ||
// Ok. | ||
case <-time.After(5 * time.Millisecond): | ||
Fail("timed out waiting for cancel") | ||
} | ||
}) | ||
|
||
// Generate test cases for closing each individual channel. | ||
// Verify that done is closed in response. | ||
for i := 0; i < n; i++ { | ||
i := i | ||
It(fmt.Sprintf("closes when channel %d is closed", i), func() { | ||
close(channels[i]) | ||
select { | ||
case <-done: | ||
// Ok. | ||
case <-time.After(5 * time.Millisecond): | ||
Fail("timed out waiting for cancel") | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good change but should be its own PR as it doesn't really have anything to do with this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I will move this into a separate PR. 👍🏻