Apache Mesos
validation.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 __MASTER_VALIDATION_HPP__
18 #define __MASTER_VALIDATION_HPP__
19 
20 #include <vector>
21 
22 #include <google/protobuf/repeated_field.h>
23 
24 #include <mesos/mesos.hpp>
25 #include <mesos/resources.hpp>
26 
28 
29 #include <mesos/master/master.hpp>
30 
32 
33 #include <stout/error.hpp>
34 #include <stout/option.hpp>
35 
37 
38 namespace mesos {
39 namespace internal {
40 namespace master {
41 
42 class Master;
43 
44 struct Framework;
45 struct Slave;
46 
47 namespace validation {
48 
49 namespace master {
50 namespace call {
51 
52 // Validates that a master:Call is well-formed.
53 //
54 // TODO(bmahler): Note that this does not validate the fields within
55 // the nested messages (e.g. `ReserveResources`) which is unintuitive.
56 // Consider moving all `master::Call` validation that does not require
57 // master state into this function.
58 //
59 // TODO(bmahler): Add unit tests.
61 
62 } // namespace call {
63 
64 namespace message {
65 
66 // Validation helpers for internal Mesos protocol messages. This is a
67 // best-effort validation, intended to prevent trivial attacks on the
68 // protocol in deployments where the network between master and agents
69 // is not secured. The longer term remedy for this is to make security
70 // guarantees at the libprocess level that would prevent arbitrary UPID
71 // impersonation (MESOS-7424).
72 
73 Option<Error> registerSlave(const RegisterSlaveMessage& message);
74 Option<Error> reregisterSlave(const ReregisterSlaveMessage& message);
75 
76 } // namespace message {
77 } // namespace master {
78 
79 
80 namespace framework {
81 namespace internal {
82 
83 // Validates the roles in given FrameworkInfo. Role, roles and
84 // MULTI_ROLE should be set according to following matrix. Also,
85 // roles should not contain duplicate entries.
86 //
87 // -- MULTI_ROLE is NOT set --
88 // +-------+-------+---------+
89 // | |Roles |No Roles |
90 // +-------+-------+---------+
91 // |Role | Error | None |
92 // +-------+-------+---------+
93 // |No Role| Error | None |
94 // +-------+-------+---------+
95 //
96 // ---- MULTI_ROLE is set ----
97 // +-------+-------+---------+
98 // | |Roles |No Roles |
99 // +-------+-------+---------+
100 // |Role | Error | Error |
101 // +-------+-------+---------+
102 // |No Role| None | None |
103 // +-------+-------+---------+
104 Option<Error> validateRoles(const mesos::FrameworkInfo& frameworkInfo);
105 
106 Option<Error> validateFrameworkId(const mesos::FrameworkInfo& frameworkInfo);
107 
108 Option<Error> validateOfferFilters(const FrameworkInfo& frameworkInfo);
109 
110 } // namespace internal {
111 
112 // Validate a FrameworkInfo.
113 Option<Error> validate(const mesos::FrameworkInfo& frameworkInfo);
114 
115 // Validate that the immutable fields of two FrameworkInfos are identical.
116 // Currently these fields are 'principal', 'user' and 'checkpoint'.
118  const FrameworkInfo& oldInfo,
119  const FrameworkInfo& newInfo);
120 
121 // Adjusts `newInfo` to ensure that the `user` and `checkpoint` fields
122 // are not modified and logs a warning if they were modified.
123 //
124 // NOTE: This is a legacy function used to preserve the behavior of
125 // re-subscription silently ignoring these fields. It should not be
126 // used in new code.
127 //
128 // TODO(asekretenko): Remove this function (see MESOS-9747).
130  const FrameworkInfo& oldInfo,
131  FrameworkInfo* newInfo);
132 
133 } // namespace framework {
134 
135 
136 namespace scheduler {
137 namespace call {
138 
139 // Validates that a scheduler::Call is well-formed.
140 // TODO(bmahler): Add unit tests.
142  const mesos::scheduler::Call& call,
144 
145 } // namespace call {
146 } // namespace scheduler {
147 
148 
149 namespace resource {
150 
151 // Functions in this namespace are only exposed for testing.
152 namespace internal {
153 
155  const google::protobuf::RepeatedPtrField<Resource>& resources);
156 
157 } // namespace internal {
158 
159 // Returns `true` if there is any overlap between set- or range-valued
160 // resources in the provided `Resources` objects.
162  const std::vector<Resources>& resources);
163 
164 // Validates resources specified by frameworks.
165 // NOTE: We cannot take 'Resources' here because invalid resources are
166 // silently ignored within its constructor.
168  const google::protobuf::RepeatedPtrField<Resource>& resources);
169 
170 } // namespace resource {
171 
172 
173 namespace executor {
174 
175 // Functions in this namespace are only exposed for testing.
176 namespace internal {
177 
178 Option<Error> validateExecutorID(const ExecutorInfo& executor);
179 
180 // Validates that fields are properly set depending on the type of the executor.
181 Option<Error> validateType(const ExecutorInfo& executor);
182 
183 // Validates resources of the executor.
184 Option<Error> validateResources(const ExecutorInfo& executor);
185 
186 } // namespace internal {
187 
188 Option<Error> validate(const ExecutorInfo& executor);
189 
190 } // namespace executor {
191 
192 
193 namespace task {
194 
195 // Validates a task that a framework attempts to launch within the
196 // offered resources. Returns an optional error which will cause the
197 // master to send a `TASK_ERROR` status update back to the framework.
198 //
199 // NOTE: This function must be called sequentially for each task, and
200 // each task needs to be launched before the next can be validated.
202  const TaskInfo& task,
203  Framework* framework,
204  Slave* slave,
205  const Resources& offered);
206 
207 
208 // Functions in this namespace are only exposed for testing.
209 namespace internal {
210 
211 // Validates resources of the task.
212 Option<Error> validateResources(const TaskInfo& task);
213 
214 // Validates resources of the task and its executor.
215 Option<Error> validateTaskAndExecutorResources(const TaskInfo& task);
216 
217 // Validates the kill policy of the task.
218 Option<Error> validateKillPolicy(const TaskInfo& task);
219 
220 // Validates `max_completion_time` of the task.
221 Option<Error> validateMaxCompletionTime(const TaskInfo& task);
222 
223 // Validates the check of the task.
224 Option<Error> validateCheck(const TaskInfo& task);
225 
226 // Validates the health check of the task.
227 Option<Error> validateHealthCheck(const TaskInfo& task);
228 
229 } // namespace internal {
230 
231 namespace group {
232 
233 // Validates a task group that a framework attempts to launch within the
234 // offered resources. Returns an optional error which will cause the
235 // master to send a `TASK_ERROR` status updates for *all* the tasks in
236 // the task group back to the framework.
237 //
238 // NOTE: Validation error of *any* task will cause all the tasks in the task
239 // group to be rejected by the master.
241  const TaskGroupInfo& taskGroup,
242  const ExecutorInfo& executor,
243  Framework* framework,
244  Slave* slave,
245  const Resources& offered);
246 
247 
248 // Functions in this namespace are only exposed for testing.
249 namespace internal {
250 
251 // Validates that the resources specified by
252 // the task group and its executor are valid.
253 //
254 // TODO(vinod): Consolidate this with `validateTaskAndExecutorResources()`.
256  const TaskGroupInfo& taskGroup,
257  const ExecutorInfo& executor);
258 
259 } // namespace internal {
260 
261 } // namespace group {
262 
263 } // namespace task {
264 
265 
266 namespace offer {
267 
268 // NOTE: These two functions are placed in the header file because we
269 // need to declare them as friends of Master.
270 Offer* getOffer(Master* master, const OfferID& offerId);
271 InverseOffer* getInverseOffer(Master* master, const OfferID& offerId);
272 Slave* getSlave(Master* master, const SlaveID& slaveId);
273 
274 
275 // Validates the given offers.
277  const google::protobuf::RepeatedPtrField<OfferID>& offerIds,
278  Master* master,
279  Framework* framework);
280 
281 
282 // Validates the given inverse offers.
284  const google::protobuf::RepeatedPtrField<OfferID>& offerIds,
285  Master* master,
286  Framework* framework);
287 
288 } // namespace offer {
289 
290 
291 namespace operation {
292 
293 // Validates the RESERVE operation.
295  const Offer::Operation::Reserve& reserve,
297  const protobuf::slave::Capabilities& agentCapabilities,
298  const Option<FrameworkInfo>& frameworkInfo = None());
299 
300 
301 // Validates the UNRESERVE operation.
303  const Offer::Operation::Unreserve& unreserve,
304  const Option<FrameworkInfo>& frameworkInfo = None());
305 
306 
307 // Validates the CREATE operation. We need slave's checkpointed resources so
308 // that we can validate persistence ID uniqueness, and we need the principal to
309 // verify that it's equal to the one in `DiskInfo.Persistence.principal`.
310 // We need the FrameworkInfo (unless the operation is requested by the
311 // operator) to ensure shared volumes are created by frameworks with the
312 // appropriate capability.
314  const Offer::Operation::Create& create,
315  const Resources& checkpointedResources,
317  const protobuf::slave::Capabilities& agentCapabilities,
318  const Option<FrameworkInfo>& frameworkInfo = None());
319 
320 
321 // Validates the DESTROY operation. We need slave's checkpointed
322 // resources to validate that the volumes to destroy actually exist.
323 // We also check that the volumes are not being used, or not assigned
324 // to any pending task.
326  const Offer::Operation::Destroy& destroy,
327  const Resources& checkpointedResources,
328  const hashmap<FrameworkID, Resources>& usedResources,
329  const hashmap<FrameworkID, hashmap<TaskID, TaskInfo>>& pendingTasks,
330  const Option<FrameworkInfo>& frameworkInfo = None());
331 
332 
334  const Offer::Operation::GrowVolume& growVolume,
335  const protobuf::slave::Capabilities& agentCapabilities);
336 
337 
339  const Offer::Operation::ShrinkVolume& shrinkVolume,
340  const protobuf::slave::Capabilities& agentCapabilities);
341 
342 
343 Option<Error> validate(const Offer::Operation::CreateDisk& createDisk);
344 
345 
346 Option<Error> validate(const Offer::Operation::DestroyDisk& destroyDisk);
347 
348 } // namespace operation {
349 
350 } // namespace validation {
351 } // namespace master {
352 } // namespace internal {
353 } // namespace mesos {
354 
355 #endif // __MASTER_VALIDATION_HPP__
Option< Error > validateExecutorID(const ExecutorInfo &executor)
Option< Error > validateFrameworkId(const mesos::FrameworkInfo &frameworkInfo)
Definition: master.hpp:27
Option< Error > validateMaxCompletionTime(const TaskInfo &task)
Definition: protobuf_utils.hpp:332
InverseOffer * getInverseOffer(Master *master, const OfferID &offerId)
mesos::v1::scheduler::Call Call
Definition: mesos.hpp:2782
Definition: resources.hpp:83
Slave * getSlave(Master *master, const SlaveID &slaveId)
Option< Error > validateResources(const TaskInfo &task)
Option< Error > reregisterSlave(const ReregisterSlaveMessage &message)
Definition: hashmap.hpp:38
Option< Error > validateUpdate(const FrameworkInfo &oldInfo, const FrameworkInfo &newInfo)
Option< Error > validateInverseOffers(const google::protobuf::RepeatedPtrField< OfferID > &offerIds, Master *master, Framework *framework)
Option< Error > validateKillPolicy(const TaskInfo &task)
Option< Error > validateRoles(const mesos::FrameworkInfo &frameworkInfo)
Definition: agent.hpp:25
Option< Error > validateType(const ExecutorInfo &executor)
process::Future< Nothing > destroy(const std::string &hierarchy, const std::string &cgroup="/")
Option< Error > validateTaskGroupAndExecutorResources(const TaskGroupInfo &taskGroup, const ExecutorInfo &executor)
Option< Error > validateCheck(const TaskInfo &task)
bool detectOverlappingSetAndRangeResources(const std::vector< Resources > &resources)
Definition: grp.hpp:26
Definition: none.hpp:27
Definition: attributes.hpp:24
Option< Error > validateHealthCheck(const TaskInfo &task)
Option< Error > validateOfferFilters(const FrameworkInfo &frameworkInfo)
Definition: master.hpp:122
Option< Error > validate(const mesos::master::Call &call)
Option< Error > validateTaskAndExecutorResources(const TaskInfo &task)
Offer * getOffer(Master *master, const OfferID &offerId)
Option< Error > validateSingleResourceProvider(const google::protobuf::RepeatedPtrField< Resource > &resources)
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
void preserveImmutableFields(const FrameworkInfo &oldInfo, FrameworkInfo *newInfo)
Definition: master.hpp:2401
Definition: master.hpp:361
Option< Error > registerSlave(const RegisterSlaveMessage &message)