You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by qi...@apache.org on 2017/08/03 07:49:43 UTC

[4/9] mesos git commit: Introduced `--default_container_dns` agent flag.

Introduced `--default_container_dns` agent flag.

Review: https://reviews.apache.org/r/60500


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ebfccf4e
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ebfccf4e
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ebfccf4e

Branch: refs/heads/master
Commit: ebfccf4ea12ccc4b02700b44e69ab17affd50019
Parents: 1793f8f
Author: Qian Zhang <zh...@gmail.com>
Authored: Wed Jun 28 14:10:01 2017 +0800
Committer: Qian Zhang <zh...@gmail.com>
Committed: Thu Aug 3 13:53:26 2017 +0800

----------------------------------------------------------------------
 docs/configuration.md    |  46 +++++++++++++++
 src/messages/flags.hpp   |  21 +++++++
 src/messages/flags.proto |  74 +++++++++++++++++++++++-
 src/slave/flags.cpp      | 128 ++++++++++++++++++++++++++++++++++++++++++
 src/slave/flags.hpp      |   1 +
 5 files changed, 268 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ebfccf4e/docs/configuration.md
----------------------------------------------------------------------
diff --git a/docs/configuration.md b/docs/configuration.md
index 058e366..041c3df 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -1329,6 +1329,52 @@ Example:
 </tr>
 <tr>
   <td>
+    --default_container_dns=VALUE
+  </td>
+  <td>
+JSON-formatted DNS information for CNI networks (Mesos containerizer)
+and CNM networks (Docker containerizer). For CNI networks, this flag
+can be used to configure `nameservers`, `domain`, `search` and
+`options`, and its priority is lower than the DNS information returned
+by a CNI plugin, but higher than the DNS information in agent host's
+/etc/resolv.conf. For CNM networks, this flag can be used to configure
+`nameservers`, `search` and `options`, it will only be used if there
+is no DNS information provided in the ContainerInfo.docker.parameters
+message.
+<p/>
+See the ContainerDNS message in `flags.proto` for the expected format.
+<p/>
+Example:
+<pre><code>{
+  "mesos": [
+    {
+      "network_mode": "CNI",
+      "network_name": "net1",
+      "dns": {
+        "nameservers": [ "8.8.8.8", "8.8.4.4" ]
+      }
+    }
+  ],
+  "docker": [
+    {
+      "network_mode": "BRIDGE",
+      "dns": {
+        "nameservers": [ "8.8.8.8", "8.8.4.4" ]
+      }
+    },
+    {
+      "network_mode": "USER",
+      "network_name": "net2",
+      "dns": {
+        "nameservers": [ "8.8.8.8", "8.8.4.4" ]
+      }
+    }
+  ]
+}</code></pre>
+  </td>
+</tr>
+<tr>
+  <td>
     --default_container_info=VALUE
   </td>
   <td>

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebfccf4e/src/messages/flags.hpp
----------------------------------------------------------------------
diff --git a/src/messages/flags.hpp b/src/messages/flags.hpp
index 70ad58c..b267677 100644
--- a/src/messages/flags.hpp
+++ b/src/messages/flags.hpp
@@ -45,6 +45,19 @@ inline Try<mesos::internal::Firewall> parse(const std::string& value)
   return protobuf::parse<mesos::internal::Firewall>(json.get());
 }
 
+
+template <>
+inline Try<mesos::internal::ContainerDNSInfo> parse(const std::string& value)
+{
+  // Convert from string or file to JSON.
+  Try<JSON::Object> json = parse<JSON::Object>(value);
+  if (json.isError()) {
+    return Error(json.error());
+  }
+
+  return protobuf::parse<mesos::internal::ContainerDNSInfo>(json.get());
+}
+
 } // namespace flags {
 
 namespace mesos {
@@ -57,6 +70,14 @@ inline std::ostream& operator<<(
   return stream << rules.DebugString();
 }
 
+
+inline std::ostream& operator<<(
+    std::ostream& stream,
+    const ContainerDNSInfo& dns)
+{
+  return stream << dns.DebugString();
+}
+
 } // namespace internal {
 } // namespace mesos {
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebfccf4e/src/messages/flags.proto
----------------------------------------------------------------------
diff --git a/src/messages/flags.proto b/src/messages/flags.proto
index e87075f..077353c 100644
--- a/src/messages/flags.proto
+++ b/src/messages/flags.proto
@@ -16,10 +16,14 @@
 
 syntax = "proto2";
 
+import "slave/containerizer/mesos/isolators/network/cni/spec.proto";
+
 package mesos.internal;
 
