Skip to content

Commit

Permalink
fix: ip pre-heating
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Jan 16, 2025
1 parent d4a58b4 commit 8ac4f62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 46 additions & 1 deletion pkg/eni/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ func (l *Local) Allocate(ctx context.Context, cni *daemon.CNI, request ResourceR
}

log := logr.FromContextOrDiscard(ctx)
log.Info(fmt.Sprintf("local request %v", localIPRequest))

expectV4 := 0
expectV6 := 0
Expand Down Expand Up @@ -455,6 +454,11 @@ func (l *Local) Allocate(ctx context.Context, cni *daemon.CNI, request ResourceR

go l.allocWorker(ctx, cni, localIPRequest, respCh)

if l.eni == nil {
log.Info("local request", "eni", "", "req", localIPRequest)
} else {
log.Info("local request", "eni", l.eni.ID, "req", localIPRequest)
}
return respCh, nil
}

Expand Down Expand Up @@ -528,6 +532,8 @@ func (l *Local) allocWorker(ctx context.Context, cni *daemon.CNI, request *Local
l.cond.L.Lock()
defer l.cond.L.Unlock()

defer l.cond.Broadcast()

defer func() {
if request == nil {
return
Expand All @@ -550,6 +556,32 @@ func (l *Local) allocWorker(ctx context.Context, cni *daemon.CNI, request *Local
}()

log := logr.FromContextOrDiscard(ctx)
if request != nil && request.NoCache {
// as we want to do preheat, so this ip will not be consumed
// so just hang there , let this ctx done

for {
select {
case <-request.workerCtx.Done():
// work ctx finished (factory cancel it)

select {
case <-ctx.Done():
close(respCh)
case respCh <- &AllocResp{}:
}

return
case <-ctx.Done():
// parent cancel the context, so close the ch
close(respCh)
return
default:
}
l.cond.Wait()
}
}

for {
select {
case <-ctx.Done():
Expand All @@ -566,6 +598,7 @@ func (l *Local) allocWorker(ctx context.Context, cni *daemon.CNI, request *Local
if l.enableIPv4 {
ipv4 = l.ipv4.PeekAvailable(cni.PodID)
if ipv4 == nil {
log.Info("waiting ipv4")
l.cond.Wait()
continue
}
Expand Down Expand Up @@ -1128,12 +1161,24 @@ func (l *Local) popNIPv4Jobs(count int) {
firstPart, secondPart := Split(l.allocatingV4, count)
l.dangingV4 = append(l.dangingV4, firstPart...)
l.allocatingV4 = secondPart

lo.ForEach(l.dangingV4, func(item *LocalIPRequest, index int) {
if item.NoCache {
item.cancel()
}
})
}

func (l *Local) popNIPv6Jobs(count int) {
firstPart, secondPart := Split(l.allocatingV6, count)
l.dangingV6 = append(l.dangingV6, firstPart...)
l.allocatingV6 = secondPart

lo.ForEach(l.dangingV6, func(item *LocalIPRequest, index int) {
if item.NoCache {
item.cancel()
}
})
}

func Split[T any](arr []T, index int) ([]T, []T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/eni/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ func (m *Manager) syncPool(ctx context.Context) {
})
if err != nil {
mgrLog.Error(err, "sync pool error")
} else {
mgrLog.Info("add ip done")
}
}()
}
Expand Down

0 comments on commit 8ac4f62

Please sign in to comment.