Skip to content

Commit

Permalink
Merge pull request #1232 from Zheaoli/manjusaka/fix-1149
Browse files Browse the repository at this point in the history
Remove duplicate host record when update the DNS
  • Loading branch information
AkihiroSuda authored Jul 17, 2022
2 parents 8203917 + df376b8 commit 8e278e2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
53 changes: 53 additions & 0 deletions cmd/nerdctl/compose_up_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,56 @@ services:
base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK()

}

func TestComposeUpWithExternalNetwork(t *testing.T) {
containerName1 := testutil.Identifier(t) + "-1"
containerName2 := testutil.Identifier(t) + "-2"
networkName := testutil.Identifier(t) + "-network"
var dockerComposeYaml1 = fmt.Sprintf(`
version: "3"
services:
%s:
image: %s
container_name: %s
networks:
%s:
aliases:
- nginx-1
networks:
%s:
external: true
`, containerName1, testutil.NginxAlpineImage, containerName1, networkName, networkName)
var dockerComposeYaml2 = fmt.Sprintf(`
version: "3"
services:
%s:
image: %s
container_name: %s
networks:
%s:
aliases:
- nginx-2
networks:
%s:
external: true
`, containerName2, testutil.NginxAlpineImage, containerName2, networkName, networkName)
comp1 := testutil.NewComposeDir(t, dockerComposeYaml1)
defer comp1.CleanUp()
comp2 := testutil.NewComposeDir(t, dockerComposeYaml2)
defer comp2.CleanUp()
base := testutil.NewBase(t)
// Create the test network
base.Cmd("network", "create", networkName).AssertOK()
defer base.Cmd("network", "rm", networkName).Run()
// Run the first compose
base.ComposeCmd("-f", comp1.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp1.YAMLFullPath(), "down", "-v").Run()
// Run the second compose
base.ComposeCmd("-f", comp2.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp2.YAMLFullPath(), "down", "-v").Run()
// Down the second compose
base.ComposeCmd("-f", comp2.YAMLFullPath(), "down", "-v").AssertOK()
// Run the second compose again
base.ComposeCmd("-f", comp2.YAMLFullPath(), "up", "-d").AssertOK()
base.Cmd("exec", containerName1, "wget", "-qO-", "http://"+containerName2).AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
}
17 changes: 13 additions & 4 deletions pkg/dnsutil/hostsstore/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (u *updater) phase2() error {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("# %s\n", markerBegin))
buf.WriteString("127.0.0.1 localhost localhost.localdomain\n")
buf.WriteString(":1 localhost localhost.localdomain\n")
buf.WriteString("::1 localhost localhost.localdomain\n")

// keep extra hosts first
if u.id == myMeta.ID {
Expand All @@ -146,17 +146,26 @@ func (u *updater) phase2() error {
if err != nil {
return err
}
serviceHosts := make(map[string]string)
for ip, host := range hosts {
if ip == "127.0.0.1" || ip == "::1" {
continue
}
serviceHosts[strings.Join(host, " ")] = ip
}

// TODO: cut off entries for the containers in other networks
for ip, nwName := range u.nwNameByIPStr {
meta := u.metaByIPStr[ip]
if line := createLine(nwName, meta, myNetworks); len(line) != 0 {
hosts[ip] = line
serviceHosts[strings.Join(line, " ")] = ip
}
}
for ip, host := range hosts {
buf.WriteString(fmt.Sprintf("%-15s %s\n", ip, strings.Join(host, " ")))

for line, ip := range serviceHosts {
buf.WriteString(fmt.Sprintf("%-15s %s\n", ip, line))
}

buf.WriteString(fmt.Sprintf("# %s\n", markerEnd))
err = os.WriteFile(path, buf.Bytes(), 0644)
if err != nil {
Expand Down

0 comments on commit 8e278e2

Please sign in to comment.