Skip to content

Commit

Permalink
feat(edges): add support for canonical service labels (istio#2804)
Browse files Browse the repository at this point in the history
* feat(edges): add support for canonical service labels in WorkloadInstance

* run clang-format

* update generated file

* clang-format

* fix go.mod

* convert to constexpr

* add missing test tmpl change
  • Loading branch information
douglas-reid authored Apr 21, 2020
1 parent 02dcb15 commit 770f866
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 66 deletions.
6 changes: 6 additions & 0 deletions extensions/common/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ const std::string kProtocolHTTP = "http";
const std::string kProtocolGRPC = "grpc";
const std::string kProtocolTCP = "tcp";

constexpr absl::string_view kCanonicalServiceLabelName =
"service.istio.io/canonical-name";
constexpr absl::string_view kCanonicalServiceRevisionLabelName =
"service.istio.io/canonical-revision";
constexpr absl::string_view kLatest = "latest";

const std::set<std::string> kGrpcContentTypes{
"application/grpc", "application/grpc+proto", "application/grpc+json"};

Expand Down
16 changes: 16 additions & 0 deletions extensions/stackdriver/edges/edge_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ void instanceFromMetadata(const ::Wasm::Common::FlatNode& node_info,
flatbuffers::GetString(node_info.workload_name()));
instance->set_workload_namespace(
flatbuffers::GetString(node_info.namespace_()));

const auto labels = node_info.labels();
if (labels) {
const auto svc_iter =
labels->LookupByKey(Wasm::Common::kCanonicalServiceLabelName.data());
if (svc_iter) {
instance->set_canonical_service(
flatbuffers::GetString(svc_iter->value()));
}
const auto rev_iter = labels->LookupByKey(
Wasm::Common::kCanonicalServiceRevisionLabelName.data());
if (rev_iter) {
instance->set_canonical_revision(
flatbuffers::GetString(rev_iter->value()));
}
}
};

} // namespace
Expand Down
8 changes: 8 additions & 0 deletions extensions/stackdriver/edges/edges.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ message WorkloadInstance {
// Namespace in which the monitored resource is deployed.
// Example: default
string workload_namespace = 6;

// Name of the Istio Canonical Service for the workload instance.
// Example: foo-canonical
string canonical_service = 7;

// Revision of the Istio Canonical Service for the workload instance.
// Example: latest
string canonical_revision = 8;
}

