Skip to content

Commit

Permalink
feat: add controller builder options and funcs (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinso authored Jul 3, 2023
1 parent 73a542a commit 85f2924
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 0 deletions.
62 changes: 62 additions & 0 deletions controllers/builderoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2023 The Katanomi 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 controllers

import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
)

// DefaultMaxConcurrentReconciles is the default number of max concurrent reconciles
const DefaultMaxConcurrentReconciles = 10

// BuilderOptionFunc is a function that can be used to configure the controller builder options
type BuilderOptionFunc func(options controller.Options) controller.Options

// BuilderOptions returns a functional set of options with conservative
// defaults.
func BuilderOptions(opts ...BuilderOptionFunc) controller.Options {
options := DefaultOptions()
for _, opt := range opts {
options = opt(options)
}
return options
}

// DefaultOptions returns the default options for the controller
func DefaultOptions() controller.Options {
return controller.Options{
MaxConcurrentReconciles: DefaultMaxConcurrentReconciles,
RateLimiter: DefaultRateLimiter(),
}
}

// MaxConCurrentReconciles sets the max concurrent reconciles
func MaxConCurrentReconciles(num int) BuilderOptionFunc {
return func(options controller.Options) controller.Options {
options.MaxConcurrentReconciles = num
return options
}
}

// RateLimiter sets the rate limiter
func RateLimiter(rl ratelimiter.RateLimiter) BuilderOptionFunc {
return func(options controller.Options) controller.Options {
options.RateLimiter = rl
return options
}
}
65 changes: 65 additions & 0 deletions controllers/builderoptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 The Katanomi 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 controllers

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

var _ = Describe("Test.BuilderOptions", func() {
var (
opts controller.Options
buildOptFuns []BuilderOptionFunc
)

BeforeEach(func() {
opts = DefaultOptions()
})
JustBeforeEach(func() {
opts = BuilderOptions(buildOptFuns...)
})

Context("BuilderOptions empty", func() {
It("should return default options", func() {
Expect(opts).To(Equal(DefaultOptions()))
})
})

Context("builderoptions with custom rate limiter", func() {
customRateLimiter := workqueue.NewMaxOfRateLimiter()

BeforeEach(func() {
buildOptFuns = append(buildOptFuns, RateLimiter(customRateLimiter))
})
It("should return options with custom rateLimiter", func() {
Expect(opts.RateLimiter).To(Equal(customRateLimiter))
})
})

Context("builderoptions with custom maxConcurrentReconciles", func() {
BeforeEach(func() {
buildOptFuns = append(buildOptFuns, MaxConCurrentReconciles(100))
})
It("should return options with custom rateLimiter", func() {
Expect(opts.MaxConcurrentReconciles).To(Equal(100))
})
})

})
41 changes: 41 additions & 0 deletions controllers/controllerbuilderoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 The Katanomi 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 controllers

import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

// ControllerBuilderOption is a function that can be used to configure the controller builder
type ControllerBuilderOption func(*builder.Builder) *builder.Builder

// ApplyControllerBuilderOptions applies the given options to the controller builder
func ApplyControllerBuilderOptions(builder *builder.Builder, funcs ...ControllerBuilderOption) *builder.Builder {
for _, builderFunc := range funcs {
builder = builderFunc(builder)
}
return builder
}

// WithBuilderOptions returns a ControllerBuilderOption that applies the given options to the controller builder
func WithBuilderOptions(opts controller.Options) ControllerBuilderOption {
return func(b *builder.Builder) *builder.Builder {
b = b.WithOptions(opts)
return b
}
}
44 changes: 44 additions & 0 deletions controllers/controllerbuilderoptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 The Katanomi 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 controllers

import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Test.ApplyControllerBuilderOptions", func() {
builder := &builder.Builder{}
builderFuncs := make([]ControllerBuilderOption, 0)

JustBeforeEach(func() {
ApplyControllerBuilderOptions(builder, builderFuncs...)
})

BeforeEach(func() {
builderFuncs = append(builderFuncs, WithBuilderOptions(controller.Options{
RateLimiter: DefaultRateLimiter(),
}))
})

It("builds options", func() {
Expect(builder).NotTo(BeNil())
})
})
29 changes: 29 additions & 0 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2023 The Katanomi 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 controllers

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controllers Suite")
}

0 comments on commit 85f2924

Please sign in to comment.