-
Notifications
You must be signed in to change notification settings - Fork 18.6k
/
structures.go
70 lines (59 loc) · 1.88 KB
/
structures.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
package ipam
import (
"fmt"
"net/netip"
"strings"
"github.com/docker/docker/libnetwork/bitmap"
"github.com/docker/docker/libnetwork/types"
)
// PoolID is the pointer to the configured pools in each address space
type PoolID struct {
AddressSpace string
SubnetKey
}
// PoolData contains the configured pool data
type PoolData struct {
addrs *bitmap.Bitmap
children map[netip.Prefix]struct{}
// Whether to implicitly release the pool once it no longer has any children.
autoRelease bool
}
// SubnetKey is the composite key to an address pool within an address space.
type SubnetKey struct {
Subnet, ChildSubnet netip.Prefix
}
// PoolIDFromString creates a new PoolID and populates the SubnetKey object
// reading it from the given string.
func PoolIDFromString(str string) (pID PoolID, err error) {
if str == "" {
return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
}
p := strings.Split(str, "/")
if len(p) != 3 && len(p) != 5 {
return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
}
pID.AddressSpace = p[0]
pID.Subnet, err = netip.ParsePrefix(p[1] + "/" + p[2])
if err != nil {
return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
}
if len(p) == 5 {
pID.ChildSubnet, err = netip.ParsePrefix(p[3] + "/" + p[4])
if err != nil {
return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
}
}
return pID, nil
}
// String returns the string form of the SubnetKey object
func (s *PoolID) String() string {
if s.ChildSubnet == (netip.Prefix{}) {
return s.AddressSpace + "/" + s.Subnet.String()
} else {
return s.AddressSpace + "/" + s.Subnet.String() + "/" + s.ChildSubnet.String()
}
}
// String returns the string form of the PoolData object
func (p *PoolData) String() string {
return fmt.Sprintf("PoolData[Children: %d]", len(p.children))
}