-
Notifications
You must be signed in to change notification settings - Fork 442
/
init.go
440 lines (393 loc) · 13.3 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package controller
import (
"context"
"fmt"
"strings"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func (c *Controller) InitOVN() error {
if err := c.initClusterRouter(); err != nil {
klog.Errorf("init cluster router failed %v", err)
return err
}
if err := c.initLoadBalancer(); err != nil {
klog.Errorf("init load balancer failed %v", err)
return err
}
if err := c.initDefaultVlan(); err != nil {
klog.Errorf("init default vlan failed %v", err)
return err
}
if err := c.initNodeSwitch(); err != nil {
klog.Errorf("init node switch failed %v", err)
return err
}
if err := c.initDefaultLogicalSwitch(); err != nil {
klog.Errorf("init default switch failed %v", err)
return err
}
return nil
}
func (c *Controller) InitDefaultVpc() error {
vpc, err := c.vpcsLister.Get(util.DefaultVpc)
if err != nil {
vpc = &kubeovnv1.Vpc{}
vpc.Name = util.DefaultVpc
vpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), vpc, metav1.CreateOptions{})
if err != nil {
klog.Errorf("init default vpc failed %v", err)
return err
}
}
vpc.Status.DefaultLogicalSwitch = c.config.DefaultLogicalSwitch
vpc.Status.Router = c.config.ClusterRouter
vpc.Status.TcpLoadBalancer = c.config.ClusterTcpLoadBalancer
vpc.Status.TcpSessionLoadBalancer = c.config.ClusterTcpSessionLoadBalancer
vpc.Status.UdpLoadBalancer = c.config.ClusterUdpLoadBalancer
vpc.Status.UdpSessionLoadBalancer = c.config.ClusterUdpSessionLoadBalancer
vpc.Status.Standby = true
vpc.Status.Default = true
bytes, err := vpc.Status.Bytes()
if err != nil {
return err
}
_, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Patch(context.Background(), vpc.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
if err != nil {
klog.Errorf("init default vpc failed %v", err)
return err
}
return nil
}
// InitDefaultLogicalSwitch init the default logical switch for ovn network
func (c *Controller) initDefaultLogicalSwitch() error {
subnet, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.DefaultLogicalSwitch, metav1.GetOptions{})
if err == nil {
if subnet != nil && util.CheckProtocol(c.config.DefaultCIDR) != util.CheckProtocol(subnet.Spec.CIDRBlock) {
// single-stack upgrade to dual-stack
if util.CheckProtocol(c.config.DefaultCIDR) == kubeovnv1.ProtocolDual {
subnet.Spec.CIDRBlock = c.config.DefaultCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed %v", c.config.DefaultLogicalSwitch, err)
return err
}
}
}
return nil
}
if !k8serrors.IsNotFound(err) {
klog.Errorf("get default subnet %s failed %v", c.config.DefaultLogicalSwitch, err)
return err
}
defaultSubnet := kubeovnv1.Subnet{
ObjectMeta: metav1.ObjectMeta{Name: c.config.DefaultLogicalSwitch},
Spec: kubeovnv1.SubnetSpec{
Vpc: util.DefaultVpc,
Default: true,
Provider: util.OvnProvider,
CIDRBlock: c.config.DefaultCIDR,
Gateway: c.config.DefaultGateway,
ExcludeIps: strings.Split(c.config.DefaultExcludeIps, ","),
NatOutgoing: true,
GatewayType: kubeovnv1.GWDistributedType,
Protocol: util.CheckProtocol(c.config.DefaultCIDR),
},
}
if c.config.NetworkType == util.NetworkTypeVlan {
defaultSubnet.Spec.Vlan = c.config.DefaultVlanName
defaultSubnet.Spec.UnderlayGateway = true
}
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Create(context.Background(), &defaultSubnet, metav1.CreateOptions{})
return err
}
// InitNodeSwitch init node switch to connect host and pod
func (c *Controller) initNodeSwitch() error {
subnet, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.NodeSwitch, metav1.GetOptions{})
if err == nil {
if subnet != nil && util.CheckProtocol(c.config.NodeSwitchCIDR) != util.CheckProtocol(subnet.Spec.CIDRBlock) {
// single-stack upgrade to dual-stack
if util.CheckProtocol(c.config.NodeSwitchCIDR) == kubeovnv1.ProtocolDual {
subnet.Spec.CIDRBlock = c.config.NodeSwitchCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed %v", c.config.NodeSwitch, err)
return err
}
}
}
return nil
}
if !k8serrors.IsNotFound(err) {
klog.Errorf("get node subnet %s failed %v", c.config.NodeSwitch, err)
return err
}
nodeSubnet := kubeovnv1.Subnet{
ObjectMeta: metav1.ObjectMeta{Name: c.config.NodeSwitch},
Spec: kubeovnv1.SubnetSpec{
Vpc: util.DefaultVpc,
Default: false,
Provider: util.OvnProvider,
CIDRBlock: c.config.NodeSwitchCIDR,
Gateway: c.config.NodeSwitchGateway,
ExcludeIps: strings.Split(c.config.NodeSwitchGateway, ","),
Protocol: util.CheckProtocol(c.config.NodeSwitchCIDR),
DisableInterConnection: true,
},
}
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Create(context.Background(), &nodeSubnet, metav1.CreateOptions{})
return err
}
// InitClusterRouter init cluster router to connect different logical switches
func (c *Controller) initClusterRouter() error {
lrs, err := c.ovnClient.ListLogicalRouter()
if err != nil {
return err
}
klog.Infof("exists routers %v", lrs)
for _, r := range lrs {
if c.config.ClusterRouter == r {
return nil
}
}
return c.ovnClient.CreateLogicalRouter(c.config.ClusterRouter)
}
// InitLoadBalancer init the default tcp and udp cluster loadbalancer
func (c *Controller) initLoadBalancer() error {
tcpLb, err := c.ovnClient.FindLoadbalancer(c.config.ClusterTcpLoadBalancer)
if err != nil {
return fmt.Errorf("failed to find tcp lb %v", err)
}
if tcpLb == "" {
klog.Infof("init cluster tcp load balancer %s", c.config.ClusterTcpLoadBalancer)
err := c.ovnClient.CreateLoadBalancer(c.config.ClusterTcpLoadBalancer, util.ProtocolTCP, "")
if err != nil {
klog.Errorf("failed to crate cluster tcp load balancer %v", err)
return err
}
} else {
klog.Infof("tcp load balancer %s exists", tcpLb)
}
tcpSessionLb, err := c.ovnClient.FindLoadbalancer(c.config.ClusterTcpSessionLoadBalancer)
if err != nil {
return fmt.Errorf("failed to find tcp session lb %v", err)
}
if tcpSessionLb == "" {
klog.Infof("init cluster tcp session load balancer %s", c.config.ClusterTcpSessionLoadBalancer)
err := c.ovnClient.CreateLoadBalancer(c.config.ClusterTcpSessionLoadBalancer, util.ProtocolTCP, "ip_src")
if err != nil {
klog.Errorf("failed to crate cluster tcp session load balancer %v", err)
return err
}
} else {
klog.Infof("tcp session load balancer %s exists", tcpSessionLb)
}
udpLb, err := c.ovnClient.FindLoadbalancer(c.config.ClusterUdpLoadBalancer)
if err != nil {
return fmt.Errorf("failed to find udp lb %v", err)
}
if udpLb == "" {
klog.Infof("init cluster udp load balancer %s", c.config.ClusterUdpLoadBalancer)
err := c.ovnClient.CreateLoadBalancer(c.config.ClusterUdpLoadBalancer, util.ProtocolUDP, "")
if err != nil {
klog.Errorf("failed to crate cluster udp load balancer %v", err)
return err
}
} else {
klog.Infof("udp load balancer %s exists", udpLb)
}
udpSessionLb, err := c.ovnClient.FindLoadbalancer(c.config.ClusterUdpSessionLoadBalancer)
if err != nil {
return fmt.Errorf("failed to find udp session lb %v", err)
}
if udpSessionLb == "" {
klog.Infof("init cluster udp session load balancer %s", c.config.ClusterUdpSessionLoadBalancer)
err := c.ovnClient.CreateLoadBalancer(c.config.ClusterUdpSessionLoadBalancer, util.ProtocolUDP, "ip_src")
if err != nil {
klog.Errorf("failed to crate cluster udp session load balancer %v", err)
return err
}
} else {
klog.Infof("udp session load balancer %s exists", udpSessionLb)
}
return nil
}
func (c *Controller) InitIPAM() error {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnet, %v", err)
return err
}
for _, subnet := range subnets {
if err := c.ipam.AddOrUpdateSubnet(subnet.Name, subnet.Spec.CIDRBlock, subnet.Spec.ExcludeIps); err != nil {
klog.Errorf("failed to init subnet %s, %v", subnet.Name, err)
}
}
pods, err := c.podsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list pods, %v", err)
return err
}
for _, pod := range pods {
if isPodAlive(pod) &&
pod.Annotations[util.AllocatedAnnotation] == "true" &&
pod.Annotations[util.LogicalSwitchAnnotation] != "" {
_, _, _, err := c.ipam.GetStaticAddress(
fmt.Sprintf("%s/%s", pod.Namespace, pod.Name),
pod.Annotations[util.IpAddressAnnotation],
pod.Annotations[util.MacAddressAnnotation],
pod.Annotations[util.LogicalSwitchAnnotation])
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s, %v", pod.Name, pod.Namespace, pod.Annotations[util.IpAddressAnnotation], err)
}
}
}
nodes, err := c.nodesLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list nodes, %v", err)
return err
}
for _, node := range nodes {
if node.Annotations[util.AllocatedAnnotation] == "true" {
portName := fmt.Sprintf("node-%s", node.Name)
v4IP, v6IP, _, err := c.ipam.GetStaticAddress(portName, node.Annotations[util.IpAddressAnnotation],
node.Annotations[util.MacAddressAnnotation],
node.Annotations[util.LogicalSwitchAnnotation])
if err != nil {
klog.Errorf("failed to init node %s.%s address %s, %v", node.Name, node.Namespace, node.Annotations[util.IpAddressAnnotation], err)
}
if v4IP != "" && v6IP != "" {
node.Annotations[util.IpAddressAnnotation] = util.GetStringIP(v4IP, v6IP)
}
}
}
return nil
}
func (c *Controller) initDefaultProviderNetwork() error {
_, err := c.providerNetworksLister.Get(c.config.DefaultProviderName)
if err == nil {
return nil
}
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to get default provider network %s: %v", c.config.DefaultProviderName, err)
return err
}
pn := kubeovnv1.ProviderNetwork{
ObjectMeta: metav1.ObjectMeta{
Name: c.config.DefaultProviderName,
},
Spec: kubeovnv1.ProviderNetworkSpec{
DefaultInterface: c.config.DefaultHostInterface,
},
}
_, err = c.config.KubeOvnClient.KubeovnV1().ProviderNetworks().Create(context.Background(), &pn, metav1.CreateOptions{})
return err
}
func (c *Controller) initDefaultVlan() error {
if c.config.NetworkType != util.NetworkTypeVlan {
return nil
}
if err := c.initDefaultProviderNetwork(); err != nil {
return err
}
_, err := c.vlansLister.Get(c.config.DefaultVlanName)
if err == nil {
return nil
}
if !k8serrors.IsNotFound(err) {
klog.Errorf("get default vlan %s failed %v", c.config.DefaultVlanName, err)
return err
}
if c.config.DefaultVlanID < 0 || c.config.DefaultVlanID > 4095 {
return fmt.Errorf("the default vlan id is not between 1-4095")
}
defaultVlan := kubeovnv1.Vlan{
ObjectMeta: metav1.ObjectMeta{Name: c.config.DefaultVlanName},
Spec: kubeovnv1.VlanSpec{
ID: c.config.DefaultVlanID,
Provider: c.config.DefaultProviderName,
},
}
_, err = c.config.KubeOvnClient.KubeovnV1().Vlans().Create(context.Background(), &defaultVlan, metav1.CreateOptions{})
return err
}
func (c *Controller) initSyncCrdIPs() error {
klog.Info("start to sync ips")
ips, err := c.config.KubeOvnClient.KubeovnV1().IPs().List(context.Background(), metav1.ListOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
for _, ipCr := range ips.Items {
ip := ipCr
v4IP, v6IP := util.SplitStringIP(ip.Spec.IPAddress)
if ip.Spec.V4IPAddress == v4IP && ip.Spec.V6IPAddress == v6IP {
continue
}
ip.Spec.V4IPAddress = v4IP
ip.Spec.V6IPAddress = v6IP
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), &ip, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to sync crd ip %s, %v", ip.Spec.IPAddress, err)
return err
}
}
return nil
}
func (c *Controller) initSyncCrdSubnets() error {
klog.Info("start to sync subnets")
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
for _, subnet := range subnets {
if util.CheckProtocol(subnet.Spec.CIDRBlock) == kubeovnv1.ProtocolDual {
err = calcDualSubnetStatusIP(subnet, c)
} else {
err = calcSubnetStatusIP(subnet, c)
}
if err != nil {
klog.Errorf("failed to calculate subnet %s used ip, %v", subnet.Name, err)
return err
}
}
return nil
}
func (c *Controller) initSyncCrdVlans() error {
klog.Info("start to sync vlans")
vlans, err := c.vlansLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
for _, vlan := range vlans {
var needUpdate bool
if vlan.Spec.VlanId != 0 && vlan.Spec.ID == 0 {
vlan.Spec.ID = vlan.Spec.VlanId
vlan.Spec.VlanId = 0
needUpdate = true
}
if vlan.Spec.ProviderInterfaceName != "" && vlan.Spec.Provider == "" {
vlan.Spec.Provider = vlan.Spec.ProviderInterfaceName
vlan.Spec.ProviderInterfaceName = ""
needUpdate = true
}
if needUpdate {
if _, err = c.config.KubeOvnClient.KubeovnV1().Vlans().Update(context.Background(), vlan, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update spec of vlan %s: %v", vlan.Name, err)
return err
}
}
}
return nil
}