Skip to content

feat: add AsReconciler wrapper to rate limit and replace controller-runtime's requeue: true behavior #160

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions singleton/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ func Source() source.Source {
},
})
}

// In response to Requeue: True being deprecated via: https://github.com/kubernetes-sigs/controller-runtime/pull/3107/files
// This uses a bucket and per item delay but the item will be the same because the key is the controller name..
// This implements the same behavior as Requeue: True.
type SingletonRateLimiter struct {
rateLimiter workqueue.TypedRateLimiter[string]
key string
}

func NewSingletonRateLimiter(controllerName string) *SingletonRateLimiter {
return &SingletonRateLimiter{
rateLimiter: workqueue.DefaultTypedControllerRateLimiter[string](),
key: controllerName,
}
}

// Delay requeues the item according to the rate limiter.
// Used like "RequeueAfter: controller.RateLimiter.Delay()"
func (s *SingletonRateLimiter) Delay() time.Duration {
return s.rateLimiter.When(s.key)
}
33 changes: 33 additions & 0 deletions singleton/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package singleton_test

import (
"testing"

"github.com/awslabs/operatorpkg/singleton"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func Test(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Singleton")
}

var _ = Describe("SingletonRateLimiter", func() {
var rateLimiter *singleton.SingletonRateLimiter

BeforeEach(func() {
rateLimiter = singleton.NewSingletonRateLimiter("test-controller")
})

It("should return increasing delays on subsequent calls", func() {
firstDelay := rateLimiter.Delay()
Expect(firstDelay).To(BeNumerically(">=", 0))

secondDelay := rateLimiter.Delay()
Expect(secondDelay).To(BeNumerically(">=", firstDelay))

thirdDelay := rateLimiter.Delay()
Expect(thirdDelay).To(BeNumerically(">=", secondDelay))
})
})