You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2016/11/29 06:15:34 UTC

[5/6] mesos git commit: Added API handler for ATTACH_CONTAINER_OUTPUT.

Added API handler for ATTACH_CONTAINER_OUTPUT.

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


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

Branch: refs/heads/master
Commit: d536a135dfee0408a5b7de8e02c449f8b69edbfc
Parents: e1be7cf
Author: Vinod Kone <vi...@gmail.com>
Authored: Sun Nov 20 22:27:26 2016 +0800
Committer: Vinod Kone <vi...@gmail.com>
Committed: Mon Nov 28 22:14:01 2016 -0800

----------------------------------------------------------------------
 src/internal/evolve.cpp  |   6 +++
 src/internal/evolve.hpp  |   1 +
 src/slave/http.cpp       | 101 +++++++++++++++++++++++++++++++++++++++++-
 src/slave/slave.hpp      |   6 +++
 src/slave/validation.cpp |  17 ++++++-
 src/tests/api_tests.cpp  |  37 ++++++++++++++++
 6 files changed, 166 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/internal/evolve.cpp
----------------------------------------------------------------------
diff --git a/src/internal/evolve.cpp b/src/internal/evolve.cpp
index 23a515c..5ef2306 100644
--- a/src/internal/evolve.cpp
+++ b/src/internal/evolve.cpp
@@ -195,6 +195,12 @@ v1::agent::Call evolve(const mesos::agent::Call& call)
 }
 
 
+v1::agent::ProcessIO evolve(const mesos::agent::ProcessIO& processIO)
+{
+  return evolve<v1::agent::ProcessIO>(processIO);
+}
+
+
 v1::agent::Response evolve(const mesos::agent::Response& response)
 {
   return evolve<v1::agent::Response>(response);

http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/internal/evolve.hpp
----------------------------------------------------------------------
diff --git a/src/internal/evolve.hpp b/src/internal/evolve.hpp
index e8b03c9..73d55eb 100644
--- a/src/internal/evolve.hpp
+++ b/src/internal/evolve.hpp
@@ -76,6 +76,7 @@ v1::TaskInfo evolve(const TaskInfo& taskInfo);
 v1::TaskStatus evolve(const TaskStatus& status);
 
 v1::agent::Call evolve(const mesos::agent::Call& call);
+v1::agent::ProcessIO evolve(const mesos::agent::ProcessIO& processIO);
 v1::agent::Response evolve(const mesos::agent::Response& response);
 
 v1::maintenance::ClusterStatus evolve(

http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/slave/http.cpp
----------------------------------------------------------------------
diff --git a/src/slave/http.cpp b/src/slave/http.cpp
index cca88b8..91bb882 100644
--- a/src/slave/http.cpp
+++ b/src/slave/http.cpp
@@ -55,6 +55,7 @@
 
 #include "common/build.hpp"
 #include "common/http.hpp"
+#include "common/recordio.hpp"
 
 #include "internal/devolve.hpp"
 
@@ -66,6 +67,10 @@
 
 #include "version/version.hpp"
 
+using mesos::agent::ProcessIO;
+
+using mesos::internal::recordio::Reader;
+
 using mesos::slave::ContainerTermination;
 
 using process::AUTHENTICATION;
@@ -80,6 +85,7 @@ using process::TLDR;
 
 using process::http::Accepted;
 using process::http::BadRequest;
+using process::http::Connection;
 using process::http::Forbidden;
 using process::http::NotFound;
 using process::http::InternalServerError;
@@ -93,6 +99,8 @@ using process::http::UnsupportedMediaType;
 
 using process::metrics::internal::MetricsProcess;
 
+using ::recordio::Decoder;
+
 using std::list;
 using std::string;
 using std::tie;
@@ -444,8 +452,10 @@ Future<Response> Slave::Http::_api(
 
     case mesos::agent::Call::LAUNCH_NESTED_CONTAINER_SESSION:
     case mesos::agent::Call::ATTACH_CONTAINER_INPUT:
-    case mesos::agent::Call::ATTACH_CONTAINER_OUTPUT:
       return NotImplemented();
+
+    case mesos::agent::Call::ATTACH_CONTAINER_OUTPUT:
+      return attachContainerOutput(call, contentType, acceptType, principal);
   }
 
   UNREACHABLE();
@@ -2091,6 +2101,95 @@ Future<Response> Slave::Http::killNestedContainer(
     });
 }
 
