fgerlits commented on a change in pull request #1257:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1257#discussion_r802894076



##########
File path: 
extensions/kubernetes/controllerservice/KubernetesControllerService.cpp
##########
@@ -0,0 +1,205 @@
+/**
+ * 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"
+
+namespace org::apache::nifi::minifi::controllers {
+
+class KubernetesControllerService::APIClient {
+ public:
+  explicit APIClient(core::logging::Logger& logger);
+  ~APIClient();
+  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();
+}
+
+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()};
+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()};
+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() {
+  if (initialized_) { return; }
+
+  std::lock_guard<std::mutex> lock(initialization_mutex_);
+  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(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::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 {};
+  }
+
+  const auto pod_list = getPods(api_client_->getClient(), *logger_);
+  if (!pod_list) {
+    logger_->log_warn("Could not find any Kubernetes pods");
+    return {};
+  }

Review comment:
       done in 
https://github.com/apache/nifi-minifi-cpp/pull/1257/commits/65411a26509db4cb71466af13284c20231059a57




-- 
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]


Reply via email to