@@ -35,7 +35,7 @@ import (
35
35
type SnapshotHandler struct {
36
36
resources map [envoy_resource_v3.Type ]xdscache.ResourceCache
37
37
defaultCache envoy_cache_v3.SnapshotCache
38
- edsCache envoy_cache_v3.SnapshotCache
38
+ edsCache * envoy_cache_v3.LinearCache
39
39
mux * envoy_cache_v3.MuxCache
40
40
log logrus.FieldLogger
41
41
}
@@ -44,7 +44,9 @@ type SnapshotHandler struct {
44
44
func NewSnapshotHandler (resources []xdscache.ResourceCache , log logrus.FieldLogger ) * SnapshotHandler {
45
45
var (
46
46
defaultCache = envoy_cache_v3 .NewSnapshotCache (false , & contour_xds_v3 .Hash , log .WithField ("context" , "defaultCache" ))
47
- edsCache = envoy_cache_v3 .NewSnapshotCache (false , & contour_xds_v3 .Hash , log .WithField ("context" , "edsCache" ))
47
+ edsCache = envoy_cache_v3 .NewLinearCache (envoy_resource_v3 .EndpointType ,
48
+ envoy_cache_v3 .WithVersionPrefix (uuid .NewString ()+ "-" ),
49
+ envoy_cache_v3 .WithLogger (log .WithField ("context" , "edsCache" )))
48
50
49
51
mux = & envoy_cache_v3.MuxCache {
50
52
Caches : map [string ]envoy_cache_v3.Cache {},
@@ -89,21 +91,40 @@ func (s *SnapshotHandler) GetCache() envoy_cache_v3.Cache {
89
91
// Refresh is called when the EndpointsTranslator updates values
90
92
// in its cache. It updates the EDS cache.
91
93
func (s * SnapshotHandler ) Refresh () {
92
- version := uuid .NewString ()
94
+ previouslyNotifiedResources := s .edsCache .GetResources ()
95
+ currentResources := envoy_cache_v3 .IndexRawResourcesByName (asResources (s .resources [envoy_resource_v3 .EndpointType ].Contents ()))
96
+
97
+ toUpdate := make (map [string ]envoy_types.Resource , len (currentResources ))
98
+ toDelete := make ([]string , 0 , len (previouslyNotifiedResources ))
99
+
100
+ for resourceName , previousResource := range previouslyNotifiedResources {
101
+ if newResource , ok := currentResources [resourceName ]; ok {
102
+ // Add resources that were updated.
103
+ if ! proto .Equal (newResource , previousResource ) {
104
+ toUpdate [resourceName ] = newResource
105
+ }
106
+ } else {
107
+ // Add resources that were deleted.
108
+ toDelete = append (toDelete , resourceName )
109
+ }
110
+ }
93
111
94
- resources := map [envoy_resource_v3.Type ][]envoy_types.Resource {
95
- envoy_resource_v3 .EndpointType : asResources (s .resources [envoy_resource_v3 .EndpointType ].Contents ()),
112
+ // Add resources that are new.
113
+ for resourceName , newResource := range currentResources {
114
+ if _ , ok := previouslyNotifiedResources [resourceName ]; ! ok {
115
+ toUpdate [resourceName ] = newResource
116
+ }
96
117
}
97
118
98
- snapshot , err := envoy_cache_v3 .NewSnapshot (version , resources )
99
- if err != nil {
100
- s .log .Errorf ("failed to generate snapshot version %q: %s" , version , err )
119
+ if len (toUpdate ) == 0 && len (toDelete ) == 0 {
120
+ s .log .Debug ("no EDS resources to update" )
101
121
return
102
122
}
103
123
104
- if err := s .edsCache .SetSnapshot (context .Background (), contour_xds_v3 .Hash .String (), snapshot ); err != nil {
105
- s .log .Errorf ("failed to store snapshot version %q: %s" , version , err )
106
- return
124
+ s .log .WithField ("toUpdate" , len (toUpdate )).WithField ("toDelete" , len (toDelete )).Debug ("refreshing EDS cache" )
125
+
126
+ if err := s .edsCache .UpdateResources (toUpdate , toDelete ); err != nil {
127
+ s .log .WithError (err ).Error ("failed to update EDS cache" )
107
128
}
108
129
}
109
130
0 commit comments