forked from k8snetworkplumbingwg/multus-cni
/
leveldb.go
208 lines (170 loc) · 4.16 KB
/
leveldb.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
package db
import (
"encoding/json"
"flag"
"fmt"
"github.com/DataWorkbench/multus-cni/pkg/hostnic/constants"
"github.com/DataWorkbench/multus-cni/pkg/hostnic/rpc"
"github.com/DataWorkbench/multus-cni/pkg/logging"
"github.com/syndtr/goleveldb/leveldb"
)
const (
defaultDBPath = "/var/lib/traffic-nic"
)
var (
LevelDB *leveldb.DB
)
type LevelDBOptions struct {
dbpath string
}
func NewLevelDBOptions() *LevelDBOptions {
return &LevelDBOptions{
dbpath: defaultDBPath,
}
}
func (opt *LevelDBOptions) AddFlags() {
flag.StringVar(&opt.dbpath, "dbpath", defaultDBPath, "set leveldb path")
}
func SetupLevelDB(opt *LevelDBOptions) error {
db, err := leveldb.OpenFile(opt.dbpath, nil)
if err != nil {
return fmt.Errorf("cannot open leveldb file %s : %v", opt.dbpath, err)
}
LevelDB = db
return nil
}
func CloseDB() {
err := LevelDB.Close()
if err != nil {
_ = logging.Errorf("failed to close leveldb, err: %v", err)
} else {
logging.Verbosef("leveldb closed")
}
}
func SetNetworkInfo(key string, info *rpc.NICMMessage) error {
value, _ := json.Marshal(info)
return LevelDB.Put([]byte(key), value, nil)
}
func DeleteNetworkInfo(nicID string) error {
err := LevelDB.Delete([]byte(nicID), nil)
if err == leveldb.ErrNotFound {
return constants.ErrNicNotFound
}
return LevelDB.Delete(getPodRefNicKey(nicID), nil)
}
func getPodRefNicKey(nicID string) []byte {
return []byte(constants.PodRefPrefix + nicID)
}
func GetActivePodsByRefNicID(nicID string, podOnNodeMap map[string]bool) ([]string, error) {
key := getPodRefNicKey(nicID)
value, err := LevelDB.Get(key, nil)
if err != nil {
return nil, err
}
podUniNames := []string{}
err = json.Unmarshal(value, &podUniNames)
if err != nil {
return nil, err
}
activePodUniNames := []string{}
for _, _podUniName := range podUniNames {
_, ok := podOnNodeMap[_podUniName]
if ok {
activePodUniNames = append(activePodUniNames, _podUniName)
}
}
newPodUniNameJson, err := json.Marshal(activePodUniNames)
if err != nil {
return nil, err
}
err = LevelDB.Put(key, newPodUniNameJson, nil)
if err != nil {
return nil, err
}
return activePodUniNames, nil
}
func AddRefPodInfo(nicID, podName, namespace string) error {
key := getPodRefNicKey(nicID)
podUniName := GetPodUniName(podName, namespace)
hasKey, err := LevelDB.Has(key, nil)
if err != nil {
return err
}
if !hasKey {
value, err := json.Marshal([]string{podUniName})
if err != nil {
return err
}
return LevelDB.Put(key, value, nil)
}
podInfoJson, err := LevelDB.Get(key, nil)
if err != nil {
return err
}
oldPodUniNames := []string{}
err = json.Unmarshal(podInfoJson, &oldPodUniNames)
if err != nil {
return err
}
for _, _pod := range oldPodUniNames {
if _pod == podUniName {
return nil
}
}
oldPodUniNames = append(oldPodUniNames, podUniName)
value, err := json.Marshal(oldPodUniNames)
if err != nil {
return err
}
return LevelDB.Put(key, value, nil)
}
func GetPodUniName(podName, namespace string) string {
return namespace + "|" + podName
}
// set nic reference pod array empty here
// wait for Sync thread to release Nic resource
func DeleteRefPodInfo(nicID, podName, namespace string) (err error) {
key := getPodRefNicKey(nicID)
hasKey, err := LevelDB.Has(key, nil)
if err != nil {
return
}
if !hasKey {
err = logging.Errorf("No Reference pod found for RefNicKey %s", key)
return
}
podUniName := GetPodUniName(podName, namespace)
podUniNameJson, err := LevelDB.Get(key, nil)
if err != nil {
return
}
oldPodUniNames := []string{}
err = json.Unmarshal(podUniNameJson, &oldPodUniNames)
if err != nil {
return
}
newPodUniNames := []string{}
for _, _pod := range oldPodUniNames {
if _pod != podUniName {
newPodUniNames = append(newPodUniNames, _pod)
}
}
if len(oldPodUniNames) == len(newPodUniNames) {
logging.Verbosef("No valid Nic found for pod %s in DB", podUniName)
return
}
value, err := json.Marshal(newPodUniNames)
if err != nil {
return
}
err = LevelDB.Put(key, value, nil)
return
}
func Iterator(fn func(key, value []byte) error) error {
iter := LevelDB.NewIterator(nil, nil)
for iter.Next() {
_ = fn(iter.Key(), iter.Value())
}
iter.Release()
return iter.Error()
}