Apache Mesos
containerizer.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 __CONTAINERIZER_HPP__
18 #define __CONTAINERIZER_HPP__
19 
20 #include <map>
21 
22 #include <mesos/mesos.hpp>
23 #include <mesos/resources.hpp>
24 
26 
28 
29 #include <process/future.hpp>
30 #include <process/http.hpp>
31 #include <process/owned.hpp>
32 #include <process/process.hpp>
33 
34 #include <stout/duration.hpp>
35 #include <stout/hashset.hpp>
36 #include <stout/option.hpp>
37 #include <stout/try.hpp>
38 
40 
41 #include "slave/csi_server.hpp"
42 #include "slave/gc.hpp"
43 
45 
47 
48 namespace mesos {
49 namespace internal {
50 namespace slave {
51 
52 // Forward declaration.
53 class Slave;
54 class Flags;
55 
56 namespace state {
57 // Forward declaration.
58 struct SlaveState;
59 } // namespace state {
60 
61 
62 // An abstraction of a Containerizer that will contain an executor and
63 // its tasks.
65 {
66 public:
67  enum class LaunchResult {
68  SUCCESS,
69  ALREADY_LAUNCHED,
70  NOT_SUPPORTED,
71  };
72 
73  // Attempts to create a containerizer as specified by 'isolation' in
74  // flags.
76  const Flags& flags,
77  bool local,
78  Fetcher* fetcher,
79  GarbageCollector* gc,
80  SecretResolver* secretResolver = nullptr,
81  VolumeGidManager* volumeGidManager = nullptr,
82  PendingFutureTracker* futureTracker = nullptr,
83  CSIServer* csiServer = nullptr);
84 
85  // Determine slave resources from flags, probing the system or
86  // querying a delegate.
87  // TODO(idownes): Consider making this non-static and moving to
88  // containerizer implementations to enable a containerizer to best
89  // determine the resources, particularly if containerizeration is
90  // delegated.
91  static Try<Resources> resources(const Flags& flags);
92 
93  virtual ~Containerizer() {}
94 
95  // Recover all containerized executors specified in state. Any
96  // containerized executors present on the system but not included in
97  // state (or state is None) will be terminated and cleaned up.
99  const Option<state::SlaveState>& state) = 0;
100 
101  // Launch a container with the specified ContainerConfig.
102  //
103  // If the ContainerID has a parent, this will attempt to launch
104  // a nested container.
105  // NOTE: For nested containers, the required `directory` field of
106  // the ContainerConfig will be determined by the containerizer.
108  const ContainerID& containerId,
109  const mesos::slave::ContainerConfig& containerConfig,
110  const std::map<std::string, std::string>& environment,
111  const Option<std::string>& pidCheckpointPath) = 0;
112 
113  // Create an HTTP connection that can be used to "attach" (i.e.,
114  // stream input to or stream output from) a container.
116  const ContainerID& containerId)
117  {
118  return process::Failure("Unsupported");
119  }
120 
121  // Update the resources for a container.
123  const ContainerID& containerId,
124  const Resources& resourceRequests,
125  const google::protobuf::Map<
126  std::string, Value::Scalar>& resourceLimits = {}) = 0;
127 
128  // Get resource usage statistics on the container.
130  const ContainerID& containerId) = 0;
131 
132  // Retrieve the run-time state of various isolator properties
133  // associated with the container. Unlike other methods in this class
134  // we are not making this pure virtual, since a `Containerizer`
135  // doesn't necessarily need to return the status of a container.
137  const ContainerID &containerId)
138  {
139  return ContainerStatus();
140  }
141 
142  // Wait on the 'ContainerTermination'. If the executor terminates,
143  // the containerizer should also destroy the containerized context.
144  // The future may be failed if an error occurs during termination of
145  // the executor or destruction of the container.
146  //
147  // Returns `None` if the container cannot be found.
148  // NOTE: For terminated nested containers, whose parent container is
149  // still running, the checkpointed `ContainerTermination` must be returned.
151  const ContainerID& containerId) = 0;
152 
153  // Destroy a starting or running container, killing all processes and
154  // releasing all resources. Returns the same result as `wait()` method,
155  // therefore calling `wait()` right before `destroy()` is not required.
157  const ContainerID& containerId) = 0;
158 
159  // Sends a signal to a running container. Returns false when the container
160  // cannot be found. The future may be failed if an error occurs in sending
161  // the signal to the running container.
163  const ContainerID& containerId,
164  int signal)
165  {
166  return process::Failure("Unsupported");
167  };
168 
169  virtual process::Future<hashset<ContainerID>> containers() = 0;
170 
171  // Remove a nested container, including its sandbox and runtime directories.
172  //
173  // NOTE: You can only remove a a nested container that has been fully
174  // destroyed and whose parent has not been destroyed yet. If the parent has
175  // already been destroyed, then the sandbox and runtime directories will be
176  // eventually garbage collected. The caller is responsible for ensuring that
177  // `containerId` belongs to a nested container.
178  virtual process::Future<Nothing> remove(const ContainerID& containerId)
179  {
180  return process::Failure("Unsupported");
181  }
182 
183  // Prune unused images from supported image stores.
184  virtual process::Future<Nothing> pruneImages(
185  const std::vector<Image>& excludedImages) = 0;
186 };
187 
188 } // namespace slave {
189 } // namespace internal {
190 } // namespace mesos {
191 
192 #endif // __CONTAINERIZER_HPP__
Definition: option.hpp:29
LaunchResult
Definition: containerizer.hpp:67
Try< bool > update(const std::string &link, const Handle &parent, uint16_t protocol, const action::Mirror &mirror)
Definition: fetcher.hpp:49
Definition: check.hpp:33
Definition: future.hpp:668
constexpr int SUCCESS
Definition: decoder.hpp:49
virtual process::Future< ContainerStatus > status(const ContainerID &containerId)
Definition: containerizer.hpp:136
Definition: csi_server.hpp:49
Definition: resources.hpp:83
Definition: volume_gid_manager.hpp:42
Definition: flags.hpp:39
Environment * environment
Try< ResourceStatistics > usage(pid_t pid, bool mem=true, bool cpus=true)
Definition: containerizer.hpp:64
Definition: future_tracker.hpp:84
process::PID< master::Master > launch(const Flags &flags, mesos::allocator::Allocator *_allocator=nullptr)
Definition: agent.hpp:25
virtual process::Future< process::http::Connection > attach(const ContainerID &containerId)
Definition: containerizer.hpp:115
process::Future< Nothing > destroy(const std::string &hierarchy, const std::string &cgroup="/")
virtual ~Containerizer()
Definition: containerizer.hpp:93
Try< State > recover(const std::string &rootDir, bool strict)
bool wait(const UPID &pid, const Duration &duration=Seconds(-1))
Wait for the process to exit for no more than the specified seconds.
virtual process::Future< bool > kill(const ContainerID &containerId, int signal)
Definition: containerizer.hpp:162
Definition: attributes.hpp:24
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
Definition: resolver.hpp:34
Definition: parse.hpp:33