Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1815957: fix(dns): fix slow dns resolution of localhost for grpc health probe #227

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/appregistry-server/main.go
Original file line number Diff line number Diff line change
@@ -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"
@@ -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
5 changes: 5 additions & 0 deletions cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
@@ -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"
@@ -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
7 changes: 7 additions & 0 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
@@ -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"
@@ -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
8 changes: 6 additions & 2 deletions cmd/registry-server/main.go
Original file line number Diff line number Diff line change
@@ -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"
)
@@ -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
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)
}
})
}
}