Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions src/kubernetes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,28 @@ KubernetesUpdater::KubernetesUpdater(const Configuration& config,
config.KubernetesUpdaterIntervalSeconds(),
[=]() { return reader_.MetadataQuery(); }) { }

const std::vector<KubernetesUpdater::WatchId>&
KubernetesUpdater::ClusterLevelObjectTypes() {
static const std::vector<WatchId>* cluster_level_object_types =
new std::vector<WatchId>{
{"cronjobs", "batch/v1beta1"},
{"daemonsets", "apps/v1"},
{"daemonsets", "extensions/v1beta1"},
{"deployments", "apps/v1"},
{"deployments", "extensions/v1beta1"},
{"endpoints", "v1"},
{"ingresses", "extensions/v1beta1"},
{"jobs", "batch/v1"},
{"namespaces", "v1"},
{"replicasets", "apps/v1"},
{"replicasets", "extensions/v1beta1"},
{"replicationcontrollers", "v1"},
{"services", "v1"},
{"statefulsets", "apps/v1"},
};
return *cluster_level_object_types;
}

void KubernetesUpdater::ValidateDynamicConfiguration() const
throw(ConfigurationValidationError) {
PollingMetadataUpdater::ValidateDynamicConfiguration();
Expand Down Expand Up @@ -982,20 +1004,20 @@ void KubernetesUpdater::StartUpdater() {
auto cb = [=](std::vector<MetadataUpdater::ResourceMetadata>&& results) {
MetadataCallback(std::move(results));
};
node_watch_thread_ = std::thread([=]() {
object_watch_threads_.emplace(WatchId("nodes", "v1"), std::thread([=]() {
reader_.WatchNodes(watched_node, cb);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized that it's probably more efficient to be capturing reader_ by reference everywhere. But we'll do that as a separate update, as it's orthogonal to this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack.

});
pod_watch_thread_ = std::thread([=]() {
}));
object_watch_threads_.emplace(WatchId("pods", "v1"), std::thread([=]() {
reader_.WatchPods(watched_node, cb);
});
if (config().KubernetesClusterLevelMetadata() &&
config().KubernetesServiceMetadata()) {
service_watch_thread_ = std::thread([=]() {
reader_.WatchObjects("services", "v1", cb);
});
endpoints_watch_thread_ = std::thread([=]() {
reader_.WatchObjects("endpoints", "v1", cb);
});
}));
if (config().KubernetesClusterLevelMetadata()) {
for (const auto& watch_id: ClusterLevelObjectTypes()) {
const std::string& plural_kind = watch_id.first;
Comment thread
supriyagarg marked this conversation as resolved.
const std::string& api_version = watch_id.second;
object_watch_threads_.emplace(watch_id, std::thread([=]() {
reader_.WatchObjects(plural_kind, api_version, cb);
}));
}
}
} else {
// Only try to poll if watch is disabled.
Expand Down
30 changes: 12 additions & 18 deletions src/kubernetes.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ class KubernetesReader {
mutable std::string current_node_;
mutable std::string kubernetes_api_token_;
mutable std::string kubernetes_namespace_;
// A memoized map from version to a map from kind to name.
mutable std::map<std::string, std::map<std::string, std::string>>
version_to_kind_to_name_;

const Configuration& config_;
HealthChecker* health_checker_;
Expand All @@ -189,17 +186,11 @@ class KubernetesUpdater : public PollingMetadataUpdater {
KubernetesUpdater(const Configuration& config, HealthChecker* health_checker,
MetadataStore* store);
~KubernetesUpdater() {
if (node_watch_thread_.joinable()) {
node_watch_thread_.join();
}
if (pod_watch_thread_.joinable()) {
pod_watch_thread_.join();
}
if (service_watch_thread_.joinable()) {
service_watch_thread_.join();
}
if (endpoints_watch_thread_.joinable()) {
endpoints_watch_thread_.join();
for (auto& thread_it: object_watch_threads_) {
std::thread& watch_thread = thread_it.second;
if (watch_thread.joinable()) {
watch_thread.join();
}
}
}

Expand All @@ -211,15 +202,18 @@ class KubernetesUpdater : public PollingMetadataUpdater {
void NotifyStopUpdater();

private:
// WatchId combines the plural Kubernetes kind and API version.
using WatchId = std::pair<std::string, std::string>;
// List of cluster level objects to watch.
static const std::vector<WatchId>& ClusterLevelObjectTypes();

// Metadata watcher callback.
void MetadataCallback(std::vector<ResourceMetadata>&& result_vector);

KubernetesReader reader_;
HealthChecker* health_checker_;
std::thread node_watch_thread_;
std::thread pod_watch_thread_;
std::thread service_watch_thread_;
std::thread endpoints_watch_thread_;
// Map from the watch IDs to the respective threads.
std::map<WatchId, std::thread> object_watch_threads_;
};

}
Expand Down