Skip to content

Commit

Permalink
Merge pull request #25008 from rhatdan/hostname
Browse files Browse the repository at this point in the history
Add --no-hostname option
  • Loading branch information
openshift-merge-bot[bot] authored Jan 16, 2025
2 parents 1a43077 + 6ad44fe commit a3bb0a1
Show file tree
Hide file tree
Showing 35 changed files with 425 additions and 902 deletions.
9 changes: 9 additions & 0 deletions cmd/podman/common/netflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func DefineNetFlags(cmd *cobra.Command) {
)
_ = cmd.RegisterFlagCompletionFunc(publishFlagName, completion.AutocompleteNone)

netFlags.Bool(
"no-hostname", false, "Do not create /etc/hostname within the container, instead use the version from the image",
)

netFlags.Bool(
"no-hosts", podmanConfig.ContainersConfDefaultsRO.Containers.NoHosts,
"Do not create /etc/hosts within the container, instead use the version from the image",
Expand Down Expand Up @@ -192,6 +196,11 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
}
}

opts.NoHostname, err = flags.GetBool("no-hostname")
if err != nil {
return nil, err
}

opts.NoHosts, err = flags.GetBool("no-hosts")
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion cmd/podman/kube/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func init() {
func playFlags(cmd *cobra.Command) {
flags := cmd.Flags()
flags.SetNormalizeFunc(utils.AliasFlags)
podmanConfig := registry.PodmanConfig()

annotationFlagName := "annotation"
flags.StringArrayVar(
Expand Down Expand Up @@ -139,7 +140,8 @@ func playFlags(cmd *cobra.Command) {
)
_ = cmd.RegisterFlagCompletionFunc(usernsFlagName, common.AutocompleteUserNamespace)

flags.BoolVar(&playOptions.NoHosts, "no-hosts", false, "Do not create /etc/hosts within the pod's containers, instead use the version from the image")
flags.BoolVar(&playOptions.NoHostname, "no-hostname", false, "Do not create /etc/hostname within the container, instead use the version from the image")
flags.BoolVar(&playOptions.NoHosts, "no-hosts", podmanConfig.ContainersConfDefaultsRO.Containers.NoHosts, "Do not create /etc/hosts within the pod's containers, instead use the version from the image")
flags.BoolVarP(&playOptions.Quiet, "quiet", "q", false, "Suppress output information when pulling images")
flags.BoolVar(&playOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
flags.BoolVar(&playOptions.StartCLI, "start", true, "Start the pod after creating it")
Expand Down
6 changes: 3 additions & 3 deletions docs/source/markdown/options/no-hostname.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
####> This option file is used in:
####> podman build, farm build
####> podman build, create, farm build, kube play, pod create, run
####> If file is edited, make sure the changes
####> are applicable to all of those.
#### **--no-hostname**

Do not create the _/etc/hostname_ file in the container for RUN instructions.
Do not create the _/etc/hostname_ file in the containers.

By default, Buildah manages the _/etc/hostname_ file, adding the container's own hostname. When the **--no-hostname** option is set, the image's _/etc/hostname_ will be preserved unmodified if it exists.
By default, Podman manages the _/etc/hostname_ file, adding the container's own hostname. When the **--no-hostname** option is set, the image's _/etc/hostname_ will be preserved unmodified if it exists.
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-container-inspect.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Valid placeholders for the Go template are listed below:
| .SizeRw | Size of upper (R/W) container layer, in bytes [1] |
| .State ... | Container state info (struct) |
| .StaticDir | Path to container metadata dir (string) |
| .UseImageHostname | Use /etc/hostname from the image if it exists? (string: true/false)
|
| .UseImageHosts | Use /etc/hosts from the image? (string: true/false)
|

[1] This format specifier requires the **--size** option

Expand Down
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 @@ -268,6 +268,8 @@ If used together with **--pod**, the container does not join the pod's network n

@@option no-healthcheck

@@option no-hostname

@@option no-hosts

This option conflicts with **--add-host**.
Expand Down
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-kube-play.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ Note: When joining multiple networks use the **--network name:mac=\<mac\>** synt

When no network option is specified and *host* network mode is not configured in the YAML file, a new network stack is created and pods are attached to it making possible pod to pod communication.

@@option no-hostname

@@option no-hosts

This option conflicts with host added in the Kubernetes YAML.
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 @@ -128,6 +128,8 @@ Invalid if using **--dns**, **--dns-option**, or **--dns-search** with **--netwo

@@option network-alias

@@option no-hostname

@@option no-hosts

This option conflicts with **--add-host**.
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 @@ -287,6 +287,8 @@ If used together with **--pod**, the container joins the pod's network namespace

@@option no-healthcheck

@@option no-hostname

@@option no-hosts

This option conflicts with **--add-host**.
Expand Down
7 changes: 6 additions & 1 deletion libpod/container_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ type ContainerNetworkConfig struct {
// DNS options to be set in container resolv.conf
// With override options in host resolv if set
DNSOption []string `json:"dnsOption,omitempty"`
// UseImageHostname indicates that /etc/hostname should not be
// bind-mounted inside the container.
UseImageHostname bool `json:"useImageHostname"`
// UseImageHosts indicates that /etc/hosts should not be
// bind-mounted inside the container.
// Conflicts with HostAdd.
UseImageHosts bool
UseImageHosts bool `json:"useImageHosts"`
// BaseHostsFile 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`.
Expand Down Expand Up @@ -472,6 +475,8 @@ type InfraInherit struct {
Volumes []*specgen.NamedVolume `json:"volumes,omitempty"`
ShmSize *int64 `json:"shm_size"`
ShmSizeSystemd *int64 `json:"shm_size_systemd"`
UseImageHosts bool `json:"use_image_hosts"`
UseImageHostname bool `json:"use_image_hostname"`
}

// IsDefaultShmSize determines if the user actually set the shm in the parent ctr or if it has been set to the default size
Expand Down
2 changes: 2 additions & 0 deletions libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func (c *Container) getContainerInspectData(size bool, driverData *define.Driver
IsService: c.IsService(),
KubeExitCodePropagation: config.KubeExitCodePropagation.String(),
LockNumber: c.lock.ID(),
UseImageHosts: c.config.UseImageHosts,
UseImageHostname: c.config.UseImageHostname,
}

if config.RootfsImageID != "" { // May not be set if the container was created with --rootfs
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,7 @@ rootless=%d
}
}

return c.makePlatformBindMounts()
return c.makeHostnameBindMount()
}

// createResolvConf create the resolv.conf file and bind mount it
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_internal_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func setVolumeAtime(mountPoint string, st os.FileInfo) error {
return nil
}

func (c *Container) makePlatformBindMounts() error {
func (c *Container) makeHostnameBindMount() error {
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,11 @@ func setVolumeAtime(mountPoint string, st os.FileInfo) error {
return nil
}

func (c *Container) makePlatformBindMounts() error {
func (c *Container) makeHostnameBindMount() error {
if c.config.UseImageHostname {
return nil
}

// Make /etc/hostname
// This should never change, so no need to recreate if it exists
if _, ok := c.state.BindMounts["/etc/hostname"]; !ok {
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 @@ -798,6 +798,8 @@ type InspectContainerData struct {
LockNumber uint32 `json:"lockNumber"`
Config *InspectContainerConfig `json:"Config"`
HostConfig *InspectContainerHostConfig `json:"HostConfig"`
UseImageHosts bool `json:"UseImageHosts"`
UseImageHostname bool `json:"UseImageHostname"`
}

// InspectExecSession contains information about a given exec session.
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 @@ -118,6 +118,9 @@ type InspectPodInfraConfig struct {
// DNSOption is a set of DNS options that will be used by the infra
// container's resolv.conf and shared with the remainder of the pod.
DNSOption []string
// NoManageHostname indicates that the pod will not manage /etc/hostname
// and instead each container will handle their own.
NoManageHostname bool
// NoManageHosts indicates that the pod will not manage /etc/hosts and
// instead each container will handle their own.
NoManageHosts bool
Expand Down
13 changes: 13 additions & 0 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,19 @@ func WithUseImageResolvConf() CtrCreateOption {
}
}

// WithUseImageHostname tells the container not to bind-mount /etc/hostname in.
func WithUseImageHostname() CtrCreateOption {
return func(ctr *Container) error {
if ctr.valid {
return define.ErrCtrFinalized
}

ctr.config.UseImageHostname = true

return nil
}
}

// WithUseImageHosts tells the container not to bind-mount /etc/hosts in.
// This conflicts with WithHosts().
func WithUseImageHosts() CtrCreateOption {
Expand Down
1 change: 1 addition & 0 deletions libpod/pod_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
infraConfig.HostNetwork = p.NetworkMode() == "host"
infraConfig.StaticIP = infra.config.ContainerNetworkConfig.StaticIP
infraConfig.NoManageResolvConf = infra.config.UseImageResolvConf
infraConfig.NoManageHostname = infra.config.UseImageHostname
infraConfig.NoManageHosts = infra.config.UseImageHosts
infraConfig.CPUPeriod = p.CPUPeriod()
infraConfig.CPUQuota = p.CPUQuota()
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
LogDriver string `schema:"logDriver"`
LogOptions []string `schema:"logOptions"`
Network []string `schema:"network"`
NoHostname bool `schema:"noHostname"`
NoHosts bool `schema:"noHosts"`
NoTrunc bool `schema:"noTrunc"`
Replace bool `schema:"replace"`
Expand Down Expand Up @@ -182,6 +183,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
LogDriver: logDriver,
LogOptions: query.LogOptions,
Networks: query.Network,
NoHostname: query.NoHostname,
NoHosts: query.NoHosts,
Password: password,
PublishPorts: query.PublishPorts,
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/kube/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type PlayOptions struct {
Password *string
// Network - name of the networks to connect to.
Network *[]string
// NoHostname - do not generate /etc/hostname file in pod's containers
NoHostname *bool
// NoHosts - do not generate /etc/hosts file in pod's containers
NoHosts *bool
// Quiet - suppress output when pulling images.
Expand Down
15 changes: 15 additions & 0 deletions pkg/bindings/kube/types_play_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type PlayKubeOptions struct {
ExitCodePropagation string
// Replace indicates whether to delete and recreate a yaml file
Replace bool
// Do not create /etc/hostname within the pod's containers,
// instead use the version from the image
NoHostname bool
// Do not create /etc/hosts within the pod's containers,
// instead use the version from the image
NoHosts bool
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 @@ -367,6 +367,7 @@ func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.Pod
s.DNSSearch = p.Net.DNSSearch
s.DNSOption = p.Net.DNSOptions
s.NoManageHosts = p.Net.NoHosts
s.NoManageHostname = p.Net.NoHostname
s.HostAdd = p.Net.AddHosts
s.HostsFile = p.Net.HostsFile
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type NetFlags struct {
MacAddr string `json:"mac-address,omitempty"`
Publish []string `json:"publish,omitempty"`
IP string `json:"ip,omitempty"`
NoHostname bool `json:"no-hostname,omitempty"`
NoHosts bool `json:"no-hosts,omitempty"`
Network string `json:"network,omitempty"`
NetworkAlias []string `json:"network-alias,omitempty"`
Expand All @@ -57,6 +58,7 @@ type NetOptions struct {
DNSServers []net.IP `json:"dns_server,omitempty"`
HostsFile string `json:"hosts_file,omitempty"`
Network specgen.Namespace `json:"netns,omitempty"`
NoHostname bool `json:"no_manage_hostname,omitempty"`
NoHosts bool `json:"no_manage_hosts,omitempty"`
PublishPorts []types.PortMapping `json:"portmappings,omitempty"`
// NetworkOptions are additional options for each network
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY

podOpt := entities.PodCreateOptions{
Infra: true,
Net: &entities.NetOptions{NoHosts: options.NoHosts},
Net: &entities.NetOptions{NoHosts: options.NoHosts, NoHostname: options.NoHostname},
ExitPolicy: string(config.PodExitPolicyStop),
}
podOpt, err = kube.ToPodOpt(ctx, podName, podOpt, options.PublishAllPorts, podYAML)
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/tunnel/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, opts en
if opts.Annotations != nil {
options.WithAnnotations(opts.Annotations)
}
options.WithNoHosts(opts.NoHosts).WithUserns(opts.Userns)
options.WithNoHostname(opts.NoHostname).WithNoHosts(opts.NoHosts).WithUserns(opts.Userns)
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
options.WithSkipTLSVerify(s == types.OptionalBoolTrue)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/specgen/generate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, containerID
specg.Networks = conf.Networks
specg.ShmSize = &conf.ShmSize
specg.ShmSizeSystemd = &conf.ShmSizeSystemd
specg.UseImageHostname = &conf.UseImageHostname
specg.UseImageHosts = &conf.UseImageHosts

mapSecurityConfig(conf, specg)

Expand Down
2 changes: 1 addition & 1 deletion pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
)

func ToPodOpt(ctx context.Context, podName string, p entities.PodCreateOptions, publishAllPorts bool, podYAML *v1.PodTemplateSpec) (entities.PodCreateOptions, error) {
p.Net = &entities.NetOptions{NoHosts: p.Net.NoHosts}
p.Net = &entities.NetOptions{NoHosts: p.Net.NoHosts, NoHostname: p.Net.NoHostname}

p.Name = podName
p.Labels = podYAML.ObjectMeta.Labels
Expand Down
3 changes: 3 additions & 0 deletions pkg/specgen/generate/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
} else if len(s.HostAdd) > 0 {
toReturn = append(toReturn, libpod.WithHosts(s.HostAdd))
}
if s.UseImageHostname != nil && *s.UseImageHostname {
toReturn = append(toReturn, libpod.WithUseImageHostname())
}
if len(s.DNSSearch) > 0 {
toReturn = append(toReturn, libpod.WithDNSSearch(s.DNSSearch))
}
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 @@ -272,6 +272,9 @@ func MapSpec(p *specgen.PodSpecGenerator) (*specgen.SpecGenerator, error) {
if p.NoManageHosts {
spec.UseImageHosts = &p.NoManageHosts
}
if p.NoManageHostname {
spec.UseImageHostname = &p.NoManageHostname
}

if len(p.InfraConmonPidFile) > 0 {
spec.ConmonPidFile = p.InfraConmonPidFile
Expand Down
4 changes: 4 additions & 0 deletions pkg/specgen/podspecgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ type PodNetworkConfig struct {
// Conflicts with NoInfra=true.
// Optional.
DNSOption []string `json:"dns_option,omitempty"`
// NoManageHostname indicates that /etc/hostname should not be managed
// by the pod. Instead, each container will create a separate
// /etc/hostname as they would if not in a pod.
NoManageHostname bool `json:"no_manage_hostname,omitempty"`
// NoManageHosts indicates that /etc/hosts should not be managed by the
// pod. Instead, each container will create a separate /etc/hosts as
// they would if not in a pod.
Expand Down
4 changes: 4 additions & 0 deletions pkg/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ type ContainerNetworkConfig struct {
// Conflicts with UseImageResolvConf.
// Optional.
DNSOptions []string `json:"dns_option,omitempty"`
// UseImageHostname indicates that /etc/hostname should not be managed by
// Podman, and instead sourced from the image.
// Optional.
UseImageHostname *bool `json:"use_image_hostname,omitempty"`
// UseImageHosts indicates that /etc/hosts should not be managed by
// Podman, and instead sourced from the image.
// Conflicts with HostAdd.
Expand Down
1 change: 1 addition & 0 deletions pkg/specgenutil/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
s.DNSSearch = c.Net.DNSSearch
s.DNSOptions = c.Net.DNSOptions
s.NetworkOptions = c.Net.NetworkOptions
s.UseImageHostname = &c.Net.NoHostname
s.UseImageHosts = &c.Net.NoHosts
}
if len(s.HostUsers) == 0 || len(c.HostUsers) != 0 {
Expand Down
Loading

0 comments on commit a3bb0a1

Please sign in to comment.