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