Skip to content

Commit

Permalink
fix ipam random get (#3200)
Browse files Browse the repository at this point in the history
* fix ipam random get

---------

Signed-off-by: bobz965 <zhangbingbing2_yewu@cmss.chinamobile.com>
  • Loading branch information
bobz965 committed Sep 12, 2023
1 parent a71201a commit 0eb4f79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pkg/ipam/ip.go
Expand Up @@ -147,27 +147,56 @@ func convertExcludeIps(excludeIps []string) IPRangeList {

func splitRange(a, b *IPRange) IPRangeList {
if b.End.LessThan(a.Start) || b.Start.GreaterThan(a.End) {
// bs----be----as----ae or as----ae----bs----be
// a is aready splited with b

return IPRangeList{a}
}

if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
(a.End.Equal(b.End) || a.End.LessThan(b.End)) {
// as(bs)---- or bs----as----
// ae(be) or ae----be
// a is the same as b
// a in b

return nil
}

if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
a.End.GreaterThan(b.End) {
// as(bs)---- or bs----as----
// be----ae
// as(bs)----be----ae
// bs----as----be----ae
// get be----ae

ipr := IPRange{Start: b.End.Add(1), End: a.End}
return IPRangeList{&ipr}
}

if (a.End.Equal(b.End) || a.End.LessThan(b.End)) &&
a.Start.LessThan(b.Start) {
// -----ae(be) or ----ae----be
// -----as----bs
// -----as----bs-----ae(be)
// -----as----bs-----ae----be
// get as----bs

ipr := IPRange{Start: a.Start, End: b.Start.Add(-1)}
return IPRangeList{&ipr}
}

ipr1 := IPRange{Start: a.Start, End: b.Start.Add(-1)}
ipr2 := IPRange{Start: b.End.Add(1), End: a.End}
return IPRangeList{&ipr1, &ipr2}
results := IPRangeList{}
if !ipr1.Start.GreaterThan(ipr1.End) {
// start <= end
results = append(results, &ipr1)
}
if !ipr2.Start.GreaterThan(ipr2.End) {
// start <= end
results = append(results, &ipr2)
}
return results
}
6 changes: 6 additions & 0 deletions pkg/ipam/subnet.go
Expand Up @@ -220,10 +220,13 @@ func (subnet *Subnet) getV4RandomAddress(podName, nicName string, mac string, sk
var ip IP
var idx int
for i, ipr := range subnet.V4FreeIPList {
klog.Infof("allocate pod %s v4 ip in the range of start: %s, end: %s", podName, ipr.Start, ipr.End)
for next := ipr.Start; !next.GreaterThan(ipr.End); next = next.Add(1) {
if !util.ContainsString(skippedAddrs, string(next)) {
ip = next
break
} else {
klog.Infof("v4 ip %s is in skipped addrs %+v", skippedAddrs)
}
}
if ip != "" {
Expand Down Expand Up @@ -282,10 +285,13 @@ func (subnet *Subnet) getV6RandomAddress(podName, nicName string, mac string, sk
var ip IP
var idx int
for i, ipr := range subnet.V6FreeIPList {
klog.Infof("allocate pod %s v6 ip in the range of start: %s, end: %s", podName, ipr.Start, ipr.End)
for next := ipr.Start; !next.GreaterThan(ipr.End); next = next.Add(1) {
if !util.ContainsString(skippedAddrs, string(next)) {
ip = next
break
} else {
klog.Infof("v6 ip %s is in skipped addrs %+v", skippedAddrs)
}
}
if ip != "" {
Expand Down

0 comments on commit 0eb4f79

Please sign in to comment.