Skip to content

Guard the provider pod IP state with a lock - #615

Open
dciangot wants to merge 1 commit into
interlink-hq:mainfrom
dciangot:refactor/provider-state-locks
Open

dciangot wants to merge 1 commit into
interlink-hq:mainfrom
dciangot:refactor/provider-state-locks

Conversation

@dciangot

@dciangot dciangot commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Provider.podIPs is read and written by CreatePod (setupVPNPodIP) and by statusLoop at the same time, with no lock. The status loop also emptied the slice before refilling it, so an allocation could run against a half-built list and hand out an address already in use.

This moves it behind a podIPTracker (new file pkg/virtualkubelet/podiptracker.go):

  • It keeps the addresses observed on cluster pods apart from the ones reserved locally by allocate.
  • Picking a free IP and reserving it is a single critical section, so two concurrent CreatePod calls cannot get the same address.
  • A refresh replaces the observed set and releases only the reservations made before the pod list was taken (begin() returns a snapshot that is passed to refresh). An IP reserved while the list is in flight is kept, so it cannot be given out twice.
  • Membership is set-based, so importing N addresses is linear.
  • The zero value is usable, so the Provider{} literals used in tests keep working.

When listing pods fails in the status loop, the previous IP set is kept instead of being cleared.

p.pods is not touched: main already protects it with podsMu.

This is step 1 of a larger maintainability refactor of pkg/virtualkubelet, and has no other user-facing change.

Tested:

  • go build ./... and go vet ./pkg/virtualkubelet/.
  • go test -race ./pkg/virtualkubelet/, with new tests in podiptracker_test.go. They include a regression test for an allocation made between taking the list snapshot and applying the refresh, and for concurrent allocations.
  • golangci-lint v2.14.0 with the repo config: 0 issues.
  • Dagger/E2E tests were not run locally.

Size: 226 changed lines in 3 files.

Rebased on main at 5f60ce8. It addresses the two Copilot findings from the first revision.


Related issue :

Copilot AI balanced review requested due to automatic review settings November 21, 2026 10:10
@netlify

netlify Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for interlink-dev ready!

Name Link
🔨 Latest commit 4cbc765
🔍 Latest deploy log https://app.netlify.com/projects/interlink-dev/deploys/6b0172c883682d5481a4b342
😎 Deploy Preview https://deploy-preview-615--interlink-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The IP refresh can discard local reservations and allow duplicate address allocation, while slice-based deduplication adds quadratic work under the allocator mutex.

2 open issues
What changed in this PR

This first-stage refactor adds synchronized containers for the virtual-kubelet provider’s pod registry and VPN IP tracking. The locks protect the containers, not the shared pod objects.

Changes:

  • Replaces direct map and slice access with locked operations and snapshot iteration.
  • Preserves tracked IPs when the status loop cannot list cluster pods.
  • Adds tests for registry operations, snapshots, and concurrent IP allocation.
File Description
pkg/​virtualkubelet/​virtualkubelet.go Integrates the stores into provider operations and status refreshes.
pkg/​virtualkubelet/​podstore.go Adds the mutex-protected pod registry and IP tracker.
pkg/​virtualkubelet/​podstore_test.go Tests store behavior, snapshot isolation, and concurrent allocation.
pkg/​virtualkubelet/​execute.go Uses the retrieved pod for terminal-state checks.

Review effort: Balanced

Comment thread pkg/virtualkubelet/virtualkubelet.go Resolved
Comment thread pkg/virtualkubelet/podstore.go Resolved
Provider.podIPs was a plain slice written by CreatePod and by the
status loop at the same time, and the status loop emptied it before
refilling it, so an allocation could run against a half-built list.

Move it behind podIPTracker, which keeps the addresses observed on
cluster pods apart from the ones reserved locally. A refresh replaces
the observed set and only releases reservations made before the pod
list was taken, so an IP handed out while the list is in flight cannot
be given out twice. Lookups are set-based, and allocation picks and
reserves an address in one critical section.

The status loop now keeps the previous IP set when listing pods fails.

Signed-off-by: dciangot <dciangot@cern.ch>
@dciangot
dciangot force-pushed the refactor/provider-state-locks branch from b6859b8 to 4cbc765 Compare November 21, 2026 10:30
@dciangot dciangot changed the title Guard provider pod and IP state with locks Guard the provider pod IP state with a lock Nov 21, 2026
@dciangot
dciangot requested a balanced review from Copilot November 21, 2026 10:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical races in podiptracker.go can prematurely delete unpublished reservations or accept stale snapshots, allowing the same IP to be allocated to multiple pods.

2 open issues
2 resolved since last review

Review effort: Balanced

Comment on lines +18 to +37
seq uint64
observed map[string]struct{}
reserved map[string]uint64 // IP -> value of seq when it was reserved
}

// begin must be called right before listing the pods from the cluster. The returned
// snapshot is passed to refresh together with the list result.
func (t *podIPTracker) begin() uint64 {
t.mu.Lock()
defer t.mu.Unlock()

return t.seq
}

// refresh replaces the observed addresses and releases the reservations made up to
// snapshot. Reservations made after it are kept.
func (t *podIPTracker) refresh(snapshot uint64, observed []string) {
t.mu.Lock()
defer t.mu.Unlock()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Fix the code for this review comment.

When a review comment includes a suggested change, apply the suggestion exactly.

Do not make changes beyond what is described in the linked review comment.

Comment on lines +44 to +45
if seq <= snapshot {
delete(t.reserved, ip)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Fix the code for this review comment.

When a review comment includes a suggested change, apply the suggestion exactly.

Do not make changes beyond what is described in the linked review comment.

@dciangot

Copy link
Copy Markdown
Member Author

@copilot Fix the code for all comments in this review thread.

When a review comment includes a suggested change, apply the suggestion exactly.

Do not make changes beyond what is described in the linked review thread.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants