-
Notifications
You must be signed in to change notification settings - Fork 343
/
clusterstate.go
90 lines (72 loc) · 1.97 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
package kubernetes
import (
"fmt"
"sort"
log "github.com/sirupsen/logrus"
)
type clusterState struct {
ingresses []*ingressItem
routeGroups []*routeGroupItem
services map[resourceID]*service
endpoints map[resourceID]*endpoint
cachedEndpoints map[endpointID][]string
}
func (state *clusterState) getService(namespace, name string) (*service, error) {
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) {
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) getEndpoints(namespace, name, servicePort, targetPort, protocol string) ([]string, error) {
epID := endpointID{
resourceID: newResourceID(namespace, name),
servicePort: servicePort,
targetPort: targetPort,
}
if cached, ok := state.cachedEndpoints[epID]; ok {
return cached, nil
}
ep, ok := state.endpoints[epID.resourceID]
if !ok {
return nil, errEndpointNotFound
}
if ep.Subsets == nil {
return nil, errEndpointNotFound
}
targets := ep.targets(servicePort, targetPort, protocol)
if len(targets) == 0 {
return nil, errEndpointNotFound
}
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets, nil
}
func (state *clusterState) getEndpointsByTarget(namespace, name string, target *backendPort) []string {
epID := endpointID{
resourceID: newResourceID(namespace, name),
targetPort: target.String(),
}
if cached, ok := state.cachedEndpoints[epID]; ok {
return cached
}
ep, ok := state.endpoints[epID.resourceID]
if !ok {
return nil
}
targets := ep.targetsByServiceTarget(target)
sort.Strings(targets)
state.cachedEndpoints[epID] = targets
return targets
}