andreaspeters commented on code in PR #586: URL: https://github.com/apache/mesos/pull/586#discussion_r1943694563
########## src/docker/executor.cpp: ########## @@ -785,6 +825,261 @@ class DockerExecutorProcess : public ProtobufProcess<DockerExecutorProcess> driver.get()->stop(); } + // Return the content of the CNI plugin config file and return it as json object + Try<JSON::Object> getNetworkConfigJSON(const string& fname) + { + Try<string> read = os::read(fname); + if (read.isError()) { + return Error("Cannot read CNI plugin file: " + read.error()); + } + + Try<JSON::Object> parse = JSON::parse<JSON::Object>(read.get()); + if (parse.isError()) { + return Error("Cannot parse CNI plugin file." + parse.error()); + } + + return parse; + } + + // Search the config file of the network in the path + Try<std::string> getNetworkConfigFile(const string& network, const string& path) + { + struct dirent *ent; + DIR *dir; + JSON::Object parse; + + // path can include mutiple path's seperated by ":". + char *sPath = strtok(const_cast<char*>(path.c_str()), ":"); + while (sPath != NULL) { + if ((dir = opendir(sPath)) != NULL) { + while ((ent = readdir(dir)) != NULL) { + if (ent->d_type != DT_REG || !strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + continue; + } + + std::string fname = string(sPath) + "/" + string(ent->d_name); + Try<string> read = os::read(fname); + if (read.isError()) { + LOG(ERROR) << "Cannot read CNI network config file: " << fname; + continue; + } + + Try<JSON::Object> parse = JSON::parse<JSON::Object>(read.get()); + if (parse.isError()) { + LOG(ERROR) << "Cannot parse CNI network config file."; + continue; + } + + Result<JSON::String> name = parse->at<JSON::String>("name"); + if (!name.isSome()) { + return Error( + "Cannot determine the 'name' of the CNI network for this " + "configuration " + + (name.isNone() ? "'" : ("': " + name.error()))); + } + + // Verify the configuration is for this network + if (network == name->value) { + return fname; + } + } + closedir(dir); + } + sPath = strtok(NULL, ":"); + } + return Error("Cannot find a CNI plugin config for this network name: " + network); + } + + // attach CNI at the container + Try<std::tuple<Subprocess, std::string>> attachCNI(const TaskInfo& task) + { + container = task.container(); + return commandCNI(container, "ADD"); + } + + // detach CNI from the container + Try<std::tuple<Subprocess, std::string>> detachCNI() + { + return commandCNI(container, "DEL"); + } + + // function to attach or detach CNI from/to the container + Try<std::tuple<Subprocess, std::string>> commandCNI( + const mesos::ContainerInfo& container, + const string command) Review Comment: It returns the CNI name in line 1011. We need it in line 335. But maybe I also misunderstand you. 😅 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@mesos.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org