-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
86 lines (73 loc) · 1.68 KB
/
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
package lazy
import (
"encoding/json"
"fmt"
)
type Node struct {
*NodeConfig
*Cluster
ID string
Domain string
Nics NodeInterfaces
VIP *NodeInterface
}
type NodeInterfaces []NodeInterface
func (node *Node) makeInterfaces(c *Config) (NodeInterfaces, error) {
nics := make(NodeInterfaces, 0, len(node.IP))
dhcpChose := false
for i, mac := range node.MAC {
var ip string
if i < len(node.IP) && len(node.IP[i]) != 0 &&
c.Cls.ContainIP(node.IP[i], i) {
ip = node.IP[i]
}
if len(ip) == 0 {
ipnet, err := c.Cls.requestIP(mac, i)
if err != nil {
return nil, err
}
ip = ipnet.String()
node.IP = append(node.IP, ip)
}
nic := NodeInterface{
MAC: mac,
IP: ip,
Interface: fmt.Sprintf("%s%d", c.N.InterfaceBase, i),
DNS: make([]string, 0),
}
if len(c.N.Gateway) != 0 && c.Cls.ContainIP(c.N.Gateway, i) {
nic.Gateway = c.N.Gateway
}
if c.DHCP.Enable && !dhcpChose {
if len(c.DHCP.Interface) == 0 || nic.Interface == c.DHCP.Interface {
nic.DHCP = true
dhcpChose = true
}
}
if len(c.D.DNS) != 0 {
for _, dns := range c.D.DNS {
if sameCIDR(nic.IP, dns) || len(nic.Gateway) != 0 {
nic.DNS = append(nic.DNS, dns)
}
}
}
nics = append(nics, nic)
}
return nics, nil
}
func (nis NodeInterfaces) String() string {
bs, _ := json.Marshal(nis)
return string(bs)
}
type NodeInterface struct {
MAC string `json:"mac"`
IP string `json:"ip"`
Interface string `json:"interface"`
DHCP bool `json:"dhcp"`
Gateway string `json:"gateway"`
DNS []string `json:"dns"`
}
func (nic NodeInterface) String() string {
bs, _ := json.Marshal(nic)
return string(bs)
}