-
Notifications
You must be signed in to change notification settings - Fork 1
/
onc.go
189 lines (161 loc) · 4.33 KB
/
onc.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
package onc
import (
"fmt"
"math"
"net"
"strconv"
"strings"
)
type Request struct {
HostPrefix int `json:"hostPrefix"`
ClusterNetwork string `json:"clusterNetwork"`
ServiceNetwork string `json:"serviceNetwork"`
Cni string `json:"cni"`
MachineNetwork string `json:"machineNetwork"`
}
type Response struct {
PodNetwork string `json:"pod-network"`
ServiceNetwork string `json:"service-network"`
MachineNetwork string `json:"machine-network"`
Cni string `json:"cni"`
NumPods int `json:"number-of-pods"`
NumServices int `json:"number-of-services"`
NumNodes NumNodes `json:"number-of-nodes"`
PodsPerNode int `json:"pods-per-node"`
Conflicts bool `json:"network-conflict"`
}
type NumNodes struct {
Want int `json:"want"`
Have int `json:"have"`
}
func CalculateNetwork(request Request) (*Response, error) {
networks := []string{request.ClusterNetwork, request.ServiceNetwork}
for _, network := range networks {
if !isValidCIDR(network) {
return nil, fmt.Errorf("Invalid network CIDR: %s", network)
}
}
podNetwork := request.ClusterNetwork
serviceNetwork := request.ServiceNetwork
machineNetwork := request.MachineNetwork
hostPrefix := request.HostPrefix
cni := request.Cni
numPods, err := countIPs(podNetwork)
if err != nil {
return nil, err
}
numNodes := len(splitSubnet(podNetwork, hostPrefix))
var totalPodsPerNode int
if numNodes != 0 {
totalPodsPerNode = numPods / numNodes
} else {
return nil, fmt.Errorf("numNodes is 0")
}
var podsPerNode int
if cni == "ovn-kubernetes" {
podsPerNode = totalPodsPerNode - 3
} else if cni == "openshift-sdn" {
podsPerNode = totalPodsPerNode - 2
}
numServices, err := countIPs(serviceNetwork)
if err != nil {
return nil, err
}
machineNetworkNodes, err := countIPs(machineNetwork)
if err != nil {
return nil, err
}
clusterNumNodes := NumNodes{
Want: numNodes,
Have: machineNetworkNodes,
}
sdnConflicts, err := checkCIDRConflict(request.ClusterNetwork, request.ServiceNetwork, request.MachineNetwork)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
joinSwitch := "100.64.0.0/16"
trasitSwitch := "100.88.0.0/16"
ovnConflicts, err := checkCIDRConflict(request.ClusterNetwork, request.ServiceNetwork, request.MachineNetwork, joinSwitch, trasitSwitch)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
var conflicts bool
if cni == "ovn-kubernetes" {
conflicts = ovnConflicts
} else if cni == "openshift-sdn" {
conflicts = sdnConflicts
}
return &Response{
PodNetwork: podNetwork,
ServiceNetwork: serviceNetwork,
MachineNetwork: machineNetwork,
NumPods: numPods,
NumServices: numServices,
NumNodes: clusterNumNodes,
PodsPerNode: podsPerNode,
Conflicts: conflicts,
Cni: cni,
}, nil
}
func isValidCIDR(cidr string) bool {
_, _, err := net.ParseCIDR(cidr)
return err == nil
}
func countIPs(cidr string) (int, error) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return 0, err
}
networkPrefix, err := strconv.Atoi(strings.Split(ipNet.String(), "/")[1])
if err != nil {
return 0, err
}
IPs := int(float64(math.Pow(2, float64(32-networkPrefix)) - 2))
return IPs, nil
}
func splitSubnet(subnet string, prefixLength int) []*net.IPNet {
var subnets []*net.IPNet
_, snet, err := net.ParseCIDR(subnet)
if err != nil {
fmt.Println("Error parsing original subnet:", err)
return subnets
}
ip := snet.IP
ones, _ := snet.Mask.Size()
subnetCount := 2 << uint(prefixLength-ones-1)
stepSize := 1 << uint(32-ones)
for i := 0; i < subnetCount; i++ {
newIP := make(net.IP, len(ip))
copy(newIP, ip)
for j := 3; j >= 0; j-- {
newIP[j] += byte((i * stepSize) >> uint((3-j)*8))
}
newSubnet := &net.IPNet{
IP: newIP,
Mask: net.CIDRMask(prefixLength, 32),
}
subnets = append(subnets, newSubnet)
}
return subnets
}
func checkCIDRConflict(cidrs ...string) (bool, error) {
var networks []*net.IPNet
for _, cidr := range cidrs {
_, netIP, err := net.ParseCIDR(cidr)
if err != nil {
return false, err
}
networks = append(networks, netIP)
}
// Check for conflicts
for i, network1 := range networks {
for j, network2 := range networks {
if i != j && (network1.Contains(network2.IP) || network2.Contains(network1.IP)) {
return true, nil
}
}
}
return false, nil
}