- Notifications
You must be signed in to change notification settings - Fork928
feat!: add interface report to coder netcheck#13562
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 13 additions & 2 deletionscli/netcheck.go
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
6 changes: 3 additions & 3 deletionscli/netcheck_test.go
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
2 changes: 2 additions & 0 deletionscoderd/healthcheck/health/model.go
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
6 changes: 6 additions & 0 deletionscodersdk/healthsdk/healthsdk.go
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
73 changes: 73 additions & 0 deletionscodersdk/healthsdk/interfaces.go
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,73 @@ | ||
package healthsdk | ||
import ( | ||
"net" | ||
"tailscale.com/net/interfaces" | ||
"github.com/coder/coder/v2/coderd/healthcheck/health" | ||
) | ||
// @typescript-ignore InterfacesReport | ||
type InterfacesReport struct { | ||
BaseReport | ||
Interfaces []Interface `json:"interfaces"` | ||
} | ||
// @typescript-ignore Interface | ||
type Interface struct { | ||
Name string `json:"name"` | ||
MTU int `json:"mtu"` | ||
Addresses []string `json:"addresses"` | ||
} | ||
func RunInterfacesReport() (InterfacesReport, error) { | ||
st, err := interfaces.GetState() | ||
if err != nil { | ||
return InterfacesReport{}, err | ||
} | ||
return generateInterfacesReport(st), nil | ||
} | ||
func generateInterfacesReport(st *interfaces.State) (report InterfacesReport) { | ||
report.Severity = health.SeverityOK | ||
for name, iface := range st.Interface { | ||
// macOS has a ton of random interfaces, so to keep things helpful, let's filter out any | ||
// that: | ||
// | ||
// - are not enabled | ||
// - don't have any addresses | ||
// - have only link-local addresses (e.g. fe80:...) | ||
if (iface.Flags & net.FlagUp) == 0 { | ||
continue | ||
} | ||
addrs := st.InterfaceIPs[name] | ||
if len(addrs) == 0 { | ||
continue | ||
} | ||
var r bool | ||
healthIface := Interface{ | ||
Name: iface.Name, | ||
MTU: iface.MTU, | ||
} | ||
for _, addr := range addrs { | ||
healthIface.Addresses = append(healthIface.Addresses, addr.String()) | ||
if addr.Addr().IsLinkLocalUnicast() || addr.Addr().IsLinkLocalMulticast() { | ||
continue | ||
} | ||
r = true | ||
} | ||
if !r { | ||
continue | ||
} | ||
report.Interfaces = append(report.Interfaces, healthIface) | ||
if iface.MTU < 1378 { | ||
report.Severity = health.SeverityWarning | ||
report.Warnings = append(report.Warnings, | ||
health.Messagef(health.CodeInterfaceSmallMTU, | ||
"network interface %s has MTU %d (less than 1378), which may cause problems with direct connections", iface.Name, iface.MTU), | ||
) | ||
} | ||
} | ||
return report | ||
} |
192 changes: 192 additions & 0 deletionscodersdk/healthsdk/interfaces_internal_test.go
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,192 @@ | ||
package healthsdk | ||
import ( | ||
"net" | ||
"net/netip" | ||
"strings" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/exp/slices" | ||
"tailscale.com/net/interfaces" | ||
"github.com/coder/coder/v2/coderd/healthcheck/health" | ||
) | ||
func Test_generateInterfacesReport(t *testing.T) { | ||
t.Parallel() | ||
testCases := []struct { | ||
name string | ||
state interfaces.State | ||
severity health.Severity | ||
expectedInterfaces []string | ||
expectedWarnings []string | ||
}{ | ||
{ | ||
name: "Empty", | ||
state: interfaces.State{}, | ||
severity: health.SeverityOK, | ||
expectedInterfaces: []string{}, | ||
}, | ||
{ | ||
name: "Normal", | ||
state: interfaces.State{ | ||
Interface: map[string]interfaces.Interface{ | ||
"en0": {Interface: &net.Interface{ | ||
MTU: 1500, | ||
Name: "en0", | ||
Flags: net.FlagUp, | ||
}}, | ||
"lo0": {Interface: &net.Interface{ | ||
MTU: 65535, | ||
Name: "lo0", | ||
Flags: net.FlagUp, | ||
}}, | ||
}, | ||
InterfaceIPs: map[string][]netip.Prefix{ | ||
"en0": { | ||
netip.MustParsePrefix("192.168.100.1/24"), | ||
netip.MustParsePrefix("fe80::c13:1a92:3fa5:dd7e/64"), | ||
}, | ||
"lo0": { | ||
netip.MustParsePrefix("127.0.0.1/8"), | ||
netip.MustParsePrefix("::1/128"), | ||
netip.MustParsePrefix("fe80::1/64"), | ||
}, | ||
}, | ||
}, | ||
severity: health.SeverityOK, | ||
expectedInterfaces: []string{"en0", "lo0"}, | ||
}, | ||
{ | ||
name: "IgnoreDisabled", | ||
state: interfaces.State{ | ||
Interface: map[string]interfaces.Interface{ | ||
"en0": {Interface: &net.Interface{ | ||
MTU: 1300, | ||
Name: "en0", | ||
Flags: 0, | ||
}}, | ||
"lo0": {Interface: &net.Interface{ | ||
MTU: 65535, | ||
Name: "lo0", | ||
Flags: net.FlagUp, | ||
}}, | ||
}, | ||
InterfaceIPs: map[string][]netip.Prefix{ | ||
"en0": {netip.MustParsePrefix("192.168.100.1/24")}, | ||
"lo0": {netip.MustParsePrefix("127.0.0.1/8")}, | ||
}, | ||
}, | ||
severity: health.SeverityOK, | ||
expectedInterfaces: []string{"lo0"}, | ||
}, | ||
{ | ||
name: "IgnoreLinkLocalOnly", | ||
state: interfaces.State{ | ||
Interface: map[string]interfaces.Interface{ | ||
"en0": {Interface: &net.Interface{ | ||
MTU: 1300, | ||
Name: "en0", | ||
Flags: net.FlagUp, | ||
}}, | ||
"lo0": {Interface: &net.Interface{ | ||
MTU: 65535, | ||
Name: "lo0", | ||
Flags: net.FlagUp, | ||
}}, | ||
}, | ||
InterfaceIPs: map[string][]netip.Prefix{ | ||
"en0": {netip.MustParsePrefix("fe80::1:1/64")}, | ||
"lo0": {netip.MustParsePrefix("127.0.0.1/8")}, | ||
}, | ||
}, | ||
severity: health.SeverityOK, | ||
expectedInterfaces: []string{"lo0"}, | ||
}, | ||
{ | ||
name: "IgnoreNoAddress", | ||
state: interfaces.State{ | ||
Interface: map[string]interfaces.Interface{ | ||
"en0": {Interface: &net.Interface{ | ||
MTU: 1300, | ||
Name: "en0", | ||
Flags: net.FlagUp, | ||
}}, | ||
"lo0": {Interface: &net.Interface{ | ||
MTU: 65535, | ||
Name: "lo0", | ||
Flags: net.FlagUp, | ||
}}, | ||
}, | ||
InterfaceIPs: map[string][]netip.Prefix{ | ||
"en0": {}, | ||
"lo0": {netip.MustParsePrefix("127.0.0.1/8")}, | ||
}, | ||
}, | ||
severity: health.SeverityOK, | ||
expectedInterfaces: []string{"lo0"}, | ||
}, | ||
{ | ||
name: "SmallMTUTunnel", | ||
state: interfaces.State{ | ||
Interface: map[string]interfaces.Interface{ | ||
"en0": {Interface: &net.Interface{ | ||
MTU: 1500, | ||
Name: "en0", | ||
Flags: net.FlagUp, | ||
}}, | ||
"lo0": {Interface: &net.Interface{ | ||
MTU: 65535, | ||
Name: "lo0", | ||
Flags: net.FlagUp, | ||
}}, | ||
"tun0": {Interface: &net.Interface{ | ||
MTU: 1280, | ||
Name: "tun0", | ||
Flags: net.FlagUp, | ||
}}, | ||
}, | ||
InterfaceIPs: map[string][]netip.Prefix{ | ||
"en0": {netip.MustParsePrefix("192.168.100.1/24")}, | ||
"tun0": {netip.MustParsePrefix("10.3.55.9/8")}, | ||
"lo0": {netip.MustParsePrefix("127.0.0.1/8")}, | ||
}, | ||
}, | ||
severity: health.SeverityWarning, | ||
expectedInterfaces: []string{"en0", "lo0", "tun0"}, | ||
expectedWarnings: []string{"tun0"}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
r := generateInterfacesReport(&tc.state) | ||
require.Equal(t, tc.severity, r.Severity) | ||
gotInterfaces := []string{} | ||
for _, i := range r.Interfaces { | ||
gotInterfaces = append(gotInterfaces, i.Name) | ||
} | ||
slices.Sort(gotInterfaces) | ||
slices.Sort(tc.expectedInterfaces) | ||
require.Equal(t, tc.expectedInterfaces, gotInterfaces) | ||
require.Len(t, r.Warnings, len(tc.expectedWarnings), | ||
"expected %d warnings, got %d", len(tc.expectedWarnings), len(r.Warnings)) | ||
for _, name := range tc.expectedWarnings { | ||
found := false | ||
for _, w := range r.Warnings { | ||
if strings.Contains(w.String(), name) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Errorf("missing warning for %s", name) | ||
} | ||
} | ||
}) | ||
} | ||
} |
11 changes: 11 additions & 0 deletionsdocs/admin/healthcheck.md
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.
Uh oh!
There was an error while loading.Please reload this page.
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.