Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
DobbyRdkPluginUtils Class Reference

Class for useful utility methods for plugins such as adding mounts and environment variables. More...

#include <DobbyRdkPluginUtils.h>

Public Member Functions

 DobbyRdkPluginUtils (const std::shared_ptr< rt_dobby_schema > &cfg, const std::string &containerId)
 
 DobbyRdkPluginUtils (const std::shared_ptr< rt_dobby_schema > &cfg, const std::shared_ptr< IDobbyStartState > &startState, const std::string &containerId)
 
 DobbyRdkPluginUtils (const std::shared_ptr< rt_dobby_schema > &cfg, const std::shared_ptr< const rt_state_schema > &state, const std::string &containerId)
 
 DobbyRdkPluginUtils (const std::shared_ptr< rt_dobby_schema > &cfg, const std::shared_ptr< const rt_state_schema > &state, const std::shared_ptr< IDobbyStartState > &startState, const std::string &containerId)
 
template<class Function , class... Args>
bool callInNamespace (pid_t pid, int nsType, Function &&f, Args &&... args) const
 Slightly nicer version of callInNamespace, handles the function bind for you automatically. More...
 
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. More...
 
void nsThread (int newNsFd, int nsType, bool *success, std::function< bool()> &func) const
 Thread helper function that implements the setns syscall. More...
 
pid_t getContainerPid () const
 Gets the container pid from the stdin string of a hook. More...
 
std::string getContainerId () const
 Gets the container ID. More...
 
bool getContainerNetworkInfo (ContainerNetworkInfo &networkInfo)
 Gets network info about the container (veth/IP) More...
 
bool getTakenVeths (std::vector< std::string > &takenVeths)
 Gets allocated veth devices. More...
 
bool writeTextFile (const std::string &path, const std::string &str, int flags, mode_t mode) const
 Simply writes a string into a file. More...
 
std::string readTextFile (const std::string &path) const
 Simply reads a file into a string. More...
 
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. More...
 
bool addEnvironmentVar (const std::string &envVar) const
 Public api to allow for adding additional environment variables. More...
 
int addFileDescriptor (const std::string &pluginName, int fd)
 Adds another file descriptor to be passed into the container. More...
 
std::list< int > files () const
 Gets all file descriptor registered by any client. More...
 
std::list< int > files (const std::string &pluginName) const
 Gets all file descriptor registered by concrete client. More...
 

Static Public Member Functions

static bool mkdirRecursive (const std::string &path, mode_t mode)
 Makes a directory and all parent directories as needed. More...
 

Public Attributes

int exitStatus
 

Private Member Functions

std::string ipAddressToString (const in_addr_t &ipAddress)
 

Private Attributes

std::mutex mLock
 
std::shared_ptr< rt_dobby_schema > mConf
 
std::shared_ptr< const rt_state_schema > mState
 
std::shared_ptr< IDobbyStartStatemStartState
 
const std::string mContainerId
 

Detailed Description

Class for useful utility methods for plugins such as adding mounts and environment variables.

Member Function Documentation

◆ addEnvironmentVar()

bool DobbyRdkPluginUtils::addEnvironmentVar ( const std::string &  envVar) const

Public api to allow for adding additional environment variables.

This can only obviously be called before the config file is persisted to disk.

Parameters
[in]cfgPointer to OCI config struct
[in]envVarThe environment variable to set
Returns
true if the env var was added, otherwise false.

◆ addFileDescriptor()

int DobbyRdkPluginUtils::addFileDescriptor ( const std::string &  pluginName,
int  fd 
)

Adds another file descriptor to be passed into the container.

The number of the file descriptor in the container namespace is returned, unless there was an error in which case a negative value is returned. File descriptors start at 3.

The method dups the supplied file descriptor so it can be closed immmediatly after the call. The file descriptor will be closed after the container is started and handed over.

File descriptors are recorded per client (plugin name).

Lastly to help find issues, this function will log an error and reject the file descriptor if it doesn't have the FD_CLOEXEC bit set.

Please mind that this call should be used only in preCreation hook. That's due to the fact preserve fds list should be initialized before container starts.

Parameters
[in]pluginNameThe plugin name for which fd will be recorded
[in]fdThe file descriptor to pass to the container
Returns
the number of the file descriptor inside the container on success, on failure -1

◆ addMount()

bool DobbyRdkPluginUtils::addMount ( const std::string &  source,
const std::string &  destination,
const std::string &  type,
const std::list< std::string > &  mountOptions 
) const

Public api to allow for adding additional mounts to a container's config file.

This can only obviously be called before the config file is persisted to disk.

Parameters
[in]sourceThe mount source
[in]destinationThe mount destination
[in]typeThe file system type of the mount
[in]mountOptionsThe mount options (mount(2) data parameter)
Returns
true if the mount point was added, otherwise false.

◆ callInNamespace()

template<class Function , class... Args>
bool DobbyRdkPluginUtils::callInNamespace ( pid_t  pid,
int  nsType,
Function &&  f,
Args &&...  args 
) const
inline

