Skip to content

Commit 3b7c581

Browse files
committed
enhance: expose GVK-based threadiness to top-level router
This allows callers to set the threadiness based on GVKs. Signed-off-by: Donnie Adams <[email protected]>
1 parent 1d4b332 commit 3b7c581

File tree

6 files changed

+42
-32
lines changed

6 files changed

+42
-32
lines changed

pkg/leader/leader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type ElectionConfig struct {
3030

3131
func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *ElectionConfig {
3232
ttl := defaultLeaderTTL
33-
if os.Getenv("BAAAH_DEV_MODE") != "" {
33+
if os.Getenv("NAH_DEV_MODE") != "" {
3434
ttl = devLeaderTTL
3535
}
3636
return &ElectionConfig{

pkg/log/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ var (
1717
//log.Printf("INFO: "+message+"\n", obj...)
1818
}
1919
Warnf = func(message string, obj ...interface{}) {
20-
log.Printf("WARN [BAAAH]: "+message+"\n", obj...)
20+
log.Printf("WARN [NAH]: "+message+"\n", obj...)
2121
}
2222
Errorf = func(message string, obj ...interface{}) {
23-
log.Printf("ERROR[BAAAH]: "+message+"\n", obj...)
23+
log.Printf("ERROR[NAH]: "+message+"\n", obj...)
2424
}
2525
Fatalf = func(message string, obj ...interface{}) {
26-
log.Fatalf("FATAL[BAAAH]: "+message+"\n", obj...)
26+
log.Fatalf("FATAL[NAH]: "+message+"\n", obj...)
2727
}
2828
Debugf = func(message string, obj ...interface{}) {
2929
//log.Printf("DEBUG: "+message+"\n", obj...)

pkg/runtime/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
var DefaultThreadiness = 5
2525

2626
func init() {
27-
i, _ := strconv.Atoi(os.Getenv("BAAAH_THREADINESS"))
27+
i, _ := strconv.Atoi(os.Getenv("NAH_THREADINESS"))
2828
if i > 0 {
2929
DefaultThreadiness = i
3030
}
@@ -67,7 +67,7 @@ func (b *Backend) start(ctx context.Context, preloadOnly bool) (err error) {
6767
if preloadOnly {
6868
err = b.cacheFactory.Preload(ctx)
6969
} else {
70-
err = b.cacheFactory.Start(ctx, DefaultThreadiness)
70+
err = b.cacheFactory.Start(ctx)
7171
}
7272
if err != nil {
7373
return err

pkg/runtime/clients.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/obot-platform/nah/pkg/mapper"
77
"github.com/obot-platform/nah/pkg/runtime/multi"
88
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
910
"k8s.io/client-go/rest"
1011
"k8s.io/client-go/util/workqueue"
1112
"sigs.k8s.io/controller-runtime/pkg/cache"
@@ -17,23 +18,32 @@ type Runtime struct {
1718
}
1819

1920
type Config struct {
21+
GroupConfig
22+
GVKThreadiness map[schema.GroupVersionKind]int
23+
}
24+
25+
type GroupConfig struct {
2026
Rest *rest.Config
2127
Namespace string
2228
}
2329

2430
func NewRuntime(cfg *rest.Config, scheme *runtime.Scheme) (*Runtime, error) {
25-
return NewRuntimeWithConfig(Config{Rest: cfg}, scheme)
31+
return NewRuntimeWithConfig(Config{
32+
GroupConfig: GroupConfig{
33+
Rest: cfg,
34+
},
35+
}, scheme)
2636
}
2737

2838
func NewRuntimeForNamespace(cfg *rest.Config, namespace string, scheme *runtime.Scheme) (*Runtime, error) {
29-
return NewRuntimeWithConfigs(Config{Rest: cfg, Namespace: namespace}, nil, scheme)
39+
return NewRuntimeWithConfigs(Config{GroupConfig: GroupConfig{Rest: cfg, Namespace: namespace}}, nil, scheme)
3040
}
3141

3242
func NewRuntimeWithConfig(cfg Config, scheme *runtime.Scheme) (*Runtime, error) {
3343
return NewRuntimeWithConfigs(cfg, nil, scheme)
3444
}
3545

36-
func NewRuntimeWithConfigs(defaultConfig Config, apiGroupConfigs map[string]Config, scheme *runtime.Scheme) (*Runtime, error) {
46+
func NewRuntimeWithConfigs(defaultConfig Config, apiGroupConfigs map[string]GroupConfig, scheme *runtime.Scheme) (*Runtime, error) {
3747
clients := make(map[string]client.WithWatch, len(apiGroupConfigs))
3848
cachedClients := make(map[string]client.Client, len(apiGroupConfigs))
3949
caches := make(map[string]cache.Cache, len(apiGroupConfigs))
@@ -49,7 +59,7 @@ func NewRuntimeWithConfigs(defaultConfig Config, apiGroupConfigs map[string]Conf
4959
cachedClients[key] = cachedClient
5060
}
5161

52-
uncachedClient, cachedClient, theCache, err := getClients(defaultConfig, scheme)
62+
uncachedClient, cachedClient, theCache, err := getClients(defaultConfig.GroupConfig, scheme)
5363
if err != nil {
5464
return nil, err
5565
}
@@ -59,7 +69,8 @@ func NewRuntimeWithConfigs(defaultConfig Config, apiGroupConfigs map[string]Conf
5969
aggCache := multi.NewCache(scheme, theCache, caches)
6070

6171
factory := NewSharedControllerFactory(aggUncachedClient, aggCache, &SharedControllerFactoryOptions{
62-
// In baaah this is only invoked when a key fails to process
72+
KindWorkers: defaultConfig.GVKThreadiness,
73+
// In nah this is only invoked when a key fails to process
6374
DefaultRateLimiter: workqueue.NewTypedMaxOfRateLimiter(
6475
// This will go .5, 1, 2, 4, 8 seconds, etc up until 15 minutes
6576
workqueue.NewTypedItemExponentialFailureRateLimiter[any](500*time.Millisecond, 15*time.Minute),
@@ -71,7 +82,7 @@ func NewRuntimeWithConfigs(defaultConfig Config, apiGroupConfigs map[string]Conf
7182
}, nil
7283
}
7384

74-
func getClients(cfg Config, scheme *runtime.Scheme) (uncachedClient client.WithWatch, cachedClient client.Client, theCache cache.Cache, err error) {
85+
func getClients(cfg GroupConfig, scheme *runtime.Scheme) (uncachedClient client.WithWatch, cachedClient client.Client, theCache cache.Cache, err error) {
7586
mapper, err := mapper.New(cfg.Rest)
7687
if err != nil {
7788
return nil, nil, nil, err

pkg/runtime/sharedcontrollerfactory.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type SharedControllerFactory interface {
1414
ForKind(gvk schema.GroupVersionKind) (SharedController, error)
1515
Preload(ctx context.Context) error
16-
Start(ctx context.Context, workers int) error
16+
Start(ctx context.Context) error
1717
}
1818

1919
type SharedControllerFactoryOptions struct {
@@ -58,19 +58,19 @@ func applyDefaultSharedOptions(opts *SharedControllerFactoryOptions) *SharedCont
5858
newOpts = *opts
5959
}
6060
if newOpts.DefaultWorkers == 0 {
61-
newOpts.DefaultWorkers = 5
61+
newOpts.DefaultWorkers = DefaultThreadiness
6262
}
6363
return &newOpts
6464
}
6565
func (s *sharedControllerFactory) Preload(ctx context.Context) error {
66-
return s.start(ctx, 0)
66+
return s.loadAndStart(ctx, false)
6767
}
6868

69-
func (s *sharedControllerFactory) Start(ctx context.Context, defaultWorkers int) error {
70-
return s.start(ctx, defaultWorkers)
69+
func (s *sharedControllerFactory) Start(ctx context.Context) error {
70+
return s.loadAndStart(ctx, true)
7171
}
7272

73-
func (s *sharedControllerFactory) start(ctx context.Context, defaultWorkers int) error {
73+
func (s *sharedControllerFactory) loadAndStart(ctx context.Context, start bool) error {
7474
s.controllerLock.Lock()
7575
defer s.controllerLock.Unlock()
7676

@@ -99,9 +99,9 @@ func (s *sharedControllerFactory) start(ctx context.Context, defaultWorkers int)
9999
s.cache.WaitForCacheSync(ctx)
100100
s.controllerLock.Lock()
101101

102-
if defaultWorkers != 0 {
102+
if start {
103103
for gvk, controller := range controllersCopy {
104-
w, err := s.getWorkers(gvk, defaultWorkers)
104+
w, err := s.getWorkers(gvk)
105105
if err != nil {
106106
return err
107107
}
@@ -150,14 +150,10 @@ func (s *sharedControllerFactory) ForKind(gvk schema.GroupVersionKind) (SharedCo
150150
return controllerResult, nil
151151
}
152152

153-
func (s *sharedControllerFactory) getWorkers(gvk schema.GroupVersionKind, workers int) (int, error) {
154-
w, ok := s.kindWorkers[gvk]
155-
if ok {
153+
func (s *sharedControllerFactory) getWorkers(gvk schema.GroupVersionKind) (int, error) {
154+
if w, ok := s.kindWorkers[gvk]; ok {
156155
return w, nil
157156
}
158-
if workers > 0 {
159-
return workers, nil
160-
}
161157
return s.workers, nil
162158
}
163159

router.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baaah
1+
package nah
22

33
import (
44
"fmt"
@@ -7,8 +7,9 @@ import (
77
"github.com/obot-platform/nah/pkg/leader"
88
"github.com/obot-platform/nah/pkg/restconfig"
99
"github.com/obot-platform/nah/pkg/router"
10-
bruntime "github.com/obot-platform/nah/pkg/runtime"
10+
nruntime "github.com/obot-platform/nah/pkg/runtime"
1111
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
1213
"k8s.io/client-go/rest"
1314
)
1415

@@ -25,11 +26,13 @@ type Options struct {
2526
Scheme *runtime.Scheme
2627
// APIGroupConfigs are keyed by an API group. This indicates to the router that all actions on this group should use the
2728
// given Config. This is useful for routers that watch different objects on different API servers.
28-
APIGroupConfigs map[string]bruntime.Config
29+
APIGroupConfigs map[string]nruntime.GroupConfig
2930
// ElectionConfig being nil represents no leader election for the router.
3031
ElectionConfig *leader.ElectionConfig
3132
// Defaults to 8888
3233
HealthzPort int
34+
// Change the threadedness per GVK
35+
GVKThreadiness map[schema.GroupVersionKind]int
3336
}
3437

3538
func (o *Options) complete() (*Options, error) {
@@ -58,8 +61,8 @@ func (o *Options) complete() (*Options, error) {
5861
}
5962
}
6063

61-
defaultConfig := bruntime.Config{Rest: result.DefaultRESTConfig, Namespace: result.DefaultNamespace}
62-
backend, err := bruntime.NewRuntimeWithConfigs(defaultConfig, result.APIGroupConfigs, result.Scheme)
64+
defaultConfig := nruntime.Config{GroupConfig: nruntime.GroupConfig{Rest: result.DefaultRESTConfig, Namespace: result.DefaultNamespace}, GVKThreadiness: result.GVKThreadiness}
65+
backend, err := nruntime.NewRuntimeWithConfigs(defaultConfig, result.APIGroupConfigs, result.Scheme)
6366
if err != nil {
6467
return nil, err
6568
}
@@ -75,7 +78,7 @@ func DefaultOptions(routerName string, scheme *runtime.Scheme) (*Options, error)
7578
if err != nil {
7679
return nil, err
7780
}
78-
rt, err := bruntime.NewRuntimeForNamespace(cfg, "", scheme)
81+
rt, err := nruntime.NewRuntimeForNamespace(cfg, "", scheme)
7982
if err != nil {
8083
return nil, err
8184
}

0 commit comments

Comments
 (0)