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

qfix: restore default resolv.conf file on application restarting/stopping #1288

Merged
Merged
Show file tree
Hide file tree
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
42 changes: 36 additions & 6 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ import (
)

type dnsContextClient struct {
chainContext context.Context
coreFilePath string
resolveConfigPath string
defaultNameServerIP string
dnsConfigManager dnscontext.Manager
updateCorefileQueue serialize.Executor
chainContext context.Context
coreFilePath string
resolveConfigPath string
storedResolvConfigPath string
defaultNameServerIP string
dnsConfigManager dnscontext.Manager
updateCorefileQueue serialize.Executor
}

// NewClient creates a new DNS client chain component. Setups all DNS traffic to the localhost. Monitors DNS configs from connections.
Expand All @@ -56,6 +57,7 @@ func NewClient(options ...DNSOption) networkservice.NetworkServiceClient {
for _, o := range options {
o.apply(c)
}
c.storedResolvConfigPath = c.resolveConfigPath + ".restore"
c.initialize()
return c
}
Expand Down Expand Up @@ -97,13 +99,41 @@ func (c *dnsContextClient) Close(ctx context.Context, conn *networkservice.Conne
return next.Client(ctx).Close(ctx, conn, opts...)
}

func (c *dnsContextClient) restoreResolvConf() {
originalResolvConf, err := ioutil.ReadFile(c.storedResolvConfigPath)
if err != nil || len(originalResolvConf) == 0 {
return
}
_ = os.WriteFile(c.resolveConfigPath, originalResolvConf, os.ModePerm)
}

func (c *dnsContextClient) storeOriginalResolvConf() {
if _, err := os.Stat(c.storedResolvConfigPath); err == nil {
return
}
originalResolvConf, err := ioutil.ReadFile(c.resolveConfigPath)
if err != nil {
return
}
_ = ioutil.WriteFile(c.storedResolvConfigPath, originalResolvConf, os.ModePerm)
}

func (c *dnsContextClient) initialize() {
c.restoreResolvConf()

r, err := dnscontext.OpenResolveConfig(c.resolveConfigPath)
if err != nil {
log.FromContext(c.chainContext).Errorf("An error during open resolve config: %v", err.Error())
return
}

c.storeOriginalResolvConf()

go func() {
defer c.restoreResolvConf()
<-c.chainContext.Done()
}()

c.dnsConfigManager.Store("", &networkservice.DNSConfig{
SearchDomains: r.Value(dnscontext.AnyDomain),
DnsServerIps: r.Value(dnscontext.NameserverProperty),
Expand Down
32 changes: 32 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
)

func Test_DNSContextClient_Restart(t *testing.T) {
corefilePath := filepath.Join(t.TempDir(), "corefile")
resolveConfigPath := filepath.Join(t.TempDir(), "resolv.conf")
err := ioutil.WriteFile(resolveConfigPath, []byte("nameserver 8.8.4.4\n"), os.ModePerm)
require.NoError(t, err)
const expectedEmptyCorefile = `. {
fanout . 8.8.4.4
log
reload
cache {
denial 0
}
}`
for i := 0; i < 1; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
var c = chain.NewNetworkServiceClient(
dnscontext.NewClient(
dnscontext.WithCorefilePath(corefilePath),
dnscontext.WithResolveConfigPath(resolveConfigPath),
dnscontext.WithChainContext(ctx),
),
)
_, _ = c.Request(ctx, &networkservice.NetworkServiceRequest{})

requireFileChanged(ctx, t, corefilePath, expectedEmptyCorefile)

cancel()
}
}

func Test_DNSContextClient_Usecases(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand All @@ -50,6 +81,7 @@ func Test_DNSContextClient_Usecases(t *testing.T) {
dnscontext.NewClient(
dnscontext.WithCorefilePath(corefilePath),
dnscontext.WithResolveConfigPath(resolveConfigPath),
dnscontext.WithChainContext(ctx),
),
)

Expand Down