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