szaszm commented on a change in pull request #1257: URL: https://github.com/apache/nifi-minifi-cpp/pull/1257#discussion_r803148116
########## File path: libminifi/include/AttributeProviderService.h ########## @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include <string> +#include <optional> +#include <unordered_map> +#include <vector> + +#include "core/controller/ControllerService.h" + +namespace org::apache::nifi::minifi::controllers { Review comment: I think this file should be under `libminifi/include/controllers`, to be consistent with other services and the namespace. ########## File path: extensions/kubernetes/controllerservice/KubernetesControllerService.cpp ########## @@ -0,0 +1,212 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KubernetesControllerService.h" + +#include <vector> + +extern "C" { +#include "config/incluster_config.h" +#include "config/kube_config.h" +#include "include/apiClient.h" +#include "api/CoreV1API.h" +} + +#include "core/Resource.h" +#include "core/logging/LoggerConfiguration.h" +#include "Exception.h" +#include "utils/gsl.h" + +namespace org::apache::nifi::minifi::controllers { + +class KubernetesControllerService::APIClient { + public: + explicit APIClient(core::logging::Logger& logger); + ~APIClient(); + + APIClient(APIClient&&) = delete; + APIClient(const APIClient&) = delete; + APIClient& operator=(APIClient&&) = delete; + APIClient& operator=(const APIClient&) = delete; + + apiClient_t* getClient() { return api_client_; } + + private: + char* base_path_ = nullptr; + sslConfig_t* ssl_config_ = nullptr; + list_t* api_keys_ = nullptr; + apiClient_t* api_client_ = nullptr; +}; + +KubernetesControllerService::APIClient::APIClient(core::logging::Logger& logger) { + int rc = load_incluster_config(&base_path_, &ssl_config_, &api_keys_); + if (rc != 0) { + logger.log_error("Cannot load kubernetes configuration in cluster"); + return; + } + api_client_ = apiClient_create_with_base_path(base_path_, ssl_config_, api_keys_); + if (!api_client_) { + logger.log_error("Cannot create a kubernetes client"); + } +} + +KubernetesControllerService::APIClient::~APIClient() { + if (api_client_) { + apiClient_free(api_client_); + api_client_ = nullptr; + } + + free_client_config(base_path_, ssl_config_, api_keys_); + base_path_ = nullptr; + ssl_config_ = nullptr; + api_keys_ = nullptr; + + apiClient_unsetupGlobalEnv(); +} + +const core::Property KubernetesControllerService::NamespaceFilter{ + core::PropertyBuilder::createProperty("Namespace Filter") + ->withDescription("Limit the output to pods in namespaces which match this regular expression") + ->withDefaultValue<std::string>("default") + ->build()}; +const core::Property KubernetesControllerService::PodNameFilter{ + core::PropertyBuilder::createProperty("Pod Name Filter") + ->withDescription("If present, limit the output to pods the name of which matches this regular expression") + ->build()}; +const core::Property KubernetesControllerService::ContainerNameFilter{ + core::PropertyBuilder::createProperty("Container Name Filter") + ->withDescription("If present, limit the output to containers the name of which matches this regular expression") + ->build()}; + +KubernetesControllerService::KubernetesControllerService(const std::string& name, const utils::Identifier& uuid) + : AttributeProviderService(name, uuid), + logger_{core::logging::LoggerFactory<KubernetesControllerService>::getLogger()}, + api_client_{std::make_unique<APIClient>(*logger_)} { +} + +KubernetesControllerService::KubernetesControllerService(const std::string& name, const std::shared_ptr<Configure>& configuration) + : KubernetesControllerService{name} { + setConfiguration(configuration); + initialize(); +} + +void KubernetesControllerService::initialize() { + std::lock_guard<std::mutex> lock(initialization_mutex_); + if (initialized_) { return; } + + ControllerService::initialize(); + setSupportedProperties({NamespaceFilter, PodNameFilter, ContainerNameFilter}); + initialized_ = true; +} + +void KubernetesControllerService::onEnable() { + std::string namespace_filter; + if (getProperty(NamespaceFilter.getName(), namespace_filter) && !namespace_filter.empty()) { + namespace_filter_ = std::regex{namespace_filter}; + } + + std::string pod_name_filter; + if (getProperty(PodNameFilter.getName(), pod_name_filter) && !pod_name_filter.empty()) { + pod_name_filter_ = std::regex{pod_name_filter}; + } + + std::string container_name_filter; + if (getProperty(ContainerNameFilter.getName(), container_name_filter) && !container_name_filter.empty()) { + container_name_filter_ = std::regex{container_name_filter}; + } +} + +namespace { + +struct v1_pod_list_t_deleter { + void operator()(v1_pod_list_t* ptr) const noexcept { v1_pod_list_free(ptr); } +}; +using v1_pod_list_unique_ptr = std::unique_ptr<v1_pod_list_t, v1_pod_list_t_deleter>; + +v1_pod_list_unique_ptr getPods(gsl::not_null<apiClient_t*> api_client, core::logging::Logger& logger) { + logger.log_info("Calling Kubernetes API listPodForAllNamespaces..."); + v1_pod_list_unique_ptr pod_list{CoreV1API_listPodForAllNamespaces(api_client, + 0, // allowWatchBookmarks + nullptr, // continue Review comment: Consider using 4 spaces as continuation indentation. The Google Style Guide recommends alignment, as you did, but I think it's a waste of horizontal space. ########## File path: extensions/standard-processors/processors/TailFile.cpp ########## @@ -398,6 +410,23 @@ void TailFile::onSchedule(const std::shared_ptr<core::ProcessContext> &context, initial_start_position_ = InitialStartPositions{utils::parsePropertyWithAllowableValuesOrThrow(*context, InitialStartPosition.getName(), InitialStartPositions::values())}; } +void TailFile::parseAttributeProviderServiceProperty(core::ProcessContext& context) { + const auto attribute_provider_service_name = context.getProperty(AttributeProviderService); + if (!attribute_provider_service_name || attribute_provider_service_name->empty()) { + return; + } + + std::shared_ptr<core::controller::ControllerService> controller_service = context.getControllerService(*attribute_provider_service_name); + if (!controller_service) { + throw minifi::Exception{ExceptionType::PROCESS_SCHEDULE_EXCEPTION, utils::StringUtils::join_pack("Controller service '", *attribute_provider_service_name, "' not found")}; + } + + attribute_provider_service_ = dynamic_cast<minifi::controllers::AttributeProviderService*>(controller_service.get()); + if (!attribute_provider_service_) { + throw minifi::Exception{ExceptionType::PROCESS_SCHEDULE_EXCEPTION, utils::StringUtils::join_pack("Controller service '", *attribute_provider_service_name, "' is not an AttributeProviderService")}; + } Review comment: Could you add a comment about ownership here? We're taking ownership of the service, leaking a non-owner pointer, then giving up ownership on return. This is only safe because controller services have shared ownership with a persistent owner somewhere else. Taking ownership in TailFile works, too. ########## File path: extensions/kubernetes/controllerservice/KubernetesControllerService.cpp ########## @@ -0,0 +1,212 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KubernetesControllerService.h" + +#include <vector> + +extern "C" { +#include "config/incluster_config.h" +#include "config/kube_config.h" +#include "include/apiClient.h" +#include "api/CoreV1API.h" +} + +#include "core/Resource.h" +#include "core/logging/LoggerConfiguration.h" +#include "Exception.h" +#include "utils/gsl.h" + +namespace org::apache::nifi::minifi::controllers { + +class KubernetesControllerService::APIClient { + public: + explicit APIClient(core::logging::Logger& logger); + ~APIClient(); + + APIClient(APIClient&&) = delete; + APIClient(const APIClient&) = delete; + APIClient& operator=(APIClient&&) = delete; + APIClient& operator=(const APIClient&) = delete; + + apiClient_t* getClient() { return api_client_; } Review comment: Consider making this `const noexcept` and `[[nodiscard]]` ########## File path: extensions/kubernetes/controllerservice/KubernetesControllerService.cpp ########## @@ -0,0 +1,212 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KubernetesControllerService.h" + +#include <vector> + +extern "C" { +#include "config/incluster_config.h" +#include "config/kube_config.h" +#include "include/apiClient.h" +#include "api/CoreV1API.h" +} + +#include "core/Resource.h" +#include "core/logging/LoggerConfiguration.h" +#include "Exception.h" +#include "utils/gsl.h" + +namespace org::apache::nifi::minifi::controllers { + +class KubernetesControllerService::APIClient { + public: + explicit APIClient(core::logging::Logger& logger); + ~APIClient(); + + APIClient(APIClient&&) = delete; + APIClient(const APIClient&) = delete; + APIClient& operator=(APIClient&&) = delete; + APIClient& operator=(const APIClient&) = delete; + + apiClient_t* getClient() { return api_client_; } + + private: + char* base_path_ = nullptr; + sslConfig_t* ssl_config_ = nullptr; + list_t* api_keys_ = nullptr; + apiClient_t* api_client_ = nullptr; +}; + +KubernetesControllerService::APIClient::APIClient(core::logging::Logger& logger) { + int rc = load_incluster_config(&base_path_, &ssl_config_, &api_keys_); + if (rc != 0) { + logger.log_error("Cannot load kubernetes configuration in cluster"); + return; + } + api_client_ = apiClient_create_with_base_path(base_path_, ssl_config_, api_keys_); + if (!api_client_) { + logger.log_error("Cannot create a kubernetes client"); + } +} + +KubernetesControllerService::APIClient::~APIClient() { + if (api_client_) { + apiClient_free(api_client_); + api_client_ = nullptr; + } + + free_client_config(base_path_, ssl_config_, api_keys_); + base_path_ = nullptr; + ssl_config_ = nullptr; + api_keys_ = nullptr; + + apiClient_unsetupGlobalEnv(); +} + +const core::Property KubernetesControllerService::NamespaceFilter{ + core::PropertyBuilder::createProperty("Namespace Filter") + ->withDescription("Limit the output to pods in namespaces which match this regular expression") + ->withDefaultValue<std::string>("default") + ->build()}; +const core::Property KubernetesControllerService::PodNameFilter{ + core::PropertyBuilder::createProperty("Pod Name Filter") + ->withDescription("If present, limit the output to pods the name of which matches this regular expression") + ->build()}; +const core::Property KubernetesControllerService::ContainerNameFilter{ + core::PropertyBuilder::createProperty("Container Name Filter") + ->withDescription("If present, limit the output to containers the name of which matches this regular expression") + ->build()}; + +KubernetesControllerService::KubernetesControllerService(const std::string& name, const utils::Identifier& uuid) + : AttributeProviderService(name, uuid), + logger_{core::logging::LoggerFactory<KubernetesControllerService>::getLogger()}, + api_client_{std::make_unique<APIClient>(*logger_)} { +} + +KubernetesControllerService::KubernetesControllerService(const std::string& name, const std::shared_ptr<Configure>& configuration) + : KubernetesControllerService{name} { + setConfiguration(configuration); + initialize(); +} + +void KubernetesControllerService::initialize() { + std::lock_guard<std::mutex> lock(initialization_mutex_); + if (initialized_) { return; } + + ControllerService::initialize(); + setSupportedProperties({NamespaceFilter, PodNameFilter, ContainerNameFilter}); + initialized_ = true; +} + +void KubernetesControllerService::onEnable() { + std::string namespace_filter; + if (getProperty(NamespaceFilter.getName(), namespace_filter) && !namespace_filter.empty()) { + namespace_filter_ = std::regex{namespace_filter}; + } + + std::string pod_name_filter; + if (getProperty(PodNameFilter.getName(), pod_name_filter) && !pod_name_filter.empty()) { + pod_name_filter_ = std::regex{pod_name_filter}; + } + + std::string container_name_filter; + if (getProperty(ContainerNameFilter.getName(), container_name_filter) && !container_name_filter.empty()) { + container_name_filter_ = std::regex{container_name_filter}; + } +} + +namespace { + +struct v1_pod_list_t_deleter { + void operator()(v1_pod_list_t* ptr) const noexcept { v1_pod_list_free(ptr); } +}; +using v1_pod_list_unique_ptr = std::unique_ptr<v1_pod_list_t, v1_pod_list_t_deleter>; + +v1_pod_list_unique_ptr getPods(gsl::not_null<apiClient_t*> api_client, core::logging::Logger& logger) { + logger.log_info("Calling Kubernetes API listPodForAllNamespaces..."); + v1_pod_list_unique_ptr pod_list{CoreV1API_listPodForAllNamespaces(api_client, + 0, // allowWatchBookmarks + nullptr, // continue + nullptr, // fieldSelector + nullptr, // labelSelector + 0, // limit + nullptr, // pretty + nullptr, // resourceVersion + nullptr, // resourceVersionMatch + 0, // timeoutSeconds + 0)}; // watch + logger.log_info("The return code of the Kubernetes API listPodForAllNamespaces call: %ld", api_client->response_code); + return pod_list; +} + +} // namespace + +std::optional<std::vector<KubernetesControllerService::AttributeMap>> KubernetesControllerService::getAttributes() { + if (!api_client_->getClient()) { + logger_->log_warn("The Kubernetes client is not valid, unable to call the Kubernetes API"); + return std::nullopt; + } + + const auto pod_list = getPods(gsl::make_not_null(api_client_->getClient()), *logger_); + if (!pod_list) { + logger_->log_warn("Could not find any Kubernetes pods"); + return std::nullopt; + } + + std::vector<AttributeMap> container_attribute_maps; + + listEntry_t* pod_entry = nullptr; + list_ForEach(pod_entry, pod_list->items) { + const auto pod = static_cast<v1_pod_t*>(pod_entry->data); Review comment: Wow, this API is terrible. ########## File path: extensions/kubernetes/controllerservice/KubernetesControllerService.cpp ########## @@ -0,0 +1,212 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KubernetesControllerService.h" + +#include <vector> + +extern "C" { +#include "config/incluster_config.h" +#include "config/kube_config.h" +#include "include/apiClient.h" +#include "api/CoreV1API.h" +} + +#include "core/Resource.h" +#include "core/logging/LoggerConfiguration.h" +#include "Exception.h" +#include "utils/gsl.h" + +namespace org::apache::nifi::minifi::controllers { + +class KubernetesControllerService::APIClient { + public: + explicit APIClient(core::logging::Logger& logger); + ~APIClient(); + + APIClient(APIClient&&) = delete; + APIClient(const APIClient&) = delete; + APIClient& operator=(APIClient&&) = delete; + APIClient& operator=(const APIClient&) = delete; + + apiClient_t* getClient() { return api_client_; } + + private: + char* base_path_ = nullptr; + sslConfig_t* ssl_config_ = nullptr; + list_t* api_keys_ = nullptr; + apiClient_t* api_client_ = nullptr; +}; + +KubernetesControllerService::APIClient::APIClient(core::logging::Logger& logger) { + int rc = load_incluster_config(&base_path_, &ssl_config_, &api_keys_); + if (rc != 0) { + logger.log_error("Cannot load kubernetes configuration in cluster"); + return; + } + api_client_ = apiClient_create_with_base_path(base_path_, ssl_config_, api_keys_); + if (!api_client_) { + logger.log_error("Cannot create a kubernetes client"); + } +} Review comment: I would prefer to tighten class invariants and not allow a null api client. This way, when we have an api client object, it represents an api client, not an "either api client or null". In other words, we should throw from the constructor to signal that creating the api client has failed. If you decide to implement this, use not_null annotations liberally. ########## File path: extensions/kubernetes/controllerservice/KubernetesControllerService.cpp ########## @@ -0,0 +1,212 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KubernetesControllerService.h" + +#include <vector> + +extern "C" { +#include "config/incluster_config.h" +#include "config/kube_config.h" +#include "include/apiClient.h" +#include "api/CoreV1API.h" +} + +#include "core/Resource.h" +#include "core/logging/LoggerConfiguration.h" +#include "Exception.h" +#include "utils/gsl.h" + +namespace org::apache::nifi::minifi::controllers { + +class KubernetesControllerService::APIClient { + public: + explicit APIClient(core::logging::Logger& logger); Review comment: Treat abbreviations as one word in CamelCase. https://google.github.io/styleguide/cppguide.html#General_Naming_Rules > For the purposes of the naming rules below, a "word" is anything that you would write in English without internal spaces. This includes abbreviations, such as acronyms and initialisms. For names written in mixed case (also sometimes referred to as "[camel case](https://en.wikipedia.org/wiki/Camel_case)" or "[Pascal case](https://en.wiktionary.org/wiki/Pascal_case)"), in which the first letter of each word is capitalized, prefer to capitalize abbreviations as single words, e.g., StartRpc() rather than StartRPC(). One benefit of this approach is that you can programmatically convert identifiers to different kinds of capitalization, e.g. if you have classes accessing a similarly named database table, but using snake_case for table names, you would want the api_client table, not the a_p_i_client one. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
