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
26 changes: 26 additions & 0 deletions src/docker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ constexpr const char kDockerContainerResourcePrefix[] = "container";
DockerReader::DockerReader(const MetadataAgentConfiguration& config)
: config_(config), environment_(config) {}

bool DockerReader::ValidateConfiguration() const {
try {
const std::string container_filter(
config_.DockerContainerFilter().empty()
? "" : "&" + config_.DockerContainerFilter());

// A limit may exist in the container_filter, however, the docker API only
// uses the first limit provided in the query params.
(void) QueryDocker(std::string(kDockerEndpointPath) +
"/json?all=true&limit=1" + container_filter);

return true;
} catch (const QueryException& e) {
// Already logged.
return false;
}
}

MetadataUpdater::ResourceMetadata DockerReader::GetContainerMetadata(
const json::Object* container, Timestamp collected_at) const
throw(json::Exception) {
Expand Down Expand Up @@ -187,4 +205,12 @@ json::value DockerReader::QueryDocker(const std::string& path) const
}
}

bool DockerUpdater::ValidateConfiguration() const {
if (!PollingMetadataUpdater::ValidateConfiguration()) {
return false;
}

return reader_.ValidateConfiguration();
}

}
11 changes: 10 additions & 1 deletion src/docker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class DockerReader {
// A Docker metadata query function.
std::vector<MetadataUpdater::ResourceMetadata> MetadataQuery() const;

// Validates the relevant configuration and returns true if it's correct.
// Returns a bool that represents if it's configured properly.
bool ValidateConfiguration() const;

private:
// A representation of all query-related errors.
class QueryException {
Expand Down Expand Up @@ -60,8 +64,13 @@ class DockerUpdater : public PollingMetadataUpdater {
public:
DockerUpdater(MetadataAgent* server)
: reader_(server->config()), PollingMetadataUpdater(
server, server->config().DockerUpdaterIntervalSeconds(),
server, "DockerUpdater",
server->config().DockerUpdaterIntervalSeconds(),
std::bind(&google::DockerReader::MetadataQuery, &reader_)) { }

protected:
bool ValidateConfiguration() const;

private:
DockerReader reader_;
};
Expand Down
3 changes: 2 additions & 1 deletion src/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class InstanceUpdater : public PollingMetadataUpdater {
public:
InstanceUpdater(MetadataAgent* server)
: reader_(server->config()), PollingMetadataUpdater(
server, server->config().InstanceUpdaterIntervalSeconds(),
server, "InstanceUpdater",
server->config().InstanceUpdaterIntervalSeconds(),
std::bind(&google::InstanceReader::MetadataQuery, &reader_)) { }
private:
InstanceReader reader_;
Expand Down
41 changes: 39 additions & 2 deletions src/kubernetes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,33 @@ json::value KubernetesReader::FindTopLevelOwner(
return FindTopLevelOwner(ns, GetOwner(ns, ref->As<json::Object>()));
}

bool KubernetesReader::ValidateConfiguration() const {
try {
(void) QueryMaster(std::string(kKubernetesEndpointPath) + "/nodes?limit=1");
} catch (const QueryException& e) {
// Already logged.
return false;
}

try {
const std::string pod_label_selector(
config_.KubernetesPodLabelSelector().empty()
? "" : "&" + config_.KubernetesPodLabelSelector());

(void) QueryMaster(std::string(kKubernetesEndpointPath) + "/pods?limit=1" +
pod_label_selector);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as for Docker -- can we confirm that the first limit=1 will take precedence over whatever is in the selector?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pod_label_selector does not imply passing a limit, it implies passing labels, if a user is passing a limit there, that's a hack, and should be validated elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it's a hack, but a limit=500000 in the (user-specified) selector should not cause us to suddenly grab all the pods in the cluster. Hence I was just asking whether this limit=1 will override it where it is now, or whether it should be at the end.

That said, I've just verified that the first occurrence of limit takes precedence, so we're good.

} catch (const QueryException& e) {
// Already logged.
return false;
}

if (CurrentNode().empty()) {
return false;
}

return true;
}

void KubernetesReader::PodCallback(
MetadataUpdater::UpdateCallback callback,
const json::Object* pod, Timestamp collected_at, bool is_deleted) const
Expand Down Expand Up @@ -1034,8 +1061,15 @@ void KubernetesReader::WatchNode(MetadataUpdater::UpdateCallback callback)
LOG(INFO) << "Watch thread (node) exiting";
}

void KubernetesUpdater::start() {
PollingMetadataUpdater::start();
bool KubernetesUpdater::ValidateConfiguration() const {
if (!PollingMetadataUpdater::ValidateConfiguration()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not also be in DockerUpdater and InstanceUpdater?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot about DockerUpdater, InstanceUpdater does not override this method.

return false;
}

return reader_.ValidateConfiguration();
}

void KubernetesUpdater::StartUpdater() {
if (config().KubernetesUseWatch()) {
// Wrap the bind expression into a function to use as a bind argument.
UpdateCallback cb = std::bind(&KubernetesUpdater::MetadataCallback, this,
Expand All @@ -1044,6 +1078,9 @@ void KubernetesUpdater::start() {
std::thread(&KubernetesReader::WatchNode, &reader_, cb);
pod_watch_thread_ =
std::thread(&KubernetesReader::WatchPods, &reader_, cb);
} else {
// Only try to poll if watch is disabled.
PollingMetadataUpdater::StartUpdater();
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/kubernetes.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class KubernetesReader {
// A Kubernetes metadata query function.
std::vector<MetadataUpdater::ResourceMetadata> MetadataQuery() const;

// Validates the relevant configuration and returns true if it's correct.
// Returns a bool that represents if it's configured properly.
bool ValidateConfiguration() const;

// Node watcher.
void WatchNode(MetadataUpdater::UpdateCallback callback) const;

Expand Down Expand Up @@ -147,7 +151,8 @@ class KubernetesUpdater : public PollingMetadataUpdater {
public:
KubernetesUpdater(MetadataAgent* server)
: reader_(server->config()), PollingMetadataUpdater(
server, server->config().KubernetesUpdaterIntervalSeconds(),
server, "KubernetesUpdater",
server->config().KubernetesUpdaterIntervalSeconds(),
std::bind(&google::KubernetesReader::MetadataQuery, &reader_)) { }
~KubernetesUpdater() {
if (node_watch_thread_.joinable()) {
Expand All @@ -158,7 +163,9 @@ class KubernetesUpdater : public PollingMetadataUpdater {
}
}

void start();
protected:
bool ValidateConfiguration() const;
void StartUpdater();

private:
// Metadata watcher callback.
Expand Down
29 changes: 23 additions & 6 deletions src/updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@

namespace google {

MetadataUpdater::MetadataUpdater(MetadataAgent* store)
: store_(store) {}
MetadataUpdater::MetadataUpdater(MetadataAgent* store, const std::string& name)
: store_(store), name_(name) {}

MetadataUpdater::~MetadataUpdater() {}

void MetadataUpdater::start() {
if (!ValidateConfiguration()) {
LOG(ERROR) << "Failed to validate configuration for " << name_;
return;
}

StartUpdater();
}

void MetadataUpdater::stop() {
StopUpdater();
}

PollingMetadataUpdater::PollingMetadataUpdater(
MetadataAgent* store, double period_s,
MetadataAgent* store, const std::string& name, double period_s,
std::function<std::vector<ResourceMetadata>()> query_metadata)
: MetadataUpdater(store),
: MetadataUpdater(store, name),
period_(period_s),
query_metadata_(query_metadata),
timer_(),
Expand All @@ -42,7 +55,11 @@ PollingMetadataUpdater::~PollingMetadataUpdater() {
}
}

void PollingMetadataUpdater::start() {
bool PollingMetadataUpdater::ValidateConfiguration() const {
return period_ >= seconds::zero();
}

void PollingMetadataUpdater::StartUpdater() {
timer_.lock();
if (config().VerboseLogging()) {
LOG(INFO) << "Timer locked";
Expand All @@ -53,7 +70,7 @@ void PollingMetadataUpdater::start() {
}
}

void PollingMetadataUpdater::stop() {
void PollingMetadataUpdater::StopUpdater() {
timer_.unlock();
if (config().VerboseLogging()) {
LOG(INFO) << "Timer unlocked";
Expand Down
29 changes: 23 additions & 6 deletions src/updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,35 @@ class MetadataUpdater {
MetadataAgent::Metadata metadata;
};

MetadataUpdater(MetadataAgent* store);
MetadataUpdater(MetadataAgent* store, const std::string& name);
virtual ~MetadataUpdater();

const MetadataAgentConfiguration& config() {
return store_->config();
}

// Starts updating.
virtual void start() = 0;
void start();

// Stops updating.
virtual void stop() = 0;
void stop();

using UpdateCallback =
std::function<void(std::vector<MetadataUpdater::ResourceMetadata>&&)>;

protected:
// Validates the relevant configuration and returns true if it's correct.
// Returns a bool that represents if it's configured properly.
virtual bool ValidateConfiguration() const {
return true;
}

// Internal method for starting the updater's logic.
virtual void StartUpdater() = 0;

// Internal method for stopping the updater's logic.
virtual void StopUpdater() = 0;

// Updates the resource map in the store.
void UpdateResourceCallback(const ResourceMetadata& result) {
store_->UpdateResource(result.ids, result.resource);
Expand All @@ -74,6 +86,9 @@ class MetadataUpdater {
}

private:
// The name of the updater provided by subclasses.
std::string name_;

// The store for the metadata.
MetadataAgent* store_;
};
Expand All @@ -82,12 +97,14 @@ class MetadataUpdater {
class PollingMetadataUpdater : public MetadataUpdater {
public:
PollingMetadataUpdater(
MetadataAgent* store, double period_s,
MetadataAgent* store, const std::string& name, double period_s,
std::function<std::vector<ResourceMetadata>()> query_metadata);
~PollingMetadataUpdater();

void start();
void stop();
protected:
bool ValidateConfiguration() const;
void StartUpdater();
void StopUpdater();

private:
using seconds = std::chrono::duration<double, std::chrono::seconds::period>;
Expand Down