Handled systems that miss /etc/hostname in CNI isolator. Review: https://reviews.apache.org/r/50108
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/8a0d7e0c Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/8a0d7e0c Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/8a0d7e0c Branch: refs/heads/1.0.x Commit: 8a0d7e0c185a46938bcce8fbeef8c4c04923ebcf Parents: 76ed504 Author: Jie Yu <yujie....@gmail.com> Authored: Fri Jul 15 22:20:19 2016 -0700 Committer: Jie Yu <yujie....@gmail.com> Committed: Mon Jul 18 10:23:39 2016 -0700 ---------------------------------------------------------------------- .../mesos/isolators/network/cni/cni.cpp | 28 +++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/8a0d7e0c/src/slave/containerizer/mesos/isolators/network/cni/cni.cpp ---------------------------------------------------------------------- diff --git a/src/slave/containerizer/mesos/isolators/network/cni/cni.cpp b/src/slave/containerizer/mesos/isolators/network/cni/cni.cpp index 122e2a8..81121a6 100644 --- a/src/slave/containerizer/mesos/isolators/network/cni/cni.cpp +++ b/src/slave/containerizer/mesos/isolators/network/cni/cni.cpp @@ -660,9 +660,14 @@ Future<Nothing> NetworkCniIsolatorProcess::isolate( setup.flags.pid = pid; setup.flags.rootfs = infos[containerId]->rootfs; setup.flags.etc_hosts_path = "/etc/hosts"; - setup.flags.etc_hostname_path = "/etc/hostname"; setup.flags.etc_resolv_conf = "/etc/resolv.conf"; + // NOTE: On some Linux distributions, `/etc/hostname` might not + // exist, but hostname is still accessible by `getHostname()`. + if (os::exists("/etc/hostname")) { + setup.flags.etc_hostname_path = "/etc/hostname"; + } + return __isolate(setup); } @@ -1447,20 +1452,28 @@ int NetworkCniIsolatorSetup::execute() return EXIT_FAILURE; } + // Initialize the host path and container path for the set of files + // that need to be setup in the container file system. + hashmap<string, string> files; + if (flags.etc_hosts_path.isNone()) { cerr << "Path to 'hosts' not specified" <<endl; return EXIT_FAILURE; } else if (!os::exists(flags.etc_hosts_path.get())) { cerr << "Unable to find '" << flags.etc_hosts_path.get() << "'" << endl; return EXIT_FAILURE; + } else { + files["/etc/hosts"] = flags.etc_hosts_path.get(); } if (flags.etc_hostname_path.isNone()) { - cerr << "Path to 'hostname' not specified" << endl; - return EXIT_FAILURE; + // This is the case where host network is used, container has an + // image, and `/etc/hostname` does not exist in the system. } else if (!os::exists(flags.etc_hostname_path.get())) { cerr << "Unable to find '" << flags.etc_hostname_path.get() << "'" << endl; return EXIT_FAILURE; + } else { + files["/etc/hostname"] = flags.etc_hostname_path.get(); } if (flags.etc_resolv_conf.isNone()) { @@ -1469,6 +1482,8 @@ int NetworkCniIsolatorSetup::execute() } else if (!os::exists(flags.etc_resolv_conf.get())) { cerr << "Unable to find '" << flags.etc_resolv_conf.get() << "'" << endl; return EXIT_FAILURE; + } else { + files["/etc/resolv.conf"] = flags.etc_resolv_conf.get(); } // Enter the mount namespace. @@ -1502,13 +1517,6 @@ int NetworkCniIsolatorSetup::execute() return EXIT_FAILURE; } - // Initialize the host path and container path for the set of files - // that need to be setup in the container file system. - hashmap<string, string> files; - files["/etc/hosts"] = flags.etc_hosts_path.get(); - files["/etc/hostname"] = flags.etc_hostname_path.get(); - files["/etc/resolv.conf"] = flags.etc_resolv_conf.get(); - foreachpair (const string& file, const string& source, files) { // Do the bind mount in the host filesystem since no process in // the new network namespace should be seeing the original network