You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2017/12/09 02:54:53 UTC

[6/7] mesos git commit: Implemented API for modifying resource provider configs.

Implemented API for modifying resource provider configs.

This patch adds the implementation for `ADD_RESOURCE_PROVIDER_CONFIG`,
`UPDATE_RESOURCE_PROVIDER_CONFIG` and `REMOVE_RESOURCE_PROVIDER_CONFIG`,
which call the corresponding methods of `LocalResourceProviderDaemon`.
The implementation of the methods is in the following patch.

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


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

Branch: refs/heads/master
Commit: 54422d591a5d3a970b1a009d10b4c91ad015e431
Parents: 8de5b27
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
Authored: Fri Dec 8 18:17:25 2017 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Dec 8 18:54:43 2017 -0800

----------------------------------------------------------------------
 src/resource_provider/daemon.cpp |  21 ++++
 src/resource_provider/daemon.hpp |   6 ++
 src/slave/http.cpp               | 177 ++++++++++++++++++++++++++++++++++
 src/slave/http.hpp               |  12 +++
 4 files changed, 216 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/54422d59/src/resource_provider/daemon.cpp
----------------------------------------------------------------------
diff --git a/src/resource_provider/daemon.cpp b/src/resource_provider/daemon.cpp
index 354054e..6159982 100644
--- a/src/resource_provider/daemon.cpp
+++ b/src/resource_provider/daemon.cpp
@@ -320,5 +320,26 @@ void LocalResourceProviderDaemon::start(const SlaveID& slaveId)
   dispatch(process.get(), &LocalResourceProviderDaemonProcess::start, slaveId);
 }
 
+
+Future<bool> LocalResourceProviderDaemon::add(const ResourceProviderInfo& info)
+{
+  return Failure("Unimplemented");
+}
+
+
+Future<bool> LocalResourceProviderDaemon::update(
+    const ResourceProviderInfo& info)
+{
+  return Failure("Unimplemented");
+}
+
+
+Future<bool> LocalResourceProviderDaemon::remove(
+    const string& type,
+    const string& name)
+{
+  return Failure("Unimplemented");
+}
+
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/54422d59/src/resource_provider/daemon.hpp
----------------------------------------------------------------------
diff --git a/src/resource_provider/daemon.hpp b/src/resource_provider/daemon.hpp
index 14bc41f..7c513a2 100644
--- a/src/resource_provider/daemon.hpp
+++ b/src/resource_provider/daemon.hpp
@@ -59,6 +59,12 @@ public:
 
   void start(const SlaveID& slaveId);
 
