forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
176 lines (150 loc) · 5.63 KB
/
discovery.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2018 Istio 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 v2
import (
"os"
"sync"
"time"
xdsapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
ads "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"google.golang.org/grpc"
networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/networking/core/v1alpha3"
"istio.io/istio/pkg/log"
)
var (
// Failsafe to implement periodic refresh, in case events or cache invalidation fail.
// TODO: remove after events get enough testing
periodicRefreshDuration = os.Getenv("V2_REFRESH")
responseTickDuration = time.Second * 15
versionMutex sync.Mutex
// version is update by registry events.
version = time.Now()
)
const (
typePrefix = "type.googleapis.com/envoy.api.v2."
// Constants used for XDS
// ClusterType is used for cluster discovery. Typically first request received
ClusterType = typePrefix + "Cluster"
// EndpointType is used for EDS and ADS endpoint discovery. Typically second request.
EndpointType = typePrefix + "ClusterLoadAssignment"
// ListenerType is sent after clusters and endpoints.
ListenerType = typePrefix + "Listener"
// RouteType is sent after listeners.
RouteType = typePrefix + "RouteConfiguration"
)
// DiscoveryServer is Pilot's gRPC implementation for Envoy's v2 xds APIs
type DiscoveryServer struct {
// env is the model environment.
env model.Environment
// MemRegistry is used for debug and load testing, allow adding services. Visible for testing.
MemRegistry *MemServiceDiscovery
// ConfigGenerator is responsible for generating data plane configuration using Istio networking
// APIs and service registry info
ConfigGenerator *v1alpha3.ConfigGeneratorImpl
// The next fields are updated by v2 discovery, based on config change events (currently
// the global invalidation). They are computed once - will not change. The new alpha3
// API should use this instead of directly accessing ServiceDiscovery or IstioConfigStore.
modelMutex sync.RWMutex
services []*model.Service
virtualServices []*networking.VirtualService
// Temp: the code in alpha3 should use VirtualService directly
virtualServiceConfigs []model.Config
//TODO: gateways []*networking.Gateway
}
// NewDiscoveryServer creates DiscoveryServer that sources data from Pilot's internal mesh data structures
func NewDiscoveryServer(env model.Environment, generator *v1alpha3.ConfigGeneratorImpl) *DiscoveryServer {
out := &DiscoveryServer{
env: env,
ConfigGenerator: generator,
}
if len(periodicRefreshDuration) > 0 {
periodicRefresh()
}
return out
}
// Register adds the ADS and EDS handles to the grpc server
func (s *DiscoveryServer) Register(rpcs *grpc.Server) {
// EDS must remain registered for 0.8, for smooth upgrade from 0.7
// 0.7 proxies will use this service.
xdsapi.RegisterEndpointDiscoveryServiceServer(rpcs, s)
ads.RegisterAggregatedDiscoveryServiceServer(rpcs, s)
}
// Singleton, refresh the cache - may not be needed if events work properly, just a failsafe
// ( will be removed after change detection is implemented, to double check all changes are
// captured)
func periodicRefresh() {
var err error
responseTickDuration, err = time.ParseDuration(periodicRefreshDuration)
if err != nil {
return
}
ticker := time.NewTicker(responseTickDuration)
defer ticker.Stop()
for range ticker.C {
PushAll()
}
}
// PushAll implements old style invalidation, generated when any rule or endpoint changes.
// Primary code path is from v1 discoveryService.clearCache(), which is added as a handler
// to the model ConfigStorageCache and Controller.
func PushAll() {
versionMutex.Lock()
version = time.Now()
versionMutex.Unlock()
adsPushAll()
}
// ClearCacheFunc returns a function that invalidates v2 caches and triggers a push.
// This is used for transition, once the new config model is in place we'll have separate
// functions for each event and push only configs that need to be pushed.
// This is currently called from v1 and has attenuation/throttling.
func (s *DiscoveryServer) ClearCacheFunc() func() {
return func() {
s.updateModel()
s.modelMutex.RLock()
log.Infof("XDS: Registry event, pushing. Services: %d, "+
"VirtualServices: %d, ConnectedEndpoints: %d", len(s.services), len(s.virtualServices), edsClientCount())
s.modelMutex.RUnlock()
PushAll()
}
}
func (s *DiscoveryServer) updateModel() {
s.modelMutex.Lock()
defer s.modelMutex.Unlock()
services, err := s.env.Services()
if err != nil {
log.Errorf("XDS: failed to update services %v", err)
} else {
s.services = services
}
vservices, err := s.env.List(model.VirtualService.Type, model.NamespaceAll)
if err != nil {
log.Errorf("XDS: failed to update virtual services %v", err)
} else {
s.virtualServiceConfigs = vservices
s.virtualServices = make([]*networking.VirtualService, len(vservices))
for _, ss := range vservices {
s.virtualServices = append(s.virtualServices, ss.Spec.(*networking.VirtualService))
}
}
}
func nonce() string {
return time.Now().String()
}
func versionInfo() string {
versionMutex.Lock()
defer versionMutex.Unlock()
return version.String()
}