|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" |
| 11 | + |
| 12 | + "google.golang.org/protobuf/proto" |
| 13 | + "google.golang.org/protobuf/types/known/anypb" |
| 14 | + "google.golang.org/protobuf/types/known/durationpb" |
| 15 | +) |
| 16 | + |
| 17 | +// Resource is the base interface for the xDS payload. |
| 18 | +type Resource interface { |
| 19 | + proto.Message |
| 20 | +} |
| 21 | + |
| 22 | +// ResourceWithTTL is a Resource with an optional TTL. |
| 23 | +type ResourceWithTTL struct { |
| 24 | + Resource Resource |
| 25 | + TTL *time.Duration |
| 26 | +} |
| 27 | + |
| 28 | +// CachedResource is used to track resources added by the user in the cache. |
| 29 | +// It contains the resource itself and its associated version (currently in two different modes). |
| 30 | +// It should not be altered once created, to allow concurrent access. |
| 31 | +type CachedResource struct { |
| 32 | + Name string |
| 33 | + typeURL string |
| 34 | + |
| 35 | + resource Resource |
| 36 | + ttl *time.Duration |
| 37 | + |
| 38 | + // cacheVersion is the version of the cache at the time of last update, used in sotw. |
| 39 | + cacheVersion string |
| 40 | + |
| 41 | + marshalFunc func() ([]byte, error) |
| 42 | + computeResourceVersionFunc func() (string, error) |
| 43 | +} |
| 44 | + |
| 45 | +type CachedResourceOption = func(*CachedResource) |
| 46 | + |
| 47 | +// WithCacheVersion allows specifying the cacheVersion when the resource is set. |
| 48 | +func WithCacheVersion(version string) CachedResourceOption { |
| 49 | + return func(r *CachedResource) { r.cacheVersion = version } |
| 50 | +} |
| 51 | + |
| 52 | +// WithMarshaledResource enables the user to provide the already marshaled bytes if they have them. |
| 53 | +// Those bytes should strive at being consistent if the object has not changed (beware protobuf non-deterministic marshaling) |
| 54 | +// or alternatively the resource version should also then be set. |
| 55 | +// By default it is computed by performing a deterministic protobuf marshaling. |
| 56 | +func WithMarshaledResource(bytes []byte) CachedResourceOption { |
| 57 | + if len(bytes) == 0 { |
| 58 | + return func(*CachedResource) {} |
| 59 | + } |
| 60 | + return func(r *CachedResource) { r.marshalFunc = func() ([]byte, error) { return bytes, nil } } |
| 61 | +} |
| 62 | + |
| 63 | +// WithResourceVersion enables the user to provide the resource version to be used. |
| 64 | +// This version should be constant if the object has not changed to avoid needlessly sending resources to clients. |
| 65 | +// By default it is computed by hashing the serialized version of the resource. |
| 66 | +func WithResourceVersion(version string) CachedResourceOption { |
| 67 | + if version == "" { |
| 68 | + return func(*CachedResource) {} |
| 69 | + } |
| 70 | + return func(r *CachedResource) { r.computeResourceVersionFunc = func() (string, error) { return version, nil } } |
| 71 | +} |
| 72 | + |
| 73 | +// WithResourceTTL sets a TTL on the resource, that will be sent to the client with the payload. |
| 74 | +func WithResourceTTL(ttl *time.Duration) CachedResourceOption { |
| 75 | + return func(r *CachedResource) { r.ttl = ttl } |
| 76 | +} |
| 77 | + |
| 78 | +func NewCachedResource(name, typeURL string, res Resource, opts ...CachedResourceOption) *CachedResource { |
| 79 | + cachedRes := &CachedResource{ |
| 80 | + Name: name, |
| 81 | + typeURL: typeURL, |
| 82 | + resource: res, |
| 83 | + } |
| 84 | + for _, opt := range opts { |
| 85 | + opt(cachedRes) |
| 86 | + } |
| 87 | + if cachedRes.marshalFunc == nil { |
| 88 | + cachedRes.marshalFunc = sync.OnceValues(func() ([]byte, error) { |
| 89 | + return marshalResource(res) |
| 90 | + }) |
| 91 | + } |
| 92 | + if cachedRes.computeResourceVersionFunc == nil { |
| 93 | + cachedRes.computeResourceVersionFunc = sync.OnceValues(func() (string, error) { |
| 94 | + marshaled, err := cachedRes.marshalFunc() |
| 95 | + if err != nil { |
| 96 | + return "", fmt.Errorf("marshaling resource: %w", err) |
| 97 | + } |
| 98 | + return hashResource(marshaled), nil |
| 99 | + }) |
| 100 | + } |
| 101 | + return cachedRes |
| 102 | +} |
| 103 | + |
| 104 | +// SetCacheVersion updates the cache version. This violates the assumption that all fields can be safely read concurrently. |
| 105 | +// It's required today for the linear cache constructor, where we are guaranteed resources are not being used concurently, |
| 106 | +// and should not be used elsewhere. |
| 107 | +func (c *CachedResource) SetCacheVersion(version string) { |
| 108 | + c.cacheVersion = version |
| 109 | +} |
| 110 | + |
| 111 | +// HasTTL returns whether the resource has a TTL set. |
| 112 | +func (c *CachedResource) HasTTL() bool { |
| 113 | + return c.ttl != nil |
| 114 | +} |
| 115 | + |
| 116 | +// getMarshaledResource lazily marshals the resource and returns the bytes. |
| 117 | +func (c *CachedResource) getMarshaledResource() ([]byte, error) { |
| 118 | + return c.marshalFunc() |
| 119 | +} |
| 120 | + |
| 121 | +// GetResourceVersion returns a stable version reflecting the resource content. |
| 122 | +// By default it is built by hashing the serialized version of the object, using deterministic serializing. |
| 123 | +func (c *CachedResource) GetResourceVersion() (string, error) { |
| 124 | + return c.computeResourceVersionFunc() |
| 125 | +} |
| 126 | + |
| 127 | +// GetVersion returns the version for the resource. |
| 128 | +// By default it returns the cache version when the resource was added, but if requested it will return a |
| 129 | +// version specific to the resource content. |
| 130 | +func (c *CachedResource) GetVersion(useResourceVersion bool) (string, error) { |
| 131 | + if !useResourceVersion { |
| 132 | + return c.cacheVersion, nil |
| 133 | + } |
| 134 | + |
| 135 | + return c.GetResourceVersion() |
| 136 | +} |
| 137 | + |
| 138 | +// GetRawResource returns the underlying resource for use in legacy accessors. |
| 139 | +func (c *CachedResource) GetRawResource() ResourceWithTTL { |
| 140 | + return ResourceWithTTL{ |
| 141 | + Resource: c.resource, |
| 142 | + TTL: c.ttl, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +var deltaResourceTypeURL = "type.googleapis.com/" + string(proto.MessageName(&discovery.Resource{})) |
| 147 | + |
| 148 | +// getResourceVersion lazily hashes the resource and returns the stable hash used to track version changes. |
| 149 | +func (c *CachedResource) GetSotwResource(isHeartbeat bool) (*anypb.Any, error) { |
| 150 | + buildResource := func() (*anypb.Any, error) { |
| 151 | + marshaled, err := c.getMarshaledResource() |
| 152 | + if err != nil { |
| 153 | + return nil, fmt.Errorf("marshaling: %w", err) |
| 154 | + } |
| 155 | + return &anypb.Any{ |
| 156 | + TypeUrl: c.typeURL, |
| 157 | + Value: marshaled, |
| 158 | + }, nil |
| 159 | + } |
| 160 | + |
| 161 | + if c.ttl == nil { |
| 162 | + return buildResource() |
| 163 | + } |
| 164 | + |
| 165 | + wrappedResource := &discovery.Resource{ |
| 166 | + Name: c.Name, |
| 167 | + Ttl: durationpb.New(*c.ttl), |
| 168 | + } |
| 169 | + |
| 170 | + if !isHeartbeat { |
| 171 | + rsrc, err := buildResource() |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + wrappedResource.Resource = rsrc |
| 176 | + } |
| 177 | + |
| 178 | + marshaled, err := marshalResource(wrappedResource) |
| 179 | + if err != nil { |
| 180 | + return nil, fmt.Errorf("marshaling discovery resource: %w", err) |
| 181 | + } |
| 182 | + |
| 183 | + return &anypb.Any{ |
| 184 | + TypeUrl: deltaResourceTypeURL, |
| 185 | + Value: marshaled, |
| 186 | + }, nil |
| 187 | +} |
| 188 | + |
| 189 | +// getResourceVersion lazily hashes the resource and returns the stable hash used to track version changes. |
| 190 | +func (c *CachedResource) GetDeltaResource() (*discovery.Resource, error) { |
| 191 | + marshaled, err := c.getMarshaledResource() |
| 192 | + if err != nil { |
| 193 | + return nil, fmt.Errorf("marshaling: %w", err) |
| 194 | + } |
| 195 | + version, err := c.GetResourceVersion() |
| 196 | + if err != nil { |
| 197 | + return nil, fmt.Errorf("computing version: %w", err) |
| 198 | + } |
| 199 | + return &discovery.Resource{ |
| 200 | + Name: c.Name, |
| 201 | + Resource: &anypb.Any{ |
| 202 | + TypeUrl: c.typeURL, |
| 203 | + Value: marshaled, |
| 204 | + }, |
| 205 | + Version: version, |
| 206 | + }, nil |
| 207 | +} |
| 208 | + |
| 209 | +// hashResource will take a resource and create a SHA256 hash sum out of the marshaled bytes |
| 210 | +func hashResource(resource []byte) string { |
| 211 | + hasher := sha256.New() |
| 212 | + hasher.Write(resource) |
| 213 | + return hex.EncodeToString(hasher.Sum(nil)) |
| 214 | +} |
| 215 | + |
| 216 | +// marshalResource converts the Resource to MarshaledResource. |
| 217 | +func marshalResource(resource Resource) ([]byte, error) { |
| 218 | + return proto.MarshalOptions{Deterministic: true}.Marshal(resource) |
| 219 | +} |
0 commit comments