-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource.go
149 lines (121 loc) · 3.6 KB
/
resource.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
142
143
144
145
146
147
148
149
package xds
import (
"strings"
"sync"
clusterv3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
endpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listenerv3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
typesv3 "github.com/envoyproxy/go-control-plane/pkg/cache/types"
cachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
)
type resource struct {
mutex *sync.RWMutex
endpoints []*endpointv3.ClusterLoadAssignment
endpointsVersion string
clusters []*clusterv3.Cluster
clustersVersion string
route *routev3.RouteConfiguration
routeVersion string
listener *listenerv3.Listener
listenerVersion string
}
func (r *resource) updateListener(version string, listener *listenerv3.Listener) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.listenerVersion = version
r.listener = listener
}
func (r *resource) currentListener() (string, *listenerv3.Listener) {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.listenerVersion, r.listener
}
func (r *resource) updateRoute(version string, route *routev3.RouteConfiguration) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.routeVersion = version
r.route = route
}
func (r *resource) currentRoute() (string, *routev3.RouteConfiguration) {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.routeVersion, r.route
}
func (r *resource) updateCluster(version string, clusters []*clusterv3.Cluster) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.clustersVersion = version
r.clusters = clusters
}
func (r *resource) currentCluster() (string, []*clusterv3.Cluster) {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.clustersVersion, r.clusters
}
func (r *resource) updateEndpoint(version string, endpoints []*endpointv3.ClusterLoadAssignment) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.endpointsVersion = version
r.endpoints = endpoints
}
func (r *resource) currentEndpoint() (string, []*endpointv3.ClusterLoadAssignment) {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.endpointsVersion, r.endpoints
}
func (r *resource) version() string {
return versionString(
r.endpointsVersion,
r.clustersVersion,
r.routeVersion,
r.listenerVersion,
)
}
func (r *resource) Snapshot() (string, *cachev3.Snapshot, error) {
r.mutex.RLock()
defer r.mutex.RUnlock()
endpoints := make([]typesv3.Resource, len(r.endpoints))
for i, e := range r.endpoints {
endpoints[i] = e
}
clusters := make([]typesv3.Resource, len(r.clusters))
for i, c := range r.clusters {
clusters[i] = c
}
version := r.version()
snapshot, err := cachev3.NewSnapshot(
version,
map[resourcev3.Type][]typesv3.Resource{
resourcev3.EndpointType: endpoints,
resourcev3.ClusterType: clusters,
resourcev3.RouteType: []typesv3.Resource{r.route},
resourcev3.ListenerType: []typesv3.Resource{r.listener},
},
)
if err != nil {
return "", &cachev3.Snapshot{}, err
}
// validate variables
if err := snapshot.Consistent(); err != nil {
return "", &cachev3.Snapshot{}, err
}
return version, snapshot, nil
}
func newResource() *resource {
return &resource{
mutex: new(sync.RWMutex),
endpoints: nil,
endpointsVersion: "0",
clusters: nil,
clustersVersion: "0",
route: nil,
routeVersion: "0",
listener: nil,
listenerVersion: "0",
}
}
func versionString(endpoint, cluster, route, listener string) string {
return strings.Join([]string{endpoint, cluster, route, listener}, ".")
}