Skip to content

Commit

Permalink
Add --base-hosts-file flag
Browse files Browse the repository at this point in the history
* Add --base-hosts-file flag to container create, container run, and pod create
* Add tests for base_hosts_file in containers.conf

Signed-off-by: Gavin Lam <[email protected]>
  • Loading branch information
gavinkflam committed Dec 20, 2023
1 parent 65266a3 commit d4afcbb
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,13 @@ func AutocompleteUserNamespace(cmd *cobra.Command, args []string, toComplete str
return results, directive
}

// AutocompleteBaseHostsFile - Autocomplete base hosts file options.
// -> "image", "none", paths
func AutocompleteBaseHostsFile(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
BaseHostsFileModes := []string{"image", "none"}
return BaseHostsFileModes, cobra.ShellCompDirectiveNoSpace
}

// AutocompleteCgroupMode - Autocomplete cgroup mode options.
// -> "enabled", "disabled", "no-conmon", "split"
func AutocompleteCgroupMode(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
7 changes: 7 additions & 0 deletions cmd/podman/common/netflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func DefineNetFlags(cmd *cobra.Command) {
)
_ = cmd.RegisterFlagCompletionFunc(addHostFlagName, completion.AutocompleteNone)

baseHostsFileFlagName := "base-hosts-file"
netFlags.String(
baseHostsFileFlagName, "",
`Path to a hosts file to copy the entries into the container, or one of the special values. ("image"|"none")`,
)
_ = cmd.RegisterFlagCompletionFunc(baseHostsFileFlagName, AutocompleteBaseHostsFile)

dnsFlagName := "dns"
netFlags.StringSlice(
dnsFlagName, podmanConfig.ContainersConf.DNSServers(),
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
if c.Flag("shm-size-systemd").Changed {
vals.ShmSizeSystemd = c.Flag("shm-size-systemd").Value.String()
}
if c.Flag("base-hosts-file").Changed {
vals.BaseHostsFile = c.Flag("base-hosts-file").Value.String()
}
if (c.Flag("dns").Changed || c.Flag("dns-option").Changed || c.Flag("dns-search").Changed) && vals.Net != nil && (vals.Net.Network.NSMode == specgen.NoNetwork || vals.Net.Network.IsContainer()) {
return vals, fmt.Errorf("conflicting options: dns and the network mode: " + string(vals.Net.Network.NSMode))
}
Expand Down
9 changes: 9 additions & 0 deletions docs/source/markdown/options/base-hosts-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
####> This option file is used in:
####> podman create, pod create, run
####> If file is edited, make sure the changes
####> are applicable to all of those.
#### **--base-hosts-file**=*path* | *image* | *none*

BaseHostsFile is the path to a hosts file, the entries from this file are added to the <<containers|pods>> _/etc/hosts_ file.
As special value "image" is allowed which uses the _/etc/hosts_ file from within the image and "none" which uses no base file at all.
If it is empty we should default to the base_hosts_file configuration in _containers.conf_.
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-create.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ and specified with a _tag_.

@@option authfile

@@option base-hosts-file

@@option blkio-weight

@@option blkio-weight-device
Expand Down
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-pod-create.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ For example, if a pod was created via **podman pod create --cpus=5**, specifying

The /etc/hosts file is shared between all containers in the pod.

@@option base-hosts-file

@@option blkio-weight

@@option blkio-weight-device
Expand Down
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-run.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ and specified with a _tag_.

@@option authfile

@@option base-hosts-file

@@option blkio-weight

@@option blkio-weight-device
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type ContainerCreateOptions struct {
Annotation []string
Attach []string
Authfile string
BaseHostsFile string
BlkIOWeight string
BlkIOWeightDevice []string
CapAdd []string
Expand Down
5 changes: 5 additions & 0 deletions pkg/specgenutil/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
if len(s.Hostname) == 0 || len(c.Hostname) != 0 {
s.Hostname = c.Hostname
}

if len(s.BaseHostsFile) == 0 || len(c.BaseHostsFile) != 0 {
s.BaseHostsFile = c.BaseHostsFile
}

sysctl := map[string]string{}
if ctl := c.Sysctl; len(ctl) > 0 {
sysctl, err = util.ValidateSysctls(ctl)
Expand Down
54 changes: 54 additions & 0 deletions test/e2e/containers_conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,60 @@ var _ = Describe("Verify podman containers.conf usage", func() {
Expect(session.OutputToString()).To(ContainSubstring("test"))
})

Describe("base_hosts_file in containers.conf", func() {
var baseHostsFile string
var session *PodmanSessionIntegration

JustBeforeEach(func() {
confFile := filepath.Join(podmanTest.TempDir, "containers.conf")
err = os.WriteFile(confFile, []byte(fmt.Sprintf("[containers]\nbase_hosts_file=\"%s\"\nno_hosts=false\n", baseHostsFile)), 0755)
Expect(err).ToNot(HaveOccurred())
os.Setenv("CONTAINERS_CONF_OVERRIDE", confFile)
if IsRemote() {
podmanTest.RestartRemoteService()
}

session = podmanTest.Podman([]string{"run", "--rm", ALPINE, "cat", "/etc/hosts"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
})

Describe("base_hosts_file=/path/to/hosts", func() {
BeforeEach(func() {
hostsFile := filepath.Join(podmanTest.TempDir, "hosts")
err := os.WriteFile(hostsFile, []byte("12.34.56.78 test.example.com"), 0755)
Expect(err).ToNot(HaveOccurred())

baseHostsFile = hostsFile
})

It("should use the hosts file from the container image", func() {
Expect(session.OutputToString()).To(ContainSubstring("12.34.56.78 test.example.com"))
})
})

Describe("base_hosts_file=image", func() {
BeforeEach(func() {
baseHostsFile = "image"
})

It("should use the hosts file from the container image", func() {
Expect(session.OutputToString()).To(ContainSubstring("localhost localhost.localdomain"))
})
})

Describe("base_hosts_file=none", func() {
BeforeEach(func() {
baseHostsFile = "none"
})

It("should use the hosts file from the container image", func() {
Expect(session.OutputToString()).ToNot(ContainSubstring("localhost.localdomain"))
Expect(session.OutputToString()).To(ContainSubstring("localhost"))
})
})
})

It("seccomp profile path", func() {
configPath := filepath.Join(podmanTest.TempDir, "containers.conf")
os.Setenv("CONTAINERS_CONF", configPath)
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/pod_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,31 @@ var _ = Describe("Podman pod create", func() {
Expect(podCreate).Should(Exit(125))
})

It("podman create pod with --base-hosts-file", func() {
confFile := filepath.Join(podmanTest.TempDir, "containers.conf")
err := os.WriteFile(confFile, []byte("[containers]\nbase_hosts_file=\"none\"\n"), 0755)
Expect(err).ToNot(HaveOccurred())
os.Setenv("CONTAINERS_CONF", confFile)
if IsRemote() {
podmanTest.RestartRemoteService()
}

hostsFile := filepath.Join(podmanTest.TempDir, "hosts")
err = os.WriteFile(hostsFile, []byte("12.34.56.78 test.example.com"), 0755)
Expect(err).ToNot(HaveOccurred())

// Create flag should override containers.conf
name := "test"
podCreate := podmanTest.Podman([]string{"pod", "create", "--base-hosts-file=" + hostsFile, "--name", name})
podCreate.WaitWithDefaultTimeout()
Expect(podCreate).Should(ExitCleanly())

session := podmanTest.Podman([]string{"run", "--pod", name, "--rm", ALPINE, "cat", "/etc/hosts"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(ContainSubstring("12.34.56.78 test.example.com"))
})

It("podman create pod with DNS server set", func() {
name := "test"
server := "12.34.56.78"
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,22 @@ VOLUME %s`, ALPINE, volPath, volPath)
Expect(session).To(ExitWithError())
})

It("podman run with --base-hosts-file", func() {
confFile := filepath.Join(podmanTest.TempDir, "containers.conf")
err = os.WriteFile(confFile, []byte("[containers]\nbase_hosts_file=\"none\"\n"), 0755)
Expect(err).ToNot(HaveOccurred())
os.Setenv("CONTAINERS_CONF", confFile)
if IsRemote() {
podmanTest.RestartRemoteService()
}

// Run flag should override containers.conf
session := podmanTest.Podman([]string{"run", "--rm", "--base-hosts-file=image", ALPINE, "cat", "/etc/hosts"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(ContainSubstring("localhost localhost.localdomain"))
})

It("podman run with restart-policy always restarts containers", func() {
testDir := filepath.Join(podmanTest.RunRoot, "restart-test")
err := os.MkdirAll(testDir, 0755)
Expand Down

0 comments on commit d4afcbb

Please sign in to comment.