forked from openshift/operator-framework-olm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dns): fix slow dns resolution of localhost for grpc health probe
see: golang/go#35305 (upstream operator-registry commit: 597b6ba90b2b13e3e0d1b90167fb1b6452505817)
- Loading branch information
1 parent
ad74703
commit cc093eb
Showing
6 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,28 @@ | ||
package dns | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
var ( | ||
GOOS = runtime.GOOS | ||
NsswitchContents = []byte("hosts: files dns") | ||
NsswitchFilename = "/etc/nsswitch.conf" | ||
) | ||
|
||
func EnsureNsswitch() error { | ||
// only linux supports nsswitch | ||
if GOOS != "linux" { | ||
return nil | ||
} | ||
|
||
// if the file already exists, don't overwrite it | ||
_, err := os.Stat(NsswitchFilename) | ||
if !os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
return ioutil.WriteFile(NsswitchFilename, NsswitchContents, 0644) | ||
} |
This file contains 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 dns | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestEnsureNsswitch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
goos string | ||
existingFile bool | ||
wantFile bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no file", | ||
goos: "linux", | ||
existingFile: false, | ||
wantFile: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "existing file", | ||
goos: "linux", | ||
existingFile: true, | ||
wantFile: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "windows", | ||
goos: "windows", | ||
wantFile: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "mac", | ||
goos: "darwin", | ||
wantFile: false, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
GOOS = tt.goos | ||
// don't want to overwrite the real nsswitch | ||
NsswitchFilename = "testfile" | ||
|
||
if tt.existingFile { | ||
require.NoError(t, ioutil.WriteFile(NsswitchFilename, []byte("test"), 0644)) | ||
} | ||
|
||
if err := EnsureNsswitch(); (err != nil) != tt.wantErr { | ||
t.Errorf("EnsureNsswitch() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
if tt.wantFile { | ||
contents, err := ioutil.ReadFile(NsswitchFilename) | ||
require.NoError(t, err) | ||
require.Equal(t, NsswitchContents, contents) | ||
os.Remove(NsswitchFilename) | ||
} | ||
if tt.existingFile { | ||
contents, err := ioutil.ReadFile(NsswitchFilename) | ||
require.NoError(t, err) | ||
require.NotEqual(t, NsswitchContents, contents) | ||
os.Remove(NsswitchFilename) | ||
} | ||
}) | ||
} | ||
} |