-
Notifications
You must be signed in to change notification settings - Fork 669
/
snapshot.go
141 lines (115 loc) · 4.49 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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 xdscache
import (
"math"
"reflect"
"strconv"
"sync"
envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
envoy_resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/projectcontour/contour/internal/dag"
"github.com/sirupsen/logrus"
)
type Snapshotter interface {
Generate(version string, resources map[envoy_resource_v3.Type][]envoy_types.Resource) error
}
// SnapshotHandler implements the xDS snapshot cache
// by responding to the OnChange() event causing a new
// snapshot to be created.
type SnapshotHandler struct {
// resources holds the cache of xDS contents.
resources map[envoy_resource_v3.Type]ResourceCache
// snapshotVersion holds the current version of the snapshot.
snapshotVersion int64
snapshotters []Snapshotter
snapLock sync.Mutex
logrus.FieldLogger
}
// NewSnapshotHandler returns an instance of SnapshotHandler.
func NewSnapshotHandler(resources []ResourceCache, logger logrus.FieldLogger) *SnapshotHandler {
return &SnapshotHandler{
resources: parseResources(resources),
FieldLogger: logger,
}
}
func (s *SnapshotHandler) AddSnapshotter(snap Snapshotter) {
s.snapLock.Lock()
defer s.snapLock.Unlock()
s.snapshotters = append(s.snapshotters, snap)
}
// Refresh is called when the EndpointsTranslator updates values
// in its cache.
func (s *SnapshotHandler) Refresh() {
s.generateNewSnapshot()
}
// OnChange is called when the DAG is rebuilt and a new snapshot is needed.
func (s *SnapshotHandler) OnChange(root *dag.DAG) {
s.generateNewSnapshot()
}
// generateNewSnapshot creates a new snapshot against
// the Contour XDS caches.
func (s *SnapshotHandler) generateNewSnapshot() {
// Generate new snapshot version.
version := s.newSnapshotVersion()
// Convert caches to envoy xDS Resources.
resources := map[envoy_resource_v3.Type][]envoy_types.Resource{
envoy_resource_v3.EndpointType: asResources(s.resources[envoy_resource_v3.EndpointType].Contents()),
envoy_resource_v3.ClusterType: asResources(s.resources[envoy_resource_v3.ClusterType].Contents()),
envoy_resource_v3.RouteType: asResources(s.resources[envoy_resource_v3.RouteType].Contents()),
envoy_resource_v3.ListenerType: asResources(s.resources[envoy_resource_v3.ListenerType].Contents()),
envoy_resource_v3.SecretType: asResources(s.resources[envoy_resource_v3.SecretType].Contents()),
envoy_resource_v3.RuntimeType: asResources(s.resources[envoy_resource_v3.RuntimeType].Contents()),
}
s.snapLock.Lock()
defer s.snapLock.Unlock()
for _, snap := range s.snapshotters {
if err := snap.Generate(version, resources); err != nil {
s.Errorf("failed to generate snapshot version %q: %s", version, err)
}
}
}
// newSnapshotVersion increments the current snapshotVersion
// and returns as a string.
func (s *SnapshotHandler) newSnapshotVersion() string {
// Reset the snapshotVersion if it ever hits max size.
if s.snapshotVersion == math.MaxInt64 {
s.snapshotVersion = 0
}
// Increment the snapshot version & return as string.
s.snapshotVersion++
return strconv.FormatInt(s.snapshotVersion, 10)
}
// asResources casts 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 interface{}) []envoy_types.Resource {
v := reflect.ValueOf(messages)
if v.Len() == 0 {
return nil
}
protos := make([]envoy_types.Resource, v.Len())
for i := range protos {
protos[i] = v.Index(i).Interface().(envoy_types.Resource)
}
return protos
}
// parseResources converts an []ResourceCache to a map[envoy_types.ResponseType]ResourceCache
// for faster indexing when creating new snapshots.
func parseResources(resources []ResourceCache) map[envoy_resource_v3.Type]ResourceCache {
resourceMap := make(map[envoy_resource_v3.Type]ResourceCache, len(resources))
for _, r := range resources {
resourceMap[r.TypeURL()] = r
}
return resourceMap
}