+  process::Future<bool> add(const ResourceProviderInfo& info);
+  process::Future<bool> update(const ResourceProviderInfo& info);
+  process::Future<bool> remove(
+      const std::string& type,
+      const std::string& name);
+
 private:
   LocalResourceProviderDaemon(
       const process::http::URL& url,

http://git-wip-us.apache.org/repos/asf/mesos/blob/54422d59/src/slave/http.cpp
----------------------------------------------------------------------
diff --git a/src/slave/http.cpp b/src/slave/http.cpp
index 3de874c..738786f 100644
--- a/src/slave/http.cpp
+++ b/src/slave/http.cpp
@@ -101,6 +101,7 @@ using process::TLDR;
 
 using process::http::Accepted;
 using process::http::BadRequest;
+using process::http::Conflict;
 using process::http::Connection;
 using process::http::Forbidden;
 using process::http::NotFound;
@@ -614,6 +615,15 @@ Future<Response> Http::_api(
 
     case mesos::agent::Call::REMOVE_CONTAINER:
       return removeContainer(call, mediaTypes.accept, principal);
+
+    case mesos::agent::Call::ADD_RESOURCE_PROVIDER_CONFIG:
+      return addResourceProviderConfig(call, principal);
+
+    case mesos::agent::Call::UPDATE_RESOURCE_PROVIDER_CONFIG:
+      return updateResourceProviderConfig(call, principal);
+
+    case mesos::agent::Call::REMOVE_RESOURCE_PROVIDER_CONFIG:
+      return removeResourceProviderConfig(call, principal);
   }
 
   UNREACHABLE();
@@ -3113,6 +3123,173 @@ Future<Response> Http::attachContainerInput(
 }
 
 
+Future<Response> Http::addResourceProviderConfig(
+    const mesos::agent::Call& call,
+    const Option<Principal>& principal) const
+{
+  CHECK_EQ(mesos::agent::Call::ADD_RESOURCE_PROVIDER_CONFIG, call.type());
+  CHECK(call.has_add_resource_provider_config());
+
+  Future<Owned<ObjectApprover>> approver;
+
+  if (slave->authorizer.isSome()) {
+    Option<authorization::Subject> subject = createSubject(principal);
+
+    approver = slave->authorizer.get()->getObjectApprover(
+        subject,
+        authorization::MODIFY_RESOURCE_PROVIDER_CONFIG);
+  } else {
+    approver = Owned<ObjectApprover>(new AcceptingObjectApprover());
+  }
+
+  return approver
+    .then(defer(slave->self(), [=](
+        const Owned<ObjectApprover>& approver) -> Future<Response> {
+      Try<bool> approved = approver->approved(ObjectApprover::Object());
+      if (approved.isError()) {
+        return InternalServerError("Authorization error: " + approved.error());
+      } else if (!approved.get()) {
+        return Forbidden();
+      }
+
+      const ResourceProviderInfo& info =
+        call.add_resource_provider_config().info();
+
+      LOG(INFO)
+        << "Processing ADD_RESOURCE_PROVIDER_CONFIG call with type '"
+        << info.type() << "' and name '" << info.name() << "'";
+
+      return slave->localResourceProviderDaemon->add(info)
+        .then([](bool added) -> Response {
+          if (!added) {
+            return Conflict();
+          }
+
+          return OK();
+        })
+        .repair([info](const Future<Response>& future) {
+          LOG(ERROR)
+            << "Failed to add resource provider config with type '"
+            << info.type() << "' and name '" << info.name() << "': "
+            << future.failure();
+
+          return InternalServerError(future.failure());
+        });
+    }));
+}
+
+
+Future<Response> Http::updateResourceProviderConfig(
+    const mesos::agent::Call& call,
+    const Option<Principal>& principal) const
+{
+  CHECK_EQ(mesos::agent::Call::UPDATE_RESOURCE_PROVIDER_CONFIG, call.type());
+  CHECK(call.has_update_resource_provider_config());
+
+  Future<Owned<ObjectApprover>> approver;
+
+  if (slave->authorizer.isSome()) {
+    Option<authorization::Subject> subject = createSubject(principal);
+
+    approver = slave->authorizer.get()->getObjectApprover(
+        subject,
+        authorization::MODIFY_RESOURCE_PROVIDER_CONFIG);
+  } else {
+    approver = Owned<ObjectApprover>(new AcceptingObjectApprover());
+  }
+
+  return approver
+    .then(defer(slave->self(), [=](
+        const Owned<ObjectApprover>& approver) -> Future<Response> {
+      Try<bool> approved = approver->approved(ObjectApprover::Object());
+      if (approved.isError()) {
+        return InternalServerError("Authorization error: " + approved.error());
+      } else if (!approved.get()) {
+        return Forbidden();
+      }
+
+      const ResourceProviderInfo& info =
+        call.update_resource_provider_config().info();
+
+      LOG(INFO)
+        << "Processing UPDATE_RESOURCE_PROVIDER_CONFIG call with type '"
+        << info.type() << "' and name '" << info.name() << "'";
+
+      return slave->localResourceProviderDaemon->update(info)
+        .then([](bool updated) -> Response {
+          if (!updated) {
+            return NotFound();
+          }
+
+          return OK();
+        })
+        .repair([info](const Future<Response>& future) {
+          LOG(ERROR)
+            << "Failed to update resource provider config with type '"
+            << info.type() << "' and name '" << info.name() << "': "
+            << future.failure();
+
+          return InternalServerError(future.failure());
+        });
+    }));
+}
+
+
+Future<Response> Http::removeResourceProviderConfig(
+    const mesos::agent::Call& call,
+    const Option<Principal>& principal) const
+{
+  CHECK_EQ(mesos::agent::Call::REMOVE_RESOURCE_PROVIDER_CONFIG, call.type());
+  CHECK(call.has_remove_resource_provider_config());
+
+  Future<Owned<ObjectApprover>> approver;
+
+  if (slave->authorizer.isSome()) {
+    Option<authorization::Subject> subject = createSubject(principal);
+
+    approver = slave->authorizer.get()->getObjectApprover(
+        subject,
+        authorization::MODIFY_RESOURCE_PROVIDER_CONFIG);
+  } else {
+    approver = Owned<ObjectApprover>(new AcceptingObjectApprover());
+  }
+
+  return approver
+    .then(defer(slave->self(), [=](
+        const Owned<ObjectApprover>& approver) -> Future<Response> {
+      Try<bool> approved = approver->approved(ObjectApprover::Object());
+      if (approved.isError()) {
+        return InternalServerError("Authorization error: " + approved.error());
+      } else if (!approved.get()) {
+        return Forbidden();
+      }
+
+      const string& type = call.remove_resource_provider_config().type();
+      const string& name = call.remove_resource_provider_config().name();
+
+      LOG(INFO)
+        << "Processing REMOVE_RESOURCE_PROVIDER_CONFIG call with type '" << type
+        << "' and name '" << name << "'";
+
+      return slave->localResourceProviderDaemon->remove(type, name)
+        .then([](bool removed) -> Response {
+          if (!removed) {
+            return NotFound();
+          }
+
+          return OK();
+        })
+        .repair([type, name](const Future<Response>& future) {
+          LOG(ERROR)
+            << "Failed to remove resource provider config with type '" << type
+            << "' and name '" << name << "': " << future.failure();
+
+          return InternalServerError(future.failure());
+        });
+    }));
+}
+
+
 // Helper that reads data from `writer` and writes to `reader`.
 // Returns a failed future if there are any errors reading or writing.
 // The future is satisfied when we get a EOF.

http://git-wip-us.apache.org/repos/asf/mesos/blob/54422d59/src/slave/http.hpp
----------------------------------------------------------------------
diff --git a/src/slave/http.hpp b/src/slave/http.hpp
index 5eecb2a..9ca0617 100644
--- a/src/slave/http.hpp
+++ b/src/slave/http.hpp
@@ -306,6 +306,18 @@ private:
       const mesos::agent::Call& call,
       const RequestMediaTypes& mediaTypes) const;
 
+  process::Future<process::http::Response> addResourceProviderConfig(
+      const mesos::agent::Call& call,
+      const Option<process::http::authentication::Principal>& principal) const;
+
+  process::Future<process::http::Response> updateResourceProviderConfig(
+      const mesos::agent::Call& call,
+      const Option<process::http::authentication::Principal>& principal) const;
+
+  process::Future<process::http::Response> removeResourceProviderConfig(
+      const mesos::agent::Call& call,
+      const Option<process::http::authentication::Principal>& principal) const;
+
   Slave* slave;
 
   // Used to rate limit the statistics endpoint.