Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
DobbyRdkPluginUtils.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2020 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19/*
20 * File: DobbyRdkPluginUtils.h
21 *
22 */
23#ifndef DOBBYRDKPLUGINUTILS_H
24#define DOBBYRDKPLUGINUTILS_H
25
26#include "rt_dobby_schema.h"
27#include "rt_state_schema.h"
28
29#if defined (DOBBY_BUILD)
30 #include <IDobbyStartState.h>
31#else
32 #include <Dobby/rdkPlugins/IDobbyStartState.h>
33#endif
34
35#include <sys/types.h>
36#include <string>
37#include <fstream>
38#include <functional>
39#include <memory>
40#include <list>
41#include <mutex>
42#include <arpa/inet.h>
43#include <vector>
44#include <map>
45
46
47// TODO:: This would be better stored in the dobby workspace dir rather than /tmp,
48// but we don't programatically know the workspace dir in this code.
49#define ADDRESS_FILE_DIR "/tmp/dobby/plugin/networking/"
50
51#define MOUNT_TUNNEL_CONTAINER_PATH "/mnt/.containermnttunnel"
52#define MOUNT_TUNNEL_HOST_PATH "/tmp/.hostmnttunnel-"
53
55{
56 std::string vethName;
57 std::string ipAddress;
58 in_addr_t ipAddressRaw;
59 std::string containerId;
60
61 bool operator==(const ContainerNetworkInfo &rhs) const
62 {
63 if (containerId.empty() || rhs.containerId.empty())
64 {
65 return ipAddressRaw == rhs.ipAddressRaw;
66 }
67 return containerId == rhs.containerId;
68 }
70
71// -----------------------------------------------------------------------------
79{
80public:
81 DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
82 const std::string &containerId);
83 DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
84 const std::shared_ptr<IDobbyStartState> &startState,
85 const std::string &containerId);
86 DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
87 const std::shared_ptr<const rt_state_schema> &state,
88 const std::string &containerId);
89 DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
90 const std::shared_ptr<const rt_state_schema> &state,
91 const std::shared_ptr<IDobbyStartState> &startState,
92 const std::string &containerId);
94
95 // -------------------------------------------------------------------------
120 template< class Function, class... Args >
121 inline bool callInNamespace(pid_t pid, int nsType, Function&& f, Args&&... args) const
122 {
123 return this->callInNamespaceImpl(pid, nsType, std::bind(std::forward<Function>(f),
124 std::forward<Args>(args)...));
125 }
126
127 bool callInNamespaceImpl(pid_t pid, int nsType,
128 const std::function<bool()>& func) const;
129
130 void nsThread(int newNsFd, int nsType, bool* success,
131 std::function<bool()>& func) const;
132
133
134 pid_t getContainerPid() const;
135 std::string getContainerId() const;
137 bool getTakenVeths(std::vector<std::string> &takenVeths);
138
139 bool writeTextFile(const std::string &path,
140 const std::string &str,
141 int flags,
142 mode_t mode) const;
143
144 std::string readTextFile(const std::string &path) const;
145
146 bool addMount(const std::string &source,
147 const std::string &target,
148 const std::string &fsType,
149 const std::list<std::string> &mountOptions) const;
150
151 static bool mkdirRecursive(const std::string& path, mode_t mode);
152
153 bool addEnvironmentVar(const std::string& envVar) const;
154
155 int addFileDescriptor(const std::string& pluginName, int fd);
156
157 std::list<int> files() const;
158
159 std::list<int> files(const std::string& pluginName) const;
160
161 bool addAnnotation(const std::string &key, const std::string &value);
162 bool removeAnnotation(const std::string &key);
163 std::map<std::string, std::string> getAnnotations() const { return mAnnotations; };
164
165 int exitStatus;
166
167private:
168 std::string ipAddressToString(const in_addr_t &ipAddress);
169
170private:
171 mutable std::mutex mLock;
172
173 std::shared_ptr<rt_dobby_schema> mConf;
174 std::shared_ptr<const rt_state_schema> mState;
175 std::shared_ptr<IDobbyStartState> mStartState;
176
177 const std::string mContainerId;
178
179 std::map<std::string, std::string> mAnnotations;
180};
181
182#endif // !defined(DOBBYRDKPLUGINUTILS_H)
Class for useful utility methods for plugins such as adding mounts and environment variables.
Definition DobbyRdkPluginUtils.h:79
bool callInNamespaceImpl(pid_t pid, int nsType, const std::function< bool()> &func) const
Utility function to run some code in a specific namespace of the container.
Definition DobbyRdkPluginUtils.cpp:305
std::string readTextFile(const std::string &path) const
Simply reads a file into a string.
Definition DobbyRdkPluginUtils.cpp:446
pid_t getContainerPid() const
Gets the container pid from the stdin string of a hook.
Definition DobbyRdkPluginUtils.cpp:107
bool getContainerNetworkInfo(ContainerNetworkInfo &networkInfo)
Gets network info about the container (veth/IP)
Definition DobbyRdkPluginUtils.cpp:146
static bool mkdirRecursive(const std::string &path, mode_t mode)
Makes a directory and all parent directories as needed.
Definition DobbyRdkPluginUtils.cpp:530
void nsThread(int newNsFd, int nsType, bool *success, std::function< bool()> &func) const
Thread helper function that implements the setns syscall.
Definition DobbyRdkPluginUtils.cpp:254
bool getTakenVeths(std::vector< std::string > &takenVeths)
Gets allocated veth devices.
Definition DobbyRdkPluginUtils.cpp:191
std::list< int > files() const
Gets all file descriptor registered by any client.
Definition DobbyRdkPluginUtils.cpp:683
bool removeAnnotation(const std::string &key)
removes a key value pair from the annotations
Definition DobbyRdkPluginUtils.cpp:770
int addFileDescriptor(const std::string &pluginName, int fd)
Adds another file descriptor to be passed into the container.
Definition DobbyRdkPluginUtils.cpp:659
std::string getContainerId() const
Gets the container ID.
Definition DobbyRdkPluginUtils.cpp:130
bool writeTextFile(const std::string &path, const std::string &str, int flags, mode_t mode) const
Simply writes a string into a file.
Definition DobbyRdkPluginUtils.cpp:395
bool addMount(const std::string &source, const std::string &target, const std::string &fsType, const std::list< std::string > &mountOptions) const
Public api to allow for adding additional mounts to a container's config file.
Definition DobbyRdkPluginUtils.cpp:480
bool callInNamespace(pid_t pid, int nsType, Function &&f, Args &&... args) const
Slightly nicer version of callInNamespace, handles the function bind for you automatically.
Definition DobbyRdkPluginUtils.h:121
bool addEnvironmentVar(const std::string &envVar) const
Public api to allow for adding additional environment variables.
Definition DobbyRdkPluginUtils.cpp:586
bool addAnnotation(const std::string &key, const std::string &value)
adds a key value pair to the annotations
Definition DobbyRdkPluginUtils.cpp:749
Definition DobbyRdkPluginUtils.h:55