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 label decorator hook is called from within the slave when
57  // receiving a run task request from the master. A module
58  // implementing the hook creates and returns a set of labels. These
59  // labels overwrite the existing labels on the task info.
61  const TaskInfo& taskInfo,
62  const ExecutorInfo& executorInfo,
63  const FrameworkInfo& frameworkInfo,
64  const SlaveInfo& slaveInfo)
65  {
66  return None();
67  }
68 
69 
70  // This hook is called when an Agent is removed i.e. deemed lost by the
71  // master. The hook is invoked after all frameworks have been informed about
72  // the loss.
73  virtual Try<Nothing> masterSlaveLostHook(const SlaveInfo& slaveInfo)
74  {
75  return Nothing();
76  }
77 
78 
79  // This environment decorator hook is called from within slave when
80  // launching a new executor. A module implementing the hook creates
81  // and returns a set of environment variables. These environment
82  // variables then become part of the executor's environment.
83  // Ideally, a hook module will also look at the existing environment
84  // variables in executorInfo and extend the values as needed in case
85  // of a conflict.
87  const ExecutorInfo& executorInfo)
88  {
89  return None();
90  }
91 
92  // This task and executor decorator is called from within the slave after
93  // receiving a run task request from the master but before the docker
94  // containerizer launches the task. A module implementing the hook can
95  // inspect the arguments and return a `Failure` if the task should be
96  // rejected with a `TASK_FAILED`.
97  // The hook can return a set of environment variables individually for
98  // both, the executor and the task. Note that for custom executors,
99  // the task environment variables, in case of conflicts, *will*
100  // overwrite the executor variables.
101  //
102  // NOTE: The order of hooks matters for environment variables.
103  // If there is a conflict, the hook loaded last will take priority.
104  //
105  // NOTE: Unlike `slaveExecutorEnvironmentDecorator`, environment variables
106  // returned from this hook, in case of conflicts, *will* overwrite
107  // environment variables inside the `ExecutorInfo`.
110  const Option<TaskInfo>& taskInfo,
111  const ExecutorInfo& executorInfo,
112  const std::string& containerName,
113  const std::string& containerWorkDirectory,
114  const std::string& mappedSandboxDirectory,
115  const Option<std::map<std::string, std::string>>& env)
116  {
117  return None();
118  }
119 
120 
121  // This hook is called from within slave after URIs and container
122  // image are fetched. A typical module implementing this hook will
123  // perform some operations on the fetched artifacts.
125  const ContainerID& containerId,
126  const std::string& directory)
127  {
128  return Nothing();
129  }
130 
131 
132  // This hook is called from within slave when an executor is being
133  // removed. A typical module implementing the hook will perform some
134  // cleanup as required.
136  const FrameworkInfo& frameworkInfo,
137  const ExecutorInfo& executorInfo)
138  {
139  return Nothing();
140  }
141 
142  // This hook is called from within slave when it receives a status update from
143  // the executor. A module implementing the hook creates and returns a
144  // TaskStatus with a set of labels and container_status. These labels and
145  // container status overwrite the existing labels on the TaskStatus. Remaining
146  // fields from the returned TaskStatus are discarded.
148  const FrameworkID& frameworkId,
149  const TaskStatus& status)
150  {
151  return None();
152  }
153 
154  // This hook is called from within the slave when it initializes. A module
155  // implementing the hook creates and returns a Resources object with the new
156  // list of resources available on the slave before they are advertised to the
157  // master. These new resources overwrite the previous ones in SlaveInfo.
159  const SlaveInfo& slaveInfo)
160  {
161  return None();
162  }
163 
164  // This hook is called from within the slave when it initializes. A module
165  // implementing the hook creates and returns an Attributes object with the
166  // new list of attributes for the slave before they are advertised to the
167  // master. These new attributes overwrite the previous ones in SlaveInfo.
169  const SlaveInfo& slaveInfo)
170  {
171  return None();
172  }
173 };
174 
175 } // namespace mesos {
176 
177 #endif // __MESOS_HOOK_HPP__
virtual Result< Environment > slaveExecutorEnvironmentDecorator(const ExecutorInfo &executorInfo)
Definition: hook.hpp:86
Definition: nothing.hpp:16
Definition: option.hpp:28
Definition: try.hpp:34
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:109
virtual Try< Nothing > masterSlaveLostHook(const SlaveInfo &slaveInfo)
Definition: hook.hpp:73
virtual Result< TaskStatus > slaveTaskStatusDecorator(const FrameworkID &frameworkId, const TaskStatus &status)
Definition: hook.hpp:147
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
virtual Try< Nothing > slaveRemoveExecutorHook(const FrameworkInfo &frameworkInfo, const ExecutorInfo &executorInfo)
Definition: hook.hpp:135
Definition: result.hpp:40
virtual Result< Resources > slaveResourcesDecorator(const SlaveInfo &slaveInfo)
Definition: hook.hpp:158
virtual Result< Labels > slaveRunTaskLabelDecorator(const TaskInfo &taskInfo, const ExecutorInfo &executorInfo, const FrameworkInfo &frameworkInfo, const SlaveInfo &slaveInfo)
Definition: hook.hpp:60
virtual Result< Labels > masterLaunchTaskLabelDecorator(const TaskInfo &taskInfo, const FrameworkInfo &frameworkInfo, const SlaveInfo &slaveInfo)
Definition: hook.hpp:48
Definition: none.hpp:27
virtual Result< Attributes > slaveAttributesDecorator(const SlaveInfo &slaveInfo)
Definition: hook.hpp:168
virtual Try< Nothing > slavePostFetchHook(const ContainerID &containerId, const std::string &directory)
Definition: hook.hpp:124
virtual ~Hook()
Definition: hook.hpp:42
Definition: hook.hpp:39
Definition: future.hpp:57