From f296976dddf1b9458ff8697a1b417170ef7fd553 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Mon, 4 Mar 2024 09:41:40 -0700 Subject: [PATCH] more cleanup Signed-off-by: Steve Kriss --- cmd/contour/serve.go | 7 +------ internal/xdscache/v3/snapshot.go | 31 +++++++++++++------------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/cmd/contour/serve.go b/cmd/contour/serve.go index 90982f8a944..0697b45a2f8 100644 --- a/cmd/contour/serve.go +++ b/cmd/contour/serve.go @@ -23,7 +23,6 @@ import ( "time" "github.com/alecthomas/kingpin/v2" - envoy_cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3" envoy_server_v3 "github.com/envoyproxy/go-control-plane/pkg/server/v3" "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" @@ -510,11 +509,7 @@ func (s *Server) doServe() error { var snapshotHandler *xdscache_v3.SnapshotHandler if contourConfiguration.XDSServer.Type == contour_v1alpha1.EnvoyServerType { - snapshotHandler = xdscache_v3.NewSnapshotHandler( - resources, - envoy_cache_v3.NewSnapshotCache(false, &contour_xds_v3.Hash, s.log.WithField("context", "snapshotCache")), - s.log.WithField("context", "snapshotHandler"), - ) + snapshotHandler = xdscache_v3.NewSnapshotHandler(resources, s.log.WithField("context", "snapshotHandler")) // register observer for endpoints updates. endpointHandler.SetObserver(contour.ComposeObservers(snapshotHandler)) diff --git a/internal/xdscache/v3/snapshot.go b/internal/xdscache/v3/snapshot.go index 2cf09fdc4b3..0f349a0ee96 100644 --- a/internal/xdscache/v3/snapshot.go +++ b/internal/xdscache/v3/snapshot.go @@ -15,7 +15,6 @@ package v3 import ( "context" - "reflect" envoy_service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types" @@ -23,6 +22,7 @@ import ( envoy_resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3" "github.com/google/uuid" "github.com/sirupsen/logrus" + "google.golang.org/protobuf/proto" "github.com/projectcontour/contour/internal/dag" contour_xds_v3 "github.com/projectcontour/contour/internal/xds/v3" @@ -30,14 +30,8 @@ import ( ) // SnapshotHandler responds to DAG builds via the OnChange() -// event and generates and caches go-control-plane Snapshots. -// -// TODO this is now a bit of a misnomer. Responds to DAG builds -// or endpoint updates, gets data out of the existing Contour xDS -// caches, populates either the go-control-plane snapshot cache or -// linear cache (for EDS). Provides a mux cache to be used by the -// go-control-plane xDS server. Essentially translates between Contour -// xDS caches and go-control-plane xDS caches. +// event and Endpoint updates via the Refresh() event and +// generates and caches go-control-plane Snapshots. type SnapshotHandler struct { resources map[envoy_resource_v3.Type]xdscache.ResourceCache snapshotCache envoy_cache_v3.SnapshotCache @@ -47,7 +41,9 @@ type SnapshotHandler struct { } // NewSnapshotHandler returns an instance of SnapshotHandler. -func NewSnapshotHandler(resources []xdscache.ResourceCache, snapshotCache envoy_cache_v3.SnapshotCache, logger logrus.FieldLogger) *SnapshotHandler { +func NewSnapshotHandler(resources []xdscache.ResourceCache, log logrus.FieldLogger) *SnapshotHandler { + snapshotCache := envoy_cache_v3.NewSnapshotCache(false, &contour_xds_v3.Hash, log.WithField("context", "snapshotCache")) + edsCache := envoy_cache_v3.NewLinearCache(envoy_resource_v3.EndpointType) muxCache := &envoy_cache_v3.MuxCache{ @@ -70,7 +66,7 @@ func NewSnapshotHandler(resources []xdscache.ResourceCache, snapshotCache envoy_ snapshotCache: snapshotCache, edsCache: edsCache, muxCache: muxCache, - log: logger, + log: log, } } @@ -119,19 +115,18 @@ func (s *SnapshotHandler) OnChange(*dag.DAG) { } } -// asResources casts the given slice of values (that implement the envoy_types.Resource +// asResources converts the given slice of values (that implement the envoy_types.Resource // interface) to a slice of envoy_types.Resource. If the length of the slice is 0, it // returns nil. -func asResources(messages any) []envoy_types.Resource { - v := reflect.ValueOf(messages) - if v.Len() == 0 { +func asResources[T proto.Message](messages []T) []envoy_types.Resource { + if len(messages) == 0 { return nil } - protos := make([]envoy_types.Resource, v.Len()) + protos := make([]envoy_types.Resource, len(messages)) - for i := range protos { - protos[i] = v.Index(i).Interface().(envoy_types.Resource) + for i, resource := range messages { + protos[i] = resource } return protos