From 23a3e91f76873315d952e15828540c248c8f5e49 Mon Sep 17 00:00:00 2001 From: Steve Sloka Date: Thu, 12 Nov 2020 12:29:40 -0500 Subject: [PATCH] remove remaining xDS resource v2 server and related converters Updates #1898 Signed-off-by: Steve Sloka --- internal/featuretests/v2/featuretests.go | 1 - internal/xds/hash.go | 14 -- internal/xds/util.go | 183 ---------------------- internal/xds/v2/contour.go | 188 ----------------------- internal/xds/v2/contour_test.go | 173 --------------------- internal/xds/v2/server.go | 43 ------ internal/xds/v2/snapshotter.go | 55 ------- internal/xds/v3/contour.go | 16 +- internal/xds/v3/snapshotter.go | 39 +---- internal/xdscache/v2/server_test.go | 1 - 10 files changed, 9 insertions(+), 704 deletions(-) delete mode 100644 internal/xds/v2/contour.go delete mode 100644 internal/xds/v2/contour_test.go delete mode 100644 internal/xds/v2/server.go delete mode 100644 internal/xds/v2/snapshotter.go diff --git a/internal/featuretests/v2/featuretests.go b/internal/featuretests/v2/featuretests.go index b544aa801cd..31040b5acfa 100644 --- a/internal/featuretests/v2/featuretests.go +++ b/internal/featuretests/v2/featuretests.go @@ -38,7 +38,6 @@ import ( "github.com/projectcontour/contour/internal/status" "github.com/projectcontour/contour/internal/workgroup" "github.com/projectcontour/contour/internal/xds" - contour_xds_v2 "github.com/projectcontour/contour/internal/xds/v2" "github.com/projectcontour/contour/internal/xdscache" xdscache_v2 "github.com/projectcontour/contour/internal/xdscache/v2" "github.com/prometheus/client_golang/prometheus" diff --git a/internal/xds/hash.go b/internal/xds/hash.go index b495e6fc7b1..ad63b859f08 100644 --- a/internal/xds/hash.go +++ b/internal/xds/hash.go @@ -14,25 +14,11 @@ package xds import ( - envoy_api_core_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" envoy_config_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" ) const CONSTANT_HASH_VALUE = "contour" -// ConstantHashV2 is a specialized node ID hasher used to allow -// any instance of Envoy to connect to Contour regardless of the -// service-node flag configured on Envoy. -type ConstantHashV2 struct{} - -func (c ConstantHashV2) ID(*envoy_api_core_v2.Node) string { - return CONSTANT_HASH_VALUE -} - -func (c ConstantHashV2) String() string { - return CONSTANT_HASH_VALUE -} - // ConstantHashV3 is the same as ConstantHashV2 but for xDS v3. type ConstantHashV3 struct{} diff --git a/internal/xds/util.go b/internal/xds/util.go index 4c26b4e617e..64dcae3db6d 100644 --- a/internal/xds/util.go +++ b/internal/xds/util.go @@ -14,32 +14,8 @@ package xds import ( - "fmt" "strings" - envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2" - envoy_api_auth_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth" - envoy_api_core_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" - envoy_config_accesslog_v2 "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2" - envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" - envoy_config_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" - envoy_config_filter_http_ext_authz_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2" - envoy_config_filter_http_lua_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/lua/v2" - envoy_config_filter_network_http_connection_manager_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2" - envoy_config_filter_network_tcp_proxy_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/tcp_proxy/v2" - envoy_config_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3" - envoy_config_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" - envoy_extensions_access_loggers_file_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/file/v3" - envoy_extensions_filters_http_ext_authz_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/ext_authz/v3" - envoy_extensions_filters_http_lua_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/lua/v3" - envoy_extensions_filters_network_http_connection_manager_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" - envoy_extensions_filters_network_tcp_proxy_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/tcp_proxy/v3" - envoy_extensions_transport_sockets_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/any" - "github.com/projectcontour/contour/internal/protobuf" - - "github.com/golang/protobuf/proto" "k8s.io/apimachinery/pkg/types" ) @@ -62,162 +38,3 @@ func ClusterLoadAssignmentName(service types.NamespacedName, portName string) st return strings.Join(name, "/") } - -// TypeMapping maps xDS type URLs from v2 to v3. -var TypeMapping map[string]string - -func init() { - TypeMapping = make(map[string]string) - - entry := func(from proto.Message, to proto.Message) { - TypeMapping[protobuf.AnyMessageTypeOf(from)] = protobuf.AnyMessageTypeOf(to) - } - - // Fundamental xDS resource types. - entry(&envoy_api_v2.Listener{}, &envoy_config_listener_v3.Listener{}) - entry(&envoy_api_v2.Cluster{}, &envoy_config_cluster_v3.Cluster{}) - entry(&envoy_api_v2.RouteConfiguration{}, &envoy_config_route_v3.RouteConfiguration{}) - entry(&envoy_api_v2.ClusterLoadAssignment{}, &envoy_config_endpoint_v3.ClusterLoadAssignment{}) - entry(&envoy_api_auth_v2.Secret{}, &envoy_extensions_transport_sockets_tls_v3.Secret{}) - - // Other embedded resources used by Contour. - entry(&envoy_config_accesslog_v2.FileAccessLog{}, - &envoy_extensions_access_loggers_file_v3.FileAccessLog{}) - - entry(&envoy_config_filter_http_ext_authz_v2.ExtAuthzPerRoute{}, - &envoy_extensions_filters_http_ext_authz_v3.ExtAuthzPerRoute{}) - - entry(&envoy_config_filter_http_ext_authz_v2.ExtAuthz{}, - &envoy_extensions_filters_http_ext_authz_v3.ExtAuthz{}) - - entry(&envoy_config_filter_http_lua_v2.Lua{}, - &envoy_extensions_filters_http_lua_v3.Lua{}) - - entry(&envoy_api_auth_v2.UpstreamTlsContext{}, - &envoy_extensions_transport_sockets_tls_v3.UpstreamTlsContext{}) - - entry(&envoy_api_auth_v2.DownstreamTlsContext{}, - &envoy_extensions_transport_sockets_tls_v3.DownstreamTlsContext{}) - - entry(&envoy_config_filter_network_http_connection_manager_v2.HttpConnectionManager{}, - &envoy_extensions_filters_network_http_connection_manager_v3.HttpConnectionManager{}) - - entry(&envoy_config_filter_network_tcp_proxy_v2.TcpProxy{}, - &envoy_extensions_filters_network_tcp_proxy_v3.TcpProxy{}) -} - -func rewriteAnyMessage(a *any.Any) { - if a != nil { - anyval := ptypes.DynamicAny{} - if err := ptypes.UnmarshalAny(a, &anyval); err != nil { - panic(fmt.Sprintf("failed to unmarshal %T: %s", a, err.Error())) - } - - newMsg := protobuf.MustMarshalAny(Rewrite(anyval.Message)) - if replacement, ok := TypeMapping[newMsg.TypeUrl]; ok { - newMsg.TypeUrl = replacement - } - - a.TypeUrl = newMsg.TypeUrl - a.Value = newMsg.Value - } -} - -func rewriteConfigSource(s *envoy_api_core_v2.ConfigSource) { - if s != nil { - s.ResourceApiVersion = envoy_api_core_v2.ApiVersion_V3 - s.GetApiConfigSource().TransportApiVersion = envoy_api_core_v2.ApiVersion_V3 - } -} - -// Rewrite changes the given xDS message to use the v3 xDS API. -// -// Since the v2 and v3 APIs are wire-compatible, we just rewrite -// the type names for type URLs in any.Any messages. This allows Envoy -// to do the actual conversion, and Envoy takes care of migrating -// deprecated fields. -func Rewrite(in proto.Message) proto.Message { - switch msg := in.(type) { - case *envoy_api_v2.ClusterLoadAssignment: - return msg - case *envoy_api_auth_v2.Secret: - return msg - - case *envoy_api_v2.Cluster: - if e := msg.GetEdsClusterConfig(); e != nil { - rewriteConfigSource(e.GetEdsConfig()) - } - - if t := msg.GetTransportSocket(); t != nil { - rewriteAnyMessage(t.GetTypedConfig()) - } - - return msg - - case *envoy_api_v2.RouteConfiguration: - for _, v := range msg.GetVirtualHosts() { - for _, r := range v.GetRoutes() { - for _, conf := range r.GetTypedPerFilterConfig() { - rewriteAnyMessage(conf) - } - } - } - - return msg - - case *envoy_api_v2.Listener: - for _, filter := range msg.ListenerFilters { - rewriteAnyMessage(filter.GetTypedConfig()) - } - - for _, chain := range msg.FilterChains { - for _, filter := range chain.Filters { - rewriteAnyMessage(filter.GetTypedConfig()) - } - - if t := chain.GetTransportSocket(); t != nil { - rewriteAnyMessage(t.GetTypedConfig()) - } - } - - for _, a := range msg.AccessLog { - rewriteAnyMessage(a.GetTypedConfig()) - } - - return msg - - case *envoy_config_filter_network_http_connection_manager_v2.HttpConnectionManager: - if r := msg.GetRds(); r != nil { - rewriteConfigSource(r.GetConfigSource()) - } - - for _, f := range msg.HttpFilters { - rewriteAnyMessage(f.GetTypedConfig()) - } - - for _, l := range msg.AccessLog { - rewriteAnyMessage(l.GetTypedConfig()) - } - - return msg - - case *envoy_api_auth_v2.DownstreamTlsContext: - for _, s := range msg.GetCommonTlsContext().TlsCertificateSdsSecretConfigs { - rewriteConfigSource(s.GetSdsConfig()) - } - - return msg - - case *envoy_api_auth_v2.UpstreamTlsContext: - for _, s := range msg.GetCommonTlsContext().TlsCertificateSdsSecretConfigs { - rewriteConfigSource(s.GetSdsConfig()) - } - - return msg - - default: - // Any messages that don't have any embedded version information - // that needs conversion can just be returned unchanged. - return msg - } -} diff --git a/internal/xds/v2/contour.go b/internal/xds/v2/contour.go deleted file mode 100644 index 5bdf7024c3e..00000000000 --- a/internal/xds/v2/contour.go +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright Project Contour Authors -// 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 v2 - -import ( - "context" - "fmt" - "strconv" - - envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2" - discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2" - "github.com/golang/protobuf/proto" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/any" - "github.com/projectcontour/contour/internal/xds" - "github.com/sirupsen/logrus" -) - -type grpcStream interface { - Context() context.Context - Send(*envoy_api_v2.DiscoveryResponse) error - Recv() (*envoy_api_v2.DiscoveryRequest, error) -} - -// NewContourServer creates an internally implemented Server that streams the -// provided set of Resource objects. The returned Server implements the xDS -// State of the World (SotW) variant. -func NewContourServer(log logrus.FieldLogger, resources ...xds.Resource) Server { - c := contourServer{ - FieldLogger: log, - resources: map[string]xds.Resource{}, - } - - for i, r := range resources { - c.resources[r.TypeURL()] = resources[i] - } - - return &c -} - -type contourServer struct { - // Since we only implement the streaming state of the world - // protocol, embed the default null implementations to handle - // the unimplemented gRPC endpoints. - discovery.UnimplementedAggregatedDiscoveryServiceServer - discovery.UnimplementedSecretDiscoveryServiceServer - envoy_api_v2.UnimplementedRouteDiscoveryServiceServer - envoy_api_v2.UnimplementedEndpointDiscoveryServiceServer - envoy_api_v2.UnimplementedClusterDiscoveryServiceServer - envoy_api_v2.UnimplementedListenerDiscoveryServiceServer - - logrus.FieldLogger - resources map[string]xds.Resource - connections xds.Counter -} - -// stream processes a stream of DiscoveryRequests. -func (s *contourServer) stream(st grpcStream) error { - // Bump connection counter and set it as a field on the logger. - log := s.WithField("connection", s.connections.Next()) - - // Notify whether the stream terminated on error. - done := func(log *logrus.Entry, err error) error { - if err != nil { - log.WithError(err).Error("stream terminated") - } else { - log.Info("stream terminated") - } - - return err - } - - ch := make(chan int, 1) - - // internally all registration values start at zero so sending - // a last that is less than zero will guarantee that each stream - // will generate a response immediately, then wait. - last := -1 - ctx := st.Context() - - // now stick in this loop until the client disconnects. - for { - // first we wait for the request from Envoy, this is part of - // the xDS protocol. - req, err := st.Recv() - if err != nil { - return done(log, err) - } - - // note: redeclare log in this scope so the next time around the loop all is forgotten. - log := log.WithField("version_info", req.VersionInfo).WithField("response_nonce", req.ResponseNonce) - if req.Node != nil { - log = log.WithField("node_id", req.Node.Id).WithField("node_version", fmt.Sprintf("v%d.%d.%d", req.Node.GetUserAgentBuildVersion().Version.MajorNumber, req.Node.GetUserAgentBuildVersion().Version.MinorNumber, req.Node.GetUserAgentBuildVersion().Version.Patch)) - } - - if status := req.ErrorDetail; status != nil { - // if Envoy rejected the last update log the details here. - // TODO(dfc) issue 1176: handle xDS ACK/NACK - log.WithField("code", status.Code).Error(status.Message) - } - - // from the request we derive the resource to stream which have - // been registered according to the typeURL. - r, ok := s.resources[req.TypeUrl] - if !ok { - return done(log, fmt.Errorf("no resource registered for typeURL %q", req.TypeUrl)) - } - - log = log.WithField("resource_names", req.ResourceNames).WithField("type_url", req.TypeUrl) - log.Info("handling v2 xDS resource request") - - // now we wait for a notification, if this is the first request received on this - // connection last will be less than zero and that will trigger a response immediately. - r.Register(ch, last, req.ResourceNames...) - select { - case last = <-ch: - // boom, something in the cache has changed. - // TODO(dfc) the thing that has changed may not be in the scope of the filter - // so we're going to be sending an update that is a no-op. See #426 - - var resources []proto.Message - switch len(req.ResourceNames) { - case 0: - // no resource hints supplied, return the full - // contents of the resource - resources = r.Contents() - default: - // resource hints supplied, return exactly those - resources = r.Query(req.ResourceNames) - } - - any := make([]*any.Any, 0, len(resources)) - for _, r := range resources { - a, err := ptypes.MarshalAny(r) - if err != nil { - return done(log, err) - } - - any = append(any, a) - } - - resp := &envoy_api_v2.DiscoveryResponse{ - VersionInfo: strconv.Itoa(last), - Resources: any, - TypeUrl: r.TypeURL(), - Nonce: strconv.Itoa(last), - } - - if err := st.Send(resp); err != nil { - return done(log, err) - } - - case <-ctx.Done(): - return done(log, ctx.Err()) - } - } -} - -func (s *contourServer) StreamClusters(srv envoy_api_v2.ClusterDiscoveryService_StreamClustersServer) error { - return s.stream(srv) -} - -func (s *contourServer) StreamEndpoints(srv envoy_api_v2.EndpointDiscoveryService_StreamEndpointsServer) error { - return s.stream(srv) -} - -func (s *contourServer) StreamListeners(srv envoy_api_v2.ListenerDiscoveryService_StreamListenersServer) error { - return s.stream(srv) -} - -func (s *contourServer) StreamRoutes(srv envoy_api_v2.RouteDiscoveryService_StreamRoutesServer) error { - return s.stream(srv) -} - -func (s *contourServer) StreamSecrets(srv discovery.SecretDiscoveryService_StreamSecretsServer) error { - return s.stream(srv) -} diff --git a/internal/xds/v2/contour_test.go b/internal/xds/v2/contour_test.go deleted file mode 100644 index 5411ef3074c..00000000000 --- a/internal/xds/v2/contour_test.go +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright Project Contour Authors -// 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 v2 - -import ( - "context" - "fmt" - "io" - "io/ioutil" - "testing" - - envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2" - "github.com/golang/protobuf/proto" - "github.com/projectcontour/contour/internal/xds" - "github.com/sirupsen/logrus" - "github.com/stretchr/testify/assert" -) - -func TestXDSHandlerStream(t *testing.T) { - log := logrus.New() - log.SetOutput(ioutil.Discard) - tests := map[string]struct { - xh contourServer - stream grpcStream - want error - }{ - "recv returns error immediately": { - xh: contourServer{FieldLogger: log}, - stream: &mockStream{ - context: context.Background, - recv: func() (*envoy_api_v2.DiscoveryRequest, error) { - return nil, io.EOF - }, - }, - want: io.EOF, - }, - "no registered typeURL": { - xh: contourServer{FieldLogger: log}, - stream: &mockStream{ - context: context.Background, - recv: func() (*envoy_api_v2.DiscoveryRequest, error) { - return &envoy_api_v2.DiscoveryRequest{ - TypeUrl: "io.projectcontour.potato", - }, nil - }, - }, - want: fmt.Errorf("no resource registered for typeURL %q", "io.projectcontour.potato"), - }, - "failed to convert values to any": { - xh: contourServer{ - FieldLogger: log, - resources: map[string]xds.Resource{ - "io.projectcontour.potato": &mockResource{ - register: func(ch chan int, i int) { - ch <- i + 1 - }, - contents: func() []proto.Message { - return []proto.Message{nil} - }, - typeurl: func() string { return "io.projectcontour.potato" }, - }, - }, - }, - stream: &mockStream{ - context: context.Background, - recv: func() (*envoy_api_v2.DiscoveryRequest, error) { - return &envoy_api_v2.DiscoveryRequest{ - TypeUrl: "io.projectcontour.potato", - }, nil - }, - }, - want: fmt.Errorf("proto: Marshal called with nil"), - }, - "failed to send": { - xh: contourServer{ - FieldLogger: log, - resources: map[string]xds.Resource{ - "io.projectcontour.potato": &mockResource{ - register: func(ch chan int, i int) { - ch <- i + 1 - }, - contents: func() []proto.Message { - return []proto.Message{new(envoy_api_v2.ClusterLoadAssignment)} - }, - typeurl: func() string { return "io.projectcontour.potato" }, - }, - }, - }, - stream: &mockStream{ - context: context.Background, - recv: func() (*envoy_api_v2.DiscoveryRequest, error) { - return &envoy_api_v2.DiscoveryRequest{ - TypeUrl: "io.projectcontour.potato", - }, nil - }, - send: func(resp *envoy_api_v2.DiscoveryResponse) error { - return io.EOF - }, - }, - want: io.EOF, - }, - "context canceled": { - xh: contourServer{ - FieldLogger: log, - resources: map[string]xds.Resource{ - "io.projectcontour.potato": &mockResource{ - register: func(ch chan int, i int) { - // do nothing - }, - typeurl: func() string { return "io.projectcontour.potato" }, - }, - }, - }, - stream: &mockStream{ - context: func() context.Context { - ctx := context.Background() - ctx, cancel := context.WithCancel(ctx) - cancel() - return ctx - }, - recv: func() (*envoy_api_v2.DiscoveryRequest, error) { - return &envoy_api_v2.DiscoveryRequest{ - TypeUrl: "io.projectcontour.potato", - }, nil - }, - send: func(resp *envoy_api_v2.DiscoveryResponse) error { - return io.EOF - }, - }, - want: context.Canceled, - }, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - got := tc.xh.stream(tc.stream) - assert.Equal(t, tc.want, got) - }) - } -} - -type mockStream struct { - context func() context.Context - send func(*envoy_api_v2.DiscoveryResponse) error - recv func() (*envoy_api_v2.DiscoveryRequest, error) -} - -func (m *mockStream) Context() context.Context { return m.context() } -func (m *mockStream) Send(resp *envoy_api_v2.DiscoveryResponse) error { return m.send(resp) } -func (m *mockStream) Recv() (*envoy_api_v2.DiscoveryRequest, error) { return m.recv() } - -type mockResource struct { - contents func() []proto.Message - query func([]string) []proto.Message - register func(chan int, int) - typeurl func() string -} - -func (m *mockResource) Contents() []proto.Message { return m.contents() } -func (m *mockResource) Query(names []string) []proto.Message { return m.query(names) } -func (m *mockResource) Register(ch chan int, last int, hints ...string) { m.register(ch, last) } -func (m *mockResource) TypeURL() string { return m.typeurl() } diff --git a/internal/xds/v2/server.go b/internal/xds/v2/server.go deleted file mode 100644 index 6cc26ee4918..00000000000 --- a/internal/xds/v2/server.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright Project Contour Authors -// 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 v2 - -import ( - api "github.com/envoyproxy/go-control-plane/envoy/api/v2" - discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2" - "google.golang.org/grpc" -) - -// Server is a collection of handlers for streaming discovery requests. -type Server interface { - api.ClusterDiscoveryServiceServer - api.EndpointDiscoveryServiceServer - api.ListenerDiscoveryServiceServer - api.RouteDiscoveryServiceServer - discovery.AggregatedDiscoveryServiceServer - discovery.SecretDiscoveryServiceServer -} - -// RegisterServer registers the given xDS protocol Server with the gRPC -// runtime. If registry is non-nil gRPC server metrics will be automatically -// configured and enabled. -func RegisterServer(srv Server, g *grpc.Server) { - // register services - discovery.RegisterAggregatedDiscoveryServiceServer(g, srv) - discovery.RegisterSecretDiscoveryServiceServer(g, srv) - api.RegisterClusterDiscoveryServiceServer(g, srv) - api.RegisterEndpointDiscoveryServiceServer(g, srv) - api.RegisterListenerDiscoveryServiceServer(g, srv) - api.RegisterRouteDiscoveryServiceServer(g, srv) -} diff --git a/internal/xds/v2/snapshotter.go b/internal/xds/v2/snapshotter.go deleted file mode 100644 index 1646bf2459f..00000000000 --- a/internal/xds/v2/snapshotter.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright Project Contour Authors -// 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 v2 - -import ( - envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types" - envoy_cache_v2 "github.com/envoyproxy/go-control-plane/pkg/cache/v2" - envoy_log "github.com/envoyproxy/go-control-plane/pkg/log" - "github.com/projectcontour/contour/internal/xds" - "github.com/projectcontour/contour/internal/xdscache" -) - -var Hash = xds.ConstantHashV2{} - -// Snapshotter is a v2 Snapshot cache that implements the xds.Snapshotter interface. -type Snapshotter interface { - xdscache.Snapshotter - envoy_cache_v2.SnapshotCache -} - -type snapshotter struct { - envoy_cache_v2.SnapshotCache -} - -func (s *snapshotter) Generate(version string, resources map[envoy_types.ResponseType][]envoy_types.Resource) error { - // Create a snapshot with all xDS resources. - snapshot := envoy_cache_v2.NewSnapshot( - version, - resources[envoy_types.Endpoint], - resources[envoy_types.Cluster], - resources[envoy_types.Route], - resources[envoy_types.Listener], - nil, - resources[envoy_types.Secret], - ) - - return s.SetSnapshot(Hash.String(), snapshot) -} - -func NewSnapshotCache(ads bool, logger envoy_log.Logger) Snapshotter { - return &snapshotter{ - SnapshotCache: envoy_cache_v2.NewSnapshotCache(ads, &Hash, logger), - } -} diff --git a/internal/xds/v3/contour.go b/internal/xds/v3/contour.go index 30ebf2878d7..60b1173d6e5 100644 --- a/internal/xds/v3/contour.go +++ b/internal/xds/v3/contour.go @@ -48,11 +48,6 @@ func NewContourServer(log logrus.FieldLogger, resources ...xds.Resource) Server for i, r := range resources { c.resources[r.TypeURL()] = resources[i] - - // Map the xDS v3 resource to this provider. - if v3, ok := xds.TypeMapping[r.TypeURL()]; ok { - c.resources[v3] = resources[i] - } } return &c @@ -149,17 +144,8 @@ func (s *contourServer) stream(st grpcStream) error { resources = r.Query(req.ResourceNames) } - // Rewrite the embedded message types to v3. - var rewritten []proto.Message + any := make([]*any.Any, 0, len(resources)) for _, r := range resources { - // Note that the resource cache - // gave us a pointer to its internal - // object, so we should rewrite a copy. - rewritten = append(rewritten, xds.Rewrite(proto.Clone(r))) - } - - any := make([]*any.Any, 0, len(rewritten)) - for _, r := range rewritten { a, err := ptypes.MarshalAny(r) if err != nil { return done(log, err) diff --git a/internal/xds/v3/snapshotter.go b/internal/xds/v3/snapshotter.go index 9844dcca787..8b94fba34a6 100644 --- a/internal/xds/v3/snapshotter.go +++ b/internal/xds/v3/snapshotter.go @@ -15,10 +15,8 @@ package v3 import ( envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types" - envoy_cache_v2 "github.com/envoyproxy/go-control-plane/pkg/cache/v2" envoy_cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3" envoy_log "github.com/envoyproxy/go-control-plane/pkg/log" - "github.com/golang/protobuf/proto" "github.com/projectcontour/contour/internal/xds" "github.com/projectcontour/contour/internal/xdscache" ) @@ -37,13 +35,14 @@ type snapshotter struct { func (s *snapshotter) Generate(version string, resources map[envoy_types.ResponseType][]envoy_types.Resource) error { // Create a snapshot with all xDS resources. - snapshot := envoy_cache_v3.Snapshot{} - - snapshot.Resources[envoy_types.Endpoint] = rewriteResources(version, resources[envoy_types.Endpoint]) - snapshot.Resources[envoy_types.Cluster] = rewriteResources(version, resources[envoy_types.Cluster]) - snapshot.Resources[envoy_types.Route] = rewriteResources(version, resources[envoy_types.Route]) - snapshot.Resources[envoy_types.Listener] = rewriteResources(version, resources[envoy_types.Listener]) - snapshot.Resources[envoy_types.Secret] = rewriteResources(version, resources[envoy_types.Secret]) + snapshot := envoy_cache_v3.NewSnapshot( + version, + resources[envoy_types.Endpoint], + resources[envoy_types.Cluster], + resources[envoy_types.Route], + resources[envoy_types.Listener], + nil, + resources[envoy_types.Secret]) return s.SetSnapshot(Hash.String(), snapshot) } @@ -53,25 +52,3 @@ func NewSnapshotCache(ads bool, logger envoy_log.Logger) Snapshotter { SnapshotCache: envoy_cache_v3.NewSnapshotCache(ads, &Hash, logger), } } - -func rewriteResources(version string, items []envoy_types.Resource) envoy_cache_v3.Resources { - // Since we are using the xDS v2 types internally, create - // the resources with the v2 package so that it indexes them - // by name using the correct v2 type switch. - v2 := envoy_cache_v2.NewResources(version, rewrite(items)) - - return envoy_cache_v3.Resources{ - Version: v2.Version, - Items: v2.Items, - } -} - -func rewrite(resources []envoy_types.Resource) []envoy_types.Resource { - rewritten := make([]envoy_types.Resource, len(resources)) - - for i, r := range resources { - rewritten[i] = xds.Rewrite(proto.Clone(r)) - } - - return rewritten -} diff --git a/internal/xdscache/v2/server_test.go b/internal/xdscache/v2/server_test.go index 2c15e4f54ec..7b68677ecac 100644 --- a/internal/xdscache/v2/server_test.go +++ b/internal/xdscache/v2/server_test.go @@ -28,7 +28,6 @@ import ( "github.com/projectcontour/contour/internal/contour" "github.com/projectcontour/contour/internal/dag" "github.com/projectcontour/contour/internal/fixture" - contour_xds_v2 "github.com/projectcontour/contour/internal/xds/v2" "github.com/projectcontour/contour/internal/xdscache" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require"