/
subnet.go
304 lines (259 loc) · 8.55 KB
/
subnet.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright 2018 ZTE Corporation. All rights reserved.
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 models
import (
"encoding/json"
"k8s.io/client-go/tools/cache"
"github.com/ZTE/Knitter/knitter-manager/err-obj"
"github.com/ZTE/Knitter/knitter-manager/public"
"github.com/ZTE/Knitter/pkg/klog"
)
type AllocationPool struct {
Start string `json:"start"`
End string `json:"end"`
}
type Subnet struct {
ID string `json:"id"`
NetworkID string `json:"network_id"`
Name string `json:"name"`
CIDR string `json:"cidr"`
GatewayIP string `json:"gateway_ip"`
TenantID string `json:"tenant_id"`
AllocPools []AllocationPool `json:"allocation_pools"`
}
func getSubnetsKey() string {
return KnitterManagerKeyRoot + "/subnets"
}
func createSubnetKey(subnetID string) string {
return getSubnetsKey() + "/" + subnetID
}
func SaveSubnet(subnet *Subnet) error {
subnetInBytes, err := json.Marshal(subnet)
if err != nil {
klog.Errorf("SaveSubnet: json.Marshal(network: %v) FAILED, error: %v", subnet, err)
return errobj.ErrMarshalFailed
}
key := createSubnetKey(subnet.ID)
err = common.GetDataBase().SaveLeaf(key, string(subnetInBytes))
if err != nil {
klog.Errorf("SaveSubnet: SaveLeaf(key: %s, value: %s) FAILED, error: %v", key, string(subnetInBytes), err)
return err
}
klog.Infof("SaveSubnet: save network[%v] SUCC", subnet)
return nil
}
func DelSubnet(subnetID string) error {
key := createSubnetKey(subnetID)
err := common.GetDataBase().DeleteLeaf(key)
if err != nil && !IsKeyNotFoundError(err) {
klog.Errorf("DelSubnet: DeleteLeaf(key: %s) FAILED, error: %v", key, err)
return err
}
klog.Infof("DelSubnet: delete subnet[id: %s] SUCC", subnetID)
return nil
}
var UnmarshalSubnet = func(value []byte) (*Subnet, error) {
var subnet Subnet
err := json.Unmarshal([]byte(value), &subnet)
if err != nil {
klog.Errorf("UnmarshalSubnet: json.Unmarshal(%s) FAILED, error: %v", string(value), err)
return nil, errobj.ErrUnmarshalFailed
}
klog.Infof("UnmarshalSubnet: subnet[%v] SUCC", subnet)
return &subnet, nil
}
func GetSubnet(subnetID string) (*Subnet, error) {
key := createSubnetKey(subnetID)
value, err := common.GetDataBase().ReadLeaf(key)
if err != nil {
klog.Errorf("GetSubnet: ReadLeaf(key: %s) FAILED, error: %v", key, err)
return nil, err
}
subnet, err := UnmarshalSubnet([]byte(value))
if err != nil {
klog.Errorf("GetSubnet: UnmarshalSubnet(%v) FAILED, error: %v", value, err)
return nil, errobj.ErrUnmarshalFailed
}
klog.Infof("GetSubnet: get subnet[%v] SUCC", subnet)
return subnet, nil
}
func GetAllSubnets() ([]*Subnet, error) {
key := getSubnetsKey()
nodes, err := common.GetDataBase().ReadDir(key)
if err != nil {
klog.Errorf("GetAllSubnets: ReadDir(key: %s) FAILED, error: %v", key, err)
return nil, err
}
subnets := make([]*Subnet, 0)
for _, node := range nodes {
subnet, err := UnmarshalSubnet([]byte(node.Value))
if err != nil {
klog.Errorf("GetAllSubnets: UnmarshalSubnet(net: %s) FAILED, error: %v", node.Value, err)
return nil, err
}
subnets = append(subnets, subnet)
}
klog.Tracef("GetAllSubnets: get all logical networks: %v SUCC", subnets)
return subnets, nil
}
type SubnetObject struct {
ID string `json:"id"`
NetworkID string `json:"network_id"`
Name string `json:"name"`
CIDR string `json:"cidr"`
GatewayIP string `json:"gateway_ip"`
TenantID string `json:"tenant_id"`
AllocPools []AllocationPool `json:"allocation_pools"`
}
type SubnetObjectRepo struct {
indexer cache.Indexer
}
var subnetObjRepo SubnetObjectRepo
func GetSubnetObjRepoSingleton() *SubnetObjectRepo {
return &subnetObjRepo
}
func SubnetObjKeyFunc(obj interface{}) (string, error) {
if obj == nil {
klog.Error("SunbetObjKeyFunc: obj arg is nil")
return "", errobj.ErrObjectPointerIsNil
}
if subnetObj, ok := obj.(string); ok {
return subnetObj, nil
}
subnetObj, ok := obj.(*SubnetObject)
if !ok {
klog.Error("SunbetObjKeyFunc: obj arg is not type: *SubnetObject")
return "", errobj.ErrArgTypeMismatch
}
return subnetObj.ID, nil
}
func SubnetNetworkIDIndexFunc(obj interface{}) ([]string, error) {
if obj == nil {
klog.Error("SubnetNetworkIDIndexFunc: obj arg is nil")
return nil, errobj.ErrObjectPointerIsNil
}
subnetObj, ok := obj.(*SubnetObject)
if !ok {
klog.Error("SubnetNetworkIDIndexFunc: obj arg is not type: *SubnetObject")
return nil, errobj.ErrArgTypeMismatch
}
return []string{subnetObj.NetworkID}, nil
}
const (
SubnetNetworkIDIndex = "network_id"
)
func (p *SubnetObjectRepo) Init() {
indexers := cache.Indexers{
SubnetNetworkIDIndex: SubnetNetworkIDIndexFunc}
p.indexer = cache.NewIndexer(SubnetObjKeyFunc, indexers)
}
func (p *SubnetObjectRepo) Add(subnetObj *SubnetObject) error {
err := p.indexer.Add(subnetObj)
if err != nil {
klog.Errorf("SubnetObjectRepo.Add: add obj[%v] to repo FAILED, error: %v", subnetObj, err)
return err
}
klog.Infof("SubnetObjectRepo.Add: add obj[%v] to repo SUCC", subnetObj)
return nil
}
func (p *SubnetObjectRepo) Del(ID string) error {
err := p.indexer.Delete(ID)
if err != nil {
klog.Errorf("SubnetObjectRepo.Del: Delete(subnetID: %s) FAILED, error: %v", ID, err)
return err
}
klog.Infof("SubnetObjectRepo.Del: Delete(subnetID: %s) SUCC", ID)
return nil
}
func (p *SubnetObjectRepo) Get(ID string) (*SubnetObject, error) {
item, exists, err := p.indexer.GetByKey(ID)
if err != nil {
klog.Errorf("SubnetObjectRepo.Get: subnetID[%s]'s object FAILED, error: %v", ID, err)
return nil, err
}
if !exists {
klog.Errorf("SubnetObjectRepo.Get: subnetID[%s]'s object not found", ID)
return nil, errobj.ErrRecordNotExist
}
subnetObj, ok := item.(*SubnetObject)
if !ok {
klog.Errorf("SubnetObjectRepo.Get: subnetID[%s]'s object[%v] type not match SubnetObject", ID, item)
return nil, errobj.ErrObjectTypeMismatch
}
klog.Infof("SubnetObjectRepo.Get: subnetID[%s]'s object[%v] SUCC", ID, subnetObj)
return subnetObj, nil
}
func (p *SubnetObjectRepo) Update(subnetObj *SubnetObject) error {
err := p.indexer.Update(subnetObj)
if err != nil {
klog.Errorf("SubnetObjectRepo.Update: Update[%v] FAILED, error: %v", subnetObj, err)
}
klog.Infof("SubnetObjectRepo.Update: Update[%v] SUCC", subnetObj)
return nil
}
func (p *SubnetObjectRepo) listByIndex(indexName, indexKey string) ([]*SubnetObject, error) {
objs, err := p.indexer.ByIndex(indexName, indexKey)
if err != nil {
klog.Errorf("SubnetObjectRepo.listByIndex: ByIndex(indexName: %s, indexKey: %s) FAILED, error: %v",
indexName, indexKey, err)
return nil, err
}
klog.Infof("SubnetObjectRepo.listByIndex: ByIndex(indexName: %s, indexKey: %s) SUCC, array: %v",
indexName, indexKey, objs)
subnetObjs := make([]*SubnetObject, 0)
for _, obj := range objs {
subnetObj, ok := obj.(*SubnetObject)
if !ok {
klog.Errorf("NetworkObjectRepo.listByIndex: index result object: %v is not type *SubnetObject, skip", obj)
continue
}
subnetObjs = append(subnetObjs, subnetObj)
}
return subnetObjs, nil
}
func (p *SubnetObjectRepo) ListByNetworID(networkID string) ([]*SubnetObject, error) {
return p.listByIndex(SubnetNetworkIDIndex, networkID)
}
func init() {
GetSubnetObjRepoSingleton().Init()
}
func TransSubnetToSubnetObject(subnet *Subnet) *SubnetObject {
return &SubnetObject{
ID: subnet.ID,
NetworkID: subnet.NetworkID,
Name: subnet.Name,
CIDR: subnet.CIDR,
GatewayIP: subnet.GatewayIP,
TenantID: subnet.TenantID,
AllocPools: subnet.AllocPools,
}
}
func LoadAllSubnetObjects() error {
subnets, err := GetAllSubnets()
if err != nil {
klog.Errorf("LoadAllSubnetObjects: GetAllSubnets FAILED, error: %v", err)
return err
}
for _, subnet := range subnets {
subnetObj := TransSubnetToSubnetObject(subnet)
err = GetSubnetObjRepoSingleton().Add(subnetObj)
if err != nil {
klog.Errorf("LoadAllSubnetObjects: GetSubnetObjRepoSingleton().Add(subnetObj: %v) FAILED, error: %v",
subnetObj, err)
return err
}
klog.Tracef("LoadAllSubnetObjects: GetSubnetObjRepoSingleton().Add(subnetObj: %v) SUCC",
subnetObj)
}
return nil
}