Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kriss <[email protected]>
  • Loading branch information
skriss committed Mar 4, 2024
1 parent be82890 commit f296976
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
7 changes: 1 addition & 6 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
31 changes: 13 additions & 18 deletions internal/xdscache/v3/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@ 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"
envoy_cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
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"
"github.com/projectcontour/contour/internal/xdscache"
)

// 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
Expand All @@ -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{
Expand All @@ -70,7 +66,7 @@ func NewSnapshotHandler(resources []xdscache.ResourceCache, snapshotCache envoy_
snapshotCache: snapshotCache,
edsCache: edsCache,
muxCache: muxCache,
log: logger,
log: log,
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f296976

Please sign in to comment.