Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/istio/control/http/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "include/istio/control/http/request_handler.h"
#include "include/istio/mixerclient/client.h"
#include "include/istio/utils/attribute_names.h"
#include "include/istio/utils/local_attributes.h"
#include "mixer/v1/config/client/client_config.pb.h"

namespace istio {
Expand Down Expand Up @@ -69,8 +71,9 @@ class Controller {
// * some functions provided by the environment (Envoy)
// * optional service config cache size.
struct Options {
Options(const ::istio::mixer::v1::config::client::HttpClientConfig& config)
: config(config) {}
Options(const ::istio::mixer::v1::config::client::HttpClientConfig& config,
const ::istio::utils::LocalNode& local_node)
: config(config), local_node(local_node) {}

// Mixer filter config
const ::istio::mixer::v1::config::client::HttpClientConfig& config;
Expand All @@ -81,6 +84,8 @@ class Controller {
// The LRU cache size for service config.
// If not set or is 0 default value, the cache size is 1000.
int service_config_cache_size{};

const ::istio::utils::LocalNode& local_node;
};

// The factory function to create a new instance of the controller.
Expand Down
1 change: 1 addition & 0 deletions include/istio/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_library(
name = "headers_lib",
hdrs = [
"attributes_builder.h",
"local_attributes.h",
"md5.h",
"protobuf.h",
"status.h",
Expand Down
4 changes: 4 additions & 0 deletions include/istio/utils/attribute_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct AttributeName {
static const char kSourceUser[];
static const char kSourcePrincipal[];
static const char kSourceNamespace[];
static const char kSourceUID[];
static const char kDestinationPrincipal[];

static const char kRequestHeaders[];
Expand Down Expand Up @@ -63,6 +64,7 @@ struct AttributeName {
static const char kDestinationIp[];
static const char kDestinationPort[];
static const char kDestinationUID[];
static const char kDestinationNamespace[];
static const char kOriginIp[];
static const char kConnectionReceviedBytes[];
static const char kConnectionReceviedTotalBytes[];
Expand All @@ -77,8 +79,10 @@ struct AttributeName {

// Context attributes
static const char kContextProtocol[];
static const char kContextReporterKind[];
static const char kContextTime[];
static const char kContextProxyErrorCode[];
static const char kContextReporterUID[];

// Check error code and message.
static const char kCheckErrorCode[];
Expand Down
55 changes: 55 additions & 0 deletions include/istio/utils/local_attributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright 2018 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.
*/

#ifndef ISTIO_UTILS_LOCAL_ATTRIBUTES_H
#define ISTIO_UTILS_LOCAL_ATTRIBUTES_H

#include "mixer/v1/attributes.pb.h"

namespace istio {
namespace utils {

struct LocalAttributes {
// local inbound attributes
::istio::mixer::v1::Attributes inbound;

// local outbound attributes
::istio::mixer::v1::Attributes outbound;

// local forward attributes
::istio::mixer::v1::Attributes forward;
};

// LocalNode is abstract information about the node from Mixer's perspective.
struct LocalNode {
// like kubernetes://podname.namespace
std::string uid;

// namespace
std::string ns;
};

void CreateLocalAttributes(const LocalNode& local,
LocalAttributes* local_attributes);

// create preserialized header to send to proxy that is fronting mixer.
// This header is used for istio self monitoring.
bool SerializeForwardedAttributes(const LocalNode& local,
std::string* serialized_forward_attributes);

} // namespace utils
} // namespace istio

#endif // ISTIO_UTILS_LOCAL_ATTRIBUTES_H
1 change: 1 addition & 0 deletions src/envoy/http/mixer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ envoy_cc_library(
"//src/envoy/utils:authn_lib",
"//src/envoy/utils:utils_lib",
"//src/istio/control/http:control_lib",
"//src/istio/utils:utils_lib",
"@envoy//source/exe:envoy_common_lib",
],
)
19 changes: 15 additions & 4 deletions src/envoy/http/mixer/control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/

#include "src/envoy/http/mixer/control.h"
#include "include/istio/utils/local_attributes.h"

using ::istio::mixer::v1::Attributes;
using ::istio::utils::LocalNode;

namespace Envoy {
namespace Http {
Expand All @@ -22,7 +26,8 @@ namespace Mixer {
Control::Control(const Config& config, Upstream::ClusterManager& cm,
Event::Dispatcher& dispatcher,
Runtime::RandomGenerator& random, Stats::Scope& scope,
Utils::MixerFilterStats& stats)
Utils::MixerFilterStats& stats,
const LocalInfo::LocalInfo& local_info)
: config_(config),
check_client_factory_(Utils::GrpcClientFactoryForCluster(
config_.check_cluster(), cm, scope)),
Expand All @@ -33,10 +38,16 @@ Control::Control(const Config& config, Upstream::ClusterManager& cm,
[this](::istio::mixerclient::Statistics* stat) -> bool {
return GetStats(stat);
}) {
Utils::SerializeForwardedAttributes(config_.config_pb().transport(),
&serialized_forward_attributes_);
auto& logger = Logger::Registry::getLog(Logger::Id::config);
LocalNode local_node;
if (!Utils::ExtractNodeInfo(local_info.node(), &local_node)) {
ENVOY_LOG_TO_LOGGER(logger, warn, "Unable to get node metadata");
}
::istio::utils::SerializeForwardedAttributes(local_node,
&serialized_forward_attributes_);

::istio::control::http::Controller::Options options(config_.config_pb());
::istio::control::http::Controller::Options options(config_.config_pb(),
local_node);

Utils::CreateEnvironment(dispatcher, random, *check_client_factory_,
*report_client_factory_,
Expand Down
6 changes: 5 additions & 1 deletion src/envoy/http/mixer/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

#pragma once

#include "common/common/logger.h"
#include "envoy/event/dispatcher.h"
#include "envoy/local_info/local_info.h"
#include "envoy/runtime/runtime.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/upstream/cluster_manager.h"
#include "include/istio/control/http/controller.h"
#include "include/istio/utils/local_attributes.h"
#include "src/envoy/http/mixer/config.h"
#include "src/envoy/utils/grpc_transport.h"
#include "src/envoy/utils/mixer_control.h"
Expand All @@ -35,7 +38,8 @@ class Control final : public ThreadLocal::ThreadLocalObject {
// The constructor.
Control(const Config& config, Upstream::ClusterManager& cm,
Event::Dispatcher& dispatcher, Runtime::RandomGenerator& random,
Stats::Scope& scope, Utils::MixerFilterStats& stats);
Stats::Scope& scope, Utils::MixerFilterStats& stats,
const LocalInfo::LocalInfo& local_info);

// Get low-level controller object.
::istio::control::http::Controller* controller() { return controller_.get(); }
Expand Down
14 changes: 9 additions & 5 deletions src/envoy/http/mixer/control_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#pragma once

#include "common/common/logger.h"
#include "envoy/local_info/local_info.h"
#include "src/envoy/http/mixer/control.h"
#include "src/envoy/utils/stats.h"

Expand All @@ -42,11 +43,14 @@ class ControlFactory : public Logger::Loggable<Logger::Id::config> {
Upstream::ClusterManager& cm = context.clusterManager();
Runtime::RandomGenerator& random = context.random();
Stats::Scope& scope = context.scope();
tls_->set([this, &cm, &random, &scope](Event::Dispatcher& dispatcher)
-> ThreadLocal::ThreadLocalObjectSharedPtr {
return std::make_shared<Control>(*config_, cm, dispatcher, random, scope,
stats_);
});
const LocalInfo::LocalInfo& local_info = context.localInfo();

tls_->set(
[this, &cm, &random, &scope, &local_info](Event::Dispatcher& dispatcher)
-> ThreadLocal::ThreadLocalObjectSharedPtr {
return std::make_shared<Control>(*config_, cm, dispatcher, random,
scope, stats_, local_info);
});
}

Control& control() { return tls_->getTyped<Control>(); }
Expand Down
13 changes: 12 additions & 1 deletion src/envoy/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ envoy_cc_test(
],
)

envoy_cc_test(
name = "mixer_control_test",
srcs = [
"mixer_control_test.cc",
],
repository = "@envoy",
deps = [
":utils_lib",
"@envoy//test/test_common:utility_lib",
],
)

cc_library(
name = "filter_names_lib",
Expand All @@ -103,4 +114,4 @@ cc_library(
"filter_names.h",
],
visibility = ["//visibility:public"],
)
)
93 changes: 93 additions & 0 deletions src/envoy/utils/mixer_control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
#include "src/envoy/utils/grpc_transport.h"

using ::istio::mixerclient::Statistics;
using ::istio::utils::AttributeName;
using ::istio::utils::LocalAttributes;
using ::istio::utils::LocalNode;

namespace Envoy {
namespace Utils {

const char kNodeUID[] = "NODE_UID";
const char kNodeNamespace[] = "NODE_NAMESPACE";

namespace {

// A class to wrap envoy timer for mixer client timer.
Expand Down Expand Up @@ -53,6 +60,18 @@ class EnvoyGrpcAsyncClientFactory : public Grpc::AsyncClientFactory {
envoy::api::v2::core::GrpcService config_;
};

inline bool ReadProtoMap(
const google::protobuf::Map<std::string, google::protobuf::Value> &meta,
const std::string &key, std::string *val) {
const auto it = meta.find(key);
if (it != meta.end()) {
*val = it->second.string_value();
return true;
}

return false;
}

} // namespace

// Create all environment functions for mixerclient
Expand Down Expand Up @@ -100,5 +119,79 @@ Grpc::AsyncClientFactoryPtr GrpcClientFactoryForCluster(
return std::make_unique<EnvoyGrpcAsyncClientFactory>(cm, service);
}

// This function is for compatibility with existing node ids.
// "sidecar~10.36.0.15~fortioclient-84469dc8d7-jbbxt.service-graph~service-graph.svc.cluster.local"
// --> {proxy_type}~{ip}~{node_name}.{node_ns}~{node_domain}
bool ExtractInfoCompat(const std::string &nodeid, LocalNode *args) {
auto &logger = Logger::Registry::getLog(Logger::Id::config);

auto parts = StringUtil::splitToken(nodeid, "~");
if (parts.size() < 3) {
ENVOY_LOG_TO_LOGGER(
logger, warn,
"ExtractInfoCompat node id did not have the correct format:",
"{proxy_type}~{ip}~{node_name}.{node_ns}~{node_domain} ", nodeid);
return false;
}

auto longname = std::string(parts[2].begin(), parts[2].end());
auto names = StringUtil::splitToken(longname, ".");
if (names.size() < 2) {
ENVOY_LOG_TO_LOGGER(logger, warn,
"error len(split(longname, '.')) < 3: ", longname);
return false;
}
auto ns = std::string(names[1].begin(), names[1].end());

args->ns = ns;
args->uid = "kubernetes://" + longname;

return true;
}

// ExtractInfo depends on NODE_UID, NODE_NAMESPACE
bool ExtractInfo(const envoy::api::v2::core::Node &node, LocalNode *args) {
auto &logger = Logger::Registry::getLog(Logger::Id::config);

const auto meta = node.metadata().fields();

if (meta.empty()) {
ENVOY_LOG_TO_LOGGER(logger, warn,
"ExtractInfo metadata empty:", node.DebugString());
return false;
}

std::string uid;
if (!ReadProtoMap(meta, kNodeUID, &uid)) {
ENVOY_LOG_TO_LOGGER(logger, warn,
"ExtractInfo metadata missing:", kNodeUID,
node.metadata().DebugString());
return false;
}

std::string ns;
if (!ReadProtoMap(meta, kNodeNamespace, &ns)) {
ENVOY_LOG_TO_LOGGER(logger, warn,
"ExtractInfo metadata missing:", kNodeNamespace,
node.metadata().DebugString());
return false;
}

args->ns = ns;
args->uid = uid;

return true;
}

bool ExtractNodeInfo(const envoy::api::v2::core::Node &node, LocalNode *args) {
if (ExtractInfo(node, args)) {
return true;
}
if (ExtractInfoCompat(node.id(), args)) {
return true;
}
return false;
}

} // namespace Utils
} // namespace Envoy
8 changes: 6 additions & 2 deletions src/envoy/utils/mixer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
#pragma once

#include "envoy/event/dispatcher.h"
#include "envoy/local_info/local_info.h"
#include "envoy/runtime/runtime.h"
#include "envoy/upstream/cluster_manager.h"
#include "include/istio/mixerclient/client.h"
#include "include/istio/utils/attribute_names.h"
#include "include/istio/utils/local_attributes.h"
#include "src/envoy/utils/config.h"

using ::istio::mixer::v1::Attributes;

namespace Envoy {
namespace Utils {

Expand All @@ -42,5 +43,8 @@ Grpc::AsyncClientFactoryPtr GrpcClientFactoryForCluster(
const std::string &cluster_name, Upstream::ClusterManager &cm,
Stats::Scope &scope);

bool ExtractNodeInfo(const envoy::api::v2::core::Node &node,
::istio::utils::LocalNode *args);

} // namespace Utils
} // namespace Envoy
Loading