Apache Mesos
spec.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 __ISOLATOR_CNI_SPEC_HPP__
18 #define __ISOLATOR_CNI_SPEC_HPP__
19 
20 #include <stout/try.hpp>
21 #include <stout/json.hpp>
22 
23 // ONLY USEFUL AFTER RUNNING PROTOC.
24 #include "slave/containerizer/mesos/isolators/network/cni/spec.pb.h"
25 
26 namespace mesos {
27 namespace internal {
28 namespace slave {
29 namespace cni {
30 namespace spec {
31 
32 constexpr char CNI_VERSION[] = "0.3.0";
33 
34 
35 // CNI commands.
36 constexpr char CNI_CMD_ADD[] = "ADD";
37 constexpr char CNI_CMD_DEL[] = "DEL";
38 
39 
40 // Well-known CNI error codes:
41 // https://github.com/containernetworking/cni/blob/master/SPEC.md#well-known-error-codes
42 constexpr uint32_t CNI_ERROR_INCOMPATIBLE_VERSION = 1;
43 constexpr uint32_t CNI_ERROR_UNSUPPORTED_FIELD = 2;
44 
45 
46 Try<NetworkConfig> parseNetworkConfig(const std::string& s);
47 Try<NetworkInfo> parseNetworkInfo(const std::string& s);
48 
49 
50 // Takes a DNS specification and returns a string containing
51 // the equivalent resolv.conf(5) format.
52 std::string formatResolverConfig(const DNS& dns);
53 
54 
55 // Creates a `spec::Error` object, with the `msg` and `code`
56 // specified. Returns a JSON string representation of the
57 // `spec::Error` object. See details in:
58 // https://github.com/containernetworking/cni/blob/master/SPEC.md#result
59 std::string error(const std::string& msg, uint32_t code);
60 
61 
62 // This class encapsulates a JSON formatted string of type
63 // `spec::Error`. It can be used by CNI plugins to return a CNI error
64 // in a `Try` object when a failure occurs.
65 class PluginError : public ::Error
66 {
67 public:
68  PluginError(const std::string& msg, uint32_t code)
69  : ::Error(error(msg, code)) {}
70 };
71 
72 
73 inline std::ostream& operator<<(std::ostream& stream, const PluginError& _error)
74 {
75  return stream << _error.message;
76 }
77 
78 } // namespace spec {
79 } // namespace cni {
80 } // namespace slave {
81 } // namespace internal {
82 } // namespace mesos {
83 
84 #endif // __ISOLATOR_CNI_SPEC_HPP__
Try< NetworkConfig > parseNetworkConfig(const std::string &s)
Definition: errorbase.hpp:35
Definition: try.hpp:34
constexpr uint32_t CNI_ERROR_UNSUPPORTED_FIELD
Definition: spec.hpp:43
Try< NetworkInfo > parseNetworkInfo(const std::string &s)
constexpr uint32_t CNI_ERROR_INCOMPATIBLE_VERSION
Definition: spec.hpp:42
constexpr char CNI_CMD_ADD[]
Definition: spec.hpp:36
constexpr char CNI_CMD_DEL[]
Definition: spec.hpp:37
std::string formatResolverConfig(const DNS &dns)
const std::string message
Definition: errorbase.hpp:45
std::string error(const std::string &msg, uint32_t code)
std::ostream & operator<<(std::ostream &stream, const PluginError &_error)
Definition: spec.hpp:73
PluginError(const std::string &msg, uint32_t code)
Definition: spec.hpp:68
constexpr char CNI_VERSION[]
Definition: spec.hpp:32