Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! fixup! fixup! feat: ipam timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Dec 2, 2024
1 parent 1d2d473 commit 0273a3c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
7 changes: 5 additions & 2 deletions pkg/ipam/core/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package ipamcore
import (
"fmt"
"net/netip"
"slices"
"time"
)

Expand Down Expand Up @@ -156,7 +155,11 @@ func (ipam *Ipam) ListIPs(prefix netip.Prefix) ([]netip.Addr, error) {
return nil, err
}
if node != nil {
return slices.Clone(node.ips), nil
addrs := make([]netip.Addr, len(node.ips))
for i := range node.ips {
addrs[i] = node.ips[i].addr
}
return addrs, nil
}
return nil, nil
}
Expand Down
36 changes: 20 additions & 16 deletions pkg/ipam/core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"time"
)

// nodeIP represents an IP address acquired by a node.
type nodeIP struct {
addr netip.Addr
creationTimestamp time.Time
}

// node represents a node in the binary tree.
type node struct {
lastUpdate time.Time
Expand All @@ -33,7 +39,7 @@ type node struct {
left *node
right *node

ips []netip.Addr
ips []nodeIP
lastip netip.Addr
}

Expand Down Expand Up @@ -149,7 +155,7 @@ func listNetworks(node *node) []netip.Prefix {

func (n *node) isAllocatedIP(ip netip.Addr) bool {
for i := range n.ips {
if n.ips[i].Compare(ip) == 0 {
if n.ips[i].addr.Compare(ip) == 0 {
return true
}
}
Expand Down Expand Up @@ -181,7 +187,7 @@ func (n *node) ipAcquire() *netip.Addr {
addr = n.prefix.Addr()
}
if !n.isAllocatedIP(addr) {
n.ips = append(n.ips, addr)
n.ips = append(n.ips, nodeIP{addr: addr, creationTimestamp: time.Now()})
n.lastip = addr
n.lastUpdate = time.Now()
return &addr
Expand All @@ -201,31 +207,30 @@ func (n *node) allocateIPWithAddr(addr netip.Addr) *netip.Addr {
}

for i := range n.ips {
if n.ips[i].Compare(addr) == 0 {
if n.ips[i].addr.Compare(addr) == 0 {
return nil
}
}

n.ips = append(n.ips, addr)
n.ips = append(n.ips, nodeIP{addr: addr, creationTimestamp: time.Now()})
n.lastUpdate = time.Now()

return &n.ips[len(n.ips)-1]
return &n.ips[len(n.ips)-1].addr
}

func (n *node) ipRelease(ip netip.Addr, gracePeriod time.Duration) *netip.Addr {
if !n.acquired {
return nil
}

if !n.lastUpdate.Add(gracePeriod).Before(time.Now()) {
return nil
}

for i, addr := range n.ips {
if addr.Compare(ip) == 0 {
for i, nodeIP := range n.ips {
if !nodeIP.creationTimestamp.Add(gracePeriod).Before(time.Now()) {
continue
}
if nodeIP.addr.Compare(ip) == 0 {
n.ips = append(n.ips[:i], n.ips[i+1:]...)
n.lastUpdate = time.Now()
return &addr
return &nodeIP.addr
}
}
return nil
Expand Down Expand Up @@ -351,12 +356,11 @@ func (n *node) toGraphvizRecursive(sb *strings.Builder) {
if n == nil {
return
}
label := n.lastUpdate.Format(time.TimeOnly)
label += "\\n" + n.prefix.String()
label := n.prefix.String()
if len(n.ips) > 0 {
ipsString := []string{}
for i := range n.ips {
ipsString = append(ipsString, n.ips[i].String())
ipsString = append(ipsString, n.ips[i].addr.String())
}
label += "\\n" + strings.Join(ipsString, "\\n")
}
Expand Down

0 comments on commit 0273a3c

Please sign in to comment.