-
Notifications
You must be signed in to change notification settings - Fork 34
/
ip.go
103 lines (83 loc) · 1.96 KB
/
ip.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
/*
Copyright 2021 The Hybridnet Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import "fmt"
func NewIPSet() IPSet {
return make(map[string]*IP)
}
func (s IPSet) Has(ip string) bool {
_, found := s[ip]
return found
}
func (s IPSet) Add(ip string, content *IP) {
s[ip] = content
}
func (s IPSet) Update(ip, podName, podNamespace, status string) {
if !s.Has(ip) {
return
}
s[ip].PodName = podName
s[ip].PodNamespace = podNamespace
s[ip].Status = status
}
func (s IPSet) UpdateStatus(ip, status string) {
if !s.Has(ip) {
return
}
s[ip].Status = status
}
func (s IPSet) Delete(ip string) {
delete(s, ip)
}
func (s IPSet) Get(ip string) *IP {
return s[ip]
}
func (s IPSet) Count() int {
return len(s)
}
func NewIPSlice() *IPSlice {
return &IPSlice{
IPs: make([]string, 0),
// for the first allocation
IPIndex: -1,
}
}
func (s *IPSlice) Add(ip string, isDefault bool) {
s.IPs = append(s.IPs, ip)
s.IPCount = len(s.IPs)
if isDefault {
s.IPIndex = s.IPCount - 1
}
}
func (s *IPSlice) Count() int {
return s.IPCount
}
func (s *IPSlice) Next() string {
s.IPIndex = (s.IPIndex + 1) % s.IPCount
return s.IPs[s.IPIndex]
}
func (s *IPSlice) Current() string {
if s.IPIndex < 0 {
return ""
}
return s.IPs[s.IPIndex]
}
func (i *IP) String() string {
return fmt.Sprintf("%s/%s/%s", i.Network, i.Subnet, i.Address.String())
}
func (i *IP) IsIPv6() bool {
if i.Address == nil || i.Address.IP == nil {
return false
}
return i.Address.IP.To4() == nil
}