-
Notifications
You must be signed in to change notification settings - Fork 11
Add implicit instance resource mapping. #55
Changes from all commits
6906804
cedbf03
83ffd88
e39adc6
c1fa702
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,9 @@ json::value ReadCredentials( | |
|
|
||
| constexpr const char kGceMetadataServerAddress[] = | ||
| "http://metadata.google.internal./computeMetadata/v1/"; | ||
|
|
||
| constexpr const char kGceInstanceResourceType[] = "gce_instance"; | ||
|
|
||
| } | ||
|
|
||
| Environment::Environment(const MetadataAgentConfiguration& config) | ||
|
|
@@ -164,6 +167,20 @@ const std::string& Environment::InstanceId() const { | |
| return instance_id_; | ||
| } | ||
|
|
||
| const std::string& Environment::InstanceResourceType() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| if (instance_resource_type_.empty()) { | ||
| if (!config_.InstanceResourceType().empty()) { | ||
| instance_resource_type_ = config_.InstanceResourceType(); | ||
| } else { | ||
| // Default to a GCE instance. | ||
| // TODO: Detect other instance resources. | ||
| instance_resource_type_ = kGceInstanceResourceType; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason that config_.InstanceResourceType cannot default to "gce_instance"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current default (empty value) means "detect via the environment". I've added a comment in |
||
| } | ||
| } | ||
| return instance_resource_type_; | ||
| } | ||
|
|
||
| const std::string& Environment::KubernetesClusterName() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| if (kubernetes_cluster_name_.empty()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright 2018 Google Inc. | ||
| * | ||
| * Licensed 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 "instance.h" | ||
|
|
||
| #include <boost/network/protocol/http/client.hpp> | ||
| #include <chrono> | ||
|
|
||
| #include "json.h" | ||
| #include "logging.h" | ||
| #include "time.h" | ||
|
|
||
| namespace http = boost::network::http; | ||
|
|
||
| namespace google { | ||
|
|
||
| InstanceReader::InstanceReader(const MetadataAgentConfiguration& config) | ||
| : config_(config), environment_(config) {} | ||
|
|
||
| /*static*/ | ||
| MonitoredResource InstanceReader::InstanceResource(const Environment& environment) { | ||
| const std::string resource_type = environment.InstanceResourceType(); | ||
| const std::string instance_id = environment.InstanceId(); | ||
| const std::string zone = environment.InstanceZone(); | ||
| return {resource_type, { | ||
| {"instance_id", instance_id}, | ||
| {"zone", zone}, | ||
| }}; | ||
| } | ||
|
|
||
| std::vector<MetadataUpdater::ResourceMetadata> | ||
| InstanceReader::MetadataQuery() const { | ||
| if (config_.VerboseLogging()) { | ||
| LOG(INFO) << "Instance Query called"; | ||
| } | ||
|
|
||
| MonitoredResource instance_resource = InstanceResource(environment_); | ||
|
|
||
| std::vector<MetadataUpdater::ResourceMetadata> result; | ||
| result.emplace_back( | ||
| std::vector<std::string>{"", environment_.InstanceId()}, | ||
| instance_resource, | ||
| // TODO: Send actual instance metadata. | ||
| MetadataAgent::Metadata::IGNORED() | ||
| ); | ||
| return result; | ||
| } | ||
|
|
||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright 2018 Google Inc. | ||
| * | ||
| * Licensed 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. | ||
| **/ | ||
| #ifndef INSTANCE_H_ | ||
| #define INSTANCE_H_ | ||
|
|
||
| //#include "config.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove if unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the pattern in all other files, just to allow using autotools later. I'd like to keep it consistent. |
||
|
|
||
| #include <vector> | ||
|
|
||
| #include "configuration.h" | ||
| #include "environment.h" | ||
| #include "resource.h" | ||
| #include "updater.h" | ||
|
|
||
| namespace google { | ||
|
|
||
| class InstanceReader { | ||
| public: | ||
| InstanceReader(const MetadataAgentConfiguration& config); | ||
| // A Instance metadata query function. | ||
| std::vector<MetadataUpdater::ResourceMetadata> MetadataQuery() const; | ||
|
|
||
| // Gets the monitored resource of the instance the agent is running on. | ||
| static MonitoredResource InstanceResource(const Environment& environment); | ||
|
|
||
| private: | ||
| const MetadataAgentConfiguration& config_; | ||
| Environment environment_; | ||
| }; | ||
|
|
||
| class InstanceUpdater : public PollingMetadataUpdater { | ||
| public: | ||
| InstanceUpdater(MetadataAgent* server) | ||
| : reader_(server->config()), PollingMetadataUpdater( | ||
| server, server->config().InstanceUpdaterIntervalSeconds(), | ||
| std::bind(&google::InstanceReader::MetadataQuery, &reader_)) { } | ||
| private: | ||
| InstanceReader reader_; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| #endif // INSTANCE_H_ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously only used for Kubernetes, is it reasonable to say that the agent will evolve all resources linearly with a single version? Is it possible for the raw content to change over time at different rates over different resource types? I'm not sure how this contract works with the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the version of the envelope schema for the metadata ingestion API. The next PR will use it for Docker container resources as well.