-
Notifications
You must be signed in to change notification settings - Fork 343
/
clusterstate.go
152 lines (131 loc) · 3.96 KB
/
clusterstate.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
package kubernetes
import (
"fmt"
"sort"
"sync"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/dataclients/kubernetes/definitions"
)
type clusterState struct {
mu sync.Mutex
ingressesV1 []*definitions.IngressV1Item
routeGroups []*definitions.RouteGroupItem
services map[definitions.ResourceID]*service
endpoints map[definitions.ResourceID]*endpoint
endpointSlices map[definitions.ResourceID]*skipperEndpointSlice
secrets map[definitions.ResourceID]*secret
cachedEndpoints map[endpointID][]string
enableEndpointSlices bool
}
func (state *clusterState) getService(namespace, name string) (*service, error) {
state.mu.Lock()
defer state.mu.Unlock()
s, ok := state.services[newResourceID(namespace, name)]
if !ok {
return nil, errServiceNotFound
}
if s.Spec == nil {
log.Debug("invalid service datagram, missing spec")
return nil, errServiceNotFound
}
return s, nil
}
func (state *clusterState) getServiceRG(namespace, name string) (*service, error) {
state.mu.Lock()
defer state.mu.Unlock()
s, ok := state.services[newResourceID(namespace, name)]
if !ok {
return nil, fmt.Errorf("service not found: %s/%s", namespace, name)
}
return s, nil
}
// GetEndpointsByService returns the skipper endpoints for kubernetes endpoints or endpointslices.
func (state *clusterState) GetEndpointsByService(namespace, name, protocol string, servicePort *servicePort) []string {
epID := endpointID{
ResourceID: newResourceID(namespace, name),
Protocol: protocol,
TargetPort: servicePort.TargetPort.String(),
}
state.mu.Lock()
defer state.mu.Unlock()
if cached, ok := state.cachedEndpoints[epID]; ok {
return cached
}
var targets []string
if state.enableEndpointSlices {
if eps, ok := state.endpointSlices[epID.ResourceID]; ok {
targets = eps.targetsByServicePort("TCP", protocol, servicePort)
} else {
return nil
}
} else {
if ep, ok := state.endpoints[epID.ResourceID]; ok {
targets = ep.targetsByServicePort(protocol, servicePort)
} else {
return nil
}
}
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}
// GetEndpointsByName returns the skipper endpoints for kubernetes endpoints or endpointslices.
// This function works only correctly for endpointslices (and likely endpoints) with one port with the same protocol ("TCP", "UDP").
func (state *clusterState) GetEndpointsByName(namespace, name, protocol, scheme string) []string {
epID := endpointID{
ResourceID: newResourceID(namespace, name),
Protocol: protocol,
}
state.mu.Lock()
defer state.mu.Unlock()
if cached, ok := state.cachedEndpoints[epID]; ok {
return cached
}
var targets []string
if state.enableEndpointSlices {
if eps, ok := state.endpointSlices[epID.ResourceID]; ok {
targets = eps.targets(protocol, scheme)
} else {
return nil
}
} else {
if ep, ok := state.endpoints[epID.ResourceID]; ok {
targets = ep.targets(scheme)
} else {
return nil
}
}
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}
// GetEndpointsByTarget returns the skipper endpoints for kubernetes endpoints or endpointslices.
func (state *clusterState) GetEndpointsByTarget(namespace, name, protocol, scheme string, target *definitions.BackendPort) []string {
epID := endpointID{
ResourceID: newResourceID(namespace, name),
Protocol: protocol,
TargetPort: target.String(),
}
state.mu.Lock()
defer state.mu.Unlock()
if cached, ok := state.cachedEndpoints[epID]; ok {
return cached
}
var targets []string
if state.enableEndpointSlices {
if eps, ok := state.endpointSlices[epID.ResourceID]; ok {
targets = eps.targetsByServiceTarget(protocol, scheme, target)
} else {
return nil
}
} else {
if ep, ok := state.endpoints[epID.ResourceID]; ok {
targets = ep.targetsByServiceTarget(scheme, target)
} else {
return nil
}
}
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}