/
apistore.go
99 lines (87 loc) · 3.6 KB
/
apistore.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
/*
Copyright 2024 Nokia.
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 ipentry
import (
"context"
builderrest "github.com/henderiw/apiserver-builder/pkg/builder/rest"
"github.com/henderiw/apiserver-store/pkg/generic/registry"
"github.com/henderiw/apiserver-store/pkg/storebackend"
ipambe1v1alpha1 "github.com/kuidio/kuid/apis/backend/ipam/v1alpha1"
"github.com/kuidio/kuid/pkg/backend"
"github.com/kuidio/kuid/pkg/backend/ipam"
"github.com/kuidio/kuid/pkg/kuidserver/store"
"go.opentelemetry.io/otel"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func NewProvider(ctx context.Context, client client.Client, storeConfig *store.Config, be backend.Backend[*ipam.CacheContext]) builderrest.ResourceHandlerProvider {
return func(ctx context.Context, scheme *runtime.Scheme, getter generic.RESTOptionsGetter) (rest.Storage, error) {
return NewREST(ctx, scheme, getter, client, storeConfig, be)
}
}
// NewPackageRevisionREST returns a RESTStorage object that will work against API services.
func NewREST(ctx context.Context, scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, client client.Client, storeConfig *store.Config, be backend.Backend[*ipam.CacheContext]) (rest.Storage, error) {
scheme.AddFieldLabelConversionFunc(
schema.GroupVersionKind{
Group: ipambe1v1alpha1.Group,
Version: ipambe1v1alpha1.Version,
Kind: ipambe1v1alpha1.IPEntryKind,
},
ipambe1v1alpha1.ConvertIPEntryFieldSelector,
)
var configStore storebackend.Storer[runtime.Object]
var err error
switch storeConfig.Type {
case store.StorageType_File:
configStore, err = store.CreateFileStore(ctx, scheme, &ipambe1v1alpha1.IPEntry{}, storeConfig.Prefix)
if err != nil {
return nil, err
}
case store.StorageType_KV:
configStore, err = store.CreateKVStore(ctx, storeConfig.DB, scheme, &ipambe1v1alpha1.IPEntry{})
if err != nil {
return nil, err
}
default:
configStore = store.CreateMemStore(ctx)
}
gr := ipambe1v1alpha1.Resource(ipambe1v1alpha1.IPEntryPlural)
strategy := NewStrategy(ctx, scheme, client, configStore, be)
// This is the etcd store
store := ®istry.Store{
Tracer: otel.Tracer("ipentry-server"),
NewFunc: func() runtime.Object { return &ipambe1v1alpha1.IPEntry{} },
NewListFunc: func() runtime.Object { return &ipambe1v1alpha1.IPEntryList{} },
PredicateFunc: Match,
DefaultQualifiedResource: gr,
SingularQualifiedResource: ipambe1v1alpha1.Resource(ipambe1v1alpha1.IPEntrySingular),
GetStrategy: strategy,
ListStrategy: strategy,
CreateStrategy: strategy,
UpdateStrategy: strategy,
DeleteStrategy: strategy,
WatchStrategy: strategy,
TableConvertor: NewTableConvertor(ipambe1v1alpha1.Resource(ipambe1v1alpha1.IPEntryPlural)),
}
options := &generic.StoreOptions{
RESTOptions: optsGetter,
AttrFunc: GetAttrs,
}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
return store, nil
}