rkt: Refactor host file mounts for host network.

Do not mount /etc/hosts/ /etc/resolv.conf if they are already mounted.
This commit is contained in:
Yifan Gu 2016-09-06 12:20:58 -07:00
parent 9dfe8df7cd
commit 25786aca98
2 changed files with 38 additions and 17 deletions

View File

@ -6,6 +6,9 @@ metadata:
labels: labels:
name: nginx name: nginx
spec: spec:
initcontainers:
name: busybox
image: busybox
containers: containers:
- name: nginx - name: nginx
image: nginx image: nginx

View File

@ -133,6 +133,9 @@ const (
// defaultRequestTimeout is the default timeout of rkt requests. // defaultRequestTimeout is the default timeout of rkt requests.
defaultRequestTimeout = 2 * time.Minute defaultRequestTimeout = 2 * time.Minute
etcHostsPath = "/etc/hosts"
etcResolvConfPath = "/etc/resolv.conf"
) )
// Runtime implements the Containerruntime for rkt. The implementation // Runtime implements the Containerruntime for rkt. The implementation
@ -657,27 +660,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. // 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) { func makeHostNetworkMount(opts *kubecontainer.RunContainerOptions) (*kubecontainer.Mount, *kubecontainer.Mount, error) {
mountHosts, mountResolvConf := true, true
for _, mnt := range opts.Mounts {
switch mnt.ContainerPath {
case etcHostsPath:
mountHosts = false
case etcResolvConfPath:
mountResolvConf = false
}
}
var hostsMount, resolvMount kubecontainer.Mount
if mountHosts {
hostsPath := filepath.Join(opts.PodContainerDir, "etc-hosts") hostsPath := filepath.Join(opts.PodContainerDir, "etc-hosts")
resolvPath := filepath.Join(opts.PodContainerDir, "etc-resolv-conf") if err := copyfile(etcHostsPath, hostsPath); err != nil {
if err := copyfile("/etc/hosts", hostsPath); err != nil {
return nil, nil, err return nil, nil, err
} }
if err := copyfile("/etc/resolv.conf", resolvPath); err != nil { hostsMount = kubecontainer.Mount{
return nil, nil, err
}
hostsMount := kubecontainer.Mount{
Name: "kubernetes-hostnetwork-hosts-conf", Name: "kubernetes-hostnetwork-hosts-conf",
ContainerPath: "/etc/hosts", ContainerPath: etcHostsPath,
HostPath: hostsPath, HostPath: hostsPath,
} }
resolvMount := kubecontainer.Mount{ opts.Mounts = append(opts.Mounts, hostsMount)
}
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", Name: "kubernetes-hostnetwork-resolv-conf",
ContainerPath: "/etc/resolv.conf", ContainerPath: etcResolvConfPath,
HostPath: resolvPath, HostPath: resolvPath,
} }
opts.Mounts = append(opts.Mounts, hostsMount, resolvMount) opts.Mounts = append(opts.Mounts, resolvMount)
}
return &hostsMount, &resolvMount, nil return &hostsMount, &resolvMount, nil
} }