cleanup: remove unused code (#2802)

* remove stale code

Signed-off-by: Kuat Yessenov <kuat@google.com>

* simplify

Signed-off-by: Kuat Yessenov <kuat@google.com>

* cleanup

Signed-off-by: Kuat Yessenov <kuat@google.com>

* fix linter

Signed-off-by: Kuat Yessenov <kuat@google.com>

* fix linter

Signed-off-by: Kuat Yessenov <kuat@google.com>
This commit is contained in:
Kuat 2020-04-14 19:39:30 -07:00 committed by GitHub
parent 1b807002d2
commit fcec9c0bf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 40 additions and 666 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ test/envoye2e/http_metadata_exchange/testoutput
.vscode
/extensions/common/flatbuffers
/extensions/common/node_info_generated.h
/extensions/common/proxy_expr.h

View File

@ -80,6 +80,7 @@ build_envoy_asan:
wasm_include:
cp -f $$(bazel info bazel-bin)/extensions/common/node_info_generated.h $(TOP)/extensions/common/
cp -fLR $$(bazel info bazel-bin)/external/com_github_google_flatbuffers/_virtual_includes/runtime_cc/flatbuffers $(TOP)/extensions/common/
cp -f $$(bazel info output_base)/external/envoy/api/wasm/cpp/contrib/proxy_expr.h $(TOP)/extensions/common/
build_wasm: wasm_include
$(foreach file, $(shell find extensions -name build_wasm.sh), cd $(TOP)/$(shell dirname $(file)) && bash ./build_wasm.sh &&) true

View File

@ -20,7 +20,6 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "extensions/common/istio_dimensions.h"
#include "extensions/common/node_info.pb.h"
#include "google/protobuf/util/json_util.h"
#ifndef NULL_PLUGIN

View File

@ -46,7 +46,6 @@ envoy_cc_library(
repository = "@envoy",
visibility = ["//visibility:public"],
deps = [
":node_info_cc_proto",
":node_info_fb_cc",
"@com_google_protobuf//:protobuf",
"@envoy//source/common/common:base64_lib",
@ -63,34 +62,6 @@ envoy_cc_library(
visibility = ["//visibility:public"],
)
envoy_cc_library(
name = "node_info_cache",
srcs = [
"node_info_cache.cc",
],
hdrs = [
"node_info_cache.h",
],
repository = "@envoy",
visibility = ["//visibility:public"],
deps = [
":context",
":node_info_cc_proto",
"@envoy//source/extensions/common/wasm/null:null_plugin_lib",
],
)
cc_proto_library(
name = "node_info_cc_proto",
visibility = ["//visibility:public"],
deps = ["node_info_proto"],
)
proto_library(
name = "node_info_proto",
srcs = ["node_info.proto"],
)
envoy_cc_test(
name = "context_test",
size = "small",

View File

@ -197,46 +197,6 @@ TrafficDirection getTrafficDirection() {
return TrafficDirection::Unspecified;
}
using google::protobuf::util::JsonStringToMessage;
using google::protobuf::util::MessageToJsonString;
// Custom-written and lenient struct parser.
google::protobuf::util::Status extractNodeMetadata(
const google::protobuf::Struct& metadata,
wasm::common::NodeInfo* node_info) {
for (const auto& it : metadata.fields()) {
if (it.first == "NAME") {
node_info->set_name(it.second.string_value());
} else if (it.first == "NAMESPACE") {
node_info->set_namespace_(it.second.string_value());
} else if (it.first == "OWNER") {
node_info->set_owner(it.second.string_value());
} else if (it.first == "WORKLOAD_NAME") {
node_info->set_workload_name(it.second.string_value());
} else if (it.first == "ISTIO_VERSION") {
node_info->set_istio_version(it.second.string_value());
} else if (it.first == "MESH_ID") {
node_info->set_mesh_id(it.second.string_value());
} else if (it.first == "LABELS") {
auto* labels = node_info->mutable_labels();
for (const auto& labels_it : it.second.struct_value().fields()) {
(*labels)[labels_it.first] = labels_it.second.string_value();
}
} else if (it.first == "PLATFORM_METADATA") {
auto* platform_metadata = node_info->mutable_platform_metadata();
for (const auto& platform_it : it.second.struct_value().fields()) {
(*platform_metadata)[platform_it.first] =
platform_it.second.string_value();
}
} else if (it.first == "APP_CONTAINERS") {
for (const auto& containers_it : it.second.list_value().values()) {
node_info->add_app_containers(containers_it.string_value());
}
}
}
return google::protobuf::util::Status::OK;
}
bool extractNodeFlatBuffer(const google::protobuf::Struct& metadata,
flatbuffers::FlatBufferBuilder& fbb) {
flatbuffers::Offset<flatbuffers::String> name, namespace_, owner,
@ -318,32 +278,6 @@ void extractEmptyNodeFlatBuffer(std::string* out) {
fbb.GetSize());
}
google::protobuf::util::Status extractNodeMetadataGeneric(
const google::protobuf::Struct& metadata,
wasm::common::NodeInfo* node_info) {
google::protobuf::util::JsonOptions json_options;
std::string metadata_json_struct;
auto status =
MessageToJsonString(metadata, &metadata_json_struct, json_options);
if (status != google::protobuf::util::Status::OK) {
return status;
}
google::protobuf::util::JsonParseOptions json_parse_options;
json_parse_options.ignore_unknown_fields = true;
return JsonStringToMessage(metadata_json_struct, node_info,
json_parse_options);
}
google::protobuf::util::Status extractLocalNodeMetadata(
wasm::common::NodeInfo* node_info) {
google::protobuf::Struct node;
if (!getMessageValue({"node", "metadata"}, &node)) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::NOT_FOUND, "metadata not found");
}
return extractNodeMetadata(node, node_info);
}
// Host header is used if use_host_header_fallback==true.
// Normally it is ok to use host header within the mesh, but not at ingress.
void populateHTTPRequestInfo(bool outbound, bool use_host_header_fallback,

View File

@ -18,7 +18,6 @@
#include <set>
#include "absl/strings/string_view.h"
#include "extensions/common/node_info.pb.h"
#include "extensions/common/node_info_generated.h"
#include "flatbuffers/flatbuffers.h"
#include "google/protobuf/struct.pb.h"
@ -152,8 +151,6 @@ struct RequestInfo {
// Some or all part may be populated depending on need.
struct RequestContext {
const bool outbound;
const wasm::common::NodeInfo& source;
const wasm::common::NodeInfo& destination;
const Common::RequestInfo& request;
};
@ -167,17 +164,6 @@ enum class TrafficDirection : int64_t {
// Retrieves the traffic direction from the configuration context.
TrafficDirection getTrafficDirection();
// Extracts NodeInfo from proxy node metadata passed in as a protobuf struct.
// It converts the metadata struct to a JSON struct and parse NodeInfo proto
// from that JSON struct.
// Returns status of protocol/JSON operations.
google::protobuf::util::Status extractNodeMetadata(
const google::protobuf::Struct& metadata,
wasm::common::NodeInfo* node_info);
google::protobuf::util::Status extractNodeMetadataGeneric(
const google::protobuf::Struct& metadata,
wasm::common::NodeInfo* node_info);
// Extract node info into a flatbuffer from a struct.
bool extractNodeFlatBuffer(const google::protobuf::Struct& metadata,
flatbuffers::FlatBufferBuilder& fbb);
@ -186,10 +172,6 @@ bool extractLocalNodeFlatBuffer(std::string* out);
// Convenience routine to create an empty node flatbuffer.
void extractEmptyNodeFlatBuffer(std::string* out);
// Read from local node metadata and populate node_info.
google::protobuf::util::Status extractLocalNodeMetadata(
wasm::common::NodeInfo* node_info);
// populateHTTPRequestInfo populates the RequestInfo struct. It needs access to
// the request context.
void populateHTTPRequestInfo(bool outbound, bool use_host_header,

View File

@ -29,7 +29,6 @@ namespace Wasm {
namespace Common {
using namespace google::protobuf::util;
using namespace wasm::common;
constexpr absl::string_view node_metadata_json = R"###(
{
@ -52,59 +51,6 @@ constexpr absl::string_view node_metadata_json = R"###(
}
)###";
static void BM_GenericStructParser(benchmark::State& state) {
google::protobuf::Struct metadata_struct;
JsonParseOptions json_parse_options;
JsonStringToMessage(std::string(node_metadata_json), &metadata_struct,
json_parse_options);
auto bytes = metadata_struct.SerializeAsString();
for (auto _ : state) {
google::protobuf::Struct test_struct;
test_struct.ParseFromArray(bytes.data(), bytes.size());
benchmark::DoNotOptimize(test_struct);
NodeInfo node_info;
extractNodeMetadataGeneric(test_struct, &node_info);
benchmark::DoNotOptimize(node_info);
}
}
BENCHMARK(BM_GenericStructParser);
static void BM_CustomStructParser(benchmark::State& state) {
google::protobuf::Struct metadata_struct;
JsonParseOptions json_parse_options;
JsonStringToMessage(std::string(node_metadata_json), &metadata_struct,
json_parse_options);
auto bytes = metadata_struct.SerializeAsString();
for (auto _ : state) {
google::protobuf::Struct test_struct;
test_struct.ParseFromArray(bytes.data(), bytes.size());
benchmark::DoNotOptimize(test_struct);
NodeInfo node_info;
extractNodeMetadata(test_struct, &node_info);
benchmark::DoNotOptimize(node_info);
}
}
BENCHMARK(BM_CustomStructParser);
static void BM_MessageParser(benchmark::State& state) {
NodeInfo node_info;
JsonParseOptions json_parse_options;
JsonStringToMessage(std::string(node_metadata_json), &node_info,
json_parse_options);
auto bytes = node_info.SerializeAsString();
for (auto _ : state) {
NodeInfo test_info;
test_info.ParseFromArray(bytes.data(), bytes.size());
benchmark::DoNotOptimize(test_info);
}
}
BENCHMARK(BM_MessageParser);
constexpr absl::string_view metadata_id_key =
"envoy.wasm.metadata_exchange.downstream_id";
constexpr absl::string_view metadata_key =
@ -125,49 +71,6 @@ static const std::string& getData(
.value();
}
typedef std::shared_ptr<const wasm::common::NodeInfo> NodeInfoPtr;
static void BM_ReadRawBytesWithCache(benchmark::State& state) {
google::protobuf::Struct metadata_struct;
JsonParseOptions json_parse_options;
JsonStringToMessage(std::string(node_metadata_json), &metadata_struct,
json_parse_options);
auto bytes = metadata_struct.SerializeAsString();
Envoy::StreamInfo::FilterStateImpl filter_state{
Envoy::StreamInfo::FilterState::LifeSpan::TopSpan};
setData(filter_state, metadata_id_key, node_id);
setData(filter_state, metadata_key, bytes);
std::unordered_map<std::string, NodeInfoPtr> cache;
size_t size = 0;
for (auto _ : state) {
// lookup cache by key
const std::string& peer_id = getData(filter_state, metadata_id_key);
auto nodeinfo_it = cache.find(peer_id);
const NodeInfo* node_info = nullptr;
if (nodeinfo_it == cache.end()) {
const std::string& bytes = getData(filter_state, metadata_key);
google::protobuf::Struct test_struct;
test_struct.ParseFromArray(bytes.data(), bytes.size());
benchmark::DoNotOptimize(test_struct);
auto node_info_ptr = std::make_shared<wasm::common::NodeInfo>();
auto status = extractNodeMetadata(test_struct, node_info_ptr.get());
node_info = node_info_ptr.get();
cache.emplace(peer_id, std::move(node_info_ptr));
} else {
node_info = nodeinfo_it->second.get();
}
size += node_info->namespace_().size() + node_info->workload_name().size() +
node_info->labels().at("app").size() +
node_info->labels().at("version").size();
benchmark::DoNotOptimize(size);
}
}
BENCHMARK(BM_ReadRawBytesWithCache);
static void BM_ReadFlatBuffer(benchmark::State& state) {
google::protobuf::Struct metadata_struct;
JsonParseOptions json_parse_options;

View File

@ -32,7 +32,6 @@ namespace Common {
using namespace google::protobuf;
using namespace google::protobuf::util;
using namespace wasm::common;
constexpr absl::string_view node_metadata_json = R"###(
{
@ -55,19 +54,6 @@ TEST(ContextTest, extractNodeMetadata) {
JsonParseOptions json_parse_options;
JsonStringToMessage(std::string(node_metadata_json), &metadata_struct,
json_parse_options);
NodeInfo node_info;
Status status = extractNodeMetadata(metadata_struct, &node_info);
EXPECT_EQ(status, Status::OK);
EXPECT_EQ(node_info.name(), "test_pod");
EXPECT_EQ(node_info.namespace_(), "test_namespace");
EXPECT_EQ(node_info.owner(), "test_owner");
EXPECT_EQ(node_info.workload_name(), "test_workload");
auto platform_metadata = node_info.platform_metadata();
EXPECT_EQ(platform_metadata["gcp_project"], "test_project");
EXPECT_EQ(platform_metadata["gcp_cluster_name"], "test_cluster");
EXPECT_EQ(platform_metadata["gcp_cluster_location"], "test_location");
EXPECT_EQ(node_info.app_containers_size(), 2);
flatbuffers::FlatBufferBuilder fbb(1024);
EXPECT_TRUE(extractNodeFlatBuffer(metadata_struct, fbb));
auto peer = flatbuffers::GetRoot<FlatNode>(fbb.GetBufferPointer());
@ -82,57 +68,6 @@ TEST(ContextTest, extractNodeMetadata) {
EXPECT_EQ(peer->app_containers()->size(), 2);
}
// Test empty node metadata.
TEST(ContextTest, extractNodeMetadataNoMetadataField) {
google::protobuf::Struct metadata_struct;
NodeInfo node_info;
Status status = extractNodeMetadata(metadata_struct, &node_info);
EXPECT_EQ(status, Status::OK);
EXPECT_EQ(node_info.name(), "");
EXPECT_EQ(node_info.namespace_(), "");
EXPECT_EQ(node_info.owner(), "");
EXPECT_EQ(node_info.workload_name(), "");
EXPECT_EQ(node_info.platform_metadata_size(), 0);
EXPECT_EQ(node_info.app_containers_size(), 0);
}
// Test missing metadata.
TEST(ContextTest, extractNodeMetadataMissingMetadata) {
std::string node_metadata_json = R"###(
{
"NAMESPACE":"test_namespace",
"NAME":"test_pod"
}
)###";
google::protobuf::Struct metadata_struct;
JsonParseOptions json_parse_options;
JsonStringToMessage(node_metadata_json, &metadata_struct, json_parse_options);
NodeInfo node_info;
Status status = extractNodeMetadata(metadata_struct, &node_info);
EXPECT_EQ(status, Status::OK);
EXPECT_EQ(node_info.name(), "test_pod");
EXPECT_EQ(node_info.namespace_(), "test_namespace");
EXPECT_EQ(node_info.owner(), "");
EXPECT_EQ(node_info.workload_name(), "");
EXPECT_EQ(node_info.platform_metadata_size(), 0);
EXPECT_EQ(node_info.app_containers_size(), 0);
}
// Test unknown field.
TEST(ContextTest, extractNodeMetadataUnknownField) {
std::string node_metadata_json = R"###(
{
"some_key":"some string",
}
)###";
google::protobuf::Struct metadata_struct;
TextFormat::ParseFromString(node_metadata_json, &metadata_struct);
NodeInfo node_info;
Status status = extractNodeMetadata(metadata_struct, &node_info);
EXPECT_EQ(status, Status::OK);
}
// Test extractNodeMetadataValue.
TEST(ContextTest, extractNodeMetadataValue) {
google::protobuf::Struct metadata_struct;

View File

@ -1,48 +0,0 @@
/* Copyright 2019 Istio 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.
*/
syntax = "proto3";
package wasm.common;
// NodeInfo represents the information extracted from proxy node metadata, or
// peer node metadata header This is used to fill metrics and log labels.
message NodeInfo {
// Name of the node. e.g. in k8s, name is the pod name.
string name = 1 [json_name = "NAME"];
// Namespace that the node runs in.
string namespace = 2 [json_name = "NAMESPACE"];
// K8s or vm workload attributes.
map<string, string> labels = 3 [json_name = "LABELS"];
string owner = 4 [json_name = "OWNER"];
string workload_name = 5 [json_name = "WORKLOAD_NAME"];
// Platform metadata uses prefixed keys
// GCP uses gcp_* keys
map<string, string> platform_metadata = 6 [json_name = "PLATFORM_METADATA"];
// Version identifier for the proxy.
string istio_version = 7 [json_name = "ISTIO_VERSION"];
// Unique identifier for the mesh. Taken from global mesh id parameter (or
// the configured trust domain when not specified).
string mesh_id = 8 [json_name = "MESH_ID"];
// List of short names for application containers that are using this proxy.
// This is only used for kubernetes, and is populated by the sidecar injector.
repeated string app_containers = 9 [json_name = "APP_CONTAINERS"];
}

View File

@ -1,100 +0,0 @@
/* Copyright 2019 Istio 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 "extensions/common/node_info_cache.h"
#include "absl/strings/str_cat.h"
#include "extensions/common/context.h"
#include "google/protobuf/util/json_util.h"
using google::protobuf::util::Status;
#ifdef NULL_PLUGIN
using Envoy::Extensions::Common::Wasm::Null::Plugin::getMessageValue;
using Envoy::Extensions::Common::Wasm::Null::Plugin::getValue;
using Envoy::Extensions::Common::Wasm::Null::Plugin::logDebug;
using Envoy::Extensions::Common::Wasm::Null::Plugin::logInfo;
#endif // NULL_PLUGIN
namespace Wasm {
namespace Common {
namespace {
// getNodeInfo fetches peer node info from host filter state. It returns true if
// no error occurs.
bool getNodeInfo(StringView peer_metadata_key,
wasm::common::NodeInfo* node_info) {
google::protobuf::Struct metadata;
if (!getMessageValue({"filter_state", peer_metadata_key}, &metadata)) {
LOG_DEBUG(absl::StrCat("cannot get metadata for: ", peer_metadata_key));
return false;
}
auto status = ::Wasm::Common::extractNodeMetadata(metadata, node_info);
if (status != Status::OK) {
LOG_DEBUG(absl::StrCat("cannot parse peer node metadata ",
metadata.DebugString(), ": ", status.ToString()));
return false;
}
return true;
}
} // namespace
NodeInfoPtr NodeInfoCache::getPeerById(StringView peer_metadata_id_key,
StringView peer_metadata_key,
std::string& peer_id) {
if (!getValue({"filter_state", peer_metadata_id_key}, &peer_id)) {
LOG_DEBUG(absl::StrCat("cannot get metadata for: ", peer_metadata_id_key));
return nullptr;
}
if (peer_id == ::Wasm::Common::kMetadataNotFoundValue) {
LOG_DEBUG(absl::StrCat("metadata not found for: ", peer_metadata_id_key));
return nullptr;
}
if (max_cache_size_ < 0) {
// Cache is disabled, fetch node info from host.
auto node_info_ptr = std::make_shared<wasm::common::NodeInfo>();
if (getNodeInfo(peer_metadata_key, node_info_ptr.get())) {
return node_info_ptr;
}
return nullptr;
}
auto nodeinfo_it = cache_.find(peer_id);
if (nodeinfo_it != cache_.end()) {
return nodeinfo_it->second;
}
// Do not let the cache grow beyond max_cache_size_.
if (int32_t(cache_.size()) > max_cache_size_) {
auto it = cache_.begin();
cache_.erase(cache_.begin(), std::next(it, max_cache_size_ / 4));
LOG_INFO(absl::StrCat("cleaned cache, new cache_size:", cache_.size()));
}
auto node_info_ptr = std::make_shared<wasm::common::NodeInfo>();
if (getNodeInfo(peer_metadata_key, node_info_ptr.get())) {
auto emplacement = cache_.emplace(peer_id, std::move(node_info_ptr));
return emplacement.first->second;
}
return nullptr;
}
} // namespace Common
} // namespace Wasm

View File

@ -1,61 +0,0 @@
/* Copyright 2019 Istio 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 <unordered_map>
#include "absl/strings/string_view.h"
#include "extensions/common/node_info.pb.h"
#ifndef NULL_PLUGIN
#include "proxy_wasm_intrinsics.h"
#else // NULL_PLUGIN
#include "extensions/common/wasm/null/null_plugin.h"
#endif // NULL_PLUGIN
namespace Wasm {
namespace Common {
const size_t DefaultNodeCacheMaxSize = 500;
const wasm::common::NodeInfo EmptyNodeInfo;
typedef std::shared_ptr<const wasm::common::NodeInfo> NodeInfoPtr;
class NodeInfoCache {
public:
// Fetches and caches Peer information by peerId. An empty ptr will be
// returned if any error conditions.
// TODO Remove this when it is cheap to directly get it from StreamInfo.
// At present this involves de-serializing to google.Protobuf.Struct and
// then another round trip to NodeInfo. This Should at most hold N entries.
// Node is owned by the cache. Do not store a reference.
NodeInfoPtr getPeerById(absl::string_view peer_metadata_id_key,
absl::string_view peer_metadata_key,
std::string& peer_id);
inline void setMaxCacheSize(int32_t size) {
max_cache_size_ = size == 0 ? DefaultNodeCacheMaxSize : size;
}
private:
std::unordered_map<std::string, NodeInfoPtr> cache_;
int32_t max_cache_size_ = DefaultNodeCacheMaxSize;
};
} // namespace Common
} // namespace Wasm

View File

@ -4,15 +4,14 @@ CPP_CONTEXT_LIB = ${CPP_API}/proxy_wasm_intrinsics.cc
ABSL = /root/abseil-cpp
ABSL_CPP = ${ABSL}/absl/strings/str_cat.cc ${ABSL}/absl/strings/str_split.cc ${ABSL}/absl/strings/numbers.cc ${ABSL}/absl/strings/ascii.cc
PROTO_SRCS = extensions/common/node_info.pb.cc config.pb.cc
PROTO_SRCS = config.pb.cc
COMMON_SRCS = extensions/common/context.cc
all: plugin.wasm
%.wasm %.wat: %.cc ${CPP_API}/proxy_wasm_intrinsics.h ${CPP_API}/proxy_wasm_enums.h ${CPP_API}/proxy_wasm_externs.h ${CPP_API}/proxy_wasm_api.h ${CPP_API}/proxy_wasm_intrinsics.js ${CPP_CONTEXT_LIB}
protoc extensions/common/node_info.proto --cpp_out=.
protoc config.proto --cpp_out=.
em++ -s STANDALONE_WASM=1 -s EMIT_EMSCRIPTEN_METADATA=1 -s EXPORTED_FUNCTIONS=['_malloc'] --std=c++17 -O3 -I${CPP_API} -I${CPP_API}/google/protobuf -Iextensions/common -I. -I/usr/local/include -I${ABSL} --js-library ${CPP_API}/proxy_wasm_intrinsics.js ${ABSL_CPP} $*.cc ${CPP_API}/proxy_wasm_intrinsics.pb.cc ${PROTO_SRCS} ${COMMON_SRCS} ${CPP_CONTEXT_LIB} ${CPP_API}/libprotobuf.a -o $*.wasm
rm -f $*.wast
rm -f extensions/common/node_info.pb.* extensions/metadata_exchange/config.pb.*
rm -f extensions/metadata_exchange/config.pb.*
chown ${uid}.${gid} $^

View File

@ -61,45 +61,31 @@ class TestMeshEdgesServiceClient : public MeshEdgesServiceClient {
TestFn request_callback_;
};
const char kNodeInfo[] = R"(
name: "test_pod"
namespace: "test_namespace"
workload_name: "test_workload"
owner: "kubernetes://test_owner"
platform_metadata: {
key: "gcp_project"
value: "test_project"
}
platform_metadata: {
key: "gcp_gke_cluster_name"
value: "test_cluster"
}
platform_metadata: {
key: "gcp_location"
value: "test_location"
}
mesh_id: "test-mesh"
)";
const char kNodeInfo[] = R"({
"NAME": "test_pod",
"NAMESPACE": "test_namespace",
"WORKLOAD_NAME": "test_workload",
"OWNER": "kubernetes://test_owner",
"PLATFORM_METADATA": {
"gcp_project": "test_project",
"gcp_gke_cluster_name": "test_cluster",
"gcp_location": "test_location"
},
"MESH_ID": "test-mesh"
})";
const char kPeerInfo[] = R"(
name: "test_peer_pod"
namespace: "test_peer_namespace"
workload_name: "test_peer_workload"
owner: "kubernetes://peer_owner"
platform_metadata: {
key: "gcp_project"
value: "test_project"
}
platform_metadata: {
key: "gcp_gke_cluster_name"
value: "test_cluster"
}
platform_metadata: {
key: "gcp_location"
value: "test_location"
}
mesh_id: "test-mesh"
)";
const char kPeerInfo[] = R"({
"NAME": "test_peer_pod",
"NAMESPACE": "test_peer_namespace",
"WORKLOAD_NAME": "test_peer_workload",
"OWNER": "kubernetes://peer_owner",
"PLATFORM_METADATA": {
"gcp_project": "test_project",
"gcp_gke_cluster_name": "test_cluster",
"gcp_location": "test_location"
},
"MESH_ID": "test-mesh"
})";
const char kWantGrpcRequest[] = R"(
parent: "projects/test_project"
@ -148,16 +134,10 @@ const char kWantUnknownGrpcRequest[] = R"(
const ::Wasm::Common::FlatNode& nodeInfo(flatbuffers::FlatBufferBuilder& fbb,
const std::string& data) {
::wasm::common::NodeInfo node_info;
TextFormat::ParseFromString(data, &node_info);
google::protobuf::util::JsonOptions json_options;
std::string metadata_json_struct;
google::protobuf::util::MessageToJsonString(node_info, &metadata_json_struct,
json_options);
google::protobuf::util::JsonParseOptions json_parse_options;
google::protobuf::Struct struct_info;
google::protobuf::util::JsonStringToMessage(metadata_json_struct,
&struct_info, json_parse_options);
google::protobuf::util::JsonStringToMessage(data, &struct_info,
json_parse_options);
::Wasm::Common::extractNodeFlatBuffer(struct_info, fbb);
return *flatbuffers::GetRoot<::Wasm::Common::FlatNode>(
fbb.GetBufferPointer());

View File

@ -54,7 +54,6 @@ using ::Wasm::Common::kDownstreamMetadataIdKey;
using ::Wasm::Common::kDownstreamMetadataKey;
using ::Wasm::Common::kUpstreamMetadataIdKey;
using ::Wasm::Common::kUpstreamMetadataKey;
using ::wasm::common::NodeInfo;
using ::Wasm::Common::RequestInfo;
constexpr char kStackdriverExporter[] = "stackdriver_exporter";

View File

@ -33,13 +33,13 @@ envoy_cc_library(
],
hdrs = [
"plugin.h",
"proxy_expr.h",
],
repository = "@envoy",
visibility = ["//visibility:public"],
deps = [
":config_cc_proto",
"//extensions/common:context",
"@envoy//api/wasm/cpp/contrib:contrib_lib",
"@envoy//source/extensions/common/wasm/null:null_plugin_lib",
],
)

View File

@ -4,15 +4,14 @@ CPP_CONTEXT_LIB = ${CPP_API}/proxy_wasm_intrinsics.cc
ABSL = /root/abseil-cpp
ABSL_CPP = ${ABSL}/absl/strings/str_cat.cc ${ABSL}/absl/strings/str_split.cc ${ABSL}/absl/strings/numbers.cc ${ABSL}/absl/strings/ascii.cc
PROTO_SRCS = extensions/common/node_info.pb.cc config.pb.cc
PROTO_SRCS = config.pb.cc
COMMON_SRCS = extensions/common/context.cc extensions/common/util.cc
all: plugin.wasm
%.wasm %.wat: %.cc ${CPP_API}/proxy_wasm_intrinsics.h ${CPP_API}/proxy_wasm_enums.h ${CPP_API}/proxy_wasm_externs.h ${CPP_API}/proxy_wasm_api.h ${CPP_API}/proxy_wasm_intrinsics.js ${CPP_CONTEXT_LIB}
protoc extensions/common/node_info.proto --cpp_out=.
protoc config.proto --cpp_out=.
em++ -s STANDALONE_WASM=1 -s EMIT_EMSCRIPTEN_METADATA=1 -s EXPORTED_FUNCTIONS=['_malloc'] --std=c++17 -O3 -I${CPP_API} -I${CPP_API}/google/protobuf -Iextensions/common -I. -I/usr/local/include -I${ABSL} -I. --js-library ${CPP_API}/proxy_wasm_intrinsics.js $*.cc ${CPP_API}/proxy_wasm_intrinsics.pb.cc ${PROTO_SRCS} ${COMMON_SRCS} ${CPP_CONTEXT_LIB} ${ABSL_CPP} ${CPP_API}/libprotobuf.a -o $*.wasm
rm -f $*.wast
rm -f extensions/common/node_info.pb.* extensions/stats/config.pb.*
rm -f extensions/stats/config.pb.*
chown ${uid}.${gid} $^

View File

@ -17,13 +17,13 @@
#include "absl/strings/ascii.h"
#include "extensions/common/util.h"
#include "extensions/stats/proxy_expr.h"
#include "google/protobuf/util/time_util.h"
using google::protobuf::util::TimeUtil;
// WASM_PROLOG
#ifndef NULL_PLUGIN
#include "extensions/common/proxy_expr.h"
#include "proxy_wasm_intrinsics.h"
#else // NULL_PLUGIN
@ -37,6 +37,8 @@ namespace Wasm {
namespace Null {
namespace Plugin {
#include "api/wasm/cpp/contrib/proxy_expr.h"
#endif // NULL_PLUGIN
// END WASM_PROLOG

View File

@ -1,122 +0,0 @@
/* Copyright 2020 Istio 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.
*/
// WASM_PROLOG
#ifndef NULL_PLUGIN
#include "proxy_wasm_intrinsics.h"
#else // NULL_PLUGIN
#include "extensions/common/wasm/null/null_plugin.h"
namespace Envoy {
namespace Extensions {
namespace Common {
namespace Wasm {
namespace Null {
namespace Plugin {
using WasmResult = Envoy::Extensions::Common::Wasm::WasmResult;
using NullPluginRegistry =
::Envoy::Extensions::Common::Wasm::Null::NullPluginRegistry;
#endif // NULL_PLUGIN
// END WASM_PROLOG
// Create an expression using a foreign function call.
inline WasmResult createExpression(StringView expr, uint32_t* token) {
std::string function = "expr_create";
char* out = nullptr;
size_t out_size = 0;
auto result =
proxy_call_foreign_function(function.data(), function.size(), expr.data(),
expr.size(), &out, &out_size);
if (result == WasmResult::Ok && out_size == sizeof(uint32_t)) {
*token = *reinterpret_cast<uint32_t*>(out);
}
::free(out);
return result;
}
// Evaluate an expression using an expression token.
inline Optional<WasmDataPtr> exprEvaluate(uint32_t token) {
std::string function = "expr_evaluate";
char* out = nullptr;
size_t out_size = 0;
auto result = proxy_call_foreign_function(
function.data(), function.size(), reinterpret_cast<const char*>(&token),
sizeof(uint32_t), &out, &out_size);
if (result != WasmResult::Ok) {
return {};
}
return std::make_unique<WasmData>(out, out_size);
}
// Delete an expression using an expression token.
inline WasmResult exprDelete(uint32_t token) {
std::string function = "expr_delete";
char* out = nullptr;
size_t out_size = 0;
auto result = proxy_call_foreign_function(
function.data(), function.size(), reinterpret_cast<const char*>(&token),
sizeof(uint32_t), &out, &out_size);
::free(out);
return result;
}
template <typename T>
inline bool evaluateExpression(uint32_t token, T* out) {
auto buf = exprEvaluate(token);
if (!buf.has_value() || buf.value()->size() != sizeof(T)) {
return false;
}
*out = *reinterpret_cast<const T*>(buf.value()->data());
return true;
}
template <>
inline bool evaluateExpression<std::string>(uint32_t token, std::string* out) {
auto buf = exprEvaluate(token);
if (!buf.has_value()) {
return false;
}
out->assign(buf.value()->data(), buf.value()->size());
return true;
}
// Specialization for message types (including struct value for lists and maps)
template <typename T>
inline bool evaluateMessage(uint32_t token, T* value_ptr) {
auto buf = exprEvaluate(token);
if (!buf.has_value()) {
return false;
}
if (buf.value()->size() == 0) {
// evaluates to null
return true;
}
return value_ptr->ParseFromArray(buf.value()->data(), buf.value()->size());
}
// WASM_EPILOG
#ifdef NULL_PLUGIN
} // namespace Plugin
} // namespace Null
} // namespace Wasm
} // namespace Common
} // namespace Extensions
} // namespace Envoy
#endif

View File

@ -45,7 +45,7 @@ import (
// MetricServer is a fake stackdriver server which implements all of monitoring v3 service method.
type MetricServer struct {
delay time.Duration
listTsResp monitoringpb.ListTimeSeriesResponse
listTSResp monitoringpb.ListTimeSeriesResponse
RcvMetricReq chan *monitoringpb.CreateTimeSeriesRequest
mux sync.Mutex
}
@ -115,7 +115,7 @@ func (s *MetricServer) DeleteMetricDescriptor(context.Context, *monitoringpb.Del
func (s *MetricServer) ListTimeSeries(context.Context, *monitoringpb.ListTimeSeriesRequest) (*monitoringpb.ListTimeSeriesResponse, error) {
s.mux.Lock()
defer s.mux.Unlock()
return &s.listTsResp, nil
return &s.listTSResp, nil
}
// CreateTimeSeries implements CreateTimeSeries method.
@ -123,7 +123,7 @@ func (s *MetricServer) CreateTimeSeries(ctx context.Context, req *monitoringpb.C
log.Printf("receive CreateTimeSeriesRequest %+v", *req)
s.mux.Lock()
defer s.mux.Unlock()
s.listTsResp.TimeSeries = append(s.listTsResp.TimeSeries, req.TimeSeries...)
s.listTSResp.TimeSeries = append(s.listTSResp.TimeSeries, req.TimeSeries...)
s.RcvMetricReq <- req
time.Sleep(s.delay)
return &empty.Empty{}, nil
@ -186,7 +186,7 @@ func (s *MetricServer) GetTimeSeries(w http.ResponseWriter, req *http.Request) {
s.mux.Lock()
defer s.mux.Unlock()
var m jsonpb.Marshaler
if s, err := m.MarshalToString(&s.listTsResp); err != nil {
if s, err := m.MarshalToString(&s.listTSResp); err != nil {
fmt.Fprintln(w, "Fail to marshal received time series")
} else {
fmt.Fprintln(w, s)