-// Initializes firewall rules to allow access control of the
-// libprocess endpoints.
+/**
+ * Initializes firewall rules to allow access control of the
+ * libprocess endpoints.
+ */
 message Firewall {
   message DisabledEndpointsRule {
     repeated string paths = 1;
@@ -27,3 +31,69 @@ message Firewall {
 
   optional DisabledEndpointsRule disabled_endpoints = 1;
 }
+
+
+/**
+ * DNS information for CNI networks (Mesos containerizer) and CNM
+ * networks (Docker containerizer).
+ */
+message ContainerDNSInfo {
+  message MesosInfo {
+    enum NetworkMode {
+      UNKNOWN = 0;
+      HOST = 1; // Currently not supported.
+      CNI = 2;
+    }
+
+    optional NetworkMode network_mode = 1;
+
+    // This field is valid only when `network_mode` is set to CNI, it informs
+    // the `network/cni` isolator about the CNI network to which the DNS
+    // configuration applies. Also, if the mode is CNI and this field is not
+    // set it implies wild card semantics for the CNI network name, informing
+    // the `network/cni` isolator that the DNS configuration applies to "all"
+    // CNI networks.
+    //
+    // NOTE: If there are multiple `MesosInfo` specified, when applying the DNS
+    // configuration to a CNI network the containerizer will always choose the
+    // most specific match based on the CNI network name.
+    optional string network_name = 2;
+
+    required slave.cni.spec.DNS dns = 3;
+  }
+
+  message DockerInfo {
+    enum NetworkMode {
+      UNKNOWN = 0;
+      HOST = 1; // Currently not supported.
+      BRIDGE = 2;
+      USER = 3;
+    }
+
+    // Only support `nameservers`, `search` and `options` but not `domain`,
+    // since Docker only has `--dns`, `--dns-search`, `--dns-option` options.
+    message DNS {
+      repeated string nameservers = 1;
+      repeated string search = 2;
+      repeated string options = 3;
+    }
+
+    optional NetworkMode network_mode = 1;
+
+    // This field is valid only when `network_mode` is set to USER, it specifies
+    // the user-defined CNM network to which this DNS configuration applies.
+    // Also, if the mode is USER and this field is not set it implies wild card
+    // semantics for CNM networks, i.e, this DNS configuration applies to "all"
+    // CNM networks.
+    //
+    // NOTE: If there are multiple `DockerInfo` specified, when applying the DNS
+    // configuration to a CNM network the containerizer will always choose the
+    // most specific match based on the CNM network name.
+    optional string network_name = 2;
+
+    required DNS dns = 3;
+  }
+
+  repeated MesosInfo mesos = 1;
+  repeated DockerInfo docker = 2;
+}

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebfccf4e/src/slave/flags.cpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.cpp b/src/slave/flags.cpp
index a4c1a0c..4171604 100644
--- a/src/slave/flags.cpp
+++ b/src/slave/flags.cpp
@@ -770,6 +770,134 @@ mesos::internal::slave::Flags::Flags()
 #endif // __WINDOWS__
       );
 
+  add(&Flags::default_container_dns,
+      "default_container_dns",
+      "JSON-formatted DNS information for CNI networks (Mesos containerizer)\n"
+      "and CNM networks (Docker containerizer). For CNI networks, this flag\n"
+      "can be used to configure `nameservers`, `domain`, `search` and\n"
+      "`options`, and its priority is lower than the DNS information returned\n"
+      "by a CNI plugin, but higher than the DNS information in agent host's\n"
+      "/etc/resolv.conf. For CNM networks, this flag can be used to configure\n"
+      "`nameservers`, `search` and `options`, it will only be used if there\n"
+      "is no DNS information provided in the ContainerInfo.docker.parameters\n"
+      "message.\n"
+      "\n"
+      "See the ContainerDNS message in `flags.proto` for the expected format.\n"
+      "\n"
+      "Example:\n"
+      "{\n"
+      "  \"mesos\": [\n"
+      "    {\n"
+      "      \"network_mode\": \"CNI\",\n"
+      "      \"network_name\": \"net1\",\n"
+      "      \"dns\": {\n"
+      "        \"nameservers\": [ \"8.8.8.8\", \"8.8.4.4\" ]\n"
+      "      }\n"
+      "    }\n"
+      "  ],\n"
+      "  \"docker\": [\n"
+      "    {\n"
+      "      \"network_mode\": \"BRIDGE\",\n"
+      "      \"dns\": {\n"
+      "        \"nameservers\": [ \"8.8.8.8\", \"8.8.4.4\" ]\n"
+      "      }\n"
+      "    },\n"
+      "    {\n"
+      "      \"network_mode\": \"USER\",\n"
+      "      \"network_name\": \"net2\",\n"
+      "      \"dns\": {\n"
+      "        \"nameservers\": [ \"8.8.8.8\", \"8.8.4.4\" ]\n"
+      "      }\n"
+      "    }\n"
+      "  ]\n"
+      "}",
+      [](const Option<ContainerDNSInfo>& defaultContainerDNS) -> Option<Error> {
+        if (defaultContainerDNS.isSome()) {
+          Option<ContainerDNSInfo::MesosInfo> defaultCniDNS;
+          hashmap<string, ContainerDNSInfo::MesosInfo> cniNetworkDNS;
+          Option<ContainerDNSInfo::DockerInfo> dockerBridgeDNS;
+          Option<ContainerDNSInfo::DockerInfo> defaultDockerUserDNS;
+          hashmap<string, ContainerDNSInfo::DockerInfo> dockerUserDNS;
+
+          foreach (const ContainerDNSInfo::MesosInfo& dnsInfo,
+                   defaultContainerDNS->mesos()) {
+            if (dnsInfo.network_mode() ==
+                ContainerDNSInfo::MesosInfo::UNKNOWN) {
+              return Error("UNKNOWN network mode configured "
+                           "in `--default_container_dns`");
+            } else if (dnsInfo.network_mode() ==
+                       ContainerDNSInfo::MesosInfo::HOST) {
+              return Error("Configuring DNS for HOST network with "
+                           "`--default_container_dns` is not yet supported");
+            } else if (dnsInfo.network_mode() ==
+                       ContainerDNSInfo::MesosInfo::CNI) {
+              if (!dnsInfo.has_network_name()) {
+                if (defaultCniDNS.isSome()) {
+                  return Error("Multiple DNS configuration without network "
+                               "name for CNI network in "
+                               "`--default_container_dns` is not allowed");
+                }
+
+                defaultCniDNS = dnsInfo;
+              } else {
+                if (cniNetworkDNS.contains(dnsInfo.network_name())) {
+                  return Error("Multiple DNS configuration with the same "
+                               "network name '" + dnsInfo.network_name() + "' "
+                               "for CNI network in `--default_container_dns` "
+                               "is not allowed");
+                }
+
+                cniNetworkDNS[dnsInfo.network_name()] = dnsInfo;
+              }
+            }
+          }
+
+          foreach (const ContainerDNSInfo::DockerInfo& dnsInfo,
+                   defaultContainerDNS->docker()) {
+            if (dnsInfo.network_mode() ==
+                ContainerDNSInfo::DockerInfo::UNKNOWN) {
+              return Error("UNKNOWN network mode configured "
+                           "in `--default_container_dns`");
+            } else if (dnsInfo.network_mode() ==
+                       ContainerDNSInfo::DockerInfo::HOST) {
+              return Error("Configuring DNS for HOST network with "
+                           "`--default_container_dns` is not yet supported");
+            } else if (dnsInfo.network_mode() ==
+                       ContainerDNSInfo::DockerInfo::BRIDGE) {
+              if (dockerBridgeDNS.isSome()) {
+                return Error("Multiple DNS configuration for Docker default "
+                             "bridge network in `--default_container_dns` is "
+                             "not allowed");
+              }
+
+              dockerBridgeDNS = dnsInfo;
+            } else if (dnsInfo.network_mode() ==
+                       ContainerDNSInfo::DockerInfo::USER) {
+              if (!dnsInfo.has_network_name()) {
+                if (defaultDockerUserDNS.isSome()) {
+                  return Error("Multiple DNS configuration without network "
+                               "name for user-defined CNM network in "
+                               "`--default_container_dns` is not allowed");
+                }
+
+                defaultDockerUserDNS = dnsInfo;
+              } else {
+                if (dockerUserDNS.contains(dnsInfo.network_name())) {
+                  return Error("Multiple DNS configuration with the same "
+                               "network name '" + dnsInfo.network_name() +
+                               "' for user-defined CNM network in "
+                               "`--default_container_dns` is not allowed");
+                }
+
+                dockerUserDNS[dnsInfo.network_name()] = dnsInfo;
+              }
+            }
+          }
+        }
+
+        return None();
+      });
+
   add(&Flags::default_container_info,
       "default_container_info",
       "JSON-formatted ContainerInfo that will be included into\n"

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebfccf4e/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index bf9adf0..032880d 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -122,6 +122,7 @@ public:
   Option<std::string> docker_mesos_image;
   Duration docker_remove_delay;
   std::string sandbox_directory;
+  Option<ContainerDNSInfo> default_container_dns;
   Option<ContainerInfo> default_container_info;
 
   // TODO(alexr): Remove this after the deprecation cycle (started in 1.0).