forked from kubernetes/ingress-gce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
264 lines (234 loc) · 7.63 KB
/
manager.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright 2017 The Kubernetes 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 neg
import (
"fmt"
"sync"
"github.com/golang/glog"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
)
type serviceKey struct {
namespace string
name string
}
// syncerManager contains all the active syncer goroutines and manage their lifecycle.
type syncerManager struct {
namer networkEndpointGroupNamer
recorder record.EventRecorder
cloud networkEndpointGroupCloud
zoneGetter zoneGetter
serviceLister cache.Indexer
endpointLister cache.Indexer
// TODO: lock per service instead of global lock
mu sync.Mutex
// svcPortMap is the canonical indicator for whether a service needs NEG.
// key consists of service namespace and name. Value is the list of target ports that requires NEG
svcPortMap map[serviceKey]sets.String
// syncerMap stores the NEG syncer
// key consists of service namespace, name and targetPort. Value is the corresponding syncer.
syncerMap map[servicePort]negSyncer
}
func newSyncerManager(namer networkEndpointGroupNamer, recorder record.EventRecorder, cloud networkEndpointGroupCloud, zoneGetter zoneGetter, serviceLister cache.Indexer, endpointLister cache.Indexer) *syncerManager {
return &syncerManager{
namer: namer,
recorder: recorder,
cloud: cloud,
zoneGetter: zoneGetter,
serviceLister: serviceLister,
endpointLister: endpointLister,
svcPortMap: make(map[serviceKey]sets.String),
syncerMap: make(map[servicePort]negSyncer),
}
}
// EnsureSyncer starts and stops syncers based on the input service ports.
func (manager *syncerManager) EnsureSyncers(namespace, name string, targetPorts sets.String) error {
manager.mu.Lock()
defer manager.mu.Unlock()
key := getServiceKey(namespace, name)
currentPorts, ok := manager.svcPortMap[key]
if !ok {
currentPorts = sets.NewString()
}
removes := currentPorts.Difference(targetPorts).List()
adds := targetPorts.Difference(currentPorts).List()
manager.svcPortMap[key] = targetPorts
glog.V(3).Infof("EnsureSyncer %v/%v: removing %v ports, adding %v ports", namespace, name, removes, adds)
// Stop syncer for removed ports
for _, port := range removes {
syncer, ok := manager.syncerMap[getSyncerKey(namespace, name, port)]
if ok {
syncer.Stop()
}
}
errList := []error{}
// Ensure a syncer is running for each port that is being added.
for _, port := range adds {
syncer, ok := manager.syncerMap[getSyncerKey(namespace, name, port)]
if !ok {
syncer = newSyncer(
servicePort{
namespace: namespace,
name: name,
targetPort: port,
},
manager.namer.NEG(namespace, name, port),
manager.recorder,
manager.cloud,
manager.zoneGetter,
manager.serviceLister,
manager.endpointLister,
)
manager.syncerMap[getSyncerKey(namespace, name, port)] = syncer
}
if syncer.IsStopped() {
if err := syncer.Start(); err != nil {
errList = append(errList, err)
}
}
}
return utilerrors.NewAggregate(errList)
}
// StopSyncer stops all syncers for the input service.
func (manager *syncerManager) StopSyncer(namespace, name string) {
manager.mu.Lock()
defer manager.mu.Unlock()
key := getServiceKey(namespace, name)
if ports, ok := manager.svcPortMap[key]; ok {
for _, port := range ports.List() {
if syncer, ok := manager.syncerMap[getSyncerKey(namespace, name, port)]; ok {
syncer.Stop()
}
}
delete(manager.svcPortMap, key)
}
return
}
// Sync signals all syncers related to the service to sync.
func (manager *syncerManager) Sync(namespace, name string) {
manager.mu.Lock()
defer manager.mu.Unlock()
key := getServiceKey(namespace, name)
if portList, ok := manager.svcPortMap[key]; ok {
for _, port := range portList.List() {
if syncer, ok := manager.syncerMap[getSyncerKey(namespace, name, port)]; ok {
if !syncer.IsStopped() {
syncer.Sync()
}
}
}
}
}
// ShutDown signals all syncers to stop
func (manager *syncerManager) ShutDown() {
manager.mu.Lock()
defer manager.mu.Unlock()
for _, s := range manager.syncerMap {
s.Stop()
}
}
// GC garbage collects syncers and NEGs.
func (manager *syncerManager) GC() error {
glog.V(2).Infof("Start NEG garbage collection.")
defer glog.V(2).Infof("NEG garbage collection finished.")
for _, key := range manager.getAllStoppedSyncerKeys() {
manager.garbageCollectSyncer(key)
}
// Garbage collect NEGs
if err := manager.garbageCollectNEG(); err != nil {
return fmt.Errorf("Failed to garbage collect negs: %v", err)
}
return nil
}
func (manager *syncerManager) garbageCollectSyncer(key servicePort) {
manager.mu.Lock()
defer manager.mu.Unlock()
if manager.syncerMap[key].IsStopped() && !manager.syncerMap[key].IsShuttingDown() {
delete(manager.syncerMap, key)
}
}
func (manager *syncerManager) getAllStoppedSyncerKeys() []servicePort {
manager.mu.Lock()
defer manager.mu.Unlock()
ret := []servicePort{}
for key, syncer := range manager.syncerMap {
if syncer.IsStopped() {
ret = append(ret, key)
}
}
return ret
}
func (manager *syncerManager) garbageCollectNEG() error {
// Retrieve aggregated NEG list from cloud
// Compare against svcPortMap and Remove unintended NEGs by best effort
zoneNEGList, err := manager.cloud.AggregatedListNetworkEndpointGroup()
if err != nil {
return fmt.Errorf("failed to retrieve aggregated NEG list: %v", err)
}
negNames := sets.String{}
for _, list := range zoneNEGList {
for _, neg := range list {
if manager.namer.IsNEG(neg.Name) {
negNames.Insert(neg.Name)
}
}
}
func() {
manager.mu.Lock()
defer manager.mu.Unlock()
for key, ports := range manager.svcPortMap {
for _, port := range ports.List() {
name := manager.namer.NEG(key.namespace, key.name, port)
negNames.Delete(name)
}
}
}()
// This section includes a potential race condition between deleting neg here and users adds the neg annotation.
// The worst outcome of the race condition is that neg is deleted in the end but user actually specifies a neg.
// This would be resolved (sync neg) when the next endpoint update or resync arrives.
// TODO: avoid race condition here
for zone := range zoneNEGList {
for _, name := range negNames.List() {
if err := manager.ensureDeleteNetworkEndpointGroup(name, zone); err != nil {
return fmt.Errorf("failed to delete NEG %q in %q: %v", name, zone, err)
}
}
}
return nil
}
// ensureDeleteNetworkEndpointGroup ensures neg is delete from zone
func (manager *syncerManager) ensureDeleteNetworkEndpointGroup(name, zone string) error {
_, err := manager.cloud.GetNetworkEndpointGroup(name, zone)
if err != nil {
// Assume error is caused by not existing
return nil
}
glog.V(2).Infof("Deleting NEG %q in %q.", name, zone)
return manager.cloud.DeleteNetworkEndpointGroup(name, zone)
}
// getSyncerKey encodes a service namespace, name and targetPort into a string key
func getSyncerKey(namespace, name, port string) servicePort {
return servicePort{
namespace: namespace,
name: name,
targetPort: port,
}
}
func getServiceKey(namespace, name string) serviceKey {
return serviceKey{
namespace: namespace,
name: name,
}
}