-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
vnids_node.go
205 lines (176 loc) · 5.24 KB
/
vnids_node.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
package plugin
import (
"fmt"
"sync"
"time"
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/client-go/tools/cache"
osclient "github.com/openshift/origin/pkg/client"
osapi "github.com/openshift/origin/pkg/sdn/apis/network"
)
type nodeVNIDMap struct {
policy osdnPolicy
osClient *osclient.Client
// Synchronizes add or remove ids/namespaces
lock sync.Mutex
ids map[string]uint32
mcEnabled map[string]bool
namespaces map[uint32]sets.String
}
func newNodeVNIDMap(policy osdnPolicy, osClient *osclient.Client) *nodeVNIDMap {
return &nodeVNIDMap{
policy: policy,
osClient: osClient,
ids: make(map[string]uint32),
mcEnabled: make(map[string]bool),
namespaces: make(map[uint32]sets.String),
}
}
func (vmap *nodeVNIDMap) addNamespaceToSet(name string, vnid uint32) {
set, found := vmap.namespaces[vnid]
if !found {
set = sets.NewString()
vmap.namespaces[vnid] = set
}
set.Insert(name)
}
func (vmap *nodeVNIDMap) removeNamespaceFromSet(name string, vnid uint32) {
if set, found := vmap.namespaces[vnid]; found {
set.Delete(name)
if set.Len() == 0 {
delete(vmap.namespaces, vnid)
}
}
}
func (vmap *nodeVNIDMap) GetNamespaces(id uint32) []string {
vmap.lock.Lock()
defer vmap.lock.Unlock()
if set, ok := vmap.namespaces[id]; ok {
return set.List()
} else {
return nil
}
}
func (vmap *nodeVNIDMap) GetMulticastEnabled(id uint32) bool {
vmap.lock.Lock()
defer vmap.lock.Unlock()
set, exists := vmap.namespaces[id]
if !exists || set.Len() == 0 {
return false
}
for _, ns := range set.List() {
if !vmap.mcEnabled[ns] {
return false
}
}
return true
}
// Nodes asynchronously watch for both NetNamespaces and services
// NetNamespaces populates vnid map and services/pod-setup depend on vnid map
// If for some reason, vnid map propagation from master to node is slow
// and if service/pod-setup tries to lookup vnid map then it may fail.
// So, use this method to alleviate this problem. This method will
// retry vnid lookup before giving up.
func (vmap *nodeVNIDMap) WaitAndGetVNID(name string) (uint32, error) {
var id uint32
backoff := utilwait.Backoff{
Duration: 100 * time.Millisecond,
Factor: 1.5,
Steps: 5,
}
err := utilwait.ExponentialBackoff(backoff, func() (bool, error) {
var err error
id, err = vmap.getVNID(name)
return err == nil, nil
})
if err == nil {
return id, nil
} else {
return 0, fmt.Errorf("failed to find netid for namespace: %s in vnid map", name)
}
}
func (vmap *nodeVNIDMap) getVNID(name string) (uint32, error) {
vmap.lock.Lock()
defer vmap.lock.Unlock()
if id, ok := vmap.ids[name]; ok {
return id, nil
}
return 0, fmt.Errorf("failed to find netid for namespace: %s in vnid map", name)
}
func (vmap *nodeVNIDMap) setVNID(name string, id uint32, mcEnabled bool) {
vmap.lock.Lock()
defer vmap.lock.Unlock()
if oldId, found := vmap.ids[name]; found {
vmap.removeNamespaceFromSet(name, oldId)
}
vmap.ids[name] = id
vmap.mcEnabled[name] = mcEnabled
vmap.addNamespaceToSet(name, id)
log.Infof("Associate netid %d to namespace %q with mcEnabled %v", id, name, mcEnabled)
}
func (vmap *nodeVNIDMap) unsetVNID(name string) (id uint32, err error) {
vmap.lock.Lock()
defer vmap.lock.Unlock()
id, found := vmap.ids[name]
if !found {
return 0, fmt.Errorf("failed to find netid for namespace: %s in vnid map", name)
}
vmap.removeNamespaceFromSet(name, id)
delete(vmap.ids, name)
delete(vmap.mcEnabled, name)
log.Infof("Dissociate netid %d from namespace %q", id, name)
return id, nil
}
func netnsIsMulticastEnabled(netns *osapi.NetNamespace) bool {
enabled, ok := netns.Annotations[osapi.MulticastEnabledAnnotation]
return enabled == "true" && ok
}
func (vmap *nodeVNIDMap) populateVNIDs() error {
nets, err := vmap.osClient.NetNamespaces().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, net := range nets.Items {
vmap.setVNID(net.Name, net.NetID, netnsIsMulticastEnabled(&net))
}
return nil
}
func (vmap *nodeVNIDMap) Start() error {
// Populate vnid map synchronously so that existing services can fetch vnid
err := vmap.populateVNIDs()
if err != nil {
return err
}
go utilwait.Forever(vmap.watchNetNamespaces, 0)
return nil
}
func (vmap *nodeVNIDMap) watchNetNamespaces() {
RunEventQueue(vmap.osClient, NetNamespaces, func(delta cache.Delta) error {
netns := delta.Object.(*osapi.NetNamespace)
log.V(5).Infof("Watch %s event for NetNamespace %q", delta.Type, netns.ObjectMeta.Name)
switch delta.Type {
case cache.Sync, cache.Added, cache.Updated:
// Skip this event if nothing has changed
oldNetID, err := vmap.getVNID(netns.NetName)
oldMCEnabled := vmap.mcEnabled[netns.NetName]
mcEnabled := netnsIsMulticastEnabled(netns)
if err == nil && oldNetID == netns.NetID && oldMCEnabled == mcEnabled {
break
}
vmap.setVNID(netns.NetName, netns.NetID, mcEnabled)
if delta.Type == cache.Added {
vmap.policy.AddNetNamespace(netns)
} else {
vmap.policy.UpdateNetNamespace(netns, oldNetID)
}
case cache.Deleted:
// Unset VNID first so further operations don't see the deleted VNID
vmap.unsetVNID(netns.NetName)
vmap.policy.DeleteNetNamespace(netns)
}
return nil
})
}