-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
159 lines (128 loc) · 3.37 KB
/
utils.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
package main
import (
"bytes"
"encoding/binary"
"io/ioutil"
"net"
"github.com/CRASH-Tech/dhcp-operator/cmd/common"
"github.com/CRASH-Tech/dhcp-operator/cmd/kubernetes/api/v1alpha1"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func readConfig(path string) (common.Config, error) {
config := common.Config{}
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
return common.Config{}, err
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return common.Config{}, err
}
return config, err
}
func getAvialablePools(ip net.IP, requested bool) ([]v1alpha1.Pool, error) {
var result []v1alpha1.Pool
pools, err := kClient.V1alpha1().Pool().GetAll()
if err != nil {
return result, err
}
for _, pool := range pools {
_, poolNet, err := net.ParseCIDR(pool.Spec.Subnet)
if err != nil {
log.Error(err)
continue
}
if requested {
if poolNet.Contains(ip) && isIPInPool(ip, pool) {
result = append(result, pool)
}
} else {
if poolNet.Contains(ip) {
result = append(result, pool)
}
}
}
return result, nil
}
func cidrHosts(netw string) []string {
// convert string to IPNet struct
_, ipv4Net, err := net.ParseCIDR(netw)
if err != nil {
log.Fatal(err)
}
// convert IPNet struct mask and address to uint32
mask := binary.BigEndian.Uint32(ipv4Net.Mask)
// find the start IP address
start := binary.BigEndian.Uint32(ipv4Net.IP)
// find the final IP address
finish := (start & mask) | (mask ^ 0xffffffff)
// make a slice to return host addresses
var hosts []string
// loop through addresses as uint32.
// I used "start + 1" and "finish - 1" to discard the network and broadcast addresses.
for i := start + 1; i <= finish-1; i++ {
// convert back to net.IPs
// Create IP address of type net.IP. IPv4 is 4 bytes, IPv6 is 16 bytes.
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, i)
hosts = append(hosts, ip.String())
}
// return a slice of strings containing IP addresses
return hosts
}
func isIPInPool(ip net.IP, pool v1alpha1.Pool) bool {
if pool.Spec.Start == "" || pool.Spec.End == "" || ip == nil {
log.Errorf("Cannot find ip: %s-%s", pool.Spec.Start, pool.Spec.End)
return false
}
from16 := net.ParseIP(pool.Spec.Start)
to16 := net.ParseIP(pool.Spec.End)
test16 := ip.To16()
if from16 == nil || to16 == nil || test16 == nil {
log.Error("An ip did not convert to a 16 byte")
return false
}
if bytes.Compare(test16, from16) >= 0 && bytes.Compare(test16, to16) <= 0 {
return true
}
return false
}
func isIPFree(ip net.IP) bool {
leases, err := kClient.V1alpha1().Lease().GetAll()
if err != nil {
return false
}
for _, lease := range leases {
if lease.Spec.Ip == ip.String() {
return false
}
}
return true
}
func getAvialableIPs(pool v1alpha1.Pool, requestedIP net.IP, requested bool) ([]net.IP, error) {
var result []net.IP
if requested && requestedIP != nil && requestedIP.String() != "0.0.0.0" {
if isIPFree(requestedIP) {
result = append(result, requestedIP)
return result, nil
}
}
leases, err := kClient.V1alpha1().Lease().GetAll()
if err != nil {
return result, err
}
for _, ip := range cidrHosts(pool.Spec.Subnet) {
var exists bool
for _, lease := range leases {
if lease.Spec.Ip == ip {
exists = true
break
}
}
if !exists && isIPInPool(net.ParseIP(ip), pool) {
result = append(result, net.ParseIP(ip))
}
}
return result, nil
}