Skip to content

Commit cf4cefc

Browse files
committed
Add --no-hostname option
Fixes: containers#25002 Also add the ability to inspect containers for UseImageHosts and UseImageHostname. Finally fixed some bugs in handling of --no-hosts for Pods, which I descovered. Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 1194557 commit cf4cefc

32 files changed

+159
-7
lines changed

cmd/podman/common/netflags.go

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func DefineNetFlags(cmd *cobra.Command) {
9595
)
9696
_ = cmd.RegisterFlagCompletionFunc(publishFlagName, completion.AutocompleteNone)
9797

98+
netFlags.Bool(
99+
"no-hostname", false, "Do not create /etc/hostname within the container, instead use the version from the image",
100+
)
101+
98102
netFlags.Bool(
99103
"no-hosts", podmanConfig.ContainersConfDefaultsRO.Containers.NoHosts,
100104
"Do not create /etc/hosts within the container, instead use the version from the image",
@@ -192,6 +196,11 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
192196
}
193197
}
194198

199+
opts.NoHostname, err = flags.GetBool("no-hostname")
200+
if err != nil {
201+
return nil, err
202+
}
203+
195204
opts.NoHosts, err = flags.GetBool("no-hosts")
196205
if err != nil {
197206
return nil, err

cmd/podman/kube/play.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func init() {
9898
func playFlags(cmd *cobra.Command) {
9999
flags := cmd.Flags()
100100
flags.SetNormalizeFunc(utils.AliasFlags)
101+
podmanConfig := registry.PodmanConfig()
101102

102103
annotationFlagName := "annotation"
103104
flags.StringArrayVar(
@@ -139,7 +140,8 @@ func playFlags(cmd *cobra.Command) {
139140
)
140141
_ = cmd.RegisterFlagCompletionFunc(usernsFlagName, common.AutocompleteUserNamespace)
141142

142-
flags.BoolVar(&playOptions.NoHosts, "no-hosts", false, "Do not create /etc/hosts within the pod's containers, instead use the version from the image")
143+
flags.BoolVar(&playOptions.NoHostname, "no-hostname", false, "Do not create /etc/hostname within the container, instead use the version from the image")
144+
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")
143145
flags.BoolVarP(&playOptions.Quiet, "quiet", "q", false, "Suppress output information when pulling images")
144146
flags.BoolVar(&playOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
145147
flags.BoolVar(&playOptions.StartCLI, "start", true, "Start the pod after creating it")
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
####> This option file is used in:
2-
####> podman build, farm build
2+
####> podman build, create, farm build, kube play, pod create, run
33
####> If file is edited, make sure the changes
44
####> are applicable to all of those.
55
#### **--no-hostname**
66

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

9-
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.
9+
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.

docs/source/markdown/podman-create.1.md.in

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ If used together with **--pod**, the container does not join the pod's network n
268268

269269
@@option no-healthcheck
270270

271+
@@option no-hostname
272+
271273
@@option no-hosts
272274

273275
This option conflicts with **--add-host**.

docs/source/markdown/podman-kube-play.1.md.in

+2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ Note: When joining multiple networks use the **--network name:mac=\<mac\>** synt
228228

229229
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.
230230

231+
@@option no-hostname
232+
231233
@@option no-hosts
232234

233235
This option conflicts with host added in the Kubernetes YAML.

docs/source/markdown/podman-pod-create.1.md.in

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ Invalid if using **--dns**, **--dns-option**, or **--dns-search** with **--netwo
128128

129129
@@option network-alias
130130

131+
@@option no-hostname
132+
131133
@@option no-hosts
132134

133135
This option conflicts with **--add-host**.

docs/source/markdown/podman-run.1.md.in

+2
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ If used together with **--pod**, the container joins the pod's network namespace
287287

288288
@@option no-healthcheck
289289

290+
@@option no-hostname
291+
290292
@@option no-hosts
291293

292294
This option conflicts with **--add-host**.

libpod/container_config.go

+5
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ type ContainerNetworkConfig struct {
286286
// DNS options to be set in container resolv.conf
287287
// With override options in host resolv if set
288288
DNSOption []string `json:"dnsOption,omitempty"`
289+
// UseImageHostname indicates that /etc/hostname should not be
290+
// bind-mounted inside the container.
291+
UseImageHostname bool
289292
// UseImageHosts indicates that /etc/hosts should not be
290293
// bind-mounted inside the container.
291294
// Conflicts with HostAdd.
@@ -472,6 +475,8 @@ type InfraInherit struct {
472475
Volumes []*specgen.NamedVolume `json:"volumes,omitempty"`
473476
ShmSize *int64 `json:"shm_size"`
474477
ShmSizeSystemd *int64 `json:"shm_size_systemd"`
478+
UseImageHosts bool `json:"use_image_hosts"`
479+
UseImageHostname bool `json:"use_image_hostname"`
475480
}
476481

477482
// IsDefaultShmSize determines if the user actually set the shm in the parent ctr or if it has been set to the default size

libpod/container_inspect.go

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func (c *Container) getContainerInspectData(size bool, driverData *define.Driver
171171
IsService: c.IsService(),
172172
KubeExitCodePropagation: config.KubeExitCodePropagation.String(),
173173
LockNumber: c.lock.ID(),
174+
UseImageHosts: c.config.UseImageHosts,
175+
UseImageHostname: c.config.UseImageHostname,
174176
}
175177

176178
if config.RootfsImageID != "" { // May not be set if the container was created with --rootfs

libpod/container_internal_linux.go

+4
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ func setVolumeAtime(mountPoint string, st os.FileInfo) error {
689689
}
690690

691691
func (c *Container) makePlatformBindMounts() error {
692+
if c.config.UseImageHostname {
693+
return nil
694+
}
695+
692696
// Make /etc/hostname
693697
// This should never change, so no need to recreate if it exists
694698
if _, ok := c.state.BindMounts["/etc/hostname"]; !ok {

libpod/define/container_inspect.go

+2
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ type InspectContainerData struct {
798798
LockNumber uint32 `json:"lockNumber"`
799799
Config *InspectContainerConfig `json:"Config"`
800800
HostConfig *InspectContainerHostConfig `json:"HostConfig"`
801+
UseImageHosts bool `json:"UseImageHosts"`
802+
UseImageHostname bool `json:"UseImageHostname"`
801803
}
802804

803805
// InspectExecSession contains information about a given exec session.

libpod/define/pod_inspect.go

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ type InspectPodInfraConfig struct {
118118
// DNSOption is a set of DNS options that will be used by the infra
119119
// container's resolv.conf and shared with the remainder of the pod.
120120
DNSOption []string
121+
// NoManageHostname indicates that the pod will not manage /etc/hostname
122+
// and instead each container will handle their own.
123+
NoManageHostname bool
121124
// NoManageHosts indicates that the pod will not manage /etc/hosts and
122125
// instead each container will handle their own.
123126
NoManageHosts bool

libpod/options.go

+13
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,19 @@ func WithUseImageResolvConf() CtrCreateOption {
13891389
}
13901390
}
13911391

1392+
// WithUseImageHostname tells the container not to bind-mount /etc/hostname in.
1393+
func WithUseImageHostname() CtrCreateOption {
1394+
return func(ctr *Container) error {
1395+
if ctr.valid {
1396+
return define.ErrCtrFinalized
1397+
}
1398+
1399+
ctr.config.UseImageHostname = true
1400+
1401+
return nil
1402+
}
1403+
}
1404+
13921405
// WithUseImageHosts tells the container not to bind-mount /etc/hosts in.
13931406
// This conflicts with WithHosts().
13941407
func WithUseImageHosts() CtrCreateOption {

libpod/pod_api.go

+1
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
677677
infraConfig.HostNetwork = p.NetworkMode() == "host"
678678
infraConfig.StaticIP = infra.config.ContainerNetworkConfig.StaticIP
679679
infraConfig.NoManageResolvConf = infra.config.UseImageResolvConf
680+
infraConfig.NoManageHostname = infra.config.UseImageHostname
680681
infraConfig.NoManageHosts = infra.config.UseImageHosts
681682
infraConfig.CPUPeriod = p.CPUPeriod()
682683
infraConfig.CPUQuota = p.CPUQuota()

pkg/api/handlers/libpod/kube.go

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
109109
LogDriver string `schema:"logDriver"`
110110
LogOptions []string `schema:"logOptions"`
111111
Network []string `schema:"network"`
112+
NoHostname bool `schema:"noHostname"`
112113
NoHosts bool `schema:"noHosts"`
113114
NoTrunc bool `schema:"noTrunc"`
114115
Replace bool `schema:"replace"`
@@ -182,6 +183,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
182183
LogDriver: logDriver,
183184
LogOptions: query.LogOptions,
184185
Networks: query.Network,
186+
NoHostname: query.NoHostname,
185187
NoHosts: query.NoHosts,
186188
Password: password,
187189
PublishPorts: query.PublishPorts,

pkg/bindings/kube/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type PlayOptions struct {
2020
Password *string
2121
// Network - name of the networks to connect to.
2222
Network *[]string
23+
// NoHostname - do not generate /etc/hostname file in pod's containers
24+
NoHostname *bool
2325
// NoHosts - do not generate /etc/hosts file in pod's containers
2426
NoHosts *bool
2527
// Quiet - suppress output when pulling images.

pkg/bindings/kube/types_play_options.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/domain/entities/play.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type PlayKubeOptions struct {
2727
ExitCodePropagation string
2828
// Replace indicates whether to delete and recreate a yaml file
2929
Replace bool
30+
// Do not create /etc/hostname within the pod's containers,
31+
// instead use the version from the image
32+
NoHostname bool
3033
// Do not create /etc/hosts within the pod's containers,
3134
// instead use the version from the image
3235
NoHosts bool

pkg/domain/entities/pods.go

+1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.Pod
367367
s.DNSSearch = p.Net.DNSSearch
368368
s.DNSOption = p.Net.DNSOptions
369369
s.NoManageHosts = p.Net.NoHosts
370+
s.NoManageHostname = p.Net.NoHostname
370371
s.HostAdd = p.Net.AddHosts
371372
s.HostsFile = p.Net.HostsFile
372373
}

pkg/domain/entities/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type NetFlags struct {
4040
MacAddr string `json:"mac-address,omitempty"`
4141
Publish []string `json:"publish,omitempty"`
4242
IP string `json:"ip,omitempty"`
43+
NoHostname bool `json:"no-hostname,omitempty"`
4344
NoHosts bool `json:"no-hosts,omitempty"`
4445
Network string `json:"network,omitempty"`
4546
NetworkAlias []string `json:"network-alias,omitempty"`
@@ -57,6 +58,7 @@ type NetOptions struct {
5758
DNSServers []net.IP `json:"dns_server,omitempty"`
5859
HostsFile string `json:"hosts_file,omitempty"`
5960
Network specgen.Namespace `json:"netns,omitempty"`
61+
NoHostname bool `json:"no_manage_hostname,omitempty"`
6062
NoHosts bool `json:"no_manage_hosts,omitempty"`
6163
PublishPorts []types.PortMapping `json:"portmappings,omitempty"`
6264
// NetworkOptions are additional options for each network

pkg/domain/infra/abi/play.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
625625

626626
podOpt := entities.PodCreateOptions{
627627
Infra: true,
628-
Net: &entities.NetOptions{NoHosts: options.NoHosts},
628+
Net: &entities.NetOptions{NoHosts: options.NoHosts, NoHostname: options.NoHostname},
629629
ExitPolicy: string(config.PodExitPolicyStop),
630630
}
631631
podOpt, err = kube.ToPodOpt(ctx, podName, podOpt, options.PublishAllPorts, podYAML)

pkg/domain/infra/tunnel/kube.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, opts en
6565
if opts.Annotations != nil {
6666
options.WithAnnotations(opts.Annotations)
6767
}
68-
options.WithNoHosts(opts.NoHosts).WithUserns(opts.Userns)
68+
options.WithNoHostname(opts.NoHostname).WithNoHosts(opts.NoHosts).WithUserns(opts.Userns)
6969
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
7070
options.WithSkipTLSVerify(s == types.OptionalBoolTrue)
7171
}

pkg/specgen/generate/container.go

+2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, containerID
495495
specg.Networks = conf.Networks
496496
specg.ShmSize = &conf.ShmSize
497497
specg.ShmSizeSystemd = &conf.ShmSizeSystemd
498+
specg.UseImageHostname = &conf.UseImageHostname
499+
specg.UseImageHosts = &conf.UseImageHosts
498500

499501
mapSecurityConfig(conf, specg)
500502

pkg/specgen/generate/kube/kube.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
)
4343

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

4747
p.Name = podName
4848
p.Labels = podYAML.ObjectMeta.Labels

pkg/specgen/generate/namespaces.go

+3
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod.
366366
} else if len(s.HostAdd) > 0 {
367367
toReturn = append(toReturn, libpod.WithHosts(s.HostAdd))
368368
}
369+
if s.UseImageHostname != nil && *s.UseImageHostname {
370+
toReturn = append(toReturn, libpod.WithUseImageHostname())
371+
}
369372
if len(s.DNSSearch) > 0 {
370373
toReturn = append(toReturn, libpod.WithDNSSearch(s.DNSSearch))
371374
}

pkg/specgen/generate/pod_create.go

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ func MapSpec(p *specgen.PodSpecGenerator) (*specgen.SpecGenerator, error) {
272272
if p.NoManageHosts {
273273
spec.UseImageHosts = &p.NoManageHosts
274274
}
275+
if p.NoManageHostname {
276+
spec.UseImageHostname = &p.NoManageHostname
277+
}
275278

276279
if len(p.InfraConmonPidFile) > 0 {
277280
spec.ConmonPidFile = p.InfraConmonPidFile

pkg/specgen/podspecgen.go

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ type PodNetworkConfig struct {
159159
// Conflicts with NoInfra=true.
160160
// Optional.
161161
DNSOption []string `json:"dns_option,omitempty"`
162+
// NoManageHostname indicates that /etc/hostname should not be managed
163+
// by the pod. Instead, each container will create a separate
164+
// /etc/hostname as they would if not in a pod.
165+
NoManageHostname bool `json:"no_manage_hostname,omitempty"`
162166
// NoManageHosts indicates that /etc/hosts should not be managed by the
163167
// pod. Instead, each container will create a separate /etc/hosts as
164168
// they would if not in a pod.

pkg/specgen/specgen.go

+4
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ type ContainerNetworkConfig struct {
534534
// Conflicts with UseImageResolvConf.
535535
// Optional.
536536
DNSOptions []string `json:"dns_option,omitempty"`
537+
// UseImageHostname indicates that /etc/hostname should not be managed by
538+
// Podman, and instead sourced from the image.
539+
// Optional.
540+
UseImageHostname *bool `json:"use_image_hostname,omitempty"`
537541
// UseImageHosts indicates that /etc/hosts should not be managed by
538542
// Podman, and instead sourced from the image.
539543
// Conflicts with HostAdd.

pkg/specgenutil/specgen.go

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
593593
s.DNSSearch = c.Net.DNSSearch
594594
s.DNSOptions = c.Net.DNSOptions
595595
s.NetworkOptions = c.Net.NetworkOptions
596+
s.UseImageHostname = &c.Net.NoHostname
596597
s.UseImageHosts = &c.Net.NoHosts
597598
}
598599
if len(s.HostUsers) == 0 || len(c.HostUsers) != 0 {

test/e2e/play_kube_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,34 @@ var _ = Describe("Podman kube play", func() {
23842384
Expect(label).To(ContainSubstring("unconfined_u:system_r:spc_t:s0"))
23852385
})
23862386

2387+
It("--no-hostname", func() {
2388+
err := writeYaml(checkInfraImagePodYaml, kubeYaml)
2389+
Expect(err).ToNot(HaveOccurred())
2390+
2391+
kube := podmanTest.Podman([]string{"kube", "play", "--no-hostname", kubeYaml})
2392+
kube.WaitWithDefaultTimeout()
2393+
Expect(kube).Should(ExitCleanly())
2394+
2395+
alpineHostname := podmanTest.Podman([]string{"run", "--rm", "--no-hostname", ALPINE, "cat", "/etc/hostname"})
2396+
alpineHostname.WaitWithDefaultTimeout()
2397+
Expect(alpineHostname).Should(ExitCleanly())
2398+
2399+
podInspect := podmanTest.Podman([]string{"pod", "inspect", "check-infra-image"})
2400+
podInspect.WaitWithDefaultTimeout()
2401+
Expect(podInspect).Should(ExitCleanly())
2402+
2403+
data := podInspect.InspectPodToJSON()
2404+
for _, ctr := range data.Containers {
2405+
if strings.HasSuffix(ctr.Name, "-infra") {
2406+
continue
2407+
}
2408+
exec := podmanTest.Podman([]string{"exec", ctr.ID, "cat", "/etc/hostname"})
2409+
exec.WaitWithDefaultTimeout()
2410+
Expect(exec).Should(ExitCleanly())
2411+
Expect(exec.OutputToString()).To(Equal(alpineHostname.OutputToString()))
2412+
}
2413+
})
2414+
23872415
It("--no-host", func() {
23882416
err := writeYaml(checkInfraImagePodYaml, kubeYaml)
23892417
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)