-
Notifications
You must be signed in to change notification settings - Fork 434
/
init.go
238 lines (209 loc) · 6.91 KB
/
init.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
package daemon
import (
"context"
"fmt"
"net"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// InitOVSBridges initializes OVS bridges
func InitOVSBridges() (map[string]string, error) {
bridges, err := ovs.Bridges()
if err != nil {
return nil, err
}
mappings := make(map[string]string)
for _, brName := range bridges {
if err = util.SetLinkUp(brName); err != nil {
klog.Error(err)
return nil, err
}
output, err := ovs.Exec("list-ports", brName)
if err != nil {
return nil, fmt.Errorf("failed to list ports of OVS bridge %s, %v: %q", brName, err, output)
}
if output != "" {
for _, port := range strings.Split(output, "\n") {
ok, err := ovs.ValidatePortVendor(port)
if err != nil {
return nil, fmt.Errorf("failed to check vendor of port %s: %v", port, err)
}
if ok {
if _, err = configProviderNic(port, brName); err != nil {
return nil, err
}
mappings[port] = brName
}
}
}
}
return mappings, nil
}
// InitNodeGateway init ovn0
func InitNodeGateway(config *Configuration) error {
var portName, ip, cidr, macAddr, gw, ipAddr string
for {
nodeName := config.NodeName
node, err := config.KubeClient.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get node %s info %v", nodeName, err)
return err
}
if node.Annotations[util.IpAddressAnnotation] == "" {
klog.Warningf("no ovn0 address for node %s, please check kube-ovn-controller logs", nodeName)
time.Sleep(3 * time.Second)
continue
}
if err := util.ValidatePodNetwork(node.Annotations); err != nil {
klog.Errorf("validate node %s address annotation failed, %v", nodeName, err)
time.Sleep(3 * time.Second)
continue
} else {
macAddr = node.Annotations[util.MacAddressAnnotation]
ip = node.Annotations[util.IpAddressAnnotation]
cidr = node.Annotations[util.CidrAnnotation]
portName = node.Annotations[util.PortNameAnnotation]
gw = node.Annotations[util.GatewayAnnotation]
break
}
}
mac, err := net.ParseMAC(macAddr)
if err != nil {
return fmt.Errorf("failed to parse mac %s %v", mac, err)
}
ipAddr = util.GetIpAddrWithMask(ip, cidr)
return configureNodeNic(portName, ipAddr, gw, mac, config.MTU)
}
func InitMirror(config *Configuration) error {
if config.EnableMirror {
return configureGlobalMirror(config.MirrorNic, config.MTU)
}
return configureEmptyMirror(config.MirrorNic, config.MTU)
}
func ovsInitProviderNetwork(provider, nic string, exchangeLinkName bool) (int, error) {
// create and configure external bridge
brName := util.ExternalBridgeName(provider)
if exchangeLinkName {
exchanged, err := changeProvideNicName(nic, brName)
if err != nil {
klog.Errorf("failed to change provider nic name from %s to %s: %v", nic, brName, err)
return 0, err
}
if exchanged {
nic, brName = brName, nic
}
}
if err := configExternalBridge(provider, brName, nic, exchangeLinkName); err != nil {
errMsg := fmt.Errorf("failed to create and configure external bridge %s: %v", brName, err)
klog.Error(errMsg)
return 0, errMsg
}
// init provider chassis mac
if err := initProviderChassisMac(provider); err != nil {
errMsg := fmt.Errorf("failed to init chassis mac for provider %s, %v", provider, err)
klog.Error(errMsg)
return 0, errMsg
}
// add host nic to the external bridge
mtu, err := configProviderNic(nic, brName)
if err != nil {
errMsg := fmt.Errorf("failed to add nic %s to external bridge %s: %v", nic, brName, err)
klog.Error(errMsg)
return 0, errMsg
}
return mtu, nil
}
func ovsCleanProviderNetwork(provider string) error {
output, err := ovs.Exec(ovs.IfExists, "get", "open", ".", "external-ids:ovn-bridge-mappings")
if err != nil {
return fmt.Errorf("failed to get ovn-bridge-mappings, %v: %q", err, output)
}
var idx int
var m, brName string
mappingPrefix := provider + ":"
brMappings := strings.Split(output, ",")
for idx, m = range brMappings {
if strings.HasPrefix(m, mappingPrefix) {
brName = m[len(mappingPrefix):]
break
}
}
if output, err = ovs.Exec("list-br"); err != nil {
return fmt.Errorf("failed to list OVS bridge %v: %q", err, output)
}
if !util.ContainsString(strings.Split(output, "\n"), brName) {
return nil
}
// get host nic
if output, err = ovs.Exec("list-ports", brName); err != nil {
return fmt.Errorf("failed to list ports of OVS bridge %s, %v: %q", brName, err, output)
}
// remove host nic from the external bridge
if output != "" {
for _, port := range strings.Split(output, "\n") {
if err = removeProviderNic(port, brName); err != nil {
errMsg := fmt.Errorf("failed to remove port %s from external bridge %s: %v", port, brName, err)
klog.Error(errMsg)
return errMsg
}
}
}
if idx != len(brMappings) {
brMappings = append(brMappings[:idx], brMappings[idx+1:]...)
if len(brMappings) == 0 {
output, err = ovs.Exec(ovs.IfExists, "remove", "open", ".", "external-ids", "ovn-bridge-mappings")
} else {
output, err = ovs.Exec("set", "open", ".", "external-ids:ovn-bridge-mappings="+strings.Join(brMappings, ","))
}
if err != nil {
return fmt.Errorf("failed to set ovn-bridge-mappings, %v: %q", err, output)
}
}
if output, err = ovs.Exec(ovs.IfExists, "get", "open", ".", "external-ids:ovn-chassis-mac-mappings"); err != nil {
return fmt.Errorf("failed to get ovn-chassis-mac-mappings, %v: %q", err, output)
}
macMappings := strings.Split(output, ",")
for _, macMap := range macMappings {
if strings.HasPrefix(macMap, mappingPrefix) {
macMappings = util.RemoveString(macMappings, macMap)
break
}
}
if len(macMappings) == 0 {
output, err = ovs.Exec(ovs.IfExists, "remove", "open", ".", "external-ids", "ovn-chassis-mac-mappings")
} else {
output, err = ovs.Exec("set", "open", ".", "external-ids:ovn-chassis-mac-mappings="+strings.Join(macMappings, ","))
}
if err != nil {
return fmt.Errorf("failed to set ovn-chassis-mac-mappings, %v: %q", err, output)
}
// get host nic
if output, err = ovs.Exec("list-ports", brName); err != nil {
return fmt.Errorf("failed to list ports of OVS bridge %s, %v: %q", brName, err, output)
}
// remove host nic from the external bridge
if output != "" {
for _, port := range strings.Split(output, "\n") {
if err = removeProviderNic(port, brName); err != nil {
errMsg := fmt.Errorf("failed to remove port %s from external bridge %s: %v", port, brName, err)
klog.Error(errMsg)
return errMsg
}
}
}
// remove OVS bridge
if output, err = ovs.Exec(ovs.IfExists, "del-br", brName); err != nil {
return fmt.Errorf("failed to remove OVS bridge %s, %v: %q", brName, err, output)
}
if br := util.ExternalBridgeName(provider); br != brName {
if _, err = changeProvideNicName(br, brName); err != nil {
klog.Errorf("failed to change provider nic name from %s to %s: %v", br, brName, err)
return err
}
}
return nil
}