mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Merge pull request #32148 from yifan-gu/hostnetwork_mount
Automatic merge from submit-queue rkt: Refactor host file mounts for host network. Do not mount /etc/hosts/ /etc/resolv.conf if they are already mounted. Ref https://github.com/coreos/rkt/issues/2430#issuecomment-244343576
This commit is contained in:
commit
56242964ca
@ -6,6 +6,9 @@ metadata:
|
||||
labels:
|
||||
name: nginx
|
||||
spec:
|
||||
initcontainers:
|
||||
name: busybox
|
||||
image: busybox
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
|
@ -133,6 +133,9 @@ const (
|
||||
|
||||
// defaultRequestTimeout is the default timeout of rkt requests.
|
||||
defaultRequestTimeout = 2 * time.Minute
|
||||
|
||||
etcHostsPath = "/etc/hosts"
|
||||
etcResolvConfPath = "/etc/resolv.conf"
|
||||
)
|
||||
|
||||
// Runtime implements the Containerruntime for rkt. The implementation
|
||||
@ -676,27 +679,42 @@ func copyfile(src, dst string) error {
|
||||
|
||||
// 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, 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
|
||||
mountHosts, mountResolvConf := true, true
|
||||
for _, mnt := range opts.Mounts {
|
||||
switch mnt.ContainerPath {
|
||||
case etcHostsPath:
|
||||
mountHosts = false
|
||||
case etcResolvConfPath:
|
||||
mountResolvConf = false
|
||||
}
|
||||
}
|
||||
|
||||
hostsMount := kubecontainer.Mount{
|
||||
Name: "kubernetes-hostnetwork-hosts-conf",
|
||||
ContainerPath: "/etc/hosts",
|
||||
HostPath: hostsPath,
|
||||
var hostsMount, resolvMount kubecontainer.Mount
|
||||
if mountHosts {
|
||||
hostsPath := filepath.Join(opts.PodContainerDir, "etc-hosts")
|
||||
if err := copyfile(etcHostsPath, hostsPath); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
hostsMount = kubecontainer.Mount{
|
||||
Name: "kubernetes-hostnetwork-hosts-conf",
|
||||
ContainerPath: etcHostsPath,
|
||||
HostPath: hostsPath,
|
||||
}
|
||||
opts.Mounts = append(opts.Mounts, hostsMount)
|
||||
}
|
||||
resolvMount := kubecontainer.Mount{
|
||||
Name: "kubernetes-hostnetwork-resolv-conf",
|
||||
ContainerPath: "/etc/resolv.conf",
|
||||
HostPath: resolvPath,
|
||||
|
||||
if mountResolvConf {
|
||||
resolvPath := filepath.Join(opts.PodContainerDir, "etc-resolv-conf")
|
||||
if err := copyfile(etcResolvConfPath, resolvPath); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
resolvMount = kubecontainer.Mount{
|
||||
Name: "kubernetes-hostnetwork-resolv-conf",
|
||||
ContainerPath: etcResolvConfPath,
|
||||
HostPath: resolvPath,
|
||||
}
|
||||
opts.Mounts = append(opts.Mounts, resolvMount)
|
||||
}
|
||||
opts.Mounts = append(opts.Mounts, hostsMount, resolvMount)
|
||||
return &hostsMount, &resolvMount, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user