Skip to content

Commit

Permalink
Add --hosts-file flag to container and pod commands
Browse files Browse the repository at this point in the history
* Add --hosts-file flag to container create, container run and pod create
* Add HostsFile field to pod inspect and container inspect results
* Test BaseHostsFile config in containers.conf
* Rename container BaseHostsFile config to HostsFile

Signed-off-by: Gavin Lam <[email protected]>
  • Loading branch information
gavinkflam committed Nov 18, 2024
1 parent 1712594 commit 6680d56
Show file tree
Hide file tree
Showing 28 changed files with 413 additions and 32 deletions.
7 changes: 7 additions & 0 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,13 @@ func AutocompleteNetworks(cmd *cobra.Command, args []string, toComplete string)
return getNetworks(cmd, toComplete, completeDefault)
}

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

// AutocompleteDefaultOneArg - Autocomplete path only for the first argument.
func AutocompleteDefaultOneArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
Expand Down
14 changes: 14 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)

hostsFileFlagName := "hosts-file"
netFlags.String(
hostsFileFlagName, "",
`Base file to create the /etc/hosts file inside the container, or one of the special values. ("image"|"none")`,
)
_ = cmd.RegisterFlagCompletionFunc(hostsFileFlagName, AutocompleteHostsFile)

dnsFlagName := "dns"
netFlags.StringSlice(
dnsFlagName, podmanConfig.ContainersConf.DNSServers(),
Expand Down Expand Up @@ -116,6 +123,13 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
}
}

if flags.Changed("hosts-file") {
opts.HostsFile, err = flags.GetString("hosts-file")
if err != nil {
return nil, err
}
}

