/
clusterstate.go
119 lines (98 loc) · 2.73 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
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
secrets map[definitions.ResourceID]*secret
cachedEndpoints map[endpointID][]string
}
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
}
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
}
ep, ok := state.endpoints[epID.ResourceID]
if !ok {
return nil
}
targets := ep.targetsByServicePort(protocol, servicePort)
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}
func (state *clusterState) GetEndpointsByName(namespace, name, protocol 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
}
ep, ok := state.endpoints[epID.ResourceID]
if !ok {
return nil
}
targets := ep.targets(protocol)
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}
func (state *clusterState) GetEndpointsByTarget(namespace, name, protocol 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
}
ep, ok := state.endpoints[epID.ResourceID]
if !ok {
return nil
}
targets := ep.targetsByServiceTarget(protocol, target)
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}