Skip to content

Commit

Permalink
Support compute_platform_override (#570)
Browse files Browse the repository at this point in the history
* Add compute_platform_override to start_esp.py

* Handle compute_platform_override in api_manager

* Run clang-format

* Replace extern global string with public static
  • Loading branch information
kyu-c authored Mar 26, 2019
1 parent 6ee6fd3 commit 9c50be7
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 39 deletions.
1 change: 0 additions & 1 deletion include/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ filegroup(
name = "headers",
srcs = [
"api_manager/api_manager.h",
"api_manager/compute_platform.h",
"api_manager/env_interface.h",
"api_manager/grpc_request.h",
"api_manager/http_request.h",
Expand Down
13 changes: 13 additions & 0 deletions src/api_manager/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ cc_library(
],
)

cc_library(
name = "compute_platform",
srcs = [
"compute_platform.cc",
],
hdrs = [
"compute_platform.h",
],
visibility = [
"//visibility:public",
],
)

cc_test(
name = "config_test",
size = "small",
Expand Down
27 changes: 27 additions & 0 deletions src/api_manager/compute_platform.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (C) Extensible Service Proxy Authors. All Rights Reserved.
*
* 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 "src/api_manager/compute_platform.h"

namespace google {
namespace api_manager {

const std::string ComputePlatform::kGaeFlex = "GAE Flex";
const std::string ComputePlatform::kGce = "GCE";
const std::string ComputePlatform::kGke = "GKE";
const std::string ComputePlatform::kUnknown = "unknown";

} // namespace api_manager
} // namespace google
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,17 @@
#ifndef API_MANAGER_COMPUTE_PLATFORM_H_
#define API_MANAGER_COMPUTE_PLATFORM_H_

#include <string>

namespace google {
namespace api_manager {

namespace compute_platform {

enum ComputePlatform { UNKNOWN = 0, GAE_FLEX = 1, GCE = 2, GKE = 3 };

inline const char *ToString(ComputePlatform p) {
switch (p) {
case GAE_FLEX:
return "GAE Flex";
case GCE:
return "GCE";
case GKE:
return "GKE";
case UNKNOWN:
default:
return "unknown";
}
}

} // namespace compute_platform
struct ComputePlatform {
static const std::string kGaeFlex;
static const std::string kGce;
static const std::string kGke;
static const std::string kUnknown;
};

} // namespace api_manager
} // namespace google
Expand Down
2 changes: 2 additions & 0 deletions src/api_manager/context/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cc_library(
"//src/api_manager/auth:authz",
"//src/api_manager/auth:service_account_token",
"//src/api_manager/cloud_trace",
"//src/api_manager:compute_platform",
"//src/api_manager/service_control",
"//src/api_manager/utils",
],
Expand All @@ -78,6 +79,7 @@ cc_test(
deps = [
"//external:googletest_main",
"//src/api_manager",
"//src/api_manager:compute_platform",
"//src/api_manager:mock_api_manager_environment",
],
)
12 changes: 8 additions & 4 deletions src/api_manager/context/global_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GlobalContext::GlobalContext(std::unique_ptr<ApiManagerEnvInterface> env,
disable_log_status_(false),
always_print_primitive_fields_(false),
intermediate_report_interval_(kIntermediateReportInterval),
platform_(compute_platform::UNKNOWN) {
platform_(ComputePlatform::kUnknown) {
// Need to load server config first.
server_config_ = Config::LoadServerConfig(env_.get(), server_config);

Expand Down Expand Up @@ -83,11 +83,11 @@ GlobalContext::GlobalContext(std::unique_ptr<ApiManagerEnvInterface> env,
if (server_config_->has_metadata_attributes()) {
const auto& metadata = server_config_->metadata_attributes();
if (!metadata.gae_server_software().empty()) {
platform_ = compute_platform::GAE_FLEX;
platform_ = ComputePlatform::kGaeFlex;
} else if (!metadata.kube_env().empty()) {
platform_ = compute_platform::GKE;
platform_ = ComputePlatform::kGke;
} else {
platform_ = compute_platform::GCE;
platform_ = ComputePlatform::kGce;
}
location_ = metadata.zone();
project_id_ = metadata.project_id();
Expand All @@ -100,6 +100,10 @@ GlobalContext::GlobalContext(std::unique_ptr<ApiManagerEnvInterface> env,
}
}

if (!server_config_->compute_platform_override().empty()) {
platform_ = server_config_->compute_platform_override();
}

if (server_config_->has_experimental()) {
const auto& experimental = server_config_->experimental();
disable_log_status_ = experimental.disable_log_status();
Expand Down
8 changes: 4 additions & 4 deletions src/api_manager/context/global_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#ifndef API_MANAGER_CONTEXT_GLOBAL_CONTEXT_H_
#define API_MANAGER_CONTEXT_GLOBAL_CONTEXT_H_

#include "include/api_manager/compute_platform.h"
#include "src/api_manager/auth/authz_cache.h"
#include "src/api_manager/auth/certs.h"
#include "src/api_manager/auth/jwt_cache.h"
#include "src/api_manager/auth/service_account_token.h"
#include "src/api_manager/cloud_trace/cloud_trace.h"
#include "src/api_manager/compute_platform.h"
#include "src/api_manager/proto/server_config.pb.h"

namespace google {
Expand Down Expand Up @@ -87,7 +87,7 @@ class GlobalContext {
void rollout_strategy(const std::string &rollout_strategy) {
rollout_strategy_ = rollout_strategy;
}
compute_platform::ComputePlatform platform() const { return platform_; }
const std::string &platform() const { return platform_; }
const std::string &location() const { return location_; }

private:
Expand Down Expand Up @@ -127,8 +127,8 @@ class GlobalContext {

// The time interval for grpc intermediate report.
int64_t intermediate_report_interval_;
// The computer platform
compute_platform::ComputePlatform platform_;
// The compute platform
std::string platform_;
// The project_id from metadata zone.
std::string project_id_;
// The location from metadata zone.
Expand Down
21 changes: 19 additions & 2 deletions src/api_manager/context/global_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "src/api_manager/compute_platform.h"
#include "src/api_manager/context/global_context.h"
#include "src/api_manager/mock_api_manager_environment.h"

Expand All @@ -36,7 +37,7 @@ TEST(GlobalContextTest, TestEmptyExperimentalFlags) {
EXPECT_FALSE(ctx.AlwaysPrintPrimitiveFields());
EXPECT_EQ(ctx.rollout_strategy(), "");

EXPECT_EQ(ctx.platform(), compute_platform::UNKNOWN);
EXPECT_EQ(ctx.platform(), ComputePlatform::kUnknown);
EXPECT_EQ(ctx.project_id(), "");
EXPECT_EQ(ctx.location(), "");

Expand Down Expand Up @@ -79,7 +80,7 @@ metadata_attributes {
new testing::NiceMock<MockApiManagerEnvironment>());
GlobalContext ctx(std::move(env), kServerConfig);

EXPECT_EQ(ctx.platform(), compute_platform::GAE_FLEX);
EXPECT_EQ(ctx.platform(), ComputePlatform::kGaeFlex);
EXPECT_EQ(ctx.project_id(), "PROJECT_ID");
EXPECT_EQ(ctx.location(), "us-west1-a");

Expand All @@ -91,6 +92,22 @@ metadata_attributes {
"TOKEN");
}

TEST(GlobalContextTest, TestComputePlatformOverride) {
const char kServerConfig[] = R"(
metadata_attributes {
gae_server_software: "abd"
}
compute_platform_override: "foo"
)";

std::unique_ptr<ApiManagerEnvInterface> env(
new testing::NiceMock<MockApiManagerEnvironment>());
GlobalContext ctx(std::move(env), kServerConfig);

EXPECT_EQ(ctx.platform(), "foo");
}

TEST(GlobalContextTest, TestInstanceIdentityTokenCache) {
const char kServerConfig[] = R"(
metadata_attributes {
Expand Down
3 changes: 3 additions & 0 deletions src/api_manager/proto/server_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ message ServerConfig {

// The attributes from the metadata server
MetadataAttributes metadata_attributes = 15;

// The overriden compute platform to be reported.
string compute_platform_override = 16;
}

// Server config for service control
Expand Down
2 changes: 2 additions & 0 deletions src/api_manager/service_control/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cc_library(
"//src/api_manager:impl_headers",
"//src/api_manager:server_config_proto",
"//src/api_manager/cloud_trace",
"//src/api_manager:compute_platform",
"//src/api_manager/utils",
],
)
Expand Down Expand Up @@ -79,6 +80,7 @@ cc_test(
deps = [
":service_control",
"//external:googletest_main",
"//src/api_manager:compute_platform",
],
)

Expand Down
8 changes: 4 additions & 4 deletions src/api_manager/service_control/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include <memory>
#include <string>

#include "include/api_manager/compute_platform.h"
#include "include/api_manager/protocol.h"
#include "include/api_manager/service_control.h"
#include "include/api_manager/utils/status.h"
#include "src/api_manager/compute_platform.h"

namespace google {
namespace api_manager {
Expand Down Expand Up @@ -144,8 +144,8 @@ struct ReportRequestInfo : public OperationInfo {
// HTTP method. all-caps string such as "GET", "POST" etc.
std::string method;

// A recognized compute platform (GAE, GCE, GKE).
compute_platform::ComputePlatform compute_platform;
// A recognized compute platform (GAE Flex, GCE, GKE, and etc.).
std::string compute_platform;

// If consumer data should be sent.
CheckResponseInfo check_response_info;
Expand Down Expand Up @@ -184,7 +184,7 @@ struct ReportRequestInfo : public OperationInfo {
response_size(-1),
frontend_protocol(protocol::UNKNOWN),
backend_protocol(protocol::UNKNOWN),
compute_platform(compute_platform::UNKNOWN),
compute_platform(ComputePlatform::kUnknown),
request_bytes(0),
response_bytes(0),
streaming_request_message_counts(0),
Expand Down
2 changes: 1 addition & 1 deletion src/api_manager/service_control/proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ Status set_api_version(const SupportedLabel& l, const ReportRequestInfo& info,
// servicecontrol.googleapis.com/platform
Status set_platform(const SupportedLabel& l, const ReportRequestInfo& info,
Map<std::string, std::string>* labels) {
(*labels)[l.name] = compute_platform::ToString(info.compute_platform);
(*labels)[l.name] = info.compute_platform;
return Status::OK;
}

Expand Down
3 changes: 2 additions & 1 deletion src/api_manager/service_control/proto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "google/protobuf/struct.pb.h"
#include "google/protobuf/text_format.h"
#include "include/api_manager/utils/version.h"
#include "src/api_manager/compute_platform.h"

namespace gasv1 = ::google::api::servicecontrol::v1;
using ::google::api_manager::utils::Status;
Expand Down Expand Up @@ -95,7 +96,7 @@ void FillReportRequestInfo(ReportRequestInfo* request) {
request->latency.backend_time_ms = 101;
request->latency.overhead_time_ms = 22;
request->frontend_protocol = protocol::HTTP;
request->compute_platform = compute_platform::GKE;
request->compute_platform = ComputePlatform::kGke;
request->auth_issuer = "auth-issuer";
request->auth_audience = "auth-audience";

Expand Down
12 changes: 10 additions & 2 deletions src/nginx/t/report_compute_platform.t
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ my $BackendPort = ApiManager::pick_port();
my $ServiceControlPort = ApiManager::pick_port();
my $MetadataPort = ApiManager::pick_port();

my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(36);
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(48);

# Save service name in the service configuration protocol buffer file.
my $config = ApiManager::get_echo_service_config .
Expand Down Expand Up @@ -97,6 +97,7 @@ metadata_attributes {
zone: "us-west1-a"
$test_case->{metadata_key}: "$test_case->{metadata_value}"
}
compute_platform_override: "$test_case->{compute_platform_override}"
EOF

$t->run_daemon(\&bookstore, $t, $BackendPort, $bookstore_log, $test_case->{platform});
Expand Down Expand Up @@ -159,7 +160,14 @@ my @test_cases = (
metadata_key => 'project_id',
metadata_value => 'esp-load-test',
platform => 'GCE',
}
},
{
name => 'compute platform override',
metadata_key => 'gae_server_software',
metadata_value => 'Google App Engine/1.9.38',
compute_platform_override => "test platform",
platform => 'test platform',
},
);

for my $test_case ( @test_cases ) {
Expand Down
3 changes: 3 additions & 0 deletions start_esp/server-auto.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ metadata_attributes {
${metadata_attributes}
}
% endif
% if compute_platform_override:
compute_platform_override: "${compute_platform_override}"
% endif
5 changes: 4 additions & 1 deletion start_esp/start_esp.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def write_server_config_template(server_config_path, args):
log_request_headers=args.request_headers,
log_response_headers=args.response_headers,
log_jwt_payloads=args.jwt_payloads,
metadata_attributes=args.metadata_attributes)
metadata_attributes=args.metadata_attributes,
compute_platform_override=args.compute_platform_override)

server_config_file = server_config_path
if server_config_file.endswith('/'):
Expand Down Expand Up @@ -858,6 +859,8 @@ def make_argparser():
`backend` configuration. This flag conflicts with a few of other flags.
''')

parser.add_argument('--compute_platform_override', default=None, help=argparse.SUPPRESS)

return parser

# Check whether there are conflict flags. If so, return the error string. Otherwise returns None.
Expand Down
1 change: 1 addition & 0 deletions start_esp/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ py_test(
"testdata/expected_transcoding_always_print_primitive_fields_server.json",
"testdata/expected_underscores_in_headers_nginx.conf",
"testdata/expected_worker_processes_nginx.conf",
"testdata/expected_compute_platform_override_server.json",
"testdata/test_service_config_1.json",
":nginx-conf-template",
":server-conf-template",
Expand Down
5 changes: 5 additions & 0 deletions start_esp/test/start_esp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ def test_backend_routing_output_is_as_expected(self):
config_generator = self.backend_routing_config_generator
self.run_test_with_expectation(expected_config_file, self.generated_nginx_config_file, config_generator)

def test_compute_platform_override_arg_output_is_as_expected(self):
expected_config_file = "./start_esp/test/testdata/expected_compute_platform_override_server.json"
config_generator = self.basic_config_generator + " --compute_platform_override test_platform"
self.run_test_with_expectation(expected_config_file, self.generated_server_config_file, config_generator)

########## The tests for validating it should generate failure on conflict flags ##########

def test_enable_backend_routing_conflicts_with_string_flag(self):
Expand Down
Loading

0 comments on commit 9c50be7

Please sign in to comment.