This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
publish.go
265 lines (225 loc) · 6.34 KB
/
publish.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
265
package ports
import (
"fmt"
"sort"
"github.com/acorn-io/baaah/pkg/typed"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
corev1 "k8s.io/api/core/v1"
)
var clusterDomainHTTPDef = ListenDef{
Protocol: v1.ProtocolHTTP,
}
func LinkService(app *v1.AppInstance, name string) string {
if name == "" {
return ""
}
for _, binding := range app.Spec.Links {
if binding.Target == name {
return binding.Service
}
}
return ""
}
func IsLinked(app *v1.AppInstance, name string) bool {
if name == "" {
return false
}
for _, binding := range app.Spec.Links {
if binding.Target == name {
return true
}
}
return false
}
func ByProtocol(ports []v1.PortDef, include bool, protocols ...v1.Protocol) (result []v1.PortDef) {
for _, port := range ports {
matched := false
for _, proto := range protocols {
if port.Complete().Protocol == proto {
matched = true
break
}
}
if matched == include {
result = append(result, port)
}
}
return result
}
type BoundPorts map[ListenDef][]v1.PortDef
func (b BoundPorts) ServicePorts() (result []corev1.ServicePort, err error) {
for listen, ports := range b {
if len(ports) > 1 {
l := typed.MapSlice(ports, func(t v1.PortDef) string {
return t.FormatString("")
})
return nil, fmt.Errorf("port [%d] is bound to [%d] ports %v, can only be bound to 1",
listen.Port, len(ports), l)
}
port := ports[0].Complete()
if listen.Port != 0 {
port.Port = listen.Port
}
result = append(result, ToServicePort(port))
}
sort.Slice(result, func(i, j int) bool {
return result[i].Name < result[j].Name
})
return
}
func (b BoundPorts) ByHostname() map[string][]v1.PortDef {
byHostname := map[string][]v1.PortDef{}
for k, v := range b {
byHostname[k.Hostname] = v
}
return byHostname
}
type ListenDef struct {
// Hostname is empty if Protocol is not http
Hostname string
// Port if tcp or udp, for http this should always be 0
Port int32
Protocol v1.Protocol
}
func listenDefFromPort(port v1.PortDef) ListenDef {
def := ListenDef{
Protocol: port.Protocol,
}
if port.Protocol == v1.ProtocolHTTP {
def.Hostname = port.Hostname
} else {
def.Port = port.Complete().Port
}
return def
}
func PortPublishForService(serviceName string, bindings []v1.PortBinding) (result []v1.PortPublish) {
for _, binding := range bindings {
if serviceMatches(serviceName, binding) {
result = append(result, v1.PortPublish{
Port: binding.Port,
Protocol: binding.Protocol,
Hostname: binding.Hostname,
TargetPort: binding.TargetPort,
})
}
}
return
}
func ApplyBindings(mode v1.PublishMode, bindings []v1.PortPublish, ports []v1.PortDef) (result BoundPorts) {
result = BoundPorts{}
if mode == v1.PublishModeNone {
return nil
}
for _, port := range ports {
var (
published bool
)
for _, binding := range bindings {
if matches(binding, port) {
published = true
def := listenDefFromPort(port)
if binding.Hostname != "" && port.Protocol == v1.ProtocolHTTP {
def.Hostname = binding.Hostname
} else if binding.Port != 0 && port.Protocol != v1.ProtocolHTTP {
def.Port = binding.Port
}
result[def] = append(result[def], port)
}
}
if !published && (mode == v1.PublishModeAll || port.Publish) {
published = true
def := listenDefFromPort(port)
result[def] = append(result[def], port)
}
if published && port.Protocol == v1.ProtocolHTTP {
found := false
for _, existingPort := range result[clusterDomainHTTPDef] {
if existingPort == port {
found = true
break
}
}
if !found {
result[clusterDomainHTTPDef] = append(result[clusterDomainHTTPDef], port)
}
}
}
return
}
func portMatches(binding v1.PortPublish, port v1.PortDef) bool {
return binding.TargetPort == 0 || binding.TargetPort == port.Port
}
func serviceMatches(serviceName string, binding v1.PortBinding) bool {
// deal with deprecated fields
if binding.Expose && !binding.Publish {
return false
}
return binding.TargetServiceName == "" || binding.TargetServiceName == serviceName
}
func protoMatches(binding v1.PortPublish, port v1.PortDef) bool {
return binding.Protocol == "" ||
binding.Protocol == port.Protocol
}
func matches(binding v1.PortPublish, port v1.PortDef) bool {
port = port.Complete()
binding = binding.Complete()
return protoMatches(binding, port) &&
portMatches(binding, port)
}
func collectPorts(seen map[int32][]int32, seenHostnames map[string]struct{}, ports []v1.PortDef, devMode bool) (result []v1.PortDef) {
for _, port := range ports {
if !devMode && port.Dev {
continue
}
// Can't have any duplicate hostnames, so check for that first
if _, exists := seenHostnames[port.Hostname]; port.Hostname != "" && exists {
continue
}
// If port.Port is 0, that means only the TargetPort has been defined, and not the public-facing Port.
// The public-facing Port will ultimately use the same number as the TargetPort, so we'll set it here
// so that the logic is correct for the rest of this function.
if port.Port == 0 {
port.Port = port.TargetPort
}
if targets, ok := seen[port.Port]; ok {
// Check for special case: the same port is exposed on multiple hostnames, so keep both.
if port.Hostname != "" {
for _, t := range targets {
if t == port.TargetPort {
// Same port and target port but different hostnames, so keep both
seen[port.Port] = append(targets, port.TargetPort)
seenHostnames[port.Hostname] = struct{}{}
result = append(result, port)
break
}
}
}
continue
}
seen[port.Port] = []int32{port.TargetPort}
if port.Hostname != "" {
seenHostnames[port.Hostname] = struct{}{}
}
result = append(result, port)
}
return
}
func FilterDevPorts(ports []v1.PortDef, devMode bool) (result []v1.PortDef) {
for _, port := range ports {
if port.Dev && !devMode {
continue
}
result = append(result, port)
}
return
}
func CollectContainerPorts(container *v1.Container, devMode bool) (result []v1.PortDef) {
// seen represents a mapping of public port numbers to target port numbers
seen := map[int32][]int32{}
seenHostnames := map[string]struct{}{}
result = append(result, collectPorts(seen, seenHostnames, container.Ports, devMode)...)
for _, entry := range typed.Sorted(container.Sidecars) {
result = append(result, collectPorts(seen, seenHostnames, entry.Value.Ports, devMode)...)
}
return
}