Slightly nicer version of callInNamespace, handles the function bind for you automatically.

You'd typically use this to perform options in the namespace of a container. The pid argument would be the pid of the containered process.

The nsType argument should be one of the following values: CLONE_NEWIPC - run in a IPC namespace CLONE_NEWNET - run in a network namespace CLONE_NEWNS - run in a mount namespace CLONE_NEWPID - run in a PID namespace CLONE_NEWUSER - run in a user namespace CLONE_NEWUTS - run in a UTS namespace

Parameters
[in]pidThe pid owner of the namespace to enter, typically the pid of the process in the container.
[in]nsTypeThe type of the namespace to enter, see above.
[in]funcThe actual function to execute.
Returns
true on success, false on failure.

◆ callInNamespaceImpl()

bool DobbyRdkPluginUtils::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.

This function uses the setns syscall and therefore it must spawn a thread to run the callback in. However this function blocks until the thread completes, so although it is multi-threaded it's API is blocking, i.e. effectively single threaded.

The nsType argument should be one of the following values: CLONE_NEWIPC - run in a IPC namespace CLONE_NEWNET - run in a network namespace CLONE_NEWNS - run in a mount namespace CLONE_NEWPID - run in a PID namespace CLONE_NEWUSER - run in a user namespace CLONE_NEWUTS - run in a UTS namespace

Parameters
[in]pidThe pid of the process namespace to enter.
[in]nsTypeThe namespace to run the function in, see above.
[in]funcThe function to execute.
Returns
true if successifully entered the namespace, otherwise false.

◆ files() [1/2]

std::list< int > DobbyRdkPluginUtils::files ( ) const

Gets all file descriptor registered by any client.

Returns
List of all file descriptors

◆ files() [2/2]

std::list< int > DobbyRdkPluginUtils::files ( const std::string &  pluginName) const

Gets all file descriptor registered by concrete client.

Parameters
[in]pluginNameRDK plugin name
Returns
List of file descriptors assiociated with given plugin name

◆ getContainerId()

std::string DobbyRdkPluginUtils::getContainerId ( ) const

Gets the container ID.

Returns
Container ID as string

◆ getContainerNetworkInfo()

bool DobbyRdkPluginUtils::getContainerNetworkInfo ( ContainerNetworkInfo networkInfo)

Gets network info about the container (veth/IP)

Designed to allow other plugins to create their own iptables rules once the networking plugin has run.

@params[out] networkInfo struct containing veth/ip address

Returns
true if successfully got info, false for failure

◆ getContainerPid()

pid_t DobbyRdkPluginUtils::getContainerPid ( ) const

Gets the container pid from the stdin string of a hook.

The stdin needs to be read from within the context of the hook. This function only parses the pid from a string.

Warning
Only returns a valid PID once the container is running. Only works for OCI hooks.
Returns
container pid, 0 if none found.

◆ getTakenVeths()

bool DobbyRdkPluginUtils::getTakenVeths ( std::vector< std::string > &  takenVeths)

Gets allocated veth devices.

As we are storing veth device names in the files we should be able to tell which veth devices are "taken".

@params[out] takenVeths vector of veth devices that are thaken

Returns
true if successfully got info, false for failure

◆ mkdirRecursive()

bool DobbyRdkPluginUtils::mkdirRecursive ( const std::string &  path,
mode_t  mode 
)
static

Makes a directory and all parent directories as needed.

This is equivalent to the 'mkdir -p' command.

All directories created will have access mode set by mode, for this reason the mode should be at least 'rwx---—'.

Parameters
[in]pathThe path to the directory to create.
[in]modeThe file access mode to give to all directories created.
Returns
true on success, false on failure.

◆ nsThread()

void DobbyRdkPluginUtils::nsThread ( int  newNsFd,
int  nsType,
bool *  success,
std::function< bool()> &  func 
) const

Thread helper function that implements the setns syscall.

This must be executed as a thread as it calls setns which switches namespaces and you don't really want to do this in the main thread.

Parameters
[in]newNsFdThe file descriptor for the namespace to switch into.
[in]nsTypeThe type of namespace to switch into i.e. "CLONE_NEWIPC".
[out]successReturn if thread execution of the function was the success.
[in]funcThe function to execute in the new namespace.

◆ readTextFile()

std::string DobbyRdkPluginUtils::readTextFile ( const std::string &  path) const

Simply reads a file into a string.

Parameters
[in]pathThe path to file to read from.
Returns
string read from file

◆ writeTextFile()

bool DobbyRdkPluginUtils::writeTextFile ( const std::string &  path,
const std::string &  str,
int  flags,
mode_t  mode 
) const

Simply writes a string into a file.

Parameters
[in]pathThe path to file to write to.
[in]strString to write to the file.
[in]flagsOpen flags, these will be OR'd with O_WRONLY and O_CLOEXEC.
[in]modeThe file access mode to set if O_CREAT was specified in flags and the file was created.
Returns
true on success, false on failure.

The documentation for this class was generated from the following files: