File tree 6 files changed +124
-2
lines changed
6 files changed +124
-2
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import (
12
12
"github.com/operator-framework/operator-registry/pkg/api"
13
13
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
14
14
"github.com/operator-framework/operator-registry/pkg/appregistry"
15
+ "github.com/operator-framework/operator-registry/pkg/lib/dns"
15
16
"github.com/operator-framework/operator-registry/pkg/lib/log"
16
17
"github.com/operator-framework/operator-registry/pkg/registry"
17
18
"github.com/operator-framework/operator-registry/pkg/server"
@@ -61,6 +62,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
61
62
if err != nil {
62
63
logrus .WithError (err ).Warn ("unable to set termination log path" )
63
64
}
65
+ // Ensure there is a default nsswitch config
66
+ if err := dns .EnsureNsswitch (); err != nil {
67
+ return err
68
+ }
64
69
kubeconfig , err := cmd .Flags ().GetString ("kubeconfig" )
65
70
if err != nil {
66
71
return err
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import (
17
17
18
18
"github.com/operator-framework/operator-registry/pkg/api"
19
19
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
20
+ "github.com/operator-framework/operator-registry/pkg/lib/dns"
20
21
"github.com/operator-framework/operator-registry/pkg/lib/log"
21
22
"github.com/operator-framework/operator-registry/pkg/registry"
22
23
"github.com/operator-framework/operator-registry/pkg/server"
@@ -67,6 +68,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
67
68
if err != nil {
68
69
logrus .WithError (err ).Warn ("unable to set termination log path" )
69
70
}
71
+ // Ensure there is a default nsswitch config
72
+ if err := dns .EnsureNsswitch (); err != nil {
73
+ return err
74
+ }
70
75
kubeconfig , err := cmd .Flags ().GetString ("kubeconfig" )
71
76
if err != nil {
72
77
return err
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
14
14
"github.com/operator-framework/operator-registry/pkg/api"
15
15
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
16
+ "github.com/operator-framework/operator-registry/pkg/lib/dns"
16
17
"github.com/operator-framework/operator-registry/pkg/lib/log"
17
18
"github.com/operator-framework/operator-registry/pkg/server"
18
19
"github.com/operator-framework/operator-registry/pkg/sqlite"
@@ -53,6 +54,12 @@ func serveFunc(cmd *cobra.Command, args []string) error {
53
54
if err != nil {
54
55
logrus .WithError (err ).Warn ("unable to set termination log path" )
55
56
}
57
+
58
+ // Ensure there is a default nsswitch config
59
+ if err := dns .EnsureNsswitch (); err != nil {
60
+ return err
61
+ }
62
+
56
63
dbName , err := cmd .Flags ().GetString ("database" )
57
64
if err != nil {
58
65
return err
Original file line number Diff line number Diff line change @@ -11,10 +11,10 @@ import (
11
11
"google.golang.org/grpc"
12
12
"google.golang.org/grpc/reflection"
13
13
14
- "github.com/operator-framework/operator-registry/pkg/lib/log"
15
-
16
14
"github.com/operator-framework/operator-registry/pkg/api"
17
15
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"
18
18
"github.com/operator-framework/operator-registry/pkg/server"
19
19
"github.com/operator-framework/operator-registry/pkg/sqlite"
20
20
)
@@ -60,6 +60,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
60
60
if err != nil {
61
61
logrus .WithError (err ).Warn ("unable to set termination log path" )
62
62
}
63
+ // Ensure there is a default nsswitch config
64
+ if err := dns .EnsureNsswitch (); err != nil {
65
+ return err
66
+ }
63
67
dbName , err := cmd .Flags ().GetString ("database" )
64
68
if err != nil {
65
69
return err
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments