client checks kernel module in /sys/module for WSL2 bridge networking (#17306)

This commit is contained in:
Jerome Eteve 2023-06-06 15:26:50 +01:00 committed by GitHub
parent aa1b33d157
commit c26f01eefd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

3
.changelog/17306.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
client: check kernel module in `/sys/module` to help with WSL2 bridge networking
```

View File

@ -56,6 +56,14 @@ func (f *BridgeFingerprint) detect(module string) error {
// accumulate errors from every place we might find the module
var errs error
// Check if the module is in /sys/modules
sysfsModulePath := fmt.Sprintf("/sys/module/%s", module)
if err := f.findDir(sysfsModulePath); err != nil {
errs = multierror.Append(errs, err)
} else {
return nil
}
// check if the module has been dynamically loaded
dynamicPath := "/proc/modules"
if err := f.searchFile(module, dynamicPath, f.regexp(dynamicModuleRe, module)); err != nil {
@ -89,6 +97,14 @@ func (f *BridgeFingerprint) detect(module string) error {
return errs
}
func (f *BridgeFingerprint) findDir(dirname string) error {
if _, err := os.Stat(dirname); err != nil {
return fmt.Errorf("failed to find %s: %v", dirname, err)
} else {
return nil
}
}
func (f *BridgeFingerprint) searchFile(module, filename string, re *regexp.Regexp) error {
file, err := os.Open(filename)
if err != nil {

View File

@ -19,11 +19,11 @@ func TestBridgeFingerprint_detect(t *testing.T) {
ci.Parallel(t)
f := &BridgeFingerprint{logger: testlog.HCLogger(t)}
require.NoError(t, f.detect("ip_tables"))
require.NoError(t, f.detect("kernel")) // kernel should be there.
err := f.detect("nonexistentmodule")
require.Error(t, err)
require.Contains(t, err.Error(), "3 errors occurred")
require.Contains(t, err.Error(), "4 errors occurred")
}
func writeFile(t *testing.T, prefix, content string) string {