-
Notifications
You must be signed in to change notification settings - Fork 53
/
crds.go
113 lines (99 loc) · 2.46 KB
/
crds.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
package crds
import (
"fmt"
"os"
"time"
"github.com/rancher/opni/apis"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/util"
)
type CRDStore struct {
CRDStoreOptions
client client.WithWatch
logger *zap.SugaredLogger
}
var _ storage.TokenStore = (*CRDStore)(nil)
var _ storage.RBACStore = (*CRDStore)(nil)
var _ storage.KeyringStoreBroker = (*CRDStore)(nil)
var (
defaultBackoff = wait.Backoff{
Steps: 20,
Duration: 10 * time.Millisecond,
Cap: 1 * time.Second,
Factor: 1.5,
Jitter: 0.1,
}
)
type CRDStoreOptions struct {
namespace string
restConfig *rest.Config
}
type CRDStoreOption func(*CRDStoreOptions)
func (o *CRDStoreOptions) apply(opts ...CRDStoreOption) {
for _, op := range opts {
op(o)
}
}
func WithNamespace(ns string) CRDStoreOption {
return func(o *CRDStoreOptions) {
o.namespace = ns
}
}
func WithRestConfig(rc *rest.Config) CRDStoreOption {
return func(o *CRDStoreOptions) {
o.restConfig = rc
}
}
func NewCRDStore(opts ...CRDStoreOption) *CRDStore {
lg := logger.New().Named("crd-store")
options := CRDStoreOptions{
namespace: os.Getenv("POD_NAMESPACE"),
}
options.apply(opts...)
if options.namespace == "" {
lg.Warn("namespace is not set, using \"default\"")
options.namespace = "default"
}
if options.restConfig == nil {
options.restConfig = util.Must(rest.InClusterConfig())
}
s := apis.NewScheme()
return &CRDStore{
CRDStoreOptions: options,
client: util.Must(client.NewWithWatch(options.restConfig, client.Options{
Scheme: s,
})),
logger: lg,
}
}
func (e *CRDStore) KeyringStore(prefix string, ref *corev1.Reference) storage.KeyringStore {
return &crdKeyringStore{
CRDStoreOptions: e.CRDStoreOptions,
client: e.client,
ref: ref,
prefix: prefix,
}
}
func init() {
storage.RegisterStoreBuilder(v1beta1.StorageTypeCRDs, func(args ...any) (any, error) {
var opts []CRDStoreOption
for _, arg := range args {
switch arg := arg.(type) {
case string:
opts = append(opts, WithNamespace(arg))
case *rest.Config:
opts = append(opts, WithRestConfig(arg))
default:
return nil, fmt.Errorf("unexpected argument %v", arg)
}
}
return NewCRDStore(opts...), nil
})
}