forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
271 lines (232 loc) · 6.6 KB
/
local.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
// Copyright 2016-2017 Authors of Cilium
//
// 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 kvstore
import (
"encoding/json"
"fmt"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/cilium/cilium/common"
"github.com/cilium/cilium/common/types"
"github.com/cilium/cilium/pkg/policy"
log "github.com/Sirupsen/logrus"
)
type LocalClient struct {
lock sync.RWMutex // lock protects the `store` map
store map[string]string
}
type LocalLocker struct {
}
func NewLocalClient() KVClient {
return &LocalClient{store: map[string]string{}}
}
func (l *LocalClient) LockPath(path string) (KVLocker, error) {
return &LocalLocker{}, nil
}
func (l *LocalLocker) Unlock() error {
return nil
}
func (l *LocalClient) GetValue(k string) (json.RawMessage, error) {
l.lock.RLock()
defer l.lock.RUnlock()
if v, ok := l.store[k]; ok {
return json.RawMessage(v), nil
}
return nil, nil
}
func (l *LocalClient) SetValue(k string, v interface{}) error {
vByte, err := json.Marshal(v)
if err != nil {
return err
}
l.lock.Lock()
l.store[k] = string(vByte)
l.lock.Unlock()
return nil
}
func (l *LocalClient) InitializeFreeID(path string, firstID uint32) error {
kvLocker, _ := l.LockPath(path)
defer kvLocker.Unlock()
k, _ := l.GetValue(path)
if k != nil {
return nil
}
if err := l.SetValue(path, firstID); err != nil {
return err
}
return nil
}
func (l *LocalClient) GetMaxID(key string, firstID uint32) (uint32, error) {
var (
attempts = 3
value json.RawMessage
err error
freeID uint32
)
for {
switch value, err = l.GetValue(key); {
case attempts == 0:
err = fmt.Errorf("Unable to retrieve last free ID because key is always empty")
log.Error(err)
fallthrough
case value == nil:
log.Debugf("Empty FreeID, setting it up with default value %d", firstID)
if err := l.InitializeFreeID(key, firstID); err != nil {
return 0, err
}
attempts--
case err == nil:
if err := json.Unmarshal(value, &freeID); err != nil {
return 0, err
}
log.Debugf("Retrieving max free ID %d", freeID)
return freeID, nil
}
}
}
func (l *LocalClient) SetMaxID(key string, firstID, maxID uint32) error {
value, _ := l.GetValue(key)
if value == nil {
// FreeID is empty? We should set it out!
log.Debugf("Empty FreeID, setting it up with default value %d", firstID)
if err := l.InitializeFreeID(key, firstID); err != nil {
return err
}
k, _ := l.GetValue(key)
if k == nil {
// Something is really wrong
errMsg := "Unable to setting ID because the key is always empty\n"
log.Errorf(errMsg)
return fmt.Errorf(errMsg)
}
}
return l.SetValue(key, maxID)
}
func (l *LocalClient) updateSecLabelIDRef(id policy.Identity) error {
key := path.Join(common.LabelIDKeyPath, strconv.FormatUint(uint64(id.ID), 10))
return l.SetValue(key, id)
}
func (l *LocalClient) setMaxLabelID(maxID uint32) error {
return l.SetMaxID(common.LastFreeLabelIDKeyPath, policy.MinimalNumericIdentity.Uint32(), maxID)
}
func (l *LocalClient) GASNewSecLabelID(basePath string, baseID uint32, pI *policy.Identity) error {
setID2Label := func(new_id uint32) error {
pI.ID = policy.NumericIdentity(new_id)
keyPath := path.Join(basePath, pI.ID.StringID())
if err := l.SetValue(keyPath, pI); err != nil {
return err
}
return l.setMaxLabelID(new_id + 1)
}
acquireFreeID := func(firstID uint32, incID *uint32) (bool, error) {
log.Debugf("Trying to acquire a new free ID %d", *incID)
keyPath := path.Join(basePath, strconv.FormatUint(uint64(*incID), 10))
locker, _ := l.LockPath(GetLockPath(keyPath))
defer locker.Unlock()
value, _ := l.GetValue(keyPath)
if value == nil {
return false, setID2Label(*incID)
}
var consulLabels policy.Identity
if err := json.Unmarshal(value, &consulLabels); err != nil {
return false, err
}
if consulLabels.RefCount() == 0 {
log.Infof("Recycling ID %d", *incID)
return false, setID2Label(*incID)
}
*incID++
if *incID > common.MaxSetOfLabels {
*incID = policy.MinimalNumericIdentity.Uint32()
}
if firstID == *incID {
return false, fmt.Errorf("reached maximum set of labels available.")
}
return true, nil
}
beginning := baseID
for {
retry, err := acquireFreeID(beginning, &baseID)
if err != nil {
return err
} else if !retry {
return nil
}
}
}
func (l *LocalClient) setMaxL3n4AddrID(maxID uint32) error {
return l.SetMaxID(common.LastFreeServiceIDKeyPath, common.FirstFreeServiceID, maxID)
}
func (l *LocalClient) GASNewL3n4AddrID(basePath string, baseID uint32, lAddrID *types.L3n4AddrID) error {
setIDtoL3n4Addr := func(id uint32) error {
lAddrID.ID = types.ServiceID(id)
keyPath := path.Join(basePath, strconv.FormatUint(uint64(lAddrID.ID), 10))
if err := l.SetValue(keyPath, lAddrID); err != nil {
return err
}
return l.setMaxL3n4AddrID(id + 1)
}
acquireFreeID := func(firstID uint32, incID *uint32) error {
log.Debugf("Trying to acquire a new free ID %d", *incID)
keyPath := path.Join(basePath, strconv.FormatUint(uint64(*incID), 10))
locker, _ := l.LockPath(GetLockPath(keyPath))
defer locker.Unlock()
value, _ := l.GetValue(keyPath)
if value == nil {
return setIDtoL3n4Addr(*incID)
}
var consulL3n4AddrID types.L3n4AddrID
if err := json.Unmarshal(value, &consulL3n4AddrID); err != nil {
return err
}
if consulL3n4AddrID.ID == 0 {
log.Infof("Recycling Service ID %d", *incID)
return setIDtoL3n4Addr(*incID)
}
*incID++
if *incID > common.MaxSetOfServiceID {
*incID = common.FirstFreeServiceID
}
if firstID == *incID {
return fmt.Errorf("reached maximum set of serviceIDs available.")
}
return nil
}
var err error
beginning := baseID
for {
if err = acquireFreeID(beginning, &baseID); err != nil {
return err
} else if beginning == baseID {
return nil
}
}
}
func (l *LocalClient) DeleteTree(path string) error {
for k := range l.store {
if strings.HasPrefix(k, path) {
delete(l.store, k)
}
}
return nil
}
func (l *LocalClient) GetWatcher(key string, timeSleep time.Duration) <-chan []policy.NumericIdentity {
return make(chan []policy.NumericIdentity, 1)
}
func (l *LocalClient) Status() (string, error) {
return "Local: OK", nil
}