-
Notifications
You must be signed in to change notification settings - Fork 10
/
snapshotter.go
162 lines (139 loc) · 4.63 KB
/
snapshotter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
package snapshot
import (
"context"
"sync"
"time"
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/envoyproxy/go-control-plane/pkg/log"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/wongnai/xds/meter"
"go.opentelemetry.io/otel/metric"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
var Logger log.Logger = &log.LoggerFuncs{
DebugFunc: func(s string, i ...interface{}) {
klog.V(4).Infof(s, i...)
},
InfoFunc: func(s string, i ...interface{}) {
klog.V(2).Infof(s, i...)
},
WarnFunc: func(s string, i ...interface{}) {
klog.Warningf(s, i...)
},
ErrorFunc: func(s string, i ...interface{}) {
klog.Errorf(s, i...)
},
}
func mapTypeURL(typeURL string) string {
switch typeURL {
case resource.ListenerType, resource.RouteType, resource.ClusterType:
return "services"
case resource.EndpointType:
return "endpoints"
default:
return ""
}
}
type Snapshotter struct {
ResyncPeriod time.Duration
client kubernetes.Interface
servicesCache cache.SnapshotCache
endpointsCache cache.SnapshotCache
muxCache cache.MuxCache
endpointResourceCache map[string]endpointCacheItem
resourcesByTypeLock sync.RWMutex
serviceResourcesByType map[string][]types.Resource
endpointResourcesByType map[string][]types.Resource
apiGatewayStats map[string]int
kubeEventCounter metric.Int64Counter
}
func New(client kubernetes.Interface) *Snapshotter {
servicesCache := cache.NewSnapshotCache(false, EmptyNodeID{}, Logger)
endpointsCache := cache.NewSnapshotCache(false, EmptyNodeID{}, Logger)
muxCache := cache.MuxCache{
Classify: func(r *cache.Request) string {
return mapTypeURL(r.TypeUrl)
},
ClassifyDelta: func(r *cache.DeltaRequest) string {
return mapTypeURL(r.TypeUrl)
},
Caches: map[string]cache.Cache{
"services": servicesCache,
"endpoints": endpointsCache,
},
}
ss := &Snapshotter{
ResyncPeriod: 10 * time.Minute,
client: client,
servicesCache: servicesCache,
endpointsCache: endpointsCache,
muxCache: muxCache,
endpointResourceCache: map[string]endpointCacheItem{},
}
meter := meter.GetMeter()
ss.kubeEventCounter, _ = meter.Int64Counter("xds_kube_events")
meter.Int64ObservableGauge("xds_snapshot_resources", metric.WithInt64Callback(ss.snapshotResourceGaugeCallback))
meter.Int64ObservableGauge("xds_apigateway_endpoints", metric.WithInt64Callback(ss.apiGatewayEndpointGaugeCallback))
return ss
}
func (s *Snapshotter) MuxCache() *cache.MuxCache {
return &s.muxCache
}
func (s *Snapshotter) Start(stopCtx context.Context) error {
group, groupCtx := errgroup.WithContext(stopCtx)
group.Go(func() error {
return s.startServices(groupCtx)
})
group.Go(func() error {
return s.startEndpoints(groupCtx)
})
return group.Wait()
}
func (s *Snapshotter) snapshotResourceGaugeCallback(_ context.Context, result metric.Int64Observer) error {
for k, r := range s.getServiceResourcesByType() {
result.Observe(int64(len(r)), metric.WithAttributes(meter.TypeURLAttrKey.String(k)))
}
for k, r := range s.getEndpointResourcesByType() {
result.Observe(int64(len(r)), metric.WithAttributes(meter.TypeURLAttrKey.String(k)))
}
return nil
}
func (s *Snapshotter) apiGatewayEndpointGaugeCallback(_ context.Context, result metric.Int64Observer) error {
for k, stat := range s.getAPIGatewayStats() {
result.Observe(int64(stat), metric.WithAttributes(meter.APIGatewayAttrKey.String(k)))
}
return nil
}
func (s *Snapshotter) setServiceResourcesByType(serviceResourcesByType map[string][]types.Resource) {
s.resourcesByTypeLock.Lock()
defer s.resourcesByTypeLock.Unlock()
s.serviceResourcesByType = serviceResourcesByType
}
func (s *Snapshotter) getServiceResourcesByType() map[string][]types.Resource {
s.resourcesByTypeLock.RLock()
defer s.resourcesByTypeLock.RUnlock()
return s.serviceResourcesByType
}
func (s *Snapshotter) setEndpointResourcesByType(endpointResourcesByType map[string][]types.Resource) {
s.resourcesByTypeLock.Lock()
defer s.resourcesByTypeLock.Unlock()
s.endpointResourcesByType = endpointResourcesByType
}
func (s *Snapshotter) getEndpointResourcesByType() map[string][]types.Resource {
s.resourcesByTypeLock.RLock()
defer s.resourcesByTypeLock.RUnlock()
return s.endpointResourcesByType
}
func (s *Snapshotter) setAPIGatewayStats(apiGatewayStats map[string]int) {
s.resourcesByTypeLock.Lock()
defer s.resourcesByTypeLock.Unlock()
s.apiGatewayStats = apiGatewayStats
}
func (s *Snapshotter) getAPIGatewayStats() map[string]int {
s.resourcesByTypeLock.RLock()
defer s.resourcesByTypeLock.RUnlock()
return s.apiGatewayStats
}