/
mesos.hpp
4133 lines (3318 loc) · 114 KB
/
mesos.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TESTS_MESOS_HPP__
#define __TESTS_MESOS_HPP__
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <mesos/executor.hpp>
#include <mesos/scheduler.hpp>
#include <mesos/v1/executor.hpp>
#include <mesos/v1/resources.hpp>
#include <mesos/v1/resource_provider.hpp>
#include <mesos/v1/scheduler.hpp>
#include <mesos/v1/executor/executor.hpp>
#include <mesos/v1/scheduler/scheduler.hpp>
#include <mesos/authentication/secret_generator.hpp>
#include <mesos/authorizer/authorizer.hpp>
#include <mesos/fetcher/fetcher.hpp>
#include <mesos/master/detector.hpp>
#include <process/future.hpp>
#include <process/gmock.hpp>
#include <process/gtest.hpp>
#include <process/http.hpp>
#include <process/io.hpp>
#include <process/owned.hpp>
#include <process/pid.hpp>
#include <process/process.hpp>
#include <process/queue.hpp>
#include <process/subprocess.hpp>
#include <process/ssl/flags.hpp>
#include <process/ssl/gtest.hpp>
#include <stout/bytes.hpp>
#include <stout/foreach.hpp>
#include <stout/gtest.hpp>
#include <stout/lambda.hpp>
#include <stout/none.hpp>
#include <stout/option.hpp>
#include <stout/stringify.hpp>
#include <stout/try.hpp>
#include <stout/unreachable.hpp>
#include <stout/uuid.hpp>
#include "authentication/executor/jwt_secret_generator.hpp"
#include "common/http.hpp"
#include "common/protobuf_utils.hpp"
#include "messages/messages.hpp" // For google::protobuf::Message.
#include "master/master.hpp"
#include "sched/constants.hpp"
#include "resource_provider/detector.hpp"
#include "slave/constants.hpp"
#include "slave/csi_server.hpp"
#include "slave/slave.hpp"
#include "slave/containerizer/containerizer.hpp"
#include "slave/containerizer/mesos/containerizer.hpp"
#include "tests/cluster.hpp"
#include "tests/limiter.hpp"
#include "tests/utils.hpp"
#ifdef MESOS_HAS_JAVA
#include "tests/zookeeper.hpp"
#endif // MESOS_HAS_JAVA
using ::testing::_;
using ::testing::An;
using ::testing::DoDefault;
using ::testing::Invoke;
using ::testing::Return;
namespace mesos {
namespace internal {
namespace tests {
constexpr char READONLY_HTTP_AUTHENTICATION_REALM[] = "test-readonly-realm";
constexpr char READWRITE_HTTP_AUTHENTICATION_REALM[] = "test-readwrite-realm";
constexpr char DEFAULT_TEST_ROLE[] = "default-role";
constexpr char DEFAULT_JWT_SECRET_KEY[] =
"72kUKUFtghAjNbIOvLzfF2RxNBfeM64Bri8g9WhpyaunwqRB/yozHAqSnyHbddAV"
"PcWRQlrJAt871oWgSH+n52vMZ3aVI+AFMzXSo8+sUfMk83IGp0WJefhzeQsjDlGH"
"GYQgCAuGim0BE2X5U+lEue8s697uQpAO8L/FFRuDH2s";
// Forward declarations.
class MockExecutor;
struct SlaveOptions
{
SlaveOptions(
mesos::master::detector::MasterDetector* detector,
bool mock = false)
: detector(detector), mock(mock)
{}
SlaveOptions& withFlags(const Option<slave::Flags>& flags)
{
this->flags = flags;
return *this;
}
SlaveOptions& withId(const Option<std::string>& id)
{
this->id = id;
return *this;
}
SlaveOptions& withContainerizer(
const Option<slave::Containerizer*>& containerizer)
{
this->containerizer = containerizer;
return *this;
}
SlaveOptions& withGc(const Option<slave::GarbageCollector*>& gc)
{
this->gc = gc;
return *this;
}
SlaveOptions& withTaskStatusUpdateManager(
const Option<slave::TaskStatusUpdateManager*>& taskStatusUpdateManager)
{
this->taskStatusUpdateManager = taskStatusUpdateManager;
return *this;
}
SlaveOptions& withResourceEstimator(
const Option<mesos::slave::ResourceEstimator*>& resourceEstimator)
{
this->resourceEstimator = resourceEstimator;
return *this;
}
SlaveOptions& withQosController(
const Option<mesos::slave::QoSController*>& qosController)
{
this->qosController = qosController;
return *this;
}
SlaveOptions& withSecretGenerator(
const Option<mesos::SecretGenerator*>& secretGenerator)
{
this->secretGenerator = secretGenerator;
return *this;
}
SlaveOptions& withAuthorizer(const Option<Authorizer*>& authorizer)
{
this->authorizer = authorizer;
return *this;
}
SlaveOptions& withFutureTracker(
const Option<PendingFutureTracker*>& futureTracker)
{
this->futureTracker = futureTracker;
return *this;
}
SlaveOptions& withCsiServer(const process::Owned<slave::CSIServer>& csiServer)
{
this->csiServer = csiServer;
return *this;
}
mesos::master::detector::MasterDetector* detector;
bool mock;
Option<slave::Flags> flags;
Option<std::string> id;
Option<slave::Containerizer*> containerizer;
Option<slave::GarbageCollector*> gc;
Option<slave::TaskStatusUpdateManager*> taskStatusUpdateManager;
Option<mesos::slave::ResourceEstimator*> resourceEstimator;
Option<mesos::slave::QoSController*> qosController;
Option<mesos::SecretGenerator*> secretGenerator;
Option<Authorizer*> authorizer;
Option<PendingFutureTracker*> futureTracker;
Option<process::Owned<slave::CSIServer>> csiServer;
};
// NOTE: `SSLTemporaryDirectoryTest` exists even when SSL is not compiled into
// Mesos. In this case, the class is an alias of `TemporaryDirectoryTest`.
class MesosTest : public SSLTemporaryDirectoryTest
{
public:
static void SetUpTestCase();
static void TearDownTestCase();
protected:
MesosTest(const Option<zookeeper::URL>& url = None());
// Returns the flags used to create masters.
virtual master::Flags CreateMasterFlags();
// Returns the flags used to create slaves.
virtual slave::Flags CreateSlaveFlags();
// Starts a master with the specified flags.
virtual Try<process::Owned<cluster::Master>> StartMaster(
const Option<master::Flags>& flags = None());
// Starts a master with the specified allocator process and flags.
virtual Try<process::Owned<cluster::Master>> StartMaster(
mesos::allocator::Allocator* allocator,
const Option<master::Flags>& flags = None());
// Starts a master with the specified authorizer and flags.
virtual Try<process::Owned<cluster::Master>> StartMaster(
Authorizer* authorizer,
const Option<master::Flags>& flags = None());
// Starts a master with a slave removal rate limiter and flags.
// NOTE: The `slaveRemovalLimiter` is a `shared_ptr` because the
// underlying `Master` process requires the pointer in this form.
virtual Try<process::Owned<cluster::Master>> StartMaster(
const std::shared_ptr<MockRateLimiter>& slaveRemovalLimiter,
const Option<master::Flags>& flags = None());
// Starts a slave with the specified options.
// NOTE: This is a preferred method to start a slave.
// The other overloads of `StartSlave` are DEPRECATED!
virtual Try<process::Owned<cluster::Slave>> StartSlave(
const SlaveOptions& options);
// Starts a slave with the specified detector and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, containerizer, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, id, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
const std::string& id,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, containerizer, id, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
const std::string& id,
const Option<slave::Flags>& flags = None());
// Starts a slave with the specified detector, GC, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::GarbageCollector* gc,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, resource estimator, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
mesos::slave::ResourceEstimator* resourceEstimator,
const Option<slave::Flags>& flags = None());
// Starts a slave with the specified detector, containerizer,
// resource estimator, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
mesos::slave::ResourceEstimator* resourceEstimator,
const Option<slave::Flags>& flags = None());
// Starts a slave with the specified detector, QoS Controller, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
mesos::slave::QoSController* qosController,
const Option<slave::Flags>& flags = None());
// Starts a slave with the specified detector, containerizer,
// QoS Controller, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
mesos::slave::QoSController* qosController,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, authorizer, and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
mesos::Authorizer* authorizer,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, containerizer, authorizer,
// and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
mesos::Authorizer* authorizer,
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, containerizer,
// secretGenerator, authorizer and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
slave::Containerizer* containerizer,
mesos::SecretGenerator* secretGenerator,
const Option<mesos::Authorizer*>& authorizer = None(),
const Option<slave::Flags>& flags = None(),
bool mock = false);
// Starts a slave with the specified detector, secretGenerator,
// and flags.
virtual Try<process::Owned<cluster::Slave>> StartSlave(
mesos::master::detector::MasterDetector* detector,
mesos::SecretGenerator* secretGenerator,
const Option<slave::Flags>& flags = None());
Option<zookeeper::URL> zookeeperUrl;
// NOTE: On Windows, most tasks are run under PowerShell, which uses ~150 MB
// of memory per-instance due to loading .NET. Realistically, PowerShell can
// be called more than once in a task, so 512 MB is the safe minimum.
// Furthermore, because the Windows `cpu` isolator is a hard-cap, 0.1 CPUs
// will cause the task (or even a check command) to timeout, so 1 CPU is the
// safe minimum.
//
// Because multiple tasks can be run, the default agent resources needs to be
// at least a multiple of the default task resources: four times seems safe.
//
// On platforms where the shell is, e.g. Bash, the minimum is much lower.
const std::string defaultAgentResourcesString{
#ifdef __WINDOWS__
"cpus:4;gpus:0;mem:2048;disk:1024;ports:[31000-32000]"
#else
"cpus:2;gpus:0;mem:1024;disk:1024;ports:[31000-32000]"
#endif // __WINDOWS__
};
const std::string defaultTaskResourcesString{
#ifdef __WINDOWS__
"cpus:1;mem:512;disk:32"
#else
"cpus:0.1;mem:32;disk:32"
#endif // __WINDOWS__
};
};
template <typename T>
class ContainerizerTest : public MesosTest {};
#ifdef __linux__
// Cgroups hierarchy used by the cgroups related tests.
const static std::string TEST_CGROUPS_HIERARCHY = "/tmp/mesos_test_cgroup";
// Name of the root cgroup used by the cgroups related tests.
const static std::string TEST_CGROUPS_ROOT = "mesos_test";
template <>
class ContainerizerTest<slave::MesosContainerizer> : public MesosTest
{
public:
static void SetUpTestCase();
static void TearDownTestCase();
protected:
slave::Flags CreateSlaveFlags() override;
void SetUp() override;
void TearDown() override;
void SetUpCgroups();
void SetUpCgroupsV2();
void TearDownCgroups();
void TearDownCgroupsV2();
// Returns true if cgroups v2 is being used. Returns false if there was
// an error while determining if cgroups v2 is configured correctly or the
// `--enable-cgroups-v2` flag was not provided.
static bool cgroupsV2();
private:
// Base hierarchy for separately mounted cgroup controllers, e.g., if the
// base hierarchy is /sys/fs/cgroup then each controller will be mounted to
// /sys/fs/cgroup/{controller}/.
std::string baseHierarchy;
// Set of cgroup subsystems used by the cgroups related tests.
hashset<std::string> subsystems;
};
#else
template <>
class ContainerizerTest<slave::MesosContainerizer> : public MesosTest
{
protected:
virtual slave::Flags CreateSlaveFlags();
};
#endif // __linux__
#ifdef MESOS_HAS_JAVA
class MesosZooKeeperTest : public MesosTest
{
public:
static void SetUpTestCase()
{
// Make sure the JVM is created.
ZooKeeperTest::SetUpTestCase();
// Launch the ZooKeeper test server.
server = new ZooKeeperTestServer();
server->startNetwork();
Try<zookeeper::URL> parse = zookeeper::URL::parse(
"zk://" + server->connectString() + "/znode");
ASSERT_SOME(parse);
url = parse.get();
}
static void TearDownTestCase()
{
delete server;
server = nullptr;
}
void SetUp() override
{
MesosTest::SetUp();
server->startNetwork();
}
void TearDown() override
{
server->shutdownNetwork();
MesosTest::TearDown();
}
protected:
MesosZooKeeperTest() : MesosTest(url) {}
master::Flags CreateMasterFlags() override
{
master::Flags flags = MesosTest::CreateMasterFlags();
// NOTE: Since we are using the replicated log with ZooKeeper
// (default storage in MesosTest), we need to specify the quorum.
flags.quorum = 1;
return flags;
}
static ZooKeeperTestServer* server;
static Option<zookeeper::URL> url;
};
#endif // MESOS_HAS_JAVA
namespace v1 {
// Alias existing `mesos::v1` namespaces so that we can easily write
// `v1::` in tests.
//
// TODO(jmlvanre): Remove these aliases once we clean up the `tests`
// namespace hierarchy.
namespace agent = mesos::v1::agent;
namespace maintenance = mesos::v1::maintenance;
namespace master = mesos::v1::master;
namespace quota = mesos::v1::quota;
using mesos::v1::OPERATION_PENDING;
using mesos::v1::OPERATION_FINISHED;
using mesos::v1::OPERATION_FAILED;
using mesos::v1::OPERATION_ERROR;
using mesos::v1::OPERATION_DROPPED;
using mesos::v1::OPERATION_UNREACHABLE;
using mesos::v1::OPERATION_GONE_BY_OPERATOR;
using mesos::v1::OPERATION_RECOVERING;
using mesos::v1::OPERATION_UNKNOWN;
using mesos::v1::TASK_STAGING;
using mesos::v1::TASK_STARTING;
using mesos::v1::TASK_RUNNING;
using mesos::v1::TASK_KILLING;
using mesos::v1::TASK_FINISHED;
using mesos::v1::TASK_FAILED;
using mesos::v1::TASK_KILLED;
using mesos::v1::TASK_ERROR;
using mesos::v1::TASK_LOST;
using mesos::v1::TASK_DROPPED;
using mesos::v1::TASK_UNREACHABLE;
using mesos::v1::TASK_GONE;
using mesos::v1::TASK_GONE_BY_OPERATOR;
using mesos::v1::TASK_UNKNOWN;
using mesos::v1::AgentID;
using mesos::v1::CheckInfo;
using mesos::v1::CommandInfo;
using mesos::v1::ContainerID;
using mesos::v1::ContainerStatus;
using mesos::v1::Environment;
using mesos::v1::ExecutorID;
using mesos::v1::ExecutorInfo;
using mesos::v1::Filters;
using mesos::v1::FrameworkID;
using mesos::v1::FrameworkInfo;
using mesos::v1::HealthCheck;
using mesos::v1::InverseOffer;
using mesos::v1::MachineID;
using mesos::v1::Metric;
using mesos::v1::Offer;
using mesos::v1::OperationID;
using mesos::v1::OperationState;
using mesos::v1::OperationStatus;
using mesos::v1::Resource;
using mesos::v1::ResourceProviderID;
using mesos::v1::ResourceProviderInfo;
using mesos::v1::Resources;
using mesos::v1::TaskID;
using mesos::v1::TaskInfo;
using mesos::v1::TaskGroupInfo;
using mesos::v1::TaskState;
using mesos::v1::TaskStatus;
using mesos::v1::UUID;
using mesos::v1::WeightInfo;
} // namespace v1 {
namespace common {
template <typename TCredential>
struct DefaultCredential
{
static TCredential create()
{
TCredential credential;
credential.set_principal("test-principal");
credential.set_secret("test-secret");
return credential;
}
};
// TODO(jmlvanre): consider factoring this out.
template <typename TCredential>
struct DefaultCredential2
{
static TCredential create()
{
TCredential credential;
credential.set_principal("test-principal-2");
credential.set_secret("test-secret-2");
return credential;
}
};
template <typename TFrameworkInfo, typename TCredential>
struct DefaultFrameworkInfo
{
static TFrameworkInfo create()
{
TFrameworkInfo framework;
framework.set_name("default");
framework.set_user(os::user().get());
framework.set_principal(
DefaultCredential<TCredential>::create().principal());
framework.add_roles("*");
framework.add_capabilities()->set_type(
TFrameworkInfo::Capability::MULTI_ROLE);
framework.add_capabilities()->set_type(
TFrameworkInfo::Capability::RESERVATION_REFINEMENT);
return framework;
}
};
} // namespace common {
// TODO(jmlvanre): Remove `inline` once we have adjusted all tests to
// distinguish between `internal` and `v1`.
inline namespace internal {
using DefaultCredential = common::DefaultCredential<Credential>;
using DefaultCredential2 = common::DefaultCredential2<Credential>;
using DefaultFrameworkInfo =
common::DefaultFrameworkInfo<FrameworkInfo, Credential>;
} // namespace internal {
namespace v1 {
using DefaultCredential = common::DefaultCredential<mesos::v1::Credential>;
using DefaultCredential2 = common::DefaultCredential2<mesos::v1::Credential>;
using DefaultFrameworkInfo =
common::DefaultFrameworkInfo<mesos::v1::FrameworkInfo, mesos::v1::Credential>;
} // namespace v1 {
// We factor out all common behavior and templatize it so that we can
// can call it from both `v1::` and `internal::`.
namespace common {
template <typename TCommandInfo>
inline TCommandInfo createCommandInfo(
const Option<std::string>& value = None(),
const std::vector<std::string>& arguments = {})
{
TCommandInfo commandInfo;
if (value.isSome()) {
commandInfo.set_value(value.get());
}
if (!arguments.empty()) {
commandInfo.set_shell(false);
foreach (const std::string& arg, arguments) {
commandInfo.add_arguments(arg);
}
}
return commandInfo;
}
template <typename TExecutorInfo,
typename TExecutorID,
typename TResources,
typename TCommandInfo,
typename TFrameworkID>
inline TExecutorInfo createExecutorInfo(
const TExecutorID& executorId,
const Option<TCommandInfo>& command,
const Option<TResources>& resources,
const Option<typename TExecutorInfo::Type>& type,
const Option<TFrameworkID>& frameworkId)
{
TExecutorInfo executor;
executor.mutable_executor_id()->CopyFrom(executorId);
if (command.isSome()) {
executor.mutable_command()->CopyFrom(command.get());
}
if (resources.isSome()) {
executor.mutable_resources()->CopyFrom(resources.get());
}
if (type.isSome()) {
executor.set_type(type.get());
}
if (frameworkId.isSome()) {
executor.mutable_framework_id()->CopyFrom(frameworkId.get());
}
return executor;
}
template <typename TExecutorInfo,
typename TExecutorID,
typename TResources,
typename TCommandInfo,
typename TFrameworkID>
inline TExecutorInfo createExecutorInfo(
const std::string& _executorId,
const Option<TCommandInfo>& command,
const Option<TResources>& resources,
const Option<typename TExecutorInfo::Type>& type,
const Option<TFrameworkID>& frameworkId)
{
TExecutorID executorId;
executorId.set_value(_executorId);
return createExecutorInfo<TExecutorInfo,
TExecutorID,
TResources,
TCommandInfo,
TFrameworkID>(
executorId, command, resources, type, frameworkId);
}
template <typename TExecutorInfo,
typename TExecutorID,
typename TResources,
typename TCommandInfo,
typename TFrameworkID>
inline TExecutorInfo createExecutorInfo(
const std::string& executorId,
const Option<TCommandInfo>& command = None(),
const Option<std::string>& resources = None(),
const Option<typename TExecutorInfo::Type>& type = None(),
const Option<TFrameworkID>& frameworkId = None())
{
if (resources.isSome()) {
return createExecutorInfo<TExecutorInfo,
TExecutorID,
TResources,
TCommandInfo,
TFrameworkID>(
executorId,
command,
TResources::parse(resources.get()).get(),
type,
frameworkId);
}
return createExecutorInfo<TExecutorInfo,
TExecutorID,
TResources,
TCommandInfo,
TFrameworkID>(
executorId, command, Option<TResources>::none(), type, frameworkId);
}
template <typename TExecutorInfo,
typename TExecutorID,
typename TResources,
typename TCommandInfo,
typename TFrameworkID>
inline TExecutorInfo createExecutorInfo(
const TExecutorID& executorId,
const Option<TCommandInfo>& command,
const std::string& resources,
const Option<typename TExecutorInfo::Type>& type = None(),
const Option<TFrameworkID>& frameworkId = None())
{
return createExecutorInfo<TExecutorInfo,
TExecutorID,
TResources,
TCommandInfo,
TFrameworkID>(
executorId,
command,
TResources::parse(resources).get(),
type,
frameworkId);
}
template <typename TExecutorInfo,
typename TExecutorID,
typename TResources,
typename TCommandInfo,
typename TFrameworkID>
inline TExecutorInfo createExecutorInfo(
const std::string& executorId,
const std::string& command,
const Option<std::string>& resources = None(),
const Option<typename TExecutorInfo::Type>& type = None(),
const Option<TFrameworkID>& frameworkId = None())
{
TCommandInfo commandInfo = createCommandInfo<TCommandInfo>(command);
return createExecutorInfo<TExecutorInfo,
TExecutorID,
TResources,
TCommandInfo,
TFrameworkID>(
executorId, commandInfo, resources, type, frameworkId);
}
template <typename TImage>
inline TImage createDockerImage(const std::string& imageName)
{
TImage image;
image.set_type(TImage::DOCKER);
image.mutable_docker()->set_name(imageName);
return image;
}
template <typename TVolume>
inline TVolume createVolumeSandboxPath(
const std::string& containerPath,
const std::string& sandboxPath,
const typename TVolume::Mode& mode)
{
TVolume volume;
volume.set_container_path(containerPath);
volume.set_mode(mode);
// TODO(jieyu): Use TVolume::Source::SANDBOX_PATH.
volume.set_host_path(sandboxPath);
return volume;
}
template <typename TVolume, typename TMountPropagation>
inline TVolume createVolumeHostPath(
const std::string& containerPath,
const std::string& hostPath,
const typename TVolume::Mode& mode,
const Option<typename TMountPropagation::Mode>& mountPropagationMode =
None())
{
TVolume volume;
volume.set_container_path(containerPath);
volume.set_mode(mode);
typename TVolume::Source* source = volume.mutable_source();
source->set_type(TVolume::Source::HOST_PATH);
source->mutable_host_path()->set_path(hostPath);
if (mountPropagationMode.isSome()) {
source
->mutable_host_path()
->mutable_mount_propagation()
->set_mode(mountPropagationMode.get());
}
return volume;
}
template <typename TVolume, typename TImage>
inline TVolume createVolumeFromDockerImage(
const std::string& containerPath,
const std::string& imageName,
const typename TVolume::Mode& mode)
{
TVolume volume;
volume.set_container_path(containerPath);
volume.set_mode(mode);
volume.mutable_image()->CopyFrom(createDockerImage<TImage>(imageName));
return volume;
}
template <typename TVolume>
inline TVolume createVolumeCsi(
const std::string& pluginName,
const std::string& volumeId,
const std::string& containerPath,
const typename TVolume::Mode& mode,
const typename TVolume::Source::CSIVolume::VolumeCapability
::AccessMode::Mode& accessMode)
{
TVolume volume;
volume.set_container_path(containerPath);
volume.set_mode(mode);
typename TVolume::Source* source = volume.mutable_source();
source->set_type(TVolume::Source::CSI_VOLUME);
source->mutable_csi_volume()->set_plugin_name(pluginName);
typename TVolume::Source::CSIVolume::StaticProvisioning* staticInfo =
source->mutable_csi_volume()->mutable_static_provisioning();
staticInfo->set_volume_id(volumeId);
staticInfo->mutable_volume_capability()->mutable_mount();
staticInfo->mutable_volume_capability()
->mutable_access_mode()->set_mode(accessMode);
return volume;
}
template <typename TNetworkInfo>
inline TNetworkInfo createNetworkInfo(
const std::string& networkName)
{
TNetworkInfo info;
info.set_name(networkName);
return info;
}
template <typename TContainerInfo, typename TVolume, typename TImage>
inline TContainerInfo createContainerInfo(
const Option<std::string>& imageName = None(),
const std::vector<TVolume>& volumes = {})
{
TContainerInfo info;
info.set_type(TContainerInfo::MESOS);
if (imageName.isSome()) {
TImage* image = info.mutable_mesos()->mutable_image();
image->CopyFrom(createDockerImage<TImage>(imageName.get()));
}
foreach (const TVolume& volume, volumes) {
info.add_volumes()->CopyFrom(volume);
}
return info;
}
inline SlaveID getAgentID(const Offer& offer)
{
return offer.slave_id();
}
inline mesos::v1::AgentID getAgentID(const mesos::v1::Offer& offer)
{
return offer.agent_id();
}
inline void setAgentID(TaskInfo* task, const SlaveID& slaveId)
{
task->mutable_slave_id()->CopyFrom(slaveId);
}
inline void setAgentID(
mesos::v1::TaskInfo* task,
const mesos::v1::AgentID& agentId)
{
task->mutable_agent_id()->CopyFrom(agentId);
}
// TODO(bmahler): Refactor this to make the distinction between
// command tasks and executor tasks clearer.
template <
typename TTaskInfo,
typename TExecutorID,
typename TSlaveID,
typename TResources,
typename TExecutorInfo,
typename TCommandInfo,
typename TOffer,
typename TScalar>
inline TTaskInfo createTask(
const TSlaveID& slaveId,
const TResources& resourceRequests,
const TCommandInfo& command,
const Option<TExecutorID>& executorId = None(),
const std::string& name = "test-task",
const std::string& id = id::UUID::random().toString(),
const google::protobuf::Map<std::string, TScalar>& resourceLimits = {})
{
TTaskInfo task;
task.set_name(name);
task.mutable_task_id()->set_value(id);
setAgentID(&task, slaveId);
task.mutable_resources()->CopyFrom(resourceRequests);
if (!resourceLimits.empty()) {
*task.mutable_limits() = resourceLimits;
}
if (executorId.isSome()) {
TExecutorInfo executor;
executor.mutable_executor_id()->CopyFrom(executorId.get());
executor.mutable_command()->CopyFrom(command);
task.mutable_executor()->CopyFrom(executor);
} else {
task.mutable_command()->CopyFrom(command);
}
return task;
}
template <
typename TTaskInfo,
typename TExecutorID,
typename TSlaveID,
typename TResources,
typename TExecutorInfo,
typename TCommandInfo,
typename TOffer,
typename TScalar>
inline TTaskInfo createTask(