Apache Mesos
hook.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_HOOK_HPP__
18 #define __MESOS_HOOK_HPP__
19 
20 #include <map>
21 #include <string>
22 
23 #include <mesos/attributes.hpp>
24 #include <mesos/mesos.hpp>
25 #include <mesos/resources.hpp>
26 
27 #include <process/future.hpp>
28 
29 #include <stout/none.hpp>
30 #include <stout/nothing.hpp>
31 #include <stout/result.hpp>
32 #include <stout/try.hpp>
33 
34 // ONLY USEFUL AFTER RUNNING PROTOC.
35 #include <mesos/module/hook.pb.h>
36 
37 namespace mesos {
38 
39 class Hook
40 {
41 public:
42  virtual ~Hook() {}
43 
44  // This label decorator hook is called from within master during
45  // the launchTask routine. A module implementing the hook creates
46  // and returns a set of labels. These labels overwrite the existing
47  // labels on the task info.
49  const TaskInfo& taskInfo,
50  const FrameworkInfo& frameworkInfo,
51  const SlaveInfo& slaveInfo)
52  {
53  return None();
54  }
55 
56  // This resource decorator hook is called from within the master during
57  // the launchTask routine. An implementation of this hook allows the user
58  // to, for example, allocate a default amount of a certain resource if it is
59  // not provided by the framework. This hook aims to provide default values for
60  // resources that must be accounted. Implementing this hook is useful when one
61  // a new resource is introduced and at least one framework does not support it
62  // yet. Note that the original resources from the TaskInfo will be overwritten
63  // by the ones returned by the hook.
65  const TaskInfo& task,
66  const Resources& slaveResources)
67  {
68  return None();
69  }
70 
71  // This label decorator hook is called from within the slave when
72  // receiving a run task request from the master. A module
73  // implementing the hook creates and returns a set of labels. These
74  // labels overwrite the existing labels on the task info.
76  const TaskInfo& taskInfo,
77  const ExecutorInfo& executorInfo,
78  const FrameworkInfo& frameworkInfo,
79  const SlaveInfo& slaveInfo)
80  {
81  return None();
82  }
83 
84 
85  // This hook is called when an Agent is removed i.e. deemed lost by the
86  // master. The hook is invoked after all frameworks have been informed about
87  // the loss.
88  virtual Try<Nothing> masterSlaveLostHook(const SlaveInfo& slaveInfo)
89  {
90  return Nothing();
91  }
92 
93 
94  // This environment decorator hook is called from within slave when
95  // launching a new executor. A module implementing the hook creates
96  // and returns a set of environment variables. These environment
97  // variables then become part of the executor's environment.
98  // Ideally, a hook module will also look at the existing environment
99  // variables in executorInfo and extend the values as needed in case
100  // of a conflict.
102  const ExecutorInfo& executorInfo)
103  {
104  return None();
105  }
106 
107  // This task and executor decorator is called from within the slave after
108  // receiving a run task request from the master but before the docker
109  // containerizer launches the task. A module implementing the hook can
110  // inspect the arguments and return a `Failure` if the task should be
111  // rejected with a `TASK_FAILED`.
112  // The hook can return a set of environment variables individually for
113  // both, the executor and the task. Note that for custom executors,
114  // the task environment variables, in case of conflicts, *will*
115  // overwrite the executor variables.
116  //
117  // NOTE: The order of hooks matters for environment variables.
118  // If there is a conflict, the hook loaded last will take priority.
119  //
120  // NOTE: Unlike `slaveExecutorEnvironmentDecorator`, environment variables
121  // returned from this hook, in case of conflicts, *will* overwrite
122  // environment variables inside the `ExecutorInfo`.
125  const Option<TaskInfo>& taskInfo,
126  const ExecutorInfo& executorInfo,
127  const std::string& containerName,
128  const std::string& containerWorkDirectory,
129  const std::string& mappedSandboxDirectory,
130  const Option<std::map<std::string, std::string>>& env)
131  {
132  return None();
133  }
134 
135 
136  // This hook is called from within slave after URIs and container
137  // image are fetched. A typical module implementing this hook will
138  // perform some operations on the fetched artifacts.
140  const ContainerID& containerId,
141  const std::string& directory)
142  {
143  return Nothing();
144  }
145 
146 
147  // This hook is called from within slave when an executor is being
148  // removed. A typical module implementing the hook will perform some
149  // cleanup as required.
151  const FrameworkInfo& frameworkInfo,
152  const ExecutorInfo& executorInfo)
153  {
154  return Nothing();
155  }
156 
157  // This hook is called from within slave when it receives a status update from
158  // the executor. A module implementing the hook creates and returns a
159  // TaskStatus with a set of labels and container_status. These labels and
160  // container status overwrite the existing labels on the TaskStatus. Remaining
161  // fields from the returned TaskStatus are discarded.
163  const FrameworkID& frameworkId,
164  const TaskStatus& status)
165  {
166  return None();
167  }
168 
169  // This hook is called from within the slave when it initializes. A module
170  // implementing the hook creates and returns a Resources object with the new
171  // list of resources available on the slave before they are advertised to the
172  // master. These new resources overwrite the previous ones in SlaveInfo.
174  const SlaveInfo& slaveInfo)
175  {
176  return None();
177  }
178 
179  // This hook is called from within the slave when it initializes. A module
180  // implementing the hook creates and returns an Attributes object with the
181  // new list of attributes for the slave before they are advertised to the
182  // master. These new attributes overwrite the previous ones in SlaveInfo.
184  const SlaveInfo& slaveInfo)
185  {
186  return None();
187  }
188 };
189 
190 } // namespace mesos {
191 
192 #endif // __MESOS_HOOK_HPP__
virtual Result< Environment > slaveExecutorEnvironmentDecorator(const ExecutorInfo &executorInfo)
Definition: hook.hpp:101
Definition: nothing.hpp:16
Definition: option.hpp:29
Definition: check.hpp:33
virtual process::Future< Option< DockerTaskExecutorPrepareInfo > > slavePreLaunchDockerTaskExecutorDecorator(const Option< TaskInfo > &taskInfo, const ExecutorInfo &executorInfo, const std::string &containerName, const std::string &containerWorkDirectory, const std::string &mappedSandboxDirectory, const Option< std::map< std::string, std::string >> &env)
Definition: hook.hpp:124
virtual Try< Nothing > masterSlaveLostHook(const SlaveInfo &slaveInfo)
Definition: hook.hpp:88
virtual Result< TaskStatus > slaveTaskStatusDecorator(const FrameworkID &frameworkId, const TaskStatus &status)
Definition: hook.hpp:162
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: resources.hpp:83
virtual Try< Nothing > slaveRemoveExecutorHook(const FrameworkInfo &frameworkInfo, const ExecutorInfo &executorInfo)
Definition: hook.hpp:150
Definition: check.hpp:30
virtual Result< Resources > slaveResourcesDecorator(const SlaveInfo &slaveInfo)
Definition: hook.hpp:173
virtual Result< Labels > slaveRunTaskLabelDecorator(const TaskInfo &taskInfo, const ExecutorInfo &executorInfo, const FrameworkInfo &frameworkInfo, const SlaveInfo &slaveInfo)
Definition: hook.hpp:75
Definition: agent.hpp:25
virtual Result< Labels > masterLaunchTaskLabelDecorator(const TaskInfo &taskInfo, const FrameworkInfo &frameworkInfo, const SlaveInfo &slaveInfo)
Definition: hook.hpp:48
Definition: none.hpp:27
virtual Result< Resources > masterLaunchTaskResourceDecorator(const TaskInfo &task, const Resources &slaveResources)
Definition: hook.hpp:64
virtual Result< Attributes > slaveAttributesDecorator(const SlaveInfo &slaveInfo)
Definition: hook.hpp:183
virtual Try< Nothing > slavePostFetchHook(const ContainerID &containerId, const std::string &directory)
Definition: hook.hpp:139
virtual ~Hook()
Definition: hook.hpp:42
Definition: hook.hpp:39
Definition: future.hpp:58