-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
resources.go
62 lines (54 loc) · 1.64 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
package xds
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/consul/agent/proxycfg"
)
// ResourceGenerator is associated with a single gRPC stream and creates xDS
// resources for a single client.
type ResourceGenerator struct {
Logger hclog.Logger
CheckFetcher HTTPCheckFetcher
CfgFetcher ConfigFetcher
IncrementalXDS bool
ProxyFeatures supportedProxyFeatures
}
func newResourceGenerator(
logger hclog.Logger,
checkFetcher HTTPCheckFetcher,
cfgFetcher ConfigFetcher,
incrementalXDS bool,
) *ResourceGenerator {
return &ResourceGenerator{
Logger: logger,
CheckFetcher: checkFetcher,
CfgFetcher: cfgFetcher,
IncrementalXDS: incrementalXDS,
}
}
func (g *ResourceGenerator) allResourcesFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot) (map[string][]proto.Message, error) {
all := make(map[string][]proto.Message)
for _, typeUrl := range []string{ListenerType, RouteType, ClusterType, EndpointType} {
res, err := g.resourcesFromSnapshot(typeUrl, cfgSnap)
if err != nil {
return nil, fmt.Errorf("failed to generate xDS resources for %q: %v", typeUrl, err)
}
all[typeUrl] = res
}
return all, nil
}
func (g *ResourceGenerator) resourcesFromSnapshot(typeUrl string, cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
switch typeUrl {
case ListenerType:
return g.listenersFromSnapshot(cfgSnap)
case RouteType:
return g.routesFromSnapshot(cfgSnap)
case ClusterType:
return g.clustersFromSnapshot(cfgSnap)
case EndpointType:
return g.endpointsFromSnapshot(cfgSnap)
default:
return nil, fmt.Errorf("unknown typeUrl: %s", typeUrl)
}
}