diff --git a/src/envoy/BUILD b/src/envoy/BUILD index a67006d7fad..e905966a903 100644 --- a/src/envoy/BUILD +++ b/src/envoy/BUILD @@ -38,10 +38,7 @@ envoy_cc_binary( "//source/extensions/filters/network/metadata_exchange:config_lib", "//source/extensions/filters/network/sni_verifier:config_lib", "//source/extensions/filters/network/tcp_cluster_rewrite:config_lib", - "//src/envoy/http/baggage_handler:config_lib", # Experimental: Ambient - "//src/envoy/metadata_to_peer_node:config_lib", # Experimental: Ambient "//src/envoy/set_internal_dst_address:filter_lib", # Experimental: Ambient - "//src/envoy/workload_metadata:config_lib", # Experimental: Ambient "@envoy//source/exe:envoy_main_entry_lib", ], ) diff --git a/src/envoy/http/baggage_handler/BUILD b/src/envoy/http/baggage_handler/BUILD deleted file mode 100644 index db20c0f99df..00000000000 --- a/src/envoy/http/baggage_handler/BUILD +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright 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. -# -################################################################################ - -load( - "@envoy//bazel:envoy_build_system.bzl", - "envoy_cc_extension", - "envoy_cc_library", - "envoy_cc_test", - "envoy_extension_package", -) - -licenses(["notice"]) # Apache 2 - -envoy_extension_package() - -envoy_cc_library( - name = "baggage_handler_filter_lib", - srcs = ["baggage_handler.cc"], - hdrs = ["baggage_handler.h"], - repository = "@envoy", - visibility = ["//visibility:public"], - deps = [ - "//extensions/common:context", - "//extensions/common:metadata_object_lib", - "//src/envoy/http/baggage_handler/config:baggage_handler_cc_proto", - "@envoy//envoy/server:filter_config_interface", - "@envoy//source/common/http:header_utility_lib", - "@envoy//source/common/http:utility_lib", - "@envoy//source/common/router:string_accessor_lib", - ], -) - -envoy_cc_extension( - name = "config_lib", - srcs = ["config.cc"], - hdrs = ["config.h"], - repository = "@envoy", - visibility = ["//visibility:public"], - deps = [ - ":baggage_handler_filter_lib", - "//src/envoy/http/baggage_handler/config:baggage_handler_cc_proto", - "@envoy//envoy/registry", - "@envoy//envoy/server:filter_config_interface", - "@envoy//source/exe:envoy_common_lib", - ], -) - -envoy_cc_test( - name = "baggage_handler_test", - srcs = ["baggage_handler_test.cc"], - repository = "@envoy", - deps = [ - ":baggage_handler_filter_lib", - "//src/envoy/http/baggage_handler/config:baggage_handler_cc_proto", - "@envoy//source/common/network:socket_option_lib", - "@envoy//test/mocks:common_lib", - "@envoy//test/mocks/network:network_mocks", - "@envoy//test/mocks/server:server_mocks", - ], -) diff --git a/src/envoy/http/baggage_handler/baggage_handler.cc b/src/envoy/http/baggage_handler/baggage_handler.cc deleted file mode 100644 index 30b3f64ce84..00000000000 --- a/src/envoy/http/baggage_handler/baggage_handler.cc +++ /dev/null @@ -1,81 +0,0 @@ -/* Copyright 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 "src/envoy/http/baggage_handler/baggage_handler.h" - -#include "absl/strings/str_cat.h" -#include "source/common/http/header_utility.h" -#include "source/common/router/string_accessor_impl.h" -#include "src/envoy/http/baggage_handler/config/baggage_handler.pb.h" - -namespace Envoy { -namespace Http { -namespace BaggageHandler { - -Http::FilterHeadersStatus BaggageHandlerFilter::decodeHeaders( - Http::RequestHeaderMap& headers, bool) { - const auto header_value = Http::HeaderUtility::getAllOfHeaderAsString( - headers, LowerCaseString("baggage")) - .result(); - - if (header_value.has_value()) { - auto source_meta = std::make_shared( - Istio::Common::WorkloadMetadataObject::fromBaggage( - header_value.value())); - - auto filter_state = decoder_callbacks_->streamInfo().filterState(); - - filter_state->setData( - Istio::Common::kSourceMetadataObjectKey, source_meta, - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Request, - StreamInfo::FilterState::StreamSharing::SharedWithUpstreamConnection); - - ENVOY_LOG(trace, absl::StrCat("baggage header found. filter state set: ", - Istio::Common::kSourceMetadataObjectKey)); - - // Setting a StringAccessor filter state which can be assigned to custom - // header with PER_REQUEST_STATE - auto accessor = std::make_shared( - header_value.value()); - filter_state->setData( - Istio::Common::kSourceMetadataBaggageKey, accessor, - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Request, - StreamInfo::FilterState::StreamSharing::SharedWithUpstreamConnection); - } else { - ENVOY_LOG(trace, "no baggage header found."); - } - return Http::FilterHeadersStatus::Continue; -} - -void BaggageHandlerFilter::setDecoderFilterCallbacks( - Http::StreamDecoderFilterCallbacks& callbacks) { - decoder_callbacks_ = &callbacks; -} - -Http::FilterHeadersStatus BaggageHandlerFilter::encodeHeaders( - Http::ResponseHeaderMap&, bool) { - return Http::FilterHeadersStatus::Continue; -} - -void BaggageHandlerFilter::setEncoderFilterCallbacks( - Http::StreamEncoderFilterCallbacks& callbacks) { - encoder_callbacks_ = &callbacks; -} - -} // namespace BaggageHandler -} // namespace Http -} // namespace Envoy diff --git a/src/envoy/http/baggage_handler/baggage_handler.h b/src/envoy/http/baggage_handler/baggage_handler.h deleted file mode 100644 index 3fe0271753d..00000000000 --- a/src/envoy/http/baggage_handler/baggage_handler.h +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright 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. - */ - -#pragma once - -#include "envoy/server/filter_config.h" -#include "extensions/common/context.h" -#include "extensions/common/metadata_object.h" -#include "src/envoy/http/baggage_handler/config/baggage_handler.pb.h" - -namespace Envoy { -namespace Http { -namespace BaggageHandler { - -class Config { - public: - Config(const istio::telemetry::baggagehandler::v1::Config&){}; -}; - -using ConfigSharedPtr = std::shared_ptr; - -class BaggageHandlerFilter : public Http::StreamFilter, - public Logger::Loggable { - public: - BaggageHandlerFilter(const ConfigSharedPtr& config) : config_(config) {} - ~BaggageHandlerFilter() = default; - - // Http::StreamFilterBase - void onDestroy() override {} - - // StreamDecoderFilter - Http::FilterHeadersStatus decodeHeaders(Http::RequestHeaderMap& headers, - bool) override; - Http::FilterDataStatus decodeData(Buffer::Instance&, bool) override { - return Http::FilterDataStatus::Continue; - } - Http::FilterTrailersStatus decodeTrailers(Http::RequestTrailerMap&) override { - return Http::FilterTrailersStatus::Continue; - } - void setDecoderFilterCallbacks( - Http::StreamDecoderFilterCallbacks& callbacks) override; - - // StreamEncoderFilter - Http::FilterHeadersStatus encode1xxHeaders( - Http::ResponseHeaderMap&) override { - return Http::FilterHeadersStatus::Continue; - } - Http::FilterHeadersStatus encodeHeaders(Http::ResponseHeaderMap& headers, - bool) override; - Http::FilterDataStatus encodeData(Buffer::Instance&, bool) override { - return Http::FilterDataStatus::Continue; - } - Http::FilterTrailersStatus encodeTrailers( - Http::ResponseTrailerMap&) override { - return Http::FilterTrailersStatus::Continue; - } - Http::FilterMetadataStatus encodeMetadata(Http::MetadataMap&) override { - return Http::FilterMetadataStatus::Continue; - } - void setEncoderFilterCallbacks( - Http::StreamEncoderFilterCallbacks& callbacks) override; - - private: - const ConfigSharedPtr config_; - Http::StreamDecoderFilterCallbacks* decoder_callbacks_{}; - Http::StreamEncoderFilterCallbacks* encoder_callbacks_{}; -}; - -} // namespace BaggageHandler -} // namespace Http -} // namespace Envoy diff --git a/src/envoy/http/baggage_handler/baggage_handler_test.cc b/src/envoy/http/baggage_handler/baggage_handler_test.cc deleted file mode 100644 index 1bf9525b01b..00000000000 --- a/src/envoy/http/baggage_handler/baggage_handler_test.cc +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 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 "baggage_handler.h" - -#include "extensions/common/metadata_object.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "source/common/http/header_map_impl.h" -#include "source/common/protobuf/protobuf.h" -#include "src/envoy/http/baggage_handler/config/baggage_handler.pb.h" -#include "test/mocks/http/mocks.h" -#include "test/mocks/network/mocks.h" -// #include "test/mocks/ssl/mocks.h" -#include "test/mocks/stream_info/mocks.h" -#include "test/test_common/utility.h" - -using testing::_; -using testing::NiceMock; -using testing::Return; -using testing::ReturnRef; - -namespace Envoy { -namespace Http { -namespace BaggageHandler { - -class BaggageHandlerFilterTest : public testing::Test { - public: - BaggageHandlerFilterTest() { ENVOY_LOG_MISC(info, "test"); } - - public: - void initializeFilter() { - istio::telemetry::baggagehandler::v1::Config config; - config_ = std::make_shared(config); - filter_ = std::make_shared(config_); - - filter_state_ = std::make_shared( - StreamInfo::FilterState::LifeSpan::Request); - - ON_CALL(decoder_callbacks_.stream_info_, filterState()) - .WillByDefault(::testing::ReturnRef(filter_state_)); - - auto connRef = - OptRef>{connection_}; - ON_CALL(decoder_callbacks_, connection()) - .WillByDefault( - ::testing::Return(OptRef{connection_})); - - filter_->setDecoderFilterCallbacks(decoder_callbacks_); - } - - ConfigSharedPtr config_; - std::shared_ptr filter_; - std::shared_ptr filter_state_; - NiceMock decoder_callbacks_; - NiceMock stream_info_; - NiceMock connection_; -}; - -TEST_F(BaggageHandlerFilterTest, BasicBaggageTest) { - initializeFilter(); - - auto baggage = absl::StrCat( - "k8s.namespace.name=default,k8s.deployment.name=foo,service.name=", - "foo-service,service.version=v1alpha3"); - Http::TestRequestHeaderMapImpl incoming_headers{{"baggage", baggage}}; - - EXPECT_EQ(Http::FilterHeadersStatus::Continue, - filter_->decodeHeaders(incoming_headers, false)); - - EXPECT_TRUE( - filter_state_->hasDataWithName(Istio::Common::kSourceMetadataObjectKey)); - auto found = - filter_state_->getDataReadOnly( - Istio::Common::kSourceMetadataObjectKey); - - EXPECT_EQ(found->canonical_name_, "foo-service"); - - filter_->onDestroy(); -} - -} // namespace BaggageHandler -} // namespace Http -} // namespace Envoy diff --git a/src/envoy/http/baggage_handler/config.cc b/src/envoy/http/baggage_handler/config.cc deleted file mode 100644 index a97df8e6773..00000000000 --- a/src/envoy/http/baggage_handler/config.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright 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 "src/envoy/http/baggage_handler/config.h" - -#include "source/common/protobuf/message_validator_impl.h" -#include "src/envoy/http/baggage_handler/baggage_handler.h" -#include "src/envoy/http/baggage_handler/config/baggage_handler.pb.h" - -namespace Envoy { -namespace Http { -namespace BaggageHandler { - -Http::FilterFactoryCb BaggageHandlerConfigFactory::createFilterFactoryFromProto( - const Protobuf::Message &config, const std::string &, - Server::Configuration::FactoryContext &) { - return createFilterFactory( - dynamic_cast( - config)); -} - -ProtobufTypes::MessagePtr -BaggageHandlerConfigFactory::createEmptyConfigProto() { - return ProtobufTypes::MessagePtr{ - new istio::telemetry::baggagehandler::v1::Config}; -} - -std::string BaggageHandlerConfigFactory::name() const { - return "istio.filters.http.baggage_handler"; -} - -Http::FilterFactoryCb BaggageHandlerConfigFactory::createFilterFactory( - const istio::telemetry::baggagehandler::v1::Config &proto_config) { - ConfigSharedPtr filter_config(std::make_shared(proto_config)); - return [filter_config](Http::FilterChainFactoryCallbacks &callbacks) -> void { - callbacks.addStreamFilter( - Http::StreamFilterSharedPtr{new BaggageHandlerFilter(filter_config)}); - }; -} - -/** - * Static registration for the baggage handler filter. @see RegisterFactory. - */ -REGISTER_FACTORY(BaggageHandlerConfigFactory, - Server::Configuration::NamedHttpFilterConfigFactory); - -} // namespace BaggageHandler -} // namespace Http -} // namespace Envoy diff --git a/src/envoy/http/baggage_handler/config.h b/src/envoy/http/baggage_handler/config.h deleted file mode 100644 index ed0b0b58836..00000000000 --- a/src/envoy/http/baggage_handler/config.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright 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. - */ - -#pragma once - -#include "source/extensions/filters/http/common/factory_base.h" -#include "src/envoy/http/baggage_handler/config/baggage_handler.pb.h" - -namespace Envoy { -namespace Http { -namespace BaggageHandler { - -/** - * Config registration for the baggage handler filter. - */ -class BaggageHandlerConfigFactory - : public Server::Configuration::NamedHttpFilterConfigFactory { - public: - // Server::Configuration::NamedHttpFilterConfigFactory - Http::FilterFactoryCb createFilterFactoryFromProto( - const Protobuf::Message &config, const std::string &stat_prefix, - Server::Configuration::FactoryContext &context) override; - - ProtobufTypes::MessagePtr createEmptyConfigProto() override; - - std::string name() const override; - - private: - Http::FilterFactoryCb createFilterFactory( - const istio::telemetry::baggagehandler::v1::Config &config_pb); -}; - -} // namespace BaggageHandler -} // namespace Http -} // namespace Envoy diff --git a/src/envoy/http/baggage_handler/config/BUILD b/src/envoy/http/baggage_handler/config/BUILD deleted file mode 100644 index 34b3d42ce98..00000000000 --- a/src/envoy/http/baggage_handler/config/BUILD +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 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. -# -################################################################################ - -package(default_visibility = ["//visibility:public"]) - -cc_proto_library( - name = "baggage_handler_cc_proto", - deps = ["baggage_handler_proto"], -) - -proto_library( - name = "baggage_handler_proto", - srcs = ["baggage_handler.proto"], -) diff --git a/src/envoy/http/baggage_handler/config/baggage_handler.proto b/src/envoy/http/baggage_handler/config/baggage_handler.proto deleted file mode 100644 index 60eb130eca3..00000000000 --- a/src/envoy/http/baggage_handler/config/baggage_handler.proto +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 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 istio.telemetry.baggagehandler.v1; - -option go_package = "istio.io/istio/pkg/baggagehandler/proto/istio_telemetry_baggagehandler_v1"; - -// Configuration for the baggage handler filter. -message Config {} diff --git a/src/envoy/metadata_to_peer_node/BUILD b/src/envoy/metadata_to_peer_node/BUILD deleted file mode 100644 index 383edd14d18..00000000000 --- a/src/envoy/metadata_to_peer_node/BUILD +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright 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. -# -################################################################################ - -load( - "@envoy//bazel:envoy_build_system.bzl", - "envoy_cc_extension", - "envoy_cc_library", - "envoy_cc_test", - "envoy_extension_package", -) - -licenses(["notice"]) # Apache 2 - -envoy_extension_package() - -envoy_cc_library( - name = "metadata_to_peer_node_lib", - srcs = ["metadata_to_peer_node.cc"], - hdrs = ["metadata_to_peer_node.h"], - repository = "@envoy", - visibility = ["//visibility:public"], - deps = [ - "//extensions/common:context", - "//extensions/common:metadata_object_lib", - "//src/envoy/metadata_to_peer_node/config:metadata_to_peer_node_cc_proto", - "@envoy//envoy/network:filter_interface", - "@envoy//envoy/network:listen_socket_interface", - "@envoy//source/common/network:address_lib", - "@envoy//source/common/network:utility_lib", - "@envoy//source/exe:envoy_common_lib", - "@envoy//source/extensions/filters/common/expr:cel_state_lib", - ], -) - -envoy_cc_test( - name = "metadata_to_peer_node_test", - srcs = ["metadata_to_peer_node_test.cc"], - repository = "@envoy", - deps = [ - ":metadata_to_peer_node_lib", - "//src/envoy/metadata_to_peer_node/config:metadata_to_peer_node_cc_proto", - "@envoy//source/common/network:socket_option_lib", - "@envoy//test/mocks:common_lib", - "@envoy//test/mocks/network:network_mocks", - "@envoy//test/mocks/stats:stats_mocks", - ], -) - -envoy_cc_extension( - name = "config_lib", - srcs = ["config.cc"], - repository = "@envoy", - deps = [ - ":metadata_to_peer_node_lib", - "//src/envoy/metadata_to_peer_node/config:metadata_to_peer_node_cc_proto", - "@envoy//envoy/registry", - "@envoy//envoy/server:filter_config_interface", - "@envoy//source/exe:envoy_common_lib", - ], -) diff --git a/src/envoy/metadata_to_peer_node/config.cc b/src/envoy/metadata_to_peer_node/config.cc deleted file mode 100644 index 55826653f77..00000000000 --- a/src/envoy/metadata_to_peer_node/config.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 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 - -#include "envoy/registry/registry.h" -#include "envoy/server/filter_config.h" -#include "src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.pb.h" -#include "src/envoy/metadata_to_peer_node/metadata_to_peer_node.h" - -namespace Envoy { -namespace MetadataToPeerNode { - -namespace { -constexpr absl::string_view kFactoryName = - "envoy.filters.listener.metadata_to_peer_node"; -} -/** - * Config registration for the metadata to peer node filter. @see - * NamedNetworkFilterConfigFactory. - */ -class MetadataToPeerNodeConfigFactory - : public Server::Configuration::NamedListenerFilterConfigFactory { - public: - // NamedListenerFilterConfigFactory - Network::ListenerFilterFactoryCb createListenerFilterFactoryFromProto( - const Protobuf::Message& message, - const Network::ListenerFilterMatcherSharedPtr& listener_filter_matcher, - Server::Configuration::ListenerFactoryContext&) override { - // downcast it to the workload metadata config - const auto& typed_config = - dynamic_cast( - message); - - ConfigSharedPtr config = std::make_shared(typed_config); - return [listener_filter_matcher, - config](Network::ListenerFilterManager& filter_manager) -> void { - filter_manager.addAcceptFilter(listener_filter_matcher, - std::make_unique(config)); - }; - } - - ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); - } - - std::string name() const override { return std::string(kFactoryName); } -}; - -/** - * Static registration for the workload metadata filter. @see RegisterFactory. - */ -REGISTER_FACTORY(MetadataToPeerNodeConfigFactory, - Server::Configuration::NamedListenerFilterConfigFactory); - -} // namespace MetadataToPeerNode -} // namespace Envoy diff --git a/src/envoy/metadata_to_peer_node/config/BUILD b/src/envoy/metadata_to_peer_node/config/BUILD deleted file mode 100644 index 0953426c987..00000000000 --- a/src/envoy/metadata_to_peer_node/config/BUILD +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 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. -# -################################################################################ - -package(default_visibility = ["//visibility:public"]) - -cc_proto_library( - name = "metadata_to_peer_node_cc_proto", - deps = ["metadata_to_peer_node_proto"], -) - -proto_library( - name = "metadata_to_peer_node_proto", - srcs = ["metadata_to_peer_node.proto"], -) diff --git a/src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.proto b/src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.proto deleted file mode 100644 index 4a2a96e8804..00000000000 --- a/src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.proto +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 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 istio.telemetry.metadatatopeernode.v1; - -option go_package = "istio.io/istio/pkg/metadatatopeernode/proto/istio_telemetry_metadatatopeernode_v1"; - -// Configuration for the metadata_to_peer_node filter. -message Config {} diff --git a/src/envoy/metadata_to_peer_node/metadata_to_peer_node.cc b/src/envoy/metadata_to_peer_node/metadata_to_peer_node.cc deleted file mode 100644 index 84544dee812..00000000000 --- a/src/envoy/metadata_to_peer_node/metadata_to_peer_node.cc +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 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 "metadata_to_peer_node.h" - -#include "absl/strings/str_cat.h" -#include "envoy/network/listen_socket.h" -#include "envoy/stats/scope.h" -#include "envoy/stream_info/filter_state.h" -#include "extensions/common/context.h" -#include "extensions/common/metadata_object.h" -#include "extensions/common/util.h" - -using Envoy::Extensions::Filters::Common::Expr::CelState; -using Istio::Common::WorkloadMetadataObject; - -namespace Envoy { -namespace MetadataToPeerNode { - -Network::FilterStatus Filter::onAccept(Network::ListenerFilterCallbacks& cb) { - ENVOY_LOG(trace, "metadata to peer: new connection accepted"); - - StreamInfo::FilterState& filter_state = cb.filterState(); - - const WorkloadMetadataObject* meta_obj = - filter_state.getDataReadOnly( - Istio::Common::kSourceMetadataObjectKey); - - if (meta_obj == nullptr) { - ENVOY_LOG(trace, "metadata to peer: no metadata object found"); - return Network::FilterStatus::Continue; - } - - // set the peer ID to the the key we want, then set the key with the FBB - auto peer_id_state = std::make_unique(Config::nodeIdPrototype()); - peer_id_state->setValue("connect_peer"); - filter_state.setData( - absl::StrCat("wasm.", - toAbslStringView(Wasm::Common::kDownstreamMetadataIdKey)), - std::move(peer_id_state), StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Connection); - - const auto fb = Istio::Common::convertWorkloadMetadataToFlatNode(*meta_obj); - auto peer_state = std::make_unique(Config::nodeInfoPrototype()); - peer_state->setValue( - absl::string_view(reinterpret_cast(fb.data()), fb.size())); - - auto key = absl::StrCat( - "wasm.", toAbslStringView(Wasm::Common::kDownstreamMetadataKey)); - filter_state.setData(key, std::move(peer_state), - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Connection); - - ENVOY_LOG( - trace, - absl::StrCat( - "metadata to peer: peer node set to filter state with key = ", key)); - - return Network::FilterStatus::Continue; -} - -Network::FilterStatus Filter::onData(Network::ListenerFilterBuffer&) { - return Network::FilterStatus::Continue; -} - -size_t Filter::maxReadBytes() const { return 0; } - -} // namespace MetadataToPeerNode -} // namespace Envoy diff --git a/src/envoy/metadata_to_peer_node/metadata_to_peer_node.h b/src/envoy/metadata_to_peer_node/metadata_to_peer_node.h deleted file mode 100644 index e47cc83c015..00000000000 --- a/src/envoy/metadata_to_peer_node/metadata_to_peer_node.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 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. - -#pragma once - -#include "envoy/network/filter.h" -#include "envoy/stats/scope.h" -#include "extensions/common/context.h" -#include "source/common/common/logger.h" -#include "source/common/common/stl_helpers.h" -#include "source/extensions/filters/common/expr/cel_state.h" -#include "src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.pb.h" - -using namespace istio::telemetry::metadatatopeernode; -using ::Envoy::Extensions::Filters::Common::Expr::CelStatePrototype; -using ::Envoy::Extensions::Filters::Common::Expr::CelStateType; - -namespace Envoy { -namespace MetadataToPeerNode { - -/** - * Global configuration for Metadata To Peer Node listener filter. - */ -class Config : public Logger::Loggable { - public: - Config(const v1::Config&){}; - - static const CelStatePrototype& nodeInfoPrototype() { - static const CelStatePrototype* const prototype = new CelStatePrototype( - true, CelStateType::FlatBuffers, - Envoy::toAbslStringView(Wasm::Common::nodeInfoSchema()), - StreamInfo::FilterState::LifeSpan::Request); - return *prototype; - } - - static const CelStatePrototype& nodeIdPrototype() { - static const CelStatePrototype* const prototype = - new CelStatePrototype(true, CelStateType::String, absl::string_view(), - StreamInfo::FilterState::LifeSpan::Request); - return *prototype; - } -}; - -using ConfigSharedPtr = std::shared_ptr; - -/** - * Metadata To Peer Node listener filter. - */ -class Filter : public Network::ListenerFilter, - Logger::Loggable { - public: - Filter(const ConfigSharedPtr& config) : config_(config) {} - - // Network::ListenerFilter - Network::FilterStatus onAccept(Network::ListenerFilterCallbacks& cb) override; - - Network::FilterStatus onData(Network::ListenerFilterBuffer&) override; - - size_t maxReadBytes() const override; - - private: - ConfigSharedPtr config_; -}; - -} // namespace MetadataToPeerNode -} // namespace Envoy diff --git a/src/envoy/metadata_to_peer_node/metadata_to_peer_node_test.cc b/src/envoy/metadata_to_peer_node/metadata_to_peer_node_test.cc deleted file mode 100644 index b657774526f..00000000000 --- a/src/envoy/metadata_to_peer_node/metadata_to_peer_node_test.cc +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright 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 "metadata_to_peer_node.h" - -#include "absl/strings/str_cat.h" -#include "extensions/common/metadata_object.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "source/common/protobuf/protobuf.h" -#include "src/envoy/metadata_to_peer_node/config/metadata_to_peer_node.pb.h" -#include "test/mocks/network/mocks.h" -#include "test/mocks/stream_info/mocks.h" -#include "test/test_common/utility.h" - -using Envoy::Extensions::Filters::Common::Expr::CelState; -using Istio::Common::WorkloadMetadataObject; -using testing::_; -using testing::NiceMock; -using testing::Return; -using testing::ReturnRef; - -namespace Envoy { -namespace MetadataToPeerNode { - -class MetadataToPeerNodeFilterTest : public testing::Test { - public: - MetadataToPeerNodeFilterTest() { ENVOY_LOG_MISC(info, "test"); } - - public: - void initializeFilter() { - istio::telemetry::metadatatopeernode::v1::Config proto_config; - config_ = std::make_shared(proto_config); - filter_ = std::make_shared(config_); - - filter_state_ = std::make_shared( - StreamInfo::FilterState::LifeSpan::Request); - - ON_CALL(callbacks_, filterState()) - .WillByDefault(Invoke( - [&]() -> StreamInfo::FilterState& { return *filter_state_; })); - } - - ConfigSharedPtr config_; - std::shared_ptr filter_; - std::shared_ptr filter_state_; - - NiceMock callbacks_; - NiceMock stream_info_; -}; - -TEST_F(MetadataToPeerNodeFilterTest, SetPeerInfoTest) { - initializeFilter(); - auto baggage = - absl::StrCat("k8s.cluster.name=foo-cluster,k8s.namespace.name=default,", - "k8s.deployment.name=foo-deploy,service.name=foo-service,", - "service.version=v1alpha3"); - auto obj = std::make_shared( - WorkloadMetadataObject::fromBaggage(baggage)); - - filter_state_->setData(Istio::Common::kSourceMetadataObjectKey, obj, - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Request); - - EXPECT_EQ(filter_->onAccept(callbacks_), Network::FilterStatus::Continue); - - auto peer_id_key = absl::StrCat( - "wasm.", toAbslStringView(Wasm::Common::kDownstreamMetadataIdKey)); - EXPECT_TRUE(filter_state_->hasDataWithName(peer_id_key)); - - auto id = filter_state_->getDataReadOnly(peer_id_key); - EXPECT_EQ("connect_peer", id->value()); - - auto peer_fbb_key = absl::StrCat( - "wasm.", toAbslStringView(Wasm::Common::kDownstreamMetadataKey)); - EXPECT_TRUE(filter_state_->hasDataWithName(peer_fbb_key)); - - auto peer_fbb_cel = filter_state_->getDataReadOnly(peer_fbb_key); - absl::string_view fbb_value = peer_fbb_cel->value(); - - auto& peer = *flatbuffers::GetRoot<::Wasm::Common::FlatNode>( - reinterpret_cast(fbb_value.data())); - - EXPECT_EQ(peer.namespace_()->string_view(), "default"); - EXPECT_EQ(peer.workload_name()->string_view(), "foo-deploy"); - EXPECT_EQ(peer.cluster_id()->string_view(), "foo-cluster"); - - auto peer_labels = peer.labels(); - auto canonical_name = peer_labels->LookupByKey( - ::Wasm::Common::kCanonicalServiceLabelName.data()); - auto canonical_rev = peer_labels->LookupByKey( - ::Wasm::Common::kCanonicalServiceRevisionLabelName.data()); - EXPECT_EQ(canonical_name->value()->string_view(), "foo-service"); - EXPECT_EQ(canonical_rev->value()->string_view(), "v1alpha3"); -} - -TEST_F(MetadataToPeerNodeFilterTest, SetPeerInfoNoClusterTest) { - initializeFilter(); - auto baggage = - absl::StrCat("k8s.namespace.name=default,k8s.deployment.name=foo-deploy,", - "service.name=foo-service,service.version=v1alpha3"); - auto obj = std::make_shared( - WorkloadMetadataObject::fromBaggage(baggage)); - - filter_state_->setData(Istio::Common::kSourceMetadataObjectKey, obj, - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Request); - - EXPECT_EQ(filter_->onAccept(callbacks_), Network::FilterStatus::Continue); - - auto peer_id_key = absl::StrCat( - "wasm.", toAbslStringView(Wasm::Common::kDownstreamMetadataIdKey)); - EXPECT_TRUE(filter_state_->hasDataWithName(peer_id_key)); - - auto id = filter_state_->getDataReadOnly(peer_id_key); - EXPECT_EQ("connect_peer", id->value()); - - auto peer_fbb_key = absl::StrCat( - "wasm.", toAbslStringView(Wasm::Common::kDownstreamMetadataKey)); - EXPECT_TRUE(filter_state_->hasDataWithName(peer_fbb_key)); - - auto peer_fbb_cel = filter_state_->getDataReadOnly(peer_fbb_key); - absl::string_view fbb_value = peer_fbb_cel->value(); - - auto& peer = *flatbuffers::GetRoot<::Wasm::Common::FlatNode>( - reinterpret_cast(fbb_value.data())); - - EXPECT_EQ(peer.namespace_()->string_view(), "default"); - EXPECT_EQ(peer.workload_name()->string_view(), "foo-deploy"); - EXPECT_EQ(peer.cluster_id()->string_view(), ""); -} - -} // namespace MetadataToPeerNode -} // namespace Envoy diff --git a/src/envoy/workload_metadata/BUILD b/src/envoy/workload_metadata/BUILD deleted file mode 100644 index cab3842a15e..00000000000 --- a/src/envoy/workload_metadata/BUILD +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright 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. -# -################################################################################ - -load( - "@envoy//bazel:envoy_build_system.bzl", - "envoy_cc_extension", - "envoy_cc_library", - "envoy_cc_test", - "envoy_extension_package", -) - -licenses(["notice"]) # Apache 2 - -envoy_extension_package() - -envoy_cc_library( - name = "workload_metadata_lib", - srcs = ["workload_metadata.cc"], - hdrs = ["workload_metadata.h"], - repository = "@envoy", - visibility = ["//visibility:public"], - deps = [ - "//extensions/common:metadata_object_lib", - "//src/envoy/workload_metadata/config:workload_metadata_cc_proto", - "@envoy//envoy/network:filter_interface", - "@envoy//envoy/network:listen_socket_interface", - "@envoy//source/common/network:address_lib", - "@envoy//source/common/network:utility_lib", - "@envoy//source/common/router:string_accessor_lib", - "@envoy//source/exe:envoy_common_lib", - ], -) - -envoy_cc_test( - name = "workload_metadata_test", - srcs = ["workload_metadata_test.cc"], - repository = "@envoy", - deps = [ - ":workload_metadata_lib", - "//src/envoy/workload_metadata/config:workload_metadata_cc_proto", - "@envoy//source/common/network:socket_option_lib", - "@envoy//source/common/router:string_accessor_lib", - "@envoy//test/mocks:common_lib", - "@envoy//test/mocks/network:network_mocks", - "@envoy//test/mocks/stats:stats_mocks", - ], -) - -envoy_cc_extension( - name = "config_lib", - srcs = ["config.cc"], - repository = "@envoy", - deps = [ - ":workload_metadata_lib", - "//src/envoy/workload_metadata/config:workload_metadata_cc_proto", - "@envoy//envoy/registry", - "@envoy//envoy/server:filter_config_interface", - "@envoy//source/exe:envoy_common_lib", - ], -) diff --git a/src/envoy/workload_metadata/config.cc b/src/envoy/workload_metadata/config.cc deleted file mode 100644 index c76ceaba19a..00000000000 --- a/src/envoy/workload_metadata/config.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 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 - -#include "envoy/registry/registry.h" -#include "envoy/server/filter_config.h" -#include "src/envoy/workload_metadata/config/workload_metadata.pb.h" -#include "src/envoy/workload_metadata/workload_metadata.h" - -namespace Envoy { -namespace WorkloadMetadata { - -namespace { -constexpr absl::string_view kFactoryName = - "envoy.filters.listener.workload_metadata"; - -constexpr char kClusterID[] = "CLUSTER_ID"; -// TODO: should this just be blank? -constexpr char kDefaultClusterID[] = "Kubernetes"; -} // namespace -/** - * Config registration for the workload metadata filter. @see - * NamedNetworkFilterConfigFactory. - */ -class WorkloadMetadataConfigFactory - : public Server::Configuration::NamedListenerFilterConfigFactory { - public: - // NamedListenerFilterConfigFactory - Network::ListenerFilterFactoryCb createListenerFilterFactoryFromProto( - const Protobuf::Message& message, - const Network::ListenerFilterMatcherSharedPtr& listener_filter_matcher, - Server::Configuration::ListenerFactoryContext& context) override { - // downcast it to the workload metadata config - - auto node = context.localInfo().node(); - auto node_meta = node.metadata(); - auto cluster_name = node_meta.fields().contains(kClusterID) - ? node_meta.fields().at(kClusterID).string_value() - : kDefaultClusterID; - - const auto& typed_config = - dynamic_cast(message); - - ConfigSharedPtr config = - std::make_shared(context.scope(), cluster_name, typed_config); - return [listener_filter_matcher, - config](Network::ListenerFilterManager& filter_manager) -> void { - filter_manager.addAcceptFilter(listener_filter_matcher, - std::make_unique(config)); - }; - } - - ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique< - istio::telemetry::workloadmetadata::v1::WorkloadMetadataResources>(); - } - - std::string name() const override { return std::string(kFactoryName); } -}; - -/** - * Static registration for the workload metadata filter. @see RegisterFactory. - */ -REGISTER_FACTORY(WorkloadMetadataConfigFactory, - Server::Configuration::NamedListenerFilterConfigFactory); - -} // namespace WorkloadMetadata -} // namespace Envoy diff --git a/src/envoy/workload_metadata/config/BUILD b/src/envoy/workload_metadata/config/BUILD deleted file mode 100644 index 3fca7dafd9c..00000000000 --- a/src/envoy/workload_metadata/config/BUILD +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 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. -# -################################################################################ - -package(default_visibility = ["//visibility:public"]) - -cc_proto_library( - name = "workload_metadata_cc_proto", - deps = ["workload_metadata_proto"], -) - -proto_library( - name = "workload_metadata_proto", - srcs = ["workload_metadata.proto"], -) diff --git a/src/envoy/workload_metadata/config/workload_metadata.proto b/src/envoy/workload_metadata/config/workload_metadata.proto deleted file mode 100644 index 11873145917..00000000000 --- a/src/envoy/workload_metadata/config/workload_metadata.proto +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 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 istio.telemetry.workloadmetadata.v1; - -option go_package = "istio.io/istio/pkg/workloadmetadata/proto/istio_telemetry_workloadmetadata_v1"; - -// WorkloadMetadataResources contains a list of workloads and their metadata for -// an Ambient L4 proxy (aka per-Kubernetes-node proxy). This will be sent in -// an xDS response message via ECDS. -message WorkloadMetadataResources { - // Provides the xDS node identifier for the ASM proxy that corresponds to the - // `workload_resources` returned in a response. - string proxy_id = 1; - - // Contains a set of workload metadata for all workload instances that - // are currently being proxied by the xDS node. - repeated WorkloadMetadataResource workload_metadata_resources = 2; -} - -// Contains the metadata for a single workload instance. -message WorkloadMetadataResource { - // Set of IP addresses associated with an individual workload instance. - repeated string ip_addresses = 1; - - // The full name of the workload instance. - string instance_name = 2; - - // The Kubernetes namespace to which the workload instance belongs. - string namespace_name = 3; - - // The set of containers (if known) that constitute the workload instance. - repeated string containers = 4; - - // The name of the workload provided by the instance. This is typically the - // Kubernetes deployment name. - string workload_name = 5; - - enum WorkloadType { - KUBERNETES_DEPLOYMENT = 0; - KUBERNETES_CRONJOB = 1; - KUBERNETES_POD = 2; - KUBERNETES_JOB = 3; - } - - // Type of workload - WorkloadType workload_type = 6; - - // Canonical name of the workload - string canonical_name = 7; - - // Canonical revision of the workload - string canonical_revision = 8; -} diff --git a/src/envoy/workload_metadata/workload_metadata.cc b/src/envoy/workload_metadata/workload_metadata.cc deleted file mode 100644 index 9024d9fe357..00000000000 --- a/src/envoy/workload_metadata/workload_metadata.cc +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 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 "workload_metadata.h" - -#include "envoy/network/listen_socket.h" -#include "envoy/stats/scope.h" -#include "envoy/stream_info/filter_state.h" -#include "source/common/router/string_accessor_impl.h" - -using Istio::Common::WorkloadMetadataObject; -using Istio::Common::WorkloadType; - -namespace Envoy { -namespace WorkloadMetadata { - -Config::Config(Stats::Scope& scope, const std::string& cluster_name, - const v1::WorkloadMetadataResources& proto_config) - : stats_{ALL_WORKLOAD_METADATA_STATS( - POOL_COUNTER_PREFIX(scope, "workload_metadata."))}, - cluster_name_(cluster_name) { - // TODO: update config counter - for (const auto& resource : proto_config.workload_metadata_resources()) { - WorkloadType workload_type = WorkloadType::Pod; - switch (resource.workload_type()) { - case v1::WorkloadMetadataResource_WorkloadType_KUBERNETES_DEPLOYMENT: - workload_type = WorkloadType::Deployment; - break; - case v1::WorkloadMetadataResource_WorkloadType_KUBERNETES_CRONJOB: - workload_type = WorkloadType::CronJob; - break; - case v1::WorkloadMetadataResource_WorkloadType_KUBERNETES_POD: - workload_type = WorkloadType::Pod; - break; - case v1::WorkloadMetadataResource_WorkloadType_KUBERNETES_JOB: - workload_type = WorkloadType::Job; - break; - default: - break; - } - WorkloadMetadataObject workload( - resource.instance_name(), cluster_name_, resource.namespace_name(), - resource.workload_name(), resource.canonical_name(), - resource.canonical_revision(), "", "", workload_type); - - for (const auto& ip_addr : resource.ip_addresses()) { - workloads_by_ips_[ip_addr] = - std::make_shared(workload); - } - } -} - -std::shared_ptr Config::metadata( - const std::string& ip_addr) { - auto workload_meta = workloads_by_ips_.find(ip_addr); - if (workload_meta != workloads_by_ips_.end()) { - return workload_meta->second; - } - - return nullptr; -} - -Network::FilterStatus Filter::onAccept(Network::ListenerFilterCallbacks& cb) { - ENVOY_LOG(debug, "workload metadata: new connection accepted"); - - const Network::ConnectionSocket& socket = cb.socket(); - auto remote_ip = - socket.connectionInfoProvider().remoteAddress()->ip()->addressAsString(); - - ENVOY_LOG(trace, "workload metadata: looking up metadata for ip {}", - remote_ip); - - // TODO: handle not found differently??? - auto metadata = config_->metadata(remote_ip); - if (!metadata) { - ENVOY_LOG(trace, "workload metadata: no metadata found for {}", remote_ip); - return Network::FilterStatus::Continue; - } - - ENVOY_LOG(trace, "workload metadata: found metadata for {}", - metadata->workload_name_); - - // Setting a StringAccessor filter state with the baggage string which can be - // assigned to custom header with PER_REQUEST_STATE. - // We set this filter state in addition to the dynamic metadata to cover - // cases where the dynamic metadata can not be passed through (e.g. when - // traffic goes through an internal lisener). - auto accessor = - std::make_shared(metadata->baggage()); - StreamInfo::FilterState& filter_state = cb.filterState(); - filter_state.setData( - Istio::Common::kSourceMetadataBaggageKey, accessor, - StreamInfo::FilterState::StateType::ReadOnly, - StreamInfo::FilterState::LifeSpan::Request, - StreamInfo::FilterState::StreamSharing::SharedWithUpstreamConnection); - - ProtobufWkt::Struct dynamic_meta; - auto& mutable_fields = *dynamic_meta.mutable_fields(); - mutable_fields[DynamicMetadataKeysSingleton::get().Baggage].set_string_value( - std::string(metadata->baggage())); - cb.setDynamicMetadata(DynamicMetadataKeysSingleton::get().FilterNamespace, - dynamic_meta); - return Network::FilterStatus::Continue; -} - -Network::FilterStatus Filter::onData(Network::ListenerFilterBuffer&) { - return Network::FilterStatus::Continue; -} - -size_t Filter::maxReadBytes() const { return 0; } - -} // namespace WorkloadMetadata -} // namespace Envoy diff --git a/src/envoy/workload_metadata/workload_metadata.h b/src/envoy/workload_metadata/workload_metadata.h deleted file mode 100644 index 49a636860db..00000000000 --- a/src/envoy/workload_metadata/workload_metadata.h +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 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. - -#pragma once - -#include "envoy/common/optref.h" -#include "envoy/network/filter.h" -#include "envoy/stats/scope.h" -#include "envoy/stats/stats_macros.h" -#include "extensions/common/metadata_object.h" -#include "source/common/common/logger.h" -#include "src/envoy/workload_metadata/config/workload_metadata.pb.h" - -using namespace istio::telemetry::workloadmetadata; -using Istio::Common::WorkloadMetadataObject; - -namespace Envoy { -namespace WorkloadMetadata { - -/** - * All stats for the workload metadata filter. @see stats_macros.h - */ -#define ALL_WORKLOAD_METADATA_STATS(COUNTER) \ - COUNTER(config_error) \ - COUNTER(config_updates) - -/** - * Definition of all stats for the Workload Metadata filter. @see stats_macros.h - */ -struct WorkloadMetadataStats { - ALL_WORKLOAD_METADATA_STATS(GENERATE_COUNTER_STRUCT) -}; - -/** - * Definition of keys in the dynamic metadata to store baggage in - */ -class DynamicMetadataKeys { - public: - const std::string FilterNamespace{"envoy.filters.listener.workload_metadata"}; - const std::string Baggage{"baggage"}; -}; - -using DynamicMetadataKeysSingleton = ConstSingleton; - -/** - * Global configuration for Workload Metadata listener filter. - */ -class Config : public Logger::Loggable { - public: - Config(Stats::Scope& scope, const std::string& cluster_name, - const v1::WorkloadMetadataResources& proto_config); - - const WorkloadMetadataStats& stats() const { return stats_; } - - std::shared_ptr metadata(const std::string& ip_addr); - - private: - WorkloadMetadataStats stats_; - const std::string cluster_name_; - absl::flat_hash_map> - workloads_by_ips_; -}; - -using ConfigSharedPtr = std::shared_ptr; - -/** - * Workload Metadata listener filter. - */ -class Filter : public Network::ListenerFilter, - Logger::Loggable { - public: - Filter(const ConfigSharedPtr& config) : config_(config) {} - - // Network::ListenerFilter - Network::FilterStatus onAccept(Network::ListenerFilterCallbacks& cb) override; - - Network::FilterStatus onData(Network::ListenerFilterBuffer&) override; - - size_t maxReadBytes() const override; - - private: - ConfigSharedPtr config_; -}; - -} // namespace WorkloadMetadata -} // namespace Envoy diff --git a/src/envoy/workload_metadata/workload_metadata_test.cc b/src/envoy/workload_metadata/workload_metadata_test.cc deleted file mode 100644 index f9a9bcbdcd9..00000000000 --- a/src/envoy/workload_metadata/workload_metadata_test.cc +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright 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 "workload_metadata.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "source/common/router/string_accessor_impl.h" -#include "src/envoy/workload_metadata/config/workload_metadata.pb.h" -#include "test/mocks/network/mocks.h" -#include "test/mocks/stats/mocks.h" - -using namespace ::istio::telemetry::workloadmetadata; -using namespace Envoy::Common; - -using testing::NiceMock; -using testing::Return; -using testing::ReturnRef; - -namespace Envoy { -namespace WorkloadMetadata { - -class FilterTest : public testing::Test { - public: - FilterTest() { ENVOY_LOG_MISC(info, "test"); } - - public: - std::unique_ptr newDefaultFilter() { - v1::WorkloadMetadataResources resources; - auto resource = resources.add_workload_metadata_resources(); - resource->set_instance_name("foo-pod-12345"); - resource->set_workload_name("foo"); - resource->set_canonical_name("foo-svc"); - resource->set_canonical_revision("v2beta1"); - resource->set_namespace_name("default"); - resource->add_ip_addresses("10.10.10.10"); - resource->add_ip_addresses("192.168.1.1"); - resource->add_containers("app"); - resource->add_containers("storage"); - - Config config(store_, "my-cluster", resources); - return std::make_unique(std::make_shared(config)); - } - - void setAddressToReturn(const std::string& address) { - callbacks_.socket_.connection_info_provider_->setRemoteAddress( - Network::Utility::resolveUrl(address)); - } - - protected: - Stats::IsolatedStoreImpl store_; - NiceMock callbacks_; -}; - -TEST_F(FilterTest, OnAccept) { - auto filter = newDefaultFilter(); - setAddressToReturn("tcp://10.10.10.10:9999"); - - auto filter_state = std::make_shared( - StreamInfo::FilterState::LifeSpan::Connection); - EXPECT_CALL(callbacks_, filterState()) - .WillOnce( - Invoke([&]() -> StreamInfo::FilterState& { return *filter_state; })); - - EXPECT_EQ(filter->onAccept(callbacks_), Network::FilterStatus::Continue); - EXPECT_TRUE( - filter_state->hasDataWithName(Istio::Common::kSourceMetadataBaggageKey)); - auto found = filter_state->getDataReadOnly( - Istio::Common::kSourceMetadataBaggageKey); - EXPECT_EQ(found->asString(), - "k8s.deployment.name=foo,k8s.cluster.name=my-cluster,k8s.namespace." - "name=default," - "service.name=foo-svc,service.version=v2beta1"); - - setAddressToReturn("tcp://192.168.1.1:5555"); - filter_state = std::make_shared( - StreamInfo::FilterState::LifeSpan::Connection); - EXPECT_CALL(callbacks_, filterState()) - .WillOnce( - Invoke([&]() -> StreamInfo::FilterState& { return *filter_state; })); - EXPECT_EQ(filter->onAccept(callbacks_), Network::FilterStatus::Continue); - EXPECT_TRUE( - filter_state->hasDataWithName(Istio::Common::kSourceMetadataBaggageKey)); - - found = filter_state->getDataReadOnly( - Istio::Common::kSourceMetadataBaggageKey); - EXPECT_EQ(found->asString(), - "k8s.deployment.name=foo,k8s.cluster.name=my-cluster,k8s.namespace." - "name=default," - "service.name=foo-svc,service.version=v2beta1"); - - setAddressToReturn("tcp://4.22.1.1:4343"); - EXPECT_CALL(callbacks_, filterState()).Times(0); - EXPECT_EQ(filter->onAccept(callbacks_), Network::FilterStatus::Continue); -} - -} // namespace WorkloadMetadata -} // namespace Envoy