forked from projectcalico/calico
/
ipreservation_storage.go
112 lines (100 loc) · 4.58 KB
/
ipreservation_storage.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
// Copyright (c) 2017-2019 Tigera, Inc. All rights reserved.
package calico
import (
"reflect"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
aapi "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
api "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
"github.com/dtest11/calico/libcalico-go/lib/clientv3"
"github.com/dtest11/calico/libcalico-go/lib/options"
"github.com/dtest11/calico/libcalico-go/lib/watch"
)
// NewIPReservationStorage creates a new libcalico-based storage.Interface implementation for IPReservations
func NewIPReservationStorage(opts Options) (registry.DryRunnableStorage, factory.DestroyFunc) {
c := CreateClientFromConfig()
createFn := func(ctx context.Context, c clientv3.Interface, obj resourceObject, opts clientOpts) (resourceObject, error) {
oso := opts.(options.SetOptions)
res := obj.(*api.IPReservation)
return c.IPReservations().Create(ctx, res, oso)
}
updateFn := func(ctx context.Context, c clientv3.Interface, obj resourceObject, opts clientOpts) (resourceObject, error) {
oso := opts.(options.SetOptions)
res := obj.(*api.IPReservation)
return c.IPReservations().Update(ctx, res, oso)
}
getFn := func(ctx context.Context, c clientv3.Interface, ns string, name string, opts clientOpts) (resourceObject, error) {
ogo := opts.(options.GetOptions)
return c.IPReservations().Get(ctx, name, ogo)
}
deleteFn := func(ctx context.Context, c clientv3.Interface, ns string, name string, opts clientOpts) (resourceObject, error) {
odo := opts.(options.DeleteOptions)
return c.IPReservations().Delete(ctx, name, odo)
}
listFn := func(ctx context.Context, c clientv3.Interface, opts clientOpts) (resourceListObject, error) {
olo := opts.(options.ListOptions)
return c.IPReservations().List(ctx, olo)
}
watchFn := func(ctx context.Context, c clientv3.Interface, opts clientOpts) (watch.Interface, error) {
olo := opts.(options.ListOptions)
return c.IPReservations().Watch(ctx, olo)
}
dryRunnableStorage := registry.DryRunnableStorage{Storage: &resourceStore{
client: c,
codec: opts.RESTOptions.StorageConfig.Codec,
versioner: APIObjectVersioner{},
aapiType: reflect.TypeOf(aapi.IPReservation{}),
aapiListType: reflect.TypeOf(aapi.IPReservationList{}),
libCalicoType: reflect.TypeOf(api.IPReservation{}),
libCalicoListType: reflect.TypeOf(api.IPReservationList{}),
isNamespaced: false,
create: createFn,
update: updateFn,
get: getFn,
delete: deleteFn,
list: listFn,
watch: watchFn,
resourceName: "IPReservation",
converter: IPReservationConverter{},
}, Codec: opts.RESTOptions.StorageConfig.Codec}
return dryRunnableStorage, func() {}
}
type IPReservationConverter struct {
}
func (gc IPReservationConverter) convertToLibcalico(aapiObj runtime.Object) resourceObject {
aapiIPReservation := aapiObj.(*aapi.IPReservation)
lcgIPReservation := &api.IPReservation{}
lcgIPReservation.TypeMeta = aapiIPReservation.TypeMeta
lcgIPReservation.ObjectMeta = aapiIPReservation.ObjectMeta
lcgIPReservation.Kind = api.KindIPReservation
lcgIPReservation.APIVersion = api.GroupVersionCurrent
lcgIPReservation.Spec = aapiIPReservation.Spec
return lcgIPReservation
}
func (gc IPReservationConverter) convertToAAPI(libcalicoObject resourceObject, aapiObj runtime.Object) {
lcgIPReservation := libcalicoObject.(*api.IPReservation)
aapiIPReservation := aapiObj.(*aapi.IPReservation)
aapiIPReservation.Spec = lcgIPReservation.Spec
aapiIPReservation.TypeMeta = lcgIPReservation.TypeMeta
aapiIPReservation.ObjectMeta = lcgIPReservation.ObjectMeta
}
func (gc IPReservationConverter) convertToAAPIList(libcalicoListObject resourceListObject, aapiListObj runtime.Object, pred storage.SelectionPredicate) {
lcgIPReservationList := libcalicoListObject.(*api.IPReservationList)
aapiIPReservationList := aapiListObj.(*aapi.IPReservationList)
if libcalicoListObject == nil {
aapiIPReservationList.Items = []aapi.IPReservation{}
return
}
aapiIPReservationList.TypeMeta = lcgIPReservationList.TypeMeta
aapiIPReservationList.ListMeta = lcgIPReservationList.ListMeta
for _, item := range lcgIPReservationList.Items {
aapiIPReservation := aapi.IPReservation{}
gc.convertToAAPI(&item, &aapiIPReservation)
if matched, err := pred.Matches(&aapiIPReservation); err == nil && matched {
aapiIPReservationList.Items = append(aapiIPReservationList.Items, aapiIPReservation)
}
}
}