forked from F5Networks/k8s-bigip-ctlr
/
vxlanMgr.go
296 lines (271 loc) · 7.65 KB
/
vxlanMgr.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
/*-
* Copyright (c) 2017,2018,2019 F5 Networks, Inc.
*
* 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 vxlan
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/F5Networks/k8s-bigip-ctlr/pkg/appmanager"
log "github.com/F5Networks/k8s-bigip-ctlr/pkg/vlogger"
"github.com/F5Networks/k8s-bigip-ctlr/pkg/writer"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type fdbSection struct {
TunnelName string `json:"name"`
Records []fdbRecord `json:"records"`
}
type fdbRecord struct {
Name string `json:"name,omitempty"`
Endpoint string `json:"endpoint"`
}
type arpSection struct {
Entries []arpEntry `json:"arps"`
}
type arpEntry struct {
Name string `json:"name"`
IPAddr string `json:"ipAddress"`
MACAddr string `json:"macAddress"`
}
type VxlanMgr struct {
mode string
vxLAN string
useNodeInt bool
config writer.Writer
podChan <-chan interface{}
}
func NewVxlanMgr(
mode string,
vxLAN string,
useNodeInternal bool,
config writer.Writer,
eventChan <-chan interface{},
) (*VxlanMgr, error) {
if 0 == len(mode) {
return nil, fmt.Errorf("required parameter mode not supplied")
} else if 0 == len(vxLAN) {
return nil, fmt.Errorf("required parameter vxlan not supplied")
} else if nil == config {
return nil, fmt.Errorf("required parameter ConfigWriter not supplied")
}
if "maintain" != mode {
return nil, fmt.Errorf("unsupported mode supplied: %s", mode)
}
vxMgr := &VxlanMgr{
mode: mode,
vxLAN: vxLAN,
useNodeInt: useNodeInternal,
config: config,
podChan: eventChan,
}
return vxMgr, nil
}
func (vxm *VxlanMgr) ProcessNodeUpdate(obj interface{}, err error) {
if nil != err {
log.Warningf("Vxlan manager (%s) unable to get list of nodes: %v",
vxm.vxLAN, err)
return
}
nodes, ok := obj.([]v1.Node)
if false == ok {
log.Warningf("Vxlan manager (%s) received poll update with unexpected type",
vxm.vxLAN)
return
}
var records []fdbRecord
var addrType v1.NodeAddressType
if vxm.useNodeInt {
addrType = v1.NodeInternalIP
} else {
addrType = v1.NodeExternalIP
}
for _, node := range nodes {
// Ignore the Nodes with status NotReady
var notExecutable bool
for _, t := range node.Spec.Taints {
if v1.TaintEffectNoExecute == t.Effect {
notExecutable = true
}
}
if notExecutable == true {
continue
}
nodeAddrs := node.Status.Addresses
rec := fdbRecord{}
for _, addr := range nodeAddrs {
if addr.Type == addrType {
rec.Endpoint = addr.Address
// Initially set the name to a fake MAC (for OpenShift use)
// For flannel, this will be overwritten with the real MAC
rec.Name = ipv4ToMac(addr.Address)
}
}
// Will only exist in Flannel/Kubernetes
if pip, ok := node.ObjectMeta.Annotations["flannel.alpha.coreos.com/public-ip"]; ok {
if rec.Endpoint != pip {
rec.Endpoint = pip
}
}
if atn, ok := node.ObjectMeta.Annotations["flannel.alpha.coreos.com/backend-data"]; ok {
var mac string
mac, err = parseVtepMac(atn, node.ObjectMeta.Name)
if nil != err {
log.Errorf("%v", err)
} else if rec.Endpoint != "" {
rec.Name = mac
}
}
if rec != (fdbRecord{}) {
records = append(records, rec)
}
}
doneCh, errCh, err := vxm.config.SendSection(
"vxlan-fdb",
fdbSection{
TunnelName: vxm.vxLAN,
Records: records,
},
)
if nil != err {
log.Warningf("Vxlan manager (%s) failed to write fdb config section: %v",
vxm.vxLAN, err)
} else {
select {
case <-doneCh:
log.Debugf("Vxlan manager (%s) wrote config section: %v",
vxm.vxLAN, records)
case e := <-errCh:
log.Warningf("Vxlan manager (%s) failed to write config section: %v",
vxm.vxLAN, e)
case <-time.After(time.Second):
log.Warningf("Vxlan manager (%s) did not receive write response in 1s",
vxm.vxLAN)
}
}
}
// Convert an IPV4 string to a fake MAC address.
func ipv4ToMac(addr string) string {
ip := strings.Split(addr, ".")
if len(ip) != 4 {
log.Errorf("Bad IPv4 address format specified for FDB record: %s", addr)
return ""
}
var intIP [4]int
for i, val := range ip {
intIP[i], _ = strconv.Atoi(val)
}
return fmt.Sprintf("0a:0a:%02x:%02x:%02x:%02x", intIP[0], intIP[1], intIP[2], intIP[3])
}
// Listen for updates from appmanager containing pod names (for arp entries)
func (vxm *VxlanMgr) ProcessAppmanagerEvents(kubeClient kubernetes.Interface) {
go func() {
log.Debugf("Vxlan Manager waiting for pod events from appManager.")
for {
select {
case pods := <-vxm.podChan:
if pods, ok := pods.([]appmanager.Member); ok {
vxm.addArpForPods(pods, kubeClient)
} else {
log.Errorf("Vxlan Manager could not read Endpoints from appManager channel.")
}
}
}
}()
return
}
func (vxm *VxlanMgr) addArpForPods(pods interface{}, kubeClient kubernetes.Interface) {
arps := arpSection{}
kubePods, err := kubeClient.CoreV1().Pods("").List(metav1.ListOptions{})
if nil != err {
log.Errorf("Vxlan Manager could not list Kubernetes Pods for ARP entries: %v", err)
return
}
kubeNodes, err := kubeClient.CoreV1().Nodes().List(metav1.ListOptions{})
if nil != err {
log.Errorf("Vxlan Manager could not list Kubernetes Nodes for ARP entries: %v", err)
return
}
for _, pod := range pods.([]appmanager.Member) {
var mac string
mac, err = getVtepMac(pod, kubePods, kubeNodes)
if nil != err {
log.Errorf("%v", err)
return
}
entry := arpEntry{
Name: fmt.Sprintf("k8s-%v", pod.Address),
IPAddr: pod.Address,
MACAddr: mac,
}
arps.Entries = append(arps.Entries, entry)
}
doneCh, errCh, err := vxm.config.SendSection(
"vxlan-arp",
arps,
)
if nil != err {
log.Warningf("Vxlan manager (%s) failed to write arp config section: %v",
vxm.vxLAN, err)
} else {
select {
case <-doneCh:
log.Debugf("Vxlan manager (%s) wrote config section: %v",
vxm.vxLAN, arps)
case e := <-errCh:
log.Warningf("Vxlan manager (%s) failed to write config section: %v",
vxm.vxLAN, e)
case <-time.After(time.Second):
log.Warningf("Vxlan manager (%s) did not receive write response in 1s",
vxm.vxLAN)
}
}
}
// Gets the VtepMac from the Node running this Pod
func getVtepMac(
pod appmanager.Member,
kubePods *v1.PodList,
kubeNodes *v1.NodeList,
) (string, error) {
for _, kPod := range kubePods.Items {
// Found the Pod with this address
if kPod.Status.PodIP == pod.Address {
// Get the Node for this Pod
for _, node := range kubeNodes.Items {
if _, ok := node.ObjectMeta.Annotations["flannel.alpha.coreos.com/public-ip"]; ok &&
node.ObjectMeta.Name == kPod.Spec.NodeName {
if mac, ok :=
node.ObjectMeta.Annotations["flannel.alpha.coreos.com/backend-data"]; ok {
return parseVtepMac(mac, node.ObjectMeta.Name)
}
}
}
}
}
return "", fmt.Errorf("Vxlan manager could not get VtepMac for %s's node.", pod.Address)
}
func parseVtepMac(mac, nodeName string) (string, error) {
split := strings.Split(mac, "\"")
if len(split) < 5 {
err := fmt.Errorf("flannel.alpha.coreos.com/backend-data annotation for "+
"node '%s' has invalid format; cannot validate VtepMac. "+
"Should be of the form: '{\"VtepMAC\":\"<mac>\"}'", nodeName)
return "", err
} else {
return split[3], nil
}
}