-
Notifications
You must be signed in to change notification settings - Fork 13
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
chore: Optimize event controller cache hydration for high-volume clusters #124
Open
engedaam
wants to merge
21
commits into
awslabs:main
Choose a base branch
from
engedaam:use-clientgo-watcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
062432a
Avoid Event cache hydration by using clinet go watcher
engedaam 1b94c3b
Fix conditional
engedaam fb096da
Update tests
engedaam 6d06c8b
Add kubebuilder installation for presubmit
engedaam d39b60d
Fix file name typo
engedaam 0289cab
Add setup-envtest
engedaam 9ab9d62
Clean-up for presubmit
engedaam e7be7d0
Clean-up for presubmit
engedaam cc8fc7d
Adding
engedaam cfb097f
Removed the source queue and pulled object from the channel
engedaam 3e10d24
Removed the source queue and pulled object from the channel
engedaam f8ec3e9
Removed the source queue and pulled object from the channel
engedaam 589d83c
Removed the source queue and pulled object from the channel
engedaam df22a8c
Removed the source queue and pulled object from the channel
engedaam 8025df2
Removed the source queue and pulled object from the channel
engedaam 13bc543
Drop controller-runtime deps
engedaam a4cf242
Drop controller-runtime deps
engedaam daf6a22
Drop controller-runtime deps
engedaam 3a1c475
Drop controller-runtime deps
engedaam 8a1bc5e
Drop controller-runtime deps
engedaam 56c5c39
Drop controller-runtime deps
engedaam 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 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 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"github.com/awslabs/operatorpkg/events" | ||
pmetrics "github.com/awslabs/operatorpkg/metrics" | ||
"github.com/awslabs/operatorpkg/object" | ||
"github.com/awslabs/operatorpkg/singleton" | ||
"github.com/awslabs/operatorpkg/test" | ||
. "github.com/awslabs/operatorpkg/test/expectations" | ||
"github.com/onsi/ginkgo/v2" | ||
|
@@ -19,10 +20,12 @@ import ( | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
clock "k8s.io/utils/clock/testing" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
crfake "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
@@ -38,6 +41,7 @@ var ctx context.Context | |
var fakeClock *clock.FakeClock | ||
var controller *events.Controller[*test.CustomObject] | ||
var kubeClient client.Client | ||
var eventChannel chan watch.Event | ||
|
||
func Test(t *testing.T) { | ||
lo.Must0(SchemeBuilder.AddToScheme(scheme.Scheme)) | ||
|
@@ -46,13 +50,18 @@ func Test(t *testing.T) { | |
} | ||
|
||
var _ = BeforeSuite(func() { | ||
ctx = log.IntoContext(context.Background(), ginkgo.GinkgoLogr) | ||
engedaam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fakeClock = clock.NewFakeClock(time.Now()) | ||
kubeClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithIndex(&corev1.Event{}, "involvedObject.kind", func(o client.Object) []string { | ||
kubeClient = crfake.NewClientBuilder().WithScheme(scheme.Scheme).WithIndex(&corev1.Event{}, "involvedObject.kind", func(o client.Object) []string { | ||
evt := o.(*corev1.Event) | ||
return []string{evt.InvolvedObject.Kind} | ||
}).Build() | ||
controller = events.NewController[*test.CustomObject](kubeClient, fakeClock) | ||
ctx = log.IntoContext(context.Background(), ginkgo.GinkgoLogr) | ||
|
||
client := fake.NewSimpleClientset() | ||
eventChannel = make(chan watch.Event, 1000) | ||
controller = events.NewController[*test.CustomObject](ctx, kubeClient, fakeClock, client) | ||
controller.EventWatchChannel = eventChannel | ||
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. Nice! |
||
}) | ||
|
||
var _ = Describe("Controller", func() { | ||
|
@@ -70,8 +79,11 @@ var _ = Describe("Controller", func() { | |
// expect an metrics for custom object to be zero, waiting on controller reconcile | ||
Expect(GetMetric("operator_customobject_event_total", conditionLabels(fmt.Sprintf("Test-type-%d", i), fmt.Sprintf("Test-reason-%d", i)))).To(BeNil()) | ||
|
||
eventChannel <- watch.Event{ | ||
Object: events[i], | ||
} | ||
// reconcile on the event | ||
_, err := reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(events[i])}) | ||
_, err := singleton.AsReconciler(controller).Reconcile(ctx, reconcile.Request{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// expect an emitted metric to for the event | ||
|
@@ -87,8 +99,11 @@ var _ = Describe("Controller", func() { | |
// expect an metrics for custom object to be zero, waiting on controller reconcile | ||
Expect(GetMetric("operator_ustomobject_event_total", conditionLabels(corev1.EventTypeNormal, "reason"))).To(BeNil()) | ||
|
||
eventChannel <- watch.Event{ | ||
Object: event, | ||
} | ||
// reconcile on the event | ||
_, err := reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) | ||
_, err := singleton.AsReconciler(controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// expect not have an emitted metric to for the event | ||
|
@@ -98,8 +113,11 @@ var _ = Describe("Controller", func() { | |
event.LastTimestamp.Time = time.Now().Add(-30 * time.Minute) | ||
ExpectApplied(ctx, kubeClient, event) | ||
|
||
eventChannel <- watch.Event{ | ||
Object: event, | ||
} | ||
// reconcile on the event | ||
_, err = reconcile.AsReconciler(kubeClient, controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) | ||
_, err = singleton.AsReconciler(controller).Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(event)}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// expect an emitted metric to for the event | ||
|
@@ -110,12 +128,14 @@ var _ = Describe("Controller", func() { | |
func createEvent(name string, eventType string, reason string) *corev1.Event { | ||
return &corev1.Event{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: test.RandomName(), | ||
Name: test.RandomName(), | ||
Namespace: "default", | ||
}, | ||
InvolvedObject: corev1.ObjectReference{ | ||
Namespace: "default", | ||
Name: name, | ||
Kind: object.GVK(&test.CustomObject{}).Kind, | ||
Namespace: "default", | ||
Name: name, | ||
Kind: object.GVK(&test.CustomObject{}).Kind, | ||
APIVersion: object.GVK(&test.CustomObject{}).GroupVersion().String(), | ||
}, | ||
LastTimestamp: metav1.Time{Time: time.Now().Add(30 * time.Minute)}, | ||
Type: eventType, | ||
|
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.
I just took a look at the underlying code and the implementation of the ResultChan uses an unbuffered channel -- that means new writes are going to get blocked on reads here -- I'm concerned that we may block the watch stream if we don't either pull these events into a buffered channel OR use multiple goroutines to process these events.
I'm trying to think of a good way to handle this while keeping the same interface so that we keep the same logging behavior and metrics -- nothing comes to mind immediately, but I think it's something that we should explore. Minimally, we can kick off a goroutine in the register that pulls data off of the watch channel and puts it in a separate buffered channel -- another option is to keep a static cache of events as we see them, enqueue a reconcile.Request and then read from that local cache (this is basically exactly what controller-runtime does with a read cache without the deletions)