/
types.go
259 lines (210 loc) · 3.88 KB
/
types.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
package eni
import (
"net/netip"
"time"
"github.com/AliyunContainerService/terway/rpc"
"github.com/AliyunContainerService/terway/types/daemon"
)
//go:generate stringer -type=ipStatus -trimprefix=ipStatus
//go:generate stringer -type=ConditionType -trimprefix=Condition
type ipStatus int
const (
ipStatusInit ipStatus = iota
ipStatusValid
ipStatusInvalid
ipStatusDeleting
)
type IP struct {
ip netip.Addr
primary bool
podID string
status ipStatus
}
func (ip *IP) String() string {
if ip == nil {
return ""
}
return ip.ip.String()
}
func NewIP(ip netip.Addr, primary bool) *IP {
return &IP{
ip: ip,
primary: primary,
status: ipStatusInit,
}
}
func NewValidIP(ip netip.Addr, primary bool) *IP {
return &IP{
ip: ip,
primary: primary,
status: ipStatusValid,
}
}
func (ip *IP) Primary() bool {
return ip.primary
}
func (ip *IP) Valid() bool {
return ip.status == ipStatusValid
}
func (ip *IP) Deleting() bool {
return ip.status == ipStatusDeleting
}
func (ip *IP) InUse() bool {
return ip.podID != ""
}
func (ip *IP) Allocate(podID string) {
ip.podID = podID
}
func (ip *IP) Release(podID string) {
if ip.podID != podID {
return
}
ip.podID = ""
}
func (ip *IP) Dispose() {
if ip.primary {
return
}
ip.status = ipStatusDeleting
}
func (ip *IP) SetInvalid() {
ip.status = ipStatusInvalid
}
func (ip *IP) Allocatable() bool {
return ip.Valid() && !ip.InUse()
}
type Set map[any]*IP
func (s Set) Idles() []*IP {
var result []*IP
for _, v := range s {
if !v.InUse() {
result = append(result, v)
}
}
return result
}
func (s Set) InUse() []*IP {
var result []*IP
for _, v := range s {
if v.InUse() {
result = append(result, v)
}
}
return result
}
func (s Set) Allocatable() []*IP {
var result []*IP
for _, v := range s {
if v.Allocatable() {
result = append(result, v)
}
}
return result
}
func (s Set) PeekAvailable(podID string) *IP {
if podID != "" {
for _, v := range s {
if v.podID == podID {
return v
}
}
}
for _, v := range s {
if v.Allocatable() {
return v
}
}
return nil
}
func (s Set) Add(ip *IP) {
s[ip.ip] = ip
}
func (s Set) PutValid(ip ...netip.Addr) {
for _, v := range ip {
s[v] = &IP{ip: v, status: ipStatusValid}
}
}
func (s Set) PutDeleting(ip ...netip.Addr) {
for _, v := range ip {
s[v] = &IP{ip: v, status: ipStatusDeleting}
}
}
func (s Set) Delete(ip ...netip.Addr) {
for _, v := range ip {
delete(s, v)
}
}
func (s Set) Release(podID string, ip netip.Addr) {
i, ok := s[ip]
if ok {
i.Release(podID)
}
}
func (s Set) Deleting() []netip.Addr {
var result []netip.Addr
for _, v := range s {
if v.Deleting() {
result = append(result, v.ip)
}
}
return result
}
func (s Set) ByPodID(podID string) *IP {
for _, v := range s {
if v.podID == podID {
return v
}
}
return nil
}
type ResourceType int
const (
ResourceTypeLocalIP = 1 << iota
ResourceTypeVeth
ResourceTypeRemoteIP
ResourceTypeRDMA
)
type ResourceRequest interface {
ResourceType() ResourceType
}
// AllocRequest represent a bunch of resource must be met.
type AllocRequest struct {
ResourceRequests []ResourceRequest
}
type AllocResp struct {
Err error
NetworkConfigs NetworkResources
}
type ReleaseRequest struct {
NetworkResources []NetworkResource
}
type NetworkResources []NetworkResource
type NetworkResource interface {
ResourceType() ResourceType
ToRPC() []*rpc.NetConf
ToStore() []daemon.ResourceItem
}
type Status struct {
NetworkInterfaceID string
MAC string
Type string
AllocInhibitExpireAt string
Usage [][]string
Status string
}
type Trace struct {
Condition ConditionType
Reason string
}
type Condition struct {
ConditionType ConditionType
Reason string
Last time.Time
}
type ConditionType int
const (
Full ConditionType = iota
ResourceTypeMismatch
NetworkInterfaceMismatch
InsufficientVSwitchIP
)