Skip to content

Commit

Permalink
admin: surface version info string to the /clusters endpoint (#2150)
Browse files Browse the repository at this point in the history
* admin: surface version info string to the /clusters endpoint

Signed-off-by: Jose Nino <[email protected]>
  • Loading branch information
junr03 authored Dec 4, 2017
1 parent 762027f commit 99baa4e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/envoy/upstream/cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class ClusterManager {
* @return GrpcMux& ADS API provider referencee.
*/
virtual Config::GrpcMux& adsMux() PURE;

/**
* Return the current version info string for dynamic clusters, if CDS is setup.
*
* @return std::string the current version info string for dynamic clusters,
* or "static" if CDS is not in use.
*/
virtual const std::string versionInfo() const PURE;
};

typedef std::unique_ptr<ClusterManager> ClusterManagerPtr;
Expand Down
7 changes: 7 additions & 0 deletions source/common/upstream/cluster_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ Http::AsyncClient& ClusterManagerImpl::httpAsyncClientForCluster(const std::stri
}
}

const std::string ClusterManagerImpl::versionInfo() const {
if (cds_api_) {
return cds_api_->versionInfo();
}
return "static";
}

ClusterManagerImpl::ThreadLocalClusterManagerImpl::ThreadLocalClusterManagerImpl(
ClusterManagerImpl& parent, Event::Dispatcher& dispatcher,
const Optional<std::string>& local_cluster_name)
Expand Down
2 changes: 2 additions & 0 deletions source/common/upstream/cluster_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class ClusterManagerImpl : public ClusterManager, Logger::Loggable<Logger::Id::u

Config::GrpcMux& adsMux() override { return *ads_mux_; }

const std::string versionInfo() const override;

private:
/**
* Thread local cached cluster data. Each thread local cluster gets updates from the parent
Expand Down
2 changes: 2 additions & 0 deletions source/server/http/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ void AdminImpl::addCircuitSettings(const std::string& cluster_name, const std::s
}

Http::Code AdminImpl::handlerClusters(const std::string&, Buffer::Instance& response) {
response.add(fmt::format("version_info::{}\n", server_.clusterManager().versionInfo()));

for (auto& cluster : server_.clusterManager().clusters()) {
addOutlierInfo(cluster.second.get().info()->name(), cluster.second.get().outlierDetector(),
response);
Expand Down
36 changes: 36 additions & 0 deletions test/common/upstream/cluster_manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,42 @@ envoy::api::v2::Bootstrap parseBootstrapFromV2Yaml(const std::string& yaml) {
return bootstrap;
}

TEST_F(ClusterManagerImplTest, VersionInfoStatic) {
const std::string json = fmt::sprintf(
R"EOF(
{
%s
}
)EOF",
clustersJson({defaultStaticClusterJson("cluster_0")}));

create(parseBootstrapFromJson(json));
EXPECT_EQ("static", cluster_manager_->versionInfo());
}

TEST_F(ClusterManagerImplTest, VersionInfoDynamic) {
const std::string json = fmt::sprintf(
R"EOF(
{
"cds": {"cluster": %s},
%s
}
)EOF",
defaultStaticClusterJson("cds_cluster"),
clustersJson({defaultStaticClusterJson("cluster_0")}));

// Setup cds api in order to control returned version info string.
MockCdsApi* cds = new MockCdsApi();
EXPECT_CALL(factory_, createCds_()).WillOnce(Return(cds));
EXPECT_CALL(*cds, setInitializedCb(_));
EXPECT_CALL(*cds, initialize());

create(parseBootstrapFromJson(json));

EXPECT_CALL(*cds, versionInfo()).WillOnce(Return("version"));
EXPECT_EQ("version", cluster_manager_->versionInfo());
}

TEST_F(ClusterManagerImplTest, OutlierEventLog) {
const std::string json = R"EOF(
{
Expand Down
1 change: 1 addition & 0 deletions test/integration/integration_admin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ TEST_P(IntegrationAdminTest, Admin) {
EXPECT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
EXPECT_THAT(response->body(), testing::HasSubstr("added_via_api"));
EXPECT_THAT(response->body(), testing::HasSubstr("version_info::\n"));

response = IntegrationUtil::makeSingleRequest(lookupPort("admin"), "GET", "/cpuprofiler", "",
downstreamProtocol(), version_);
Expand Down
1 change: 1 addition & 0 deletions test/mocks/upstream/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class MockClusterManager : public ClusterManager {
MOCK_METHOD0(shutdown, void());
MOCK_CONST_METHOD0(sourceAddress, const Network::Address::InstanceConstSharedPtr&());
MOCK_METHOD0(adsMux, Config::GrpcMux&());
MOCK_CONST_METHOD0(versionInfo, const std::string());

NiceMock<Http::ConnectionPool::MockInstance> conn_pool_;
NiceMock<Http::MockAsyncClient> async_client_;
Expand Down

0 comments on commit 99baa4e

Please sign in to comment.