-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
vnids_master.go
319 lines (272 loc) · 8.32 KB
/
vnids_master.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package plugin
import (
"fmt"
"sync"
log "github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
utilwait "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
kapi "k8s.io/kubernetes/pkg/api"
osclient "github.com/openshift/origin/pkg/client"
osapi "github.com/openshift/origin/pkg/sdn/apis/network"
pnetid "github.com/openshift/origin/pkg/sdn/plugin/netid"
)
type masterVNIDMap struct {
// Synchronizes assign, revoke and update VNID
lock sync.Mutex
ids map[string]uint32
netIDManager *pnetid.Allocator
adminNamespaces sets.String
allowRenumbering bool
}
func newMasterVNIDMap(allowRenumbering bool) *masterVNIDMap {
netIDRange, err := pnetid.NewNetIDRange(osapi.MinVNID, osapi.MaxVNID)
if err != nil {
panic(err)
}
return &masterVNIDMap{
netIDManager: pnetid.NewInMemory(netIDRange),
adminNamespaces: sets.NewString(metav1.NamespaceDefault),
ids: make(map[string]uint32),
allowRenumbering: allowRenumbering,
}
}
func (vmap *masterVNIDMap) getVNID(name string) (uint32, bool) {
id, found := vmap.ids[name]
return id, found
}
func (vmap *masterVNIDMap) setVNID(name string, id uint32) {
vmap.ids[name] = id
}
func (vmap *masterVNIDMap) unsetVNID(name string) (uint32, bool) {
id, found := vmap.ids[name]
delete(vmap.ids, name)
return id, found
}
func (vmap *masterVNIDMap) getVNIDCount(id uint32) int {
count := 0
for _, netid := range vmap.ids {
if id == netid {
count = count + 1
}
}
return count
}
func (vmap *masterVNIDMap) isAdminNamespace(nsName string) bool {
if vmap.adminNamespaces.Has(nsName) {
return true
}
return false
}
func (vmap *masterVNIDMap) populateVNIDs(osClient *osclient.Client) error {
netnsList, err := osClient.NetNamespaces().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, netns := range netnsList.Items {
vmap.setVNID(netns.NetName, netns.NetID)
// Skip GlobalVNID, not part of netID allocation range
if netns.NetID == osapi.GlobalVNID {
continue
}
switch err := vmap.netIDManager.Allocate(netns.NetID); err {
case nil: // Expected normal case
case pnetid.ErrAllocated: // Expected when project networks are joined
default:
return fmt.Errorf("unable to allocate netid %d: %v", netns.NetID, err)
}
}
return nil
}
func (vmap *masterVNIDMap) allocateNetID(nsName string) (uint32, bool, error) {
// Nothing to do if the netid is in the vnid map
exists := false
if netid, found := vmap.getVNID(nsName); found {
exists = true
return netid, exists, nil
}
// NetNamespace not found, so allocate new NetID
var netid uint32
if vmap.isAdminNamespace(nsName) {
netid = osapi.GlobalVNID
} else {
var err error
netid, err = vmap.netIDManager.AllocateNext()
if err != nil {
return 0, exists, err
}
}
vmap.setVNID(nsName, netid)
log.Infof("Allocated netid %d for namespace %q", netid, nsName)
return netid, exists, nil
}
func (vmap *masterVNIDMap) releaseNetID(nsName string) error {
// Remove NetID from vnid map
netid, found := vmap.unsetVNID(nsName)
if !found {
return fmt.Errorf("netid not found for namespace %q", nsName)
}
// Skip osapi.GlobalVNID as it is not part of NetID allocation
if netid == osapi.GlobalVNID {
return nil
}
// Check if this netid is used by any other namespaces
// If not, then release the netid
if count := vmap.getVNIDCount(netid); count == 0 {
if err := vmap.netIDManager.Release(netid); err != nil {
return fmt.Errorf("Error while releasing netid %d for namespace %q, %v", netid, nsName, err)
}
log.Infof("Released netid %d for namespace %q", netid, nsName)
} else {
log.V(5).Infof("netid %d for namespace %q is still in use", netid, nsName)
}
return nil
}
func (vmap *masterVNIDMap) updateNetID(nsName string, action osapi.PodNetworkAction, args string) (uint32, error) {
var netid uint32
allocated := false
// Check if the given namespace exists or not
oldnetid, found := vmap.getVNID(nsName)
if !found {
return 0, fmt.Errorf("netid not found for namespace %q", nsName)
}
// Determine new network ID
switch action {
case osapi.GlobalPodNetwork:
netid = osapi.GlobalVNID
case osapi.JoinPodNetwork:
joinNsName := args
var found bool
if netid, found = vmap.getVNID(joinNsName); !found {
return 0, fmt.Errorf("netid not found for namespace %q", joinNsName)
}
case osapi.IsolatePodNetwork:
// Check if the given namespace is already isolated
if count := vmap.getVNIDCount(oldnetid); count == 1 {
return oldnetid, nil
}
var err error
netid, err = vmap.netIDManager.AllocateNext()
if err != nil {
return 0, err
}
allocated = true
default:
return 0, fmt.Errorf("invalid pod network action: %v", action)
}
// Release old network ID
if err := vmap.releaseNetID(nsName); err != nil {
if allocated {
vmap.netIDManager.Release(netid)
}
return 0, err
}
// Set new network ID
vmap.setVNID(nsName, netid)
log.Infof("Updated netid %d for namespace %q", netid, nsName)
return netid, nil
}
// assignVNID, revokeVNID and updateVNID methods updates in-memory structs and persists etcd objects
func (vmap *masterVNIDMap) assignVNID(osClient *osclient.Client, nsName string) error {
vmap.lock.Lock()
defer vmap.lock.Unlock()
netid, exists, err := vmap.allocateNetID(nsName)
if err != nil {
return err
}
if !exists {
// Create NetNamespace Object and update vnid map
netns := &osapi.NetNamespace{
TypeMeta: metav1.TypeMeta{Kind: "NetNamespace"},
ObjectMeta: metav1.ObjectMeta{Name: nsName},
NetName: nsName,
NetID: netid,
}
_, err := osClient.NetNamespaces().Create(netns)
if err != nil {
vmap.releaseNetID(nsName)
return err
}
}
return nil
}
func (vmap *masterVNIDMap) revokeVNID(osClient *osclient.Client, nsName string) error {
vmap.lock.Lock()
defer vmap.lock.Unlock()
// Delete NetNamespace object
if err := osClient.NetNamespaces().Delete(nsName); err != nil {
return err
}
if err := vmap.releaseNetID(nsName); err != nil {
return err
}
return nil
}
func (vmap *masterVNIDMap) updateVNID(osClient *osclient.Client, netns *osapi.NetNamespace) error {
action, args, err := osapi.GetChangePodNetworkAnnotation(netns)
if err == osapi.ErrorPodNetworkAnnotationNotFound {
// Nothing to update
return nil
} else if !vmap.allowRenumbering {
osapi.DeleteChangePodNetworkAnnotation(netns)
_, _ = osClient.NetNamespaces().Update(netns)
return fmt.Errorf("network plugin does not allow NetNamespace renumbering")
}
vmap.lock.Lock()
defer vmap.lock.Unlock()
netid, err := vmap.updateNetID(netns.NetName, action, args)
if err != nil {
return err
}
netns.NetID = netid
osapi.DeleteChangePodNetworkAnnotation(netns)
if _, err := osClient.NetNamespaces().Update(netns); err != nil {
return err
}
return nil
}
//--------------------- Master methods ----------------------
func (master *OsdnMaster) VnidStartMaster() error {
err := master.vnids.populateVNIDs(master.osClient)
if err != nil {
return err
}
master.watchNamespaces()
go utilwait.Forever(master.watchNetNamespaces, 0)
return nil
}
func (master *OsdnMaster) watchNamespaces() {
RegisterSharedInformerEventHandlers(master.informers,
master.handleAddOrUpdateNamespace, master.handleDeleteNamespace, Namespaces)
}
func (master *OsdnMaster) handleAddOrUpdateNamespace(obj, _ interface{}, eventType watch.EventType) {
ns := obj.(*kapi.Namespace)
log.V(5).Infof("Watch %s event for Namespace %q", eventType, ns.Name)
if err := master.vnids.assignVNID(master.osClient, ns.Name); err != nil {
log.Errorf("Error assigning netid: %v", err)
}
}
func (master *OsdnMaster) handleDeleteNamespace(obj interface{}) {
ns := obj.(*kapi.Namespace)
log.V(5).Infof("Watch %s event for Namespace %q", watch.Deleted, ns.Name)
if err := master.vnids.revokeVNID(master.osClient, ns.Name); err != nil {
log.Errorf("Error revoking netid: %v", err)
}
}
func (master *OsdnMaster) watchNetNamespaces() {
RunEventQueue(master.osClient, NetNamespaces, func(delta cache.Delta) error {
netns := delta.Object.(*osapi.NetNamespace)
name := netns.ObjectMeta.Name
log.V(5).Infof("Watch %s event for NetNamespace %q", delta.Type, name)
switch delta.Type {
case cache.Sync, cache.Added, cache.Updated:
err := master.vnids.updateVNID(master.osClient, netns)
if err != nil {
return fmt.Errorf("error updating netid: %v", err)
}
}
return nil
})
}