| 147 | |
| 148 | |
| 149 | Try<Nothing> CSIServerProcess::initializePlugin(const Option<string>& name) |
| 150 | { |
| 151 | if (name.isSome()) { |
| 152 | CHECK(!plugins.contains(name.get())); |
| 153 | } |
| 154 | |
| 155 | Try<list<string>> entries = os::ls(pluginConfigDir); |
| 156 | if (entries.isError()) { |
| 157 | return Error( |
| 158 | "Unable to list the CSI plugin configuration directory '" + |
| 159 | pluginConfigDir + "': " + entries.error()); |
| 160 | } |
| 161 | |
| 162 | // We are either looking for one specific plugin (if `name` is SOME), or we |
| 163 | // are loading all configs we find (if `name` is NONE). First, we populate |
| 164 | // `pluginConfigs` with one or more valid configurations. Then, we will |
| 165 | // initialize the plugin(s) based on the configuration(s) found. |
| 166 | hashmap<string, CSIPluginInfo> pluginConfigs; |
| 167 | |
| 168 | foreach (const string& entry, entries.get()) { |
| 169 | const string path = path::join(pluginConfigDir, entry); |
| 170 | |
| 171 | // Ignore directory entries. |
| 172 | if (os::stat::isdir(path)) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | Try<string> read = os::read(path); |
| 177 | if (read.isError()) { |
| 178 | // In case of an error we log and skip to the next entry. |
| 179 | LOG(ERROR) << "Failed to read CSI plugin configuration file '" |
| 180 | << path << "': " << read.error(); |
| 181 | |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | Try<JSON::Object> json = JSON::parse<JSON::Object>(read.get()); |
| 186 | if (json.isError()) { |
| 187 | return Error("JSON parse of '" + path + "' failed: " + json.error()); |
| 188 | } |
| 189 | |
| 190 | Try<CSIPluginInfo> parse = ::protobuf::parse<CSIPluginInfo>(json.get()); |
| 191 | if (parse.isError()) { |
| 192 | return Error("Protobuf parse of '" + path + "' failed: " + parse.error()); |
| 193 | } |
| 194 | |
| 195 | const CSIPluginInfo& csiPluginConfig = parse.get(); |
| 196 | const string& type = csiPluginConfig.type(); |
| 197 | |
| 198 | if (pluginConfigs.contains(type)) { |
| 199 | LOG(ERROR) << "Multiple configurations for a CSI plugin are not allowed. " |
| 200 | << "Skipping configuration file '" << path << "' since CSI " |
| 201 | << "plugin '" << type << "' already exists"; |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | if (name.isNone() || name.get() == type) { |
| 206 | pluginConfigs[type] = csiPluginConfig; |