if flags.Changed("dns") {
servers, err := flags.GetStringSlice("dns")
if err != nil {
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 @@ -323,6 +323,9 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
if noHosts && c.Flag("add-host").Changed {
return vals, errors.New("--no-hosts and --add-host cannot be set together")
}
if noHosts && c.Flag("hosts-file").Changed {
return vals, errors.New("--no-hosts and --hosts-file cannot be set together")
}

if !isInfra && c.Flag("entrypoint").Changed {
val := c.Flag("entrypoint").Value.String()
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/pods/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func create(cmd *cobra.Command, args []string) error {
}
podSpec.InfraContainerSpec = specgen.NewSpecGenerator(imageName, false)
podSpec.InfraContainerSpec.RawImageName = rawImageName
podSpec.InfraContainerSpec.HostsFile = podSpec.PodNetworkConfig.HostsFile
podSpec.InfraContainerSpec.NetworkOptions = podSpec.NetworkOptions
podSpec.InfraContainerSpec.RestartPolicy = podSpec.RestartPolicy
err = specgenutil.FillOutSpecGen(podSpec.InfraContainerSpec, &infraOptions, []string{})
Expand Down
12 changes: 12 additions & 0 deletions docs/source/markdown/options/hosts-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
####> 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.
#### **--hosts-file**=*path* | *none* | *image*

Base file to create the `/etc/hosts` file inside the container. This must either
be an absolute path to a file on the host system, or one of the following
special flags:
"" Follow the `base_hosts_file` configuration in _containers.conf_ (the default)
`none` Do not use a base file (i.e. start with an empty file)
`image` Use the container image's `/etc/hosts` file as base file
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 @@ -199,6 +199,8 @@ Print usage statement

@@option hostname.container

@@option hosts-file

@@option hostuser

@@option http-proxy
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 @@ -88,6 +88,8 @@ Print usage statement.

@@option hostname.pod

@@option hosts-file

#### **--infra**

Create an infra container and associate it with the pod. An infra container is a lightweight container used to coordinate the shared kernel namespace of a pod. Default: true.
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 @@ -233,6 +233,8 @@ Print usage statement

@@option hostname.container

@@option hosts-file

@@option hostuser

@@option http-proxy
Expand Down
11 changes: 5 additions & 6 deletions libpod/container_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ type ContainerNetworkConfig struct {
// bind-mounted inside the container.
// Conflicts with HostAdd.
UseImageHosts bool
// BaseHostsFile is the path to a hosts file, the entries from this file
// are added to the containers 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.
BaseHostsFile string `json:"baseHostsFile,omitempty"`
// Hosts to add in container
// Will be appended to host's host file
HostAdd []string `json:"hostsAdd,omitempty"`
// HostsFile is the base file to create the `/etc/hosts` file inside the container.
// This must either be an absolute path to a file on the host system, or one of the
// special flags `image` or `none`.
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
HostsFile string `json:"hostsFile,omitempty"`
// Network names with the network specific options.
// Please note that these can be altered at runtime. The actual list is
// stored in the DB and should be retrieved from there via c.networks()
Expand Down
2 changes: 2 additions & 0 deletions libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named
hostConfig.GroupAdd = make([]string, 0, len(c.config.Groups))
hostConfig.GroupAdd = append(hostConfig.GroupAdd, c.config.Groups...)

hostConfig.HostsFile = c.config.HostsFile

if ctrSpec.Process != nil {
if ctrSpec.Process.OOMScoreAdj != nil {
hostConfig.OomScoreAdj = *ctrSpec.Process.OOMScoreAdj
Expand Down
10 changes: 5 additions & 5 deletions libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2297,13 +2297,13 @@ func (c *Container) addHosts() error {
return fmt.Errorf("failed to get container ip host entries: %w", err)
}

// Consider container level BaseHostsFile configuration first.
// Consider container level value first.
// If it is empty, fallback to containers.conf level configuration.
baseHostsFileConf := c.config.BaseHostsFile
if baseHostsFileConf == "" {
baseHostsFileConf = c.runtime.config.Containers.BaseHostsFile
hostsFile := c.config.HostsFile
if hostsFile == "" {
hostsFile = c.runtime.config.Containers.BaseHostsFile
}
baseHostFile, err := etchosts.GetBaseHostFile(baseHostsFileConf, c.state.Mountpoint)
baseHostFile, err := etchosts.GetBaseHostFile(hostsFile, c.state.Mountpoint)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions libpod/define/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ type InspectContainerHostConfig struct {
// ExtraHosts contains hosts that will be added to the container's
// /etc/hosts.
ExtraHosts []string `json:"ExtraHosts"`
// HostsFile is the base file to create the `/etc/hosts` file inside the container.
HostsFile string `json:"HostsFile"`
// GroupAdd contains groups that the user inside the container will be
// added to.
GroupAdd []string `json:"GroupAdd"`
Expand Down
3 changes: 3 additions & 0 deletions libpod/define/pod_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type InspectPodInfraConfig struct {
// HostAdd adds a number of hosts to the infra container's resolv.conf
// which will be shared with the rest of the pod.
HostAdd []string
// HostsFile is the base file to create the `/etc/hosts` file inside the infra container
// which will be shared with the rest of the pod.
HostsFile string
// Networks is a list of networks the pod will join.
Networks []string
// NetworkOptions are additional options for each network
Expand Down
6 changes: 3 additions & 3 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2407,14 +2407,14 @@ func WithGroupEntry(groupEntry string) CtrCreateOption {
}
}

// WithBaseHostsFile sets the option to copy /etc/hosts file.
func WithBaseHostsFile(baseHostsFile string) CtrCreateOption {
// WithHostsFile sets the option to copy /etc/hosts file.
func WithHostsFile(hostsFile string) CtrCreateOption {
return func(ctr *Container) error {
if ctr.valid {
return define.ErrCtrFinalized
}

ctr.config.BaseHostsFile = baseHostsFile
ctr.config.HostsFile = hostsFile

return nil
}
Expand Down
3 changes: 3 additions & 0 deletions libpod/pod_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,9 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
infraConfig.HostAdd = make([]string, 0, len(infra.config.HostAdd))
infraConfig.HostAdd = append(infraConfig.HostAdd, infra.config.HostAdd...)
}
if len(infra.config.HostsFile) > 0 {
infraConfig.HostsFile = infra.config.HostsFile
}

networks, err := infra.networks()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/handlers/compat/containers_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
localTrue := true
sg.CreateWorkingDir = &localTrue
// moby doesn't inherit /etc/hosts from host
sg.BaseHostsFile = "none"
sg.HostsFile = "none"

ic := abi.ContainerEngine{Libpod: runtime}
report, err := ic.ContainerCreate(r.Context(), sg)
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 @@ -368,6 +368,7 @@ func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.Pod
s.DNSOption = p.Net.DNSOptions
s.NoManageHosts = p.Net.NoHosts
s.HostAdd = p.Net.AddHosts
s.HostsFile = p.Net.HostsFile
}

// Cgroup
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type NetOptions struct {
DNSOptions []string `json:"dns_option,omitempty"`
DNSSearch []string `json:"dns_search,omitempty"`
DNSServers []net.IP `json:"dns_server,omitempty"`
HostsFile string `json:"hosts_file,omitempty"`
Network specgen.Namespace `json:"netns,omitempty"`
NoHosts bool `json:"no_manage_hosts,omitempty"`
PublishPorts []types.PortMapping `json:"portmappings,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/specgen/generate/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *l
if s.GroupEntry != "" {
options = append(options, libpod.WithGroupEntry(s.GroupEntry))
}
if s.BaseHostsFile != "" {
options = append(options, libpod.WithBaseHostsFile(s.BaseHostsFile))
if s.HostsFile != "" {
options = append(options, libpod.WithHostsFile(s.HostsFile))
}

if s.IsPrivileged() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/specgen/generate/pod_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func MapSpec(p *specgen.PodSpecGenerator) (*specgen.SpecGenerator, error) {
if len(p.HostAdd) > 0 {
spec.HostAdd = p.HostAdd
}
if len(p.HostsFile) > 0 {
spec.HostsFile = p.HostsFile
}
if len(p.DNSServer) > 0 {
var dnsServers []net.IP
dnsServers = append(dnsServers, p.DNSServer...)
Expand Down
12 changes: 10 additions & 2 deletions pkg/specgen/pod_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (p *PodSpecGenerator) Validate() error {
if len(p.HostAdd) > 0 {
return exclusivePodOptions("NoInfra", "HostAdd")
}
if len(p.HostsFile) > 0 {
return exclusivePodOptions("NoInfra", "HostsFile")
}
if p.NoManageResolvConf {
return exclusivePodOptions("NoInfra", "NoManageResolvConf")
}
Expand All @@ -79,8 +82,13 @@ func (p *PodSpecGenerator) Validate() error {
return exclusivePodOptions("NoManageResolvConf", "DNSOption")
}
}
if p.NoManageHosts && len(p.HostAdd) > 0 {
return exclusivePodOptions("NoManageHosts", "HostAdd")
if p.NoManageHosts {
if len(p.HostAdd) > 0 {
return exclusivePodOptions("NoManageHosts", "HostAdd")
}
if len(p.HostsFile) > 0 {
return exclusivePodOptions("NoManageHosts", "HostsFile")
}
}

return nil
Expand Down
7 changes: 7 additions & 0 deletions pkg/specgen/podspecgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ type PodNetworkConfig struct {
// Conflicts with NoInfra=true and NoManageHosts.
// Optional.
HostAdd []string `json:"hostadd,omitempty"`
// HostsFile is the base file to create the `/etc/hosts` file inside the infra container.
// This must either be an absolute path to a file on the host system, or one of the
// special flags `image` or `none`.
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
// Conflicts with NoInfra=true and NoManageHosts.
// Optional.
HostsFile string `json:"hostsFile,omitempty"`
// NetworkOptions are additional options for each network
// Optional.
NetworkOptions map[string][]string `json:"network_options,omitempty"`
Expand Down
14 changes: 7 additions & 7 deletions pkg/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,18 +539,18 @@ type ContainerNetworkConfig struct {
// Conflicts with HostAdd.
// Optional.
UseImageHosts *bool `json:"use_image_hosts,omitempty"`
// BaseHostsFile is the path to a hosts file, the entries from this file
// are added to the containers 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.
// Optional.
BaseHostsFile string `json:"base_hosts_file,omitempty"`
// HostAdd is a set of hosts which will be added to the container's
// /etc/hosts file.
// Conflicts with UseImageHosts.
// Optional.
HostAdd []string `json:"hostadd,omitempty"`
// HostsFile is the base file to create the `/etc/hosts` file inside the infra container.
// This must either be an absolute path to a file on the host system, or one of the
// special flags `image` or `none`.
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
// Conflicts with UseImageHosts.
// Optional.
HostsFile string `json:"hosts_file,omitempty"`
// NetworkOptions are additional options for each network
// Optional.
NetworkOptions map[string][]string `json:"network_options,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/specgenutil/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions

if c.Net != nil {
s.HostAdd = c.Net.AddHosts
s.HostsFile = c.Net.HostsFile
s.UseImageResolvConf = &c.Net.UseImageResolvConf
s.DNSServers = c.Net.DNSServers
s.DNSSearch = c.Net.DNSSearch
Expand Down
13 changes: 8 additions & 5 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,14 @@ func (p *PodmanTestIntegration) RunLsContainerInPod(name, pod string) (*PodmanSe

// BuildImage uses podman build and buildah to build an image
// called imageName based on a string dockerfile
func (p *PodmanTestIntegration) BuildImage(dockerfile, imageName string, layers string) string {
return p.buildImage(dockerfile, imageName, layers, "")
func (p *PodmanTestIntegration) BuildImage(dockerfile, imageName string, layers string, extraOptions ...string) string {
return p.buildImage(dockerfile, imageName, layers, "", extraOptions)
}

// BuildImageWithLabel uses podman build and buildah to build an image
// called imageName based on a string dockerfile, adds desired label to paramset
func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string, layers string, label string) string {
return p.buildImage(dockerfile, imageName, layers, label)
func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string, layers string, label string, extraOptions ...string) string {
return p.buildImage(dockerfile, imageName, layers, label, extraOptions)
}

// PodmanPID execs podman and returns its PID
Expand Down Expand Up @@ -1299,7 +1299,7 @@ func (s *PodmanSessionIntegration) jq(jqCommand string) (string, error) {
return strings.TrimRight(out.String(), "\n"), err
}

func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers string, label string) string {
func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers string, label string, extraOptions []string) string {
dockerfilePath := filepath.Join(p.TempDir, "Dockerfile-"+stringid.GenerateRandomID())
err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0755)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -1310,6 +1310,9 @@ func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers
if len(imageName) > 0 {
cmd = append(cmd, []string{"-t", imageName}...)
}
if len(extraOptions) > 0 {
cmd = append(cmd, extraOptions...)
}
cmd = append(cmd, p.TempDir)
session := p.Podman(cmd)
session.Wait(240)
Expand Down
Loading

0 comments on commit 6680d56

Please sign in to comment.