+
+Future<Response> Slave::Http::attachContainerOutput(
+    const mesos::agent::Call& call,
+    ContentType contentType,
+    ContentType acceptType,
+    const Option<string>& principal) const
+{
+  CHECK_EQ(mesos::agent::Call::ATTACH_CONTAINER_OUTPUT, call.type());
+  CHECK(call.has_attach_container_output());
+
+  const ContainerID& containerId =
+    call.attach_container_output().container_id();
+
+  return slave->containerizer->attach(containerId)
+    .then([call, contentType, acceptType](Connection connection)
+        -> Future<Response> {
+      Request request;
+      request.method = "POST";
+      request.headers = {{"Accept", stringify(acceptType)},
+                         {"Content-Type", stringify(contentType)}};
+
+      // The 'HOST' header must be EMPTY for non Internet addresses.
+      // TODO(vinod): Instead of setting domain to empty string (which results
+      // in an empty HOST header), add a new URL constructor that doesn't
+      // require domain or IP.
+      request.url.domain = "";
+
+      // NOTE: The path is currently ignored by the switch board.
+      request.url.path = "/";
+
+      request.type = Request::BODY;
+      request.body = serialize(contentType, call);
+
+      // We capture `connection` here to ensure that it doesn't go
+      // out of scope until the `onAny` handler on `transform` is executed.
+      return connection.send(request, true)
+        .then([connection, acceptType](const Response& response)
+            -> Future<Response> {
+          if (response.status != OK().status) {
+            return response;
+          }
+
+          // Evolve the `ProcessIO` records in the Response body to v1
+          // before sending them to the client.
+          Pipe pipe;
+          Pipe::Writer writer = pipe.writer();
+
+          OK ok;
+          ok.headers["Content-Type"] = stringify(acceptType);
+          ok.type = Response::PIPE;
+          ok.reader = pipe.reader();
+
+          CHECK_EQ(Response::PIPE, response.type);
+          CHECK_SOME(response.reader);
+          Pipe::Reader reader = response.reader.get();
+
+          auto deserializer = lambda::bind(
+              deserialize<ProcessIO>, acceptType, lambda::_1);
+
+          Owned<Reader<ProcessIO>> decoder(new Reader<ProcessIO>(
+              Decoder<ProcessIO>(deserializer), reader));
+
+          auto encoder = [acceptType](const ProcessIO& processIO) {
+            ::recordio::Encoder<v1::agent::ProcessIO> encoder (lambda::bind(
+                serialize, acceptType, lambda::_1));
+
+            return encoder.encode(evolve(processIO));
+          };
+
+          recordio::transform<ProcessIO>(std::move(decoder), encoder, writer)
+            .onAny([writer, reader, connection](
+                const Future<Nothing>& future) mutable {
+              CHECK(!future.isDiscarded());
+
+              if (future.isFailed()) {
+                writer.fail(future.failure());
+                reader.close();
+                return;
+              }
+
+              writer.close();
+              reader.close();
+            });
+
+          return ok;
+        });
+    });
+}
+
 } // namespace slave {
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/slave/slave.hpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.hpp b/src/slave/slave.hpp
index 0ede185..05865a0 100644
--- a/src/slave/slave.hpp
+++ b/src/slave/slave.hpp
@@ -645,6 +645,12 @@ private:
         const Option<std::string>& principal,
         ContentType contentType) const;
 
+    process::Future<process::http::Response> attachContainerOutput(
+        const mesos::agent::Call& call,
+        ContentType contentType,
+        ContentType acceptType,
+        const Option<std::string>& principal) const;
+
     Slave* slave;
 
     // Used to rate limit the statistics endpoint.

http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/slave/validation.cpp
----------------------------------------------------------------------
diff --git a/src/slave/validation.cpp b/src/slave/validation.cpp
index b3d8bcd..46e8436 100644
--- a/src/slave/validation.cpp
+++ b/src/slave/validation.cpp
@@ -224,8 +224,23 @@ Option<Error> validate(
 
     case mesos::agent::Call::LAUNCH_NESTED_CONTAINER_SESSION:
     case mesos::agent::Call::ATTACH_CONTAINER_INPUT:
-    case mesos::agent::Call::ATTACH_CONTAINER_OUTPUT:
       return Error("Unsupported");
+
+    case mesos::agent::Call::ATTACH_CONTAINER_OUTPUT: {
+      if (!call.has_attach_container_output()) {
+        return Error("Expecting 'attach_container_output' to be present");
+      }
+
+      Option<Error> error = validation::container::validateContainerId(
+          call.attach_container_output().container_id());
+
+      if (error.isSome()) {
+        return Error("'attach_container_output.container_id' is invalid"
+                     ": " + error->message);
+      }
+
+      return None();
+    }
   }
 
   UNREACHABLE();

http://git-wip-us.apache.org/repos/asf/mesos/blob/d536a135/src/tests/api_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/api_tests.cpp b/src/tests/api_tests.cpp
index 03e8590..b2a70f3 100644
--- a/src/tests/api_tests.cpp
+++ b/src/tests/api_tests.cpp
@@ -3616,6 +3616,43 @@ TEST_P(AgentAPITest, NestedContainerLaunch)
   driver.join();
 }
 
+
+// TODO(vinod): Update the test when mesos containerizer
+// adds support for `attach`.
+TEST_P(AgentAPITest, AttachContainerOutputFailure)
+{
+  ContentType contentType = GetParam();
+
+  Clock::pause();
+
+  Future<Nothing> __recover = FUTURE_DISPATCH(_, &Slave::__recover);
+
+  StandaloneMasterDetector detector;
+  Try<Owned<cluster::Slave>> slave = StartSlave(&detector);
+
+  ASSERT_SOME(slave);
+
+  // Wait for the agent to finish recovery.
+  AWAIT_READY(__recover);
+  Clock::settle();
+
+  v1::agent::Call call;
+  call.set_type(v1::agent::Call::ATTACH_CONTAINER_OUTPUT);
+
+  call.mutable_attach_container_output()->mutable_container_id()
+    ->set_value(UUID::random().toString());
+
+  Future<http::Response> response = http::post(
+    slave.get()->pid,
+    "api/v1",
+    createBasicAuthHeaders(DEFAULT_CREDENTIAL),
+    serialize(contentType, call),
+    stringify(contentType));
+
+  AWAIT_EXPECT_RESPONSE_STATUS_EQ(http::InternalServerError().status, response);
+  AWAIT_EXPECT_RESPONSE_BODY_EQ("Unsupported", response);
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {