mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
Merge pull request #29024 from yifan-gu/copy_etc_hosts_resolv
Automatic merge from submit-queue rkt: Copy the /etc/hosts /etc/resolv.conf into pod dir before mounting. rkt: Copy the /etc/hosts /etc/resolv.conf into pod dir before mounting. This enables the container to modify the /etc/hosts/ /etc/resolv.conf without changing the host's ones. With this PR, we now match the docker's behavior. Fix https://github.com/kubernetes/kubernetes/issues/29022 cc @kubernetes/sig-rktnetes @quentin-m
This commit is contained in:
commit
d6336c4f4b
@ -651,22 +651,38 @@ func (r *Runtime) makePodManifest(pod *api.Pod, podIP string, pullSecrets []api.
|
|||||||
return manifest, nil
|
return manifest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyfile(src, dst string) error {
|
||||||
|
data, err := ioutil.ReadFile(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ioutil.WriteFile(dst, data, 0640)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(yifan): Can make rkt handle this when '--net=host'. See https://github.com/coreos/rkt/issues/2430.
|
// TODO(yifan): Can make rkt handle this when '--net=host'. See https://github.com/coreos/rkt/issues/2430.
|
||||||
func makeHostNetworkMount(opts *kubecontainer.RunContainerOptions) (*kubecontainer.Mount, *kubecontainer.Mount) {
|
func makeHostNetworkMount(opts *kubecontainer.RunContainerOptions) (*kubecontainer.Mount, *kubecontainer.Mount, error) {
|
||||||
|
hostsPath := filepath.Join(opts.PodContainerDir, "etc-hosts")
|
||||||
|
resolvPath := filepath.Join(opts.PodContainerDir, "etc-resolv-conf")
|
||||||
|
|
||||||
|
if err := copyfile("/etc/hosts", hostsPath); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if err := copyfile("/etc/resolv.conf", resolvPath); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
hostsMount := kubecontainer.Mount{
|
hostsMount := kubecontainer.Mount{
|
||||||
Name: "kubernetes-hostnetwork-hosts-conf",
|
Name: "kubernetes-hostnetwork-hosts-conf",
|
||||||
ContainerPath: "/etc/hosts",
|
ContainerPath: "/etc/hosts",
|
||||||
HostPath: "/etc/hosts",
|
HostPath: hostsPath,
|
||||||
ReadOnly: true,
|
|
||||||
}
|
}
|
||||||
resolvMount := kubecontainer.Mount{
|
resolvMount := kubecontainer.Mount{
|
||||||
Name: "kubernetes-hostnetwork-resolv-conf",
|
Name: "kubernetes-hostnetwork-resolv-conf",
|
||||||
ContainerPath: "/etc/resolv.conf",
|
ContainerPath: "/etc/resolv.conf",
|
||||||
HostPath: "/etc/resolv.conf",
|
HostPath: resolvPath,
|
||||||
ReadOnly: true,
|
|
||||||
}
|
}
|
||||||
opts.Mounts = append(opts.Mounts, hostsMount, resolvMount)
|
opts.Mounts = append(opts.Mounts, hostsMount, resolvMount)
|
||||||
return &hostsMount, &resolvMount
|
return &hostsMount, &resolvMount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// podFinishedMarkerPath returns the path to a file which should be used to
|
// podFinishedMarkerPath returns the path to a file which should be used to
|
||||||
@ -769,11 +785,14 @@ func (r *Runtime) newAppcRuntimeApp(pod *api.Pod, podIP string, c api.Container,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If run in 'hostnetwork' mode, then mount the host's /etc/resolv.conf and /etc/hosts,
|
// If run in 'hostnetwork' mode, then copy and mount the host's /etc/resolv.conf and /etc/hosts,
|
||||||
// and add volumes.
|
// and add volumes.
|
||||||
var hostsMnt, resolvMnt *kubecontainer.Mount
|
var hostsMnt, resolvMnt *kubecontainer.Mount
|
||||||
if kubecontainer.IsHostNetworkPod(pod) {
|
if kubecontainer.IsHostNetworkPod(pod) {
|
||||||
hostsMnt, resolvMnt = makeHostNetworkMount(opts)
|
hostsMnt, resolvMnt, err = makeHostNetworkMount(opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
manifest.Volumes = append(manifest.Volumes, appctypes.Volume{
|
manifest.Volumes = append(manifest.Volumes, appctypes.Volume{
|
||||||
Name: convertToACName(hostsMnt.Name),
|
Name: convertToACName(hostsMnt.Name),
|
||||||
Kind: "host",
|
Kind: "host",
|
||||||
|
Loading…
Reference in New Issue
Block a user