// Represents an observed communication between two WorkloadInstances within
Expand Down
41 changes: 24 additions & 17 deletions extensions/stackdriver/metric/record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ namespace Extensions {
namespace Stackdriver {
namespace Metric {

constexpr char kCanonicalNameLabel[] = "service.istio.io/canonical-name";
constexpr char kCanonicalRevisionLabel[] =
"service.istio.io/canonical-revision";
constexpr char kLatest[] = "latest";

void record(bool is_outbound, const ::Wasm::Common::FlatNode& local_node_info,
const ::Wasm::Common::FlatNode& peer_node_info,
const ::Wasm::Common::RequestInfo& request_info,
Expand All @@ -44,24 +39,32 @@ void record(bool is_outbound, const ::Wasm::Common::FlatNode& local_node_info,
const auto peer_labels = peer_node_info.labels();

const auto local_name_iter =
local_labels ? local_labels->LookupByKey(kCanonicalNameLabel) : nullptr;
local_labels ? local_labels->LookupByKey(
Wasm::Common::kCanonicalServiceLabelName.data())
: nullptr;
const auto local_canonical_name = local_name_iter
? local_name_iter->value()
: local_node_info.workload_name();

const auto peer_name_iter =
peer_labels ? peer_labels->LookupByKey(kCanonicalNameLabel) : nullptr;
peer_labels ? peer_labels->LookupByKey(
Wasm::Common::kCanonicalServiceLabelName.data())
: nullptr;
const auto peer_canonical_name =
peer_name_iter ? peer_name_iter->value() : peer_node_info.workload_name();

const auto local_rev_iter =
local_labels ? local_labels->LookupByKey(kCanonicalRevisionLabel)
: nullptr;
local_labels
? local_labels->LookupByKey(
Wasm::Common::kCanonicalServiceRevisionLabelName.data())
: nullptr;
const auto local_canonical_rev =
local_rev_iter ? local_rev_iter->value() : nullptr;

const auto peer_rev_iter =
peer_labels ? peer_labels->LookupByKey(kCanonicalRevisionLabel) : nullptr;
peer_labels ? peer_labels->LookupByKey(
Wasm::Common::kCanonicalServiceRevisionLabelName.data())
: nullptr;
const auto peer_canonical_rev =
peer_rev_iter ? peer_rev_iter->value() : nullptr;

Expand Down Expand Up @@ -95,13 +98,15 @@ void record(bool is_outbound, const ::Wasm::Common::FlatNode& local_node_info,
{destinationCanonicalServiceNamespaceKey(),
flatbuffers::GetString(peer_node_info.namespace_())},
{destinationCanonicalRevisionKey(),
peer_canonical_rev ? peer_canonical_rev->str() : kLatest},
peer_canonical_rev ? peer_canonical_rev->str()
: ::Wasm::Common::kLatest.data()},
{sourceCanonicalServiceNameKey(),
flatbuffers::GetString(local_canonical_name)},
{sourceCanonicalServiceNamespaceKey(),
flatbuffers::GetString(local_node_info.namespace_())},
{sourceCanonicalRevisionKey(),
local_canonical_rev ? local_canonical_rev->str() : kLatest}};
{sourceCanonicalRevisionKey(), local_canonical_rev
? local_canonical_rev->str()
: ::Wasm::Common::kLatest.data()}};

if (record_http_size_metrics) {
opencensus::stats::Record(
Expand Down Expand Up @@ -147,14 +152,16 @@ void record(bool is_outbound, const ::Wasm::Common::FlatNode& local_node_info,
flatbuffers::GetString(local_canonical_name)},
{destinationCanonicalServiceNamespaceKey(),
flatbuffers::GetString(local_node_info.namespace_())},
{destinationCanonicalRevisionKey(),
local_canonical_rev ? local_canonical_rev->str() : kLatest},
{destinationCanonicalRevisionKey(), local_canonical_rev
? local_canonical_rev->str()
: ::Wasm::Common::kLatest.data()},
{sourceCanonicalServiceNameKey(),
flatbuffers::GetString(peer_canonical_name)},
{sourceCanonicalServiceNamespaceKey(),
flatbuffers::GetString(peer_node_info.namespace_())},
{sourceCanonicalRevisionKey(),
peer_canonical_rev ? peer_canonical_rev->str() : kLatest}};
{sourceCanonicalRevisionKey(), peer_canonical_rev
? peer_canonical_rev->str()
: ::Wasm::Common::kLatest.data()}};

if (record_http_size_metrics) {
opencensus::stats::Record(
Expand Down
23 changes: 12 additions & 11 deletions extensions/stats/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ void map_node(IstioDimensions& instance, bool is_source,
auto version = version_iter ? version_iter->value() : nullptr;
FB_ASSIGN(source_version, version);

auto canonical_name =
source_labels->LookupByKey("service.istio.io/canonical-name");
auto canonical_name = source_labels->LookupByKey(
::Wasm::Common::kCanonicalServiceLabelName.data());
auto name =
canonical_name ? canonical_name->value() : node.workload_name();
FB_ASSIGN(source_canonical_service, name);

auto rev =
source_labels->LookupByKey("service.istio.io/canonical-revision");
auto rev = source_labels->LookupByKey(
::Wasm::Common::kCanonicalServiceRevisionLabelName.data());
if (rev) {
FB_ASSIGN(source_canonical_revision, rev->value());
} else {
instance[source_canonical_revision] = "latest";
instance[source_canonical_revision] = ::Wasm::Common::kLatest.data();
}
} else {
instance[source_app] = "";
instance[source_version] = "";
instance[source_canonical_service] = "";
instance[source_canonical_revision] = "latest";
instance[source_canonical_revision] = ::Wasm::Common::kLatest.data();
}
} else {
FB_ASSIGN(destination_workload, node.workload_name());
Expand All @@ -103,24 +103,25 @@ void map_node(IstioDimensions& instance, bool is_source,
auto version = version_iter ? version_iter->value() : nullptr;
FB_ASSIGN(destination_version, version);

auto canonical_name =
destination_labels->LookupByKey("service.istio.io/canonical-name");
auto canonical_name = destination_labels->LookupByKey(
::Wasm::Common::kCanonicalServiceLabelName.data());
auto name =
canonical_name ? canonical_name->value() : node.workload_name();
FB_ASSIGN(destination_canonical_service, name);

auto rev = destination_labels->LookupByKey(
"service.istio.io/canonical-revision");
::Wasm::Common::kCanonicalServiceRevisionLabelName.data());
if (rev) {
FB_ASSIGN(destination_canonical_revision, rev->value());
} else {
instance[destination_canonical_revision] = "latest";
instance[destination_canonical_revision] =
::Wasm::Common::kLatest.data();
}
} else {
instance[destination_app] = "";
instance[destination_version] = "";
instance[destination_canonical_service] = "";
instance[destination_canonical_revision] = "latest";
instance[destination_canonical_revision] = ::Wasm::Common::kLatest.data();
}

FB_ASSIGN(destination_service_namespace, node.namespace_());
Expand Down
103 changes: 66 additions & 37 deletions test/envoye2e/stackdriver_plugin/edges/edges.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion testdata/stackdriver/traffic_assertion.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ traffic_assertions:
owner_uid: kubernetes://apis/apps/v1/namespaces/default/deployments/productpage-v1
workload_name: productpage-v1
workload_namespace: default
canonical_service: productpage-v1
canonical_revision: version-1
destination:
uid: kubernetes://ratings-v1-84975bc778-pxz2w.default
location: us-east4-b
cluster_name: test-cluster
owner_uid: kubernetes://apis/apps/v1/namespaces/default/deployments/ratings-v1
workload_name: ratings-v1
workload_namespace: default
workload_namespace: default
canonical_service: ratings
canonical_revision: version-1

0 comments on commit 770f866

Please sign in to comment.