Skip to content

Commit

Permalink
Allow Podman to send the container's hostname to Netavark
Browse files Browse the repository at this point in the history
* Added ContainerHostname to NetworkOptions. Podman will set this
  and Netavark will read it.

* Added the `container_name_as_hostname` option to the
  CONTAINERS table in containers.conf.  Currently, if you don't
  explicitly set a hostname when creating a container, podman will
  set it to the short ID. If this option set to `true` and a
  hostname isn't explicitly set, podman will use the container's
  name, with characters not in the set `[0-9a-zA-Z.-]` removed,
  as the hostname instead of the short ID. Set to false by default
  to preserve existing behavior.

Signed-off-by: George Joseph <[email protected]>
  • Loading branch information
gtjoseph committed Nov 28, 2024
1 parent 6ed8cc7 commit 2a3e1c1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/containers.conf.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ Options are:
`private` Create private Cgroup Namespace for the container.
`host` Share host Cgroup Namespace with the container.

**container_name_as_hostname**=true|false

When no hostname is set for a container, use the container's name, with
characters not valid for a hostname removed, as the hostname instead of
the first 12 characters of the container's ID. Containers not running
in a private UTS namespace will have their hostname set to the host's
hostname regardless of this setting.

Default is false.

**default_capabilities**=[]

List of default capabilities for containers.
Expand Down
4 changes: 3 additions & 1 deletion libnetwork/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ type PerNetworkOptions struct {
type NetworkOptions struct {
// ContainerID is the container id, used for iptables comments and ipam allocation.
ContainerID string `json:"container_id"`
// ContainerName is the container name, used as dns name.
// ContainerName is the container name.
ContainerName string `json:"container_name"`
// PortMappings contains the port mappings for this container
PortMappings []PortMapping `json:"port_mappings,omitempty"`
Expand All @@ -287,6 +287,8 @@ type NetworkOptions struct {
// List of custom DNS server for podman's DNS resolver.
// Priority order will be kept as defined by user in the configuration.
DNSServers []string `json:"dns_servers,omitempty"`
// ContainerHostname is the configured DNS hostname of the container.
ContainerHostname string `json:"container_hostname"`
}

// PortMapping is one or more ports that will be mapped into the container.
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ type ContainersConfig struct {
// "memory.high=1073741824" sets the memory.high limit to 1GB.
CgroupConf attributedstring.Slice `toml:"cgroup_conf,omitempty"`

// When no hostname is set for a container, use the container's name, with
// characters not valid for a hostname removed, as the hostname instead of
// the first 12 characters of the container's ID. Containers not running
// in a private UTS namespace will have their hostname set to the host's
// hostname regardless of this setting.
ContainerNameAsHostName bool `toml:"container_name_as_hostname,omitempty"`

// Capabilities to add to all containers.
DefaultCapabilities attributedstring.Slice `toml:"default_capabilities,omitempty"`

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ image_copy_tmp_dir="storage"`
// prior to reading local config, shows hard coded defaults
gomega.Expect(defaultConfig.Containers.HTTPProxy).To(gomega.BeTrue())
gomega.Expect(defaultConfig.Engine.HealthcheckEvents).To(gomega.BeTrue())
gomega.Expect(defaultConfig.Containers.ContainerNameAsHostName).To(gomega.BeFalse())

err := readConfigFromFile("testdata/containers_default.conf", defaultConfig, false)

Expand Down Expand Up @@ -322,6 +323,7 @@ image_copy_tmp_dir="storage"`
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defaultConfig.Engine.CgroupManager).To(gomega.Equal("systemd"))
gomega.Expect(defaultConfig.Containers.ContainerNameAsHostName).To(gomega.BeTrue())
gomega.Expect(defaultConfig.Containers.Env.Get()).To(gomega.BeEquivalentTo(envs))
gomega.Expect(defaultConfig.Containers.Mounts.Get()).To(gomega.BeEquivalentTo(mounts))
gomega.Expect(defaultConfig.Containers.PidsLimit).To(gomega.BeEquivalentTo(2048))
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
#
#cgroups = "enabled"

# When no hostname is set for a container, use the container's name, with
# characters not valid for a hostname removed, as the hostname instead of
# the first 12 characters of the container's ID. Containers not running
# in a private UTS namespace will have their hostname set to the host's
# hostname regardless of this setting.
#
#container_name_as_hostname = false

# List of default capabilities for containers. If it is empty or commented out,
# the default capabilities defined in the container engine will be added.
#
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/containers.conf-freebsd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
#
#base_hosts_file = ""

# When no hostname is set for a container, use the container's name, with
# characters not valid for a hostname removed, as the hostname instead of
# the first 12 characters of the container's ID.
#
#container_name_as_hostname = false

# The database backend of Podman. Supported values are "" (default), "boltdb"
# and "sqlite". An empty value means it will check whenever a boltdb already
# exists and use it when it does, otherwise it will use sqlite as default
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/testdata/containers_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ apparmor_profile = "container-default"

base_hosts_file = "/etc/hosts2"

container_name_as_hostname = true

# List of default capabilities for containers. If it is empty or commented out,
# only the capabilities defined in the containers json file by the user/kube
# will be added.
Expand Down

0 comments on commit 2a3e1c1

Please sign in to comment.