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/gc.hpp"
42 
44 
46 
47 namespace mesos {
48 namespace internal {
49 namespace slave {
50 
51 // Forward declaration.
52 class Slave;
53 class Flags;
54 
55 namespace state {
56 // Forward declaration.
57 struct SlaveState;
58 } // namespace state {
59 
60 
61 // An abstraction of a Containerizer that will contain an executor and
62 // its tasks.
64 {
65 public:
66  enum class LaunchResult {
67  SUCCESS,
68  ALREADY_LAUNCHED,
69  NOT_SUPPORTED,
70  };
71 
72  // Attempts to create a containerizer as specified by 'isolation' in
73  // flags.
75  const Flags& flags,
76  bool local,
77  Fetcher* fetcher,
78  GarbageCollector* gc,
79  SecretResolver* secretResolver = nullptr,
80  VolumeGidManager* volumeGidManager = nullptr,
81  PendingFutureTracker* futureTracker = nullptr);
82 
83  // Determine slave resources from flags, probing the system or
84  // querying a delegate.
85  // TODO(idownes): Consider making this non-static and moving to
86  // containerizer implementations to enable a containerizer to best
87  // determine the resources, particularly if containerizeration is
88  // delegated.
89  static Try<Resources> resources(const Flags& flags);
90 
91  virtual ~Containerizer() {}
92 
93  // Recover all containerized executors specified in state. Any
94  // containerized executors present on the system but not included in
95  // state (or state is None) will be terminated and cleaned up.
97  const Option<state::SlaveState>& state) = 0;
98 
99  // Launch a container with the specified ContainerConfig.
100  //
101  // If the ContainerID has a parent, this will attempt to launch
102  // a nested container.
103  // NOTE: For nested containers, the required `directory` field of
104  // the ContainerConfig will be determined by the containerizer.
106  const ContainerID& containerId,
107  const mesos::slave::ContainerConfig& containerConfig,
108  const std::map<std::string, std::string>& environment,
109  const Option<std::string>& pidCheckpointPath) = 0;
110 
111  // Create an HTTP connection that can be used to "attach" (i.e.,
112  // stream input to or stream output from) a container.
114  const ContainerID& containerId)
115  {
116  return process::Failure("Unsupported");
117  }
118 
119  // Update the resources for a container.
121  const ContainerID& containerId,
122  const Resources& resources) = 0;
123 
124  // Get resource usage statistics on the container.
126  const ContainerID& containerId) = 0;
127 
128  // Retrieve the run-time state of various isolator properties
129  // associated with the container. Unlike other methods in this class
130  // we are not making this pure virtual, since a `Containerizer`
131  // doesn't necessarily need to return the status of a container.
133  const ContainerID &containerId)
134  {
135  return ContainerStatus();
136  }
137 
138  // Wait on the 'ContainerTermination'. If the executor terminates,
139  // the containerizer should also destroy the containerized context.
140  // The future may be failed if an error occurs during termination of
141  // the executor or destruction of the container.
142  //
143  // Returns `None` if the container cannot be found.
144  // NOTE: For terminated nested containers, whose parent container is
145  // still running, the checkpointed `ContainerTermination` must be returned.
147  const ContainerID& containerId) = 0;
148 
149  // Destroy a starting or running container, killing all processes and
150  // releasing all resources. Returns the same result as `wait()` method,
151  // therefore calling `wait()` right before `destroy()` is not required.
153  const ContainerID& containerId) = 0;
154 
155  // Sends a signal to a running container. Returns false when the container
156  // cannot be found. The future may be failed if an error occurs in sending
157  // the signal to the running container.
159  const ContainerID& containerId,
160  int signal)
161  {
162  return process::Failure("Unsupported");
163  };
164 
165  virtual process::Future<hashset<ContainerID>> containers() = 0;
166 
167  // Remove a nested container, including its sandbox and runtime directories.
168  //
169  // NOTE: You can only remove a a nested container that has been fully
170  // destroyed and whose parent has not been destroyed yet. If the parent has
171  // already been destroyed, then the sandbox and runtime directories will be
172  // eventually garbage collected. The caller is responsible for ensuring that
173  // `containerId` belongs to a nested container.
174  virtual process::Future<Nothing> remove(const ContainerID& containerId)
175  {
176  return process::Failure("Unsupported");
177  }
178 
179  // Prune unused images from supported image stores.
180  virtual process::Future<Nothing> pruneImages(
181  const std::vector<Image>& excludedImages) = 0;
182 };
183 
184 } // namespace slave {
185 } // namespace internal {
186 } // namespace mesos {
187 
188 #endif // __CONTAINERIZER_HPP__
Definition: option.hpp:29
LaunchResult
Definition: containerizer.hpp:66
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:132
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:63
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:113
process::Future< Nothing > destroy(const std::string &hierarchy, const std::string &cgroup="/")
virtual ~Containerizer()
Definition: containerizer.hpp:91
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:158
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