-
Notifications
You must be signed in to change notification settings - Fork 343
/
ingressdefinitions.go
184 lines (147 loc) · 4.38 KB
/
ingressdefinitions.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
package kubernetes
import (
"fmt"
"strconv"
"github.com/zalando/skipper/dataclients/kubernetes/definitions"
)
type servicePort struct {
Name string `json:"name"`
Port int `json:"port"`
TargetPort *definitions.BackendPort `json:"targetPort"` // string or int
}
func (sp servicePort) matchingPort(svcPort definitions.BackendPort) bool {
s := svcPort.String()
spt := strconv.Itoa(sp.Port)
return s != "" && (spt == s || sp.Name == s)
}
func (sp servicePort) matchingPortV1(svcPort definitions.BackendPortV1) bool {
s := svcPort.String()
spt := strconv.Itoa(sp.Port)
return s != "" && (spt == s || sp.Name == s)
}
func (sp servicePort) String() string {
return fmt.Sprintf("%s %d %s", sp.Name, sp.Port, sp.TargetPort)
}
type serviceSpec struct {
Type string `json:"type"`
ClusterIP string `json:"clusterIP"`
ExternalName string `json:"externalName"`
Ports []*servicePort `json:"ports"`
}
type service struct {
Meta *definitions.Metadata `json:"Metadata"`
Spec *serviceSpec `json:"spec"`
}
type serviceList struct {
Items []*service `json:"items"`
}
func (s service) getServicePort(port definitions.BackendPort) (*servicePort, error) {
for _, sp := range s.Spec.Ports {
if sp.matchingPort(port) && sp.TargetPort != nil {
return sp, nil
}
}
return nil, fmt.Errorf("getServicePort: service port not found %v given %v", s.Spec.Ports, port)
}
func (s service) getServicePortV1(port definitions.BackendPortV1) (*servicePort, error) {
for _, sp := range s.Spec.Ports {
if sp.matchingPortV1(port) && sp.TargetPort != nil {
return sp, nil
}
}
return nil, fmt.Errorf("getServicePortV1: service port not found %v given %v", s.Spec.Ports, port)
}
func (s service) getTargetPortByValue(p int) (*definitions.BackendPort, bool) {
for _, pi := range s.Spec.Ports {
if pi.Port == p {
return pi.TargetPort, true
}
}
return nil, false
}
type endpoint struct {
Meta *definitions.Metadata `json:"Metadata"`
Subsets []*subset `json:"subsets"`
}
type endpointList struct {
Items []*endpoint `json:"items"`
}
func formatEndpoint(a *address, p *port, protocol string) string {
return fmt.Sprintf("%s://%s:%d", protocol, a.IP, p.Port)
}
func formatEndpointsForSubsetAddresses(addresses []*address, port *port, protocol string) []string {
var result []string
for _, address := range addresses {
result = append(result, formatEndpoint(address, port, protocol))
}
return result
}
func (ep endpoint) targetsByServicePort(protocol string, servicePort *servicePort) []string {
for _, s := range ep.Subsets {
// If only one port exists in the endpoint, use it
if len(s.Ports) == 1 {
return formatEndpointsForSubsetAddresses(s.Addresses, s.Ports[0], protocol)
}
// Otherwise match port by name
for _, p := range s.Ports {
if p.Name != servicePort.Name {
continue
}
return formatEndpointsForSubsetAddresses(s.Addresses, p, protocol)
}
}
return nil
}
func (ep endpoint) targetsByServiceTarget(protocol string, serviceTarget *definitions.BackendPort) []string {
portName, named := serviceTarget.Value.(string)
portValue, byValue := serviceTarget.Value.(int)
for _, s := range ep.Subsets {
for _, p := range s.Ports {
if named && p.Name != portName || byValue && p.Port != portValue {
continue
}
var result []string
for _, a := range s.Addresses {
result = append(result, formatEndpoint(a, p, protocol))
}
return result
}
}
return nil
}
type subset struct {
Addresses []*address `json:"addresses"`
Ports []*port `json:"ports"`
}
type address struct {
IP string `json:"ip"`
Node string `json:"nodeName"`
}
type port struct {
Name string `json:"name"`
Port int `json:"port"`
Protocol string `json:"protocol"`
}
func newResourceID(namespace, name string) definitions.ResourceID {
return definitions.ResourceID{Namespace: namespace, Name: name}
}
type endpointID struct {
definitions.ResourceID
targetPort string
protocol string
}
type ClusterResource struct {
Name string `json:"name"`
}
type ClusterResourceList struct {
// Items, aka "resources".
Items []*ClusterResource `json:"resources"`
}
type secret struct {
Metadata *definitions.Metadata `json:"metadata"`
Type string `json:"type"`
Data map[string]string `json:"data"`
}
type secretList struct {
Items []*secret `json:"items"`
}