-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
129 lines (114 loc) · 3.9 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package nah
import (
"fmt"
"github.com/obot-platform/nah/pkg/backend"
"github.com/obot-platform/nah/pkg/leader"
"github.com/obot-platform/nah/pkg/restconfig"
"github.com/obot-platform/nah/pkg/router"
nruntime "github.com/obot-platform/nah/pkg/runtime"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const defaultHealthzPort = 8888
type Options struct {
// If the backend is nil, then DefaultRESTConfig, DefaultNamespace, and Scheme are used to create a backend.
Backend backend.Backend
// If a Backend is provided, then this is ignored. If not provided and needed, then a default is created with Scheme.
RESTConfig *rest.Config
// If a Backend is provided, then this is ignored.
Namespace string
// If a Backend is provided, then this is ignored.
LabelSelector labels.Selector
// If a Backend is provided, then this is ignored.
FieldSelector fields.Selector
// If a Backend is provided, then this is ignored.
ByObject map[client.Object]cache.ByObject
// If a Backend is provided, then this is ignored.
Scheme *runtime.Scheme
// ElectionConfig being nil represents no leader election for the router.
ElectionConfig *leader.ElectionConfig
// Defaults to 8888
HealthzPort int
// Change the threadedness per GVK
GVKThreadiness map[schema.GroupVersionKind]int
// Split the worker queues for these GVKs
GVKQueueSplitters map[schema.GroupVersionKind]nruntime.WorkerQueueSplitter
}
func (o *Options) complete() (*Options, error) {
var result Options
if o != nil {
result = *o
}
if result.Scheme == nil {
return nil, fmt.Errorf("scheme is required to be set")
}
if result.HealthzPort == 0 {
result.HealthzPort = defaultHealthzPort
}
if result.Backend != nil {
return &result, nil
}
if result.RESTConfig == nil {
var err error
result.RESTConfig, err = restconfig.New(result.Scheme)
if err != nil {
return nil, err
}
}
defaultConfig := nruntime.Config{
Rest: result.RESTConfig,
Namespace: result.Namespace,
LabelSelector: result.LabelSelector,
FieldSelector: result.FieldSelector,
ByObject: result.ByObject,
GVKThreadiness: result.GVKThreadiness,
GVKQueueSplitters: result.GVKQueueSplitters,
}
backend, err := nruntime.NewRuntimeWithConfig(defaultConfig, result.Scheme)
if err != nil {
return nil, err
}
result.Backend = backend.Backend
return &result, nil
}
// DefaultOptions represent the standard options for a Router.
// The default leader election uses a lease lock and a TTL of 15 seconds.
func DefaultOptions(routerName string, scheme *runtime.Scheme) (*Options, error) {
cfg, err := restconfig.New(scheme)
if err != nil {
return nil, err
}
rt, err := nruntime.NewRuntimeForNamespace(cfg, "", scheme)
if err != nil {
return nil, err
}
return &Options{
Backend: rt.Backend,
RESTConfig: cfg,
Scheme: scheme,
ElectionConfig: leader.NewDefaultElectionConfig("", routerName, cfg),
HealthzPort: defaultHealthzPort,
}, nil
}
// DefaultRouter The routerName is important as this name will be used to assign ownership of objects created by this
// router. Specifically, the routerName is assigned to the sub-context in the apply actions. Additionally, the routerName
// will be used for the leader election lease lock.
func DefaultRouter(routerName string, scheme *runtime.Scheme) (*router.Router, error) {
opts, err := DefaultOptions(routerName, scheme)
if err != nil {
return nil, err
}
return NewRouter(routerName, opts)
}
func NewRouter(handlerName string, opts *Options) (*router.Router, error) {
opts, err := opts.complete()
if err != nil {
return nil, err
}
return router.New(router.NewHandlerSet(handlerName, opts.Backend.Scheme(), opts.Backend), opts.ElectionConfig, opts.HealthzPort), nil
}