Skip to content

Commit 1186793

Browse files
authoredMar 30, 2020
Merge pull request #227 from ecordell/nsswitch
fix(dns): fix slow dns resolution of localhost for grpc health probe
2 parents a204357 + 597b6ba commit 1186793

File tree

6 files changed

+124
-2
lines changed

6 files changed

+124
-2
lines changed
 

‎cmd/appregistry-server/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/operator-framework/operator-registry/pkg/api"
1313
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
1414
"github.com/operator-framework/operator-registry/pkg/appregistry"
15+
"github.com/operator-framework/operator-registry/pkg/lib/dns"
1516
"github.com/operator-framework/operator-registry/pkg/lib/log"
1617
"github.com/operator-framework/operator-registry/pkg/registry"
1718
"github.com/operator-framework/operator-registry/pkg/server"
@@ -61,6 +62,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
6162
if err != nil {
6263
logrus.WithError(err).Warn("unable to set termination log path")
6364
}
65+
// Ensure there is a default nsswitch config
66+
if err := dns.EnsureNsswitch(); err != nil {
67+
return err
68+
}
6469
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
6570
if err != nil {
6671
return err

‎cmd/configmap-server/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/operator-framework/operator-registry/pkg/api"
1919
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
20+
"github.com/operator-framework/operator-registry/pkg/lib/dns"
2021
"github.com/operator-framework/operator-registry/pkg/lib/log"
2122
"github.com/operator-framework/operator-registry/pkg/registry"
2223
"github.com/operator-framework/operator-registry/pkg/server"
@@ -67,6 +68,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
6768
if err != nil {
6869
logrus.WithError(err).Warn("unable to set termination log path")
6970
}
71+
// Ensure there is a default nsswitch config
72+
if err := dns.EnsureNsswitch(); err != nil {
73+
return err
74+
}
7075
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
7176
if err != nil {
7277
return err

‎cmd/opm/registry/serve.go

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/operator-framework/operator-registry/pkg/api"
1515
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
16+
"github.com/operator-framework/operator-registry/pkg/lib/dns"
1617
"github.com/operator-framework/operator-registry/pkg/lib/log"
1718
"github.com/operator-framework/operator-registry/pkg/server"
1819
"github.com/operator-framework/operator-registry/pkg/sqlite"
@@ -53,6 +54,12 @@ func serveFunc(cmd *cobra.Command, args []string) error {
5354
if err != nil {
5455
logrus.WithError(err).Warn("unable to set termination log path")
5556
}
57+
58+
// Ensure there is a default nsswitch config
59+
if err := dns.EnsureNsswitch(); err != nil {
60+
return err
61+
}
62+
5663
dbName, err := cmd.Flags().GetString("database")
5764
if err != nil {
5865
return err

‎cmd/registry-server/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"google.golang.org/grpc"
1212
"google.golang.org/grpc/reflection"
1313

14-
"github.com/operator-framework/operator-registry/pkg/lib/log"
15-
1614
"github.com/operator-framework/operator-registry/pkg/api"
1715
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
16+
"github.com/operator-framework/operator-registry/pkg/lib/dns"
17+
"github.com/operator-framework/operator-registry/pkg/lib/log"
1818
"github.com/operator-framework/operator-registry/pkg/server"
1919
"github.com/operator-framework/operator-registry/pkg/sqlite"
2020
)
@@ -60,6 +60,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
6060
if err != nil {
6161
logrus.WithError(err).Warn("unable to set termination log path")
6262
}
63+
// Ensure there is a default nsswitch config
64+
if err := dns.EnsureNsswitch(); err != nil {
65+
return err
66+
}
6367
dbName, err := cmd.Flags().GetString("database")
6468
if err != nil {
6569
return err

‎pkg/lib/dns/nsswitch.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dns
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"runtime"
7+
)
8+
9+
var (
10+
GOOS = runtime.GOOS
11+
NsswitchContents = []byte("hosts: files dns")
12+
NsswitchFilename = "/etc/nsswitch.conf"
13+
)
14+
15+
func EnsureNsswitch() error {
16+
// only linux supports nsswitch
17+
if GOOS != "linux" {
18+
return nil
19+
}
20+
21+
// if the file already exists, don't overwrite it
22+
_, err := os.Stat(NsswitchFilename)
23+
if !os.IsNotExist(err) {
24+
return nil
25+
}
26+
27+
return ioutil.WriteFile(NsswitchFilename, NsswitchContents, 0644)
28+
}

‎pkg/lib/dns/nsswitch_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dns
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"io/ioutil"
6+
"os"
7+
"testing"
8+
)
9+
10+
func TestEnsureNsswitch(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
goos string
14+
existingFile bool
15+
wantFile bool
16+
wantErr bool
17+
}{
18+
{
19+
name: "no file",
20+
goos: "linux",
21+
existingFile: false,
22+
wantFile: true,
23+
wantErr: false,
24+
},
25+
{
26+
name: "existing file",
27+
goos: "linux",
28+
existingFile: true,
29+
wantFile: false,
30+
wantErr: false,
31+
},
32+
{
33+
name: "windows",
34+
goos: "windows",
35+
wantFile: false,
36+
wantErr: false,
37+
},
38+
{
39+
name: "mac",
40+
goos: "darwin",
41+
wantFile: false,
42+
wantErr: false,
43+
},
44+
}
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
GOOS = tt.goos
48+
// don't want to overwrite the real nsswitch
49+
NsswitchFilename = "testfile"
50+
51+
if tt.existingFile {
52+
require.NoError(t, ioutil.WriteFile(NsswitchFilename, []byte("test"), 0644))
53+
}
54+
55+
if err := EnsureNsswitch(); (err != nil) != tt.wantErr {
56+
t.Errorf("EnsureNsswitch() error = %v, wantErr %v", err, tt.wantErr)
57+
}
58+
59+
if tt.wantFile {
60+
contents, err := ioutil.ReadFile(NsswitchFilename)
61+
require.NoError(t, err)
62+
require.Equal(t, NsswitchContents, contents)
63+
os.Remove(NsswitchFilename)
64+
}
65+
if tt.existingFile {
66+
contents, err := ioutil.ReadFile(NsswitchFilename)
67+
require.NoError(t, err)
68+
require.NotEqual(t, NsswitchContents, contents)
69+
os.Remove(NsswitchFilename)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)
Please sign in to comment.