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
see: golang/go#35305
(upstream operator-registry commit: 597b6ba90b2b13e3e0d1b90167fb1b6452505817)
  • Loading branch information
ecordell authored and ankitathomas committed Apr 5, 2021
1 parent ad74703 commit cc093eb
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
5 changes: 5 additions & 0 deletions staging/operator-registry/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 staging/operator-registry/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 staging/operator-registry/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 staging/operator-registry/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 staging/operator-registry/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 staging/operator-registry/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 cc093eb

Please sign in to comment.