-
Notifications
You must be signed in to change notification settings - Fork 488
fix(NSC): harden Network Services Controller against panics, races, and sync errors #2041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aauren
merged 3 commits into
cloudnativelabs:master
from
Aprazor:fix/nsc-harden-panics-races-sync-errors
Apr 1, 2026
+120
−3
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "net" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestShuffleDoesNotPanicOnEmptySlice(t *testing.T) { | ||
| // shuffle should handle empty and single-element slices safely | ||
| tests := []struct { | ||
| name string | ||
| input []endpointSliceInfo | ||
| }{ | ||
| { | ||
| name: "empty slice", | ||
| input: []endpointSliceInfo{}, | ||
| }, | ||
| { | ||
| name: "single element", | ||
| input: []endpointSliceInfo{{ip: "10.0.0.1", port: 80}}, | ||
| }, | ||
| { | ||
| name: "multiple elements", | ||
| input: []endpointSliceInfo{ | ||
| {ip: "10.0.0.1", port: 80}, | ||
| {ip: "10.0.0.2", port: 80}, | ||
| {ip: "10.0.0.3", port: 80}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Should not panic | ||
| result := shuffle(tt.input) | ||
| assert.Equal(t, len(tt.input), len(result), "shuffle should preserve slice length") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNodePortHealthCheckConcurrentAccess(t *testing.T) { | ||
|
||
| // Verify that concurrent reads and writes to the healthcheck controller | ||
| // do not cause a data race (this test is meaningful with -race flag) | ||
| nphc := NewNodePortHealthCheck() | ||
|
|
||
| svcMap := serviceInfoMap{ | ||
| "test-svc": &serviceInfo{ | ||
| healthCheckNodePort: 0, // no actual listener needed | ||
| }, | ||
| } | ||
| epMap := endpointSliceInfoMap{ | ||
| "test-svc": { | ||
| {ip: "10.0.0.1", port: 80, isLocal: true}, | ||
| }, | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| // Concurrent writes | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < 100; i++ { | ||
| _ = nphc.UpdateServicesInfo(svcMap, epMap) | ||
| } | ||
| }() | ||
|
|
||
| // Concurrent reads (simulating what the HTTP handler does) | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < 100; i++ { | ||
| nphc.mu.RLock() | ||
| _ = nphc.endpointsInfoMap["test-svc"] | ||
| nphc.mu.RUnlock() | ||
| } | ||
| }() | ||
|
|
||
| wg.Wait() | ||
| } | ||
|
|
||
| func TestSetupMangleTableRuleRejectsInvalidIP(t *testing.T) { | ||
|
||
| // Verify that net.ParseIP returning nil is handled gracefully | ||
| // rather than causing a nil pointer dereference on .To4() | ||
| tests := []struct { | ||
| name string | ||
| ip string | ||
| }{ | ||
| {name: "empty string", ip: ""}, | ||
| {name: "garbage", ip: "not-an-ip"}, | ||
| {name: "incomplete", ip: "192.168.1"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| parsedIP := net.ParseIP(tt.ip) | ||
| assert.Nil(t, parsedIP, "net.ParseIP should return nil for invalid IP %q", tt.ip) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please move this into network_services_controller_test.go?