forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
129 lines (102 loc) · 3.8 KB
/
resources.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
// Copyright © 2017 Heptio
// 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 grpc
import (
"sort"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/gogo/protobuf/proto"
)
// Resource types in xDS v2.
const (
googleApis = "type.googleapis.com/"
typePrefix = googleApis + "envoy.api.v2."
endpointType = typePrefix + "ClusterLoadAssignment"
clusterType = typePrefix + "Cluster"
routeType = typePrefix + "RouteConfiguration"
listenerType = typePrefix + "Listener"
)
// cache represents a source of proto.Message valus that can be registered
// for interest.
type Cache interface {
// Values returns a slice of proto.Message implementations that match
// the provided filter.
Values(func(string) bool) []proto.Message
// Register registers ch to receive a value when Notify is called.
Register(chan int, int)
}
// CDS implements the CDS v2 gRPC API.
type CDS struct {
Cache
}
// Values returns a sorted list of Clusters.
func (c *CDS) Values(filter func(string) bool) []proto.Message {
v := c.Cache.Values(filter)
sort.Stable(clusterByName(v))
return v
}
func (c *CDS) TypeURL() string { return clusterType }
type clusterByName []proto.Message
func (c clusterByName) Len() int { return len(c) }
func (c clusterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c clusterByName) Less(i, j int) bool { return c[i].(*v2.Cluster).Name < c[j].(*v2.Cluster).Name }
// EDS implements the EDS v2 gRPC API.
type EDS struct {
Cache
}
// Values returns a sorted list of ClusterLoadAssignments.
func (e *EDS) Values(filter func(string) bool) []proto.Message {
v := e.Cache.Values(filter)
sort.Stable(clusterLoadAssignmentsByName(v))
return v
}
func (e *EDS) TypeURL() string { return endpointType }
type clusterLoadAssignmentsByName []proto.Message
func (c clusterLoadAssignmentsByName) Len() int { return len(c) }
func (c clusterLoadAssignmentsByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c clusterLoadAssignmentsByName) Less(i, j int) bool {
return c[i].(*v2.ClusterLoadAssignment).ClusterName < c[j].(*v2.ClusterLoadAssignment).ClusterName
}
// LDS implements the LDS v2 gRPC API.
type LDS struct {
Cache
}
// Values returns a sorted list of Listeners.
func (l *LDS) Values(filter func(string) bool) []proto.Message {
v := l.Cache.Values(filter)
sort.Stable(listenersByName(v))
return v
}
func (l *LDS) TypeURL() string { return listenerType }
type listenersByName []proto.Message
func (l listenersByName) Len() int { return len(l) }
func (l listenersByName) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l listenersByName) Less(i, j int) bool {
return l[i].(*v2.Listener).Name < l[j].(*v2.Listener).Name
}
// RDS implements the RDS v2 gRPC API.
type RDS struct {
Cache
}
// Values returns a sorted list of RouteConfigurations.
func (r *RDS) Values(filter func(string) bool) []proto.Message {
v := r.Cache.Values(filter)
sort.Stable(routeConfigurationsByName(v))
return v
}
func (r *RDS) TypeURL() string { return routeType }
type routeConfigurationsByName []proto.Message
func (r routeConfigurationsByName) Len() int { return len(r) }
func (r routeConfigurationsByName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r routeConfigurationsByName) Less(i, j int) bool {
return r[i].(*v2.RouteConfiguration).Name < r[j].(*v2.RouteConfiguration).Name
}