Skip to content

Commit

Permalink
fix(dns): fix slow dns resolution of localhost for grpc health probe
Browse files Browse the repository at this point in the history
  • Loading branch information
ecordell committed Mar 26, 2020
1 parent d2e8855 commit 57183c2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/appregistry-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/appregistry"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -61,6 +62,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
// Ensure there is a default nsswitch config
if err := dns.EnsureNsswitch(); err != nil {
return err
}
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -67,6 +68,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
// Ensure there is a default nsswitch config
if err := dns.EnsureNsswitch(); err != nil {
return err
}
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/server"
"github.com/operator-framework/operator-registry/pkg/sqlite"
Expand Down Expand Up @@ -53,6 +54,12 @@ func serveFunc(cmd *cobra.Command, args []string) error {
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}

// Ensure there is a default nsswitch config
if err := dns.EnsureNsswitch(); err != nil {
return err
}

dbName, err := cmd.Flags().GetString("database")
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions cmd/registry-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/operator-framework/operator-registry/pkg/lib/log"

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/server"
"github.com/operator-framework/operator-registry/pkg/sqlite"
)
Expand Down Expand Up @@ -60,6 +60,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
// Ensure there is a default nsswitch config
if err := dns.EnsureNsswitch(); err != nil {
return err
}
dbName, err := cmd.Flags().GetString("database")
if err != nil {
return err
Expand Down
28 changes: 28 additions & 0 deletions pkg/lib/dns/nsswitch.go
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)
}
73 changes: 73 additions & 0 deletions pkg/lib/dns/nsswitch_test.go
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)
}
})
}
}

0 comments on commit 57183c2

Please sign in to comment.