-
Notifications
You must be signed in to change notification settings - Fork 9
/
output.go
310 lines (269 loc) · 8.89 KB
/
output.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// nolint directives: stylecheck:sa1019
package storage
import (
"fmt"
"strings"
"namespacelabs.dev/foundation/framework/planning/render"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/uniquestrings"
fnschema "namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/storage"
)
type PortFwd struct {
Endpoint *fnschema.Endpoint
LocalPort uint32
}
func ToStorageNetworkPlan(localHostname string, stack *fnschema.Stack, focus []fnschema.PackageName, portFwds []*PortFwd, ingressFragments []*fnschema.IngressFragment) *storage.NetworkPlan {
r := &storage.NetworkPlan{
LocalHostname: localHostname,
}
r.FocusedServerPackages = fnschema.Strs(focus...)
for _, i := range ingressFragments {
domain := convertDomain(i.Domain)
endpoint := convertEndpoint(i.Endpoint, 0 /* localPort */, stack)
fragment := &storage.IngressFragment{
Name: i.Name,
Owner: i.Owner,
Domain: domain,
Endpoint: endpoint,
Manager: i.Manager,
}
for _, httpPath := range i.HttpPath {
fragment.HttpPath = append(fragment.HttpPath, convertHttpPath(httpPath))
}
for _, grpcService := range i.GrpcService {
fragment.GrpcService = append(fragment.GrpcService, convertGrpcService(grpcService))
}
r.IngressFragments = append(r.IngressFragments, fragment)
}
for _, pfwd := range portFwds {
endpoint := convertEndpoint(pfwd.Endpoint, pfwd.LocalPort, stack)
r.Endpoints = append(r.Endpoints, endpoint)
}
// TODO: remove once "internal" is migrated to "networkplanutils"
addDeprecatedFields(r)
return r
}
func convertDomain(d *fnschema.Domain) *storage.Domain {
if d == nil {
return nil
}
managedType, _ := convertManagedType(d.Managed)
return &storage.Domain{
Fqdn: d.Fqdn,
Managed: managedType,
TlsFrontend: d.TlsFrontend,
}
}
func convertGrpcService(s *fnschema.IngressFragment_IngressGrpcService) *storage.IngressGrpcService {
return &storage.IngressGrpcService{
GrpcService: s.GrpcService,
Owner: s.Owner,
Service: s.Service,
Method: s.Method,
ServicePort: s.ServicePort,
BackendTls: s.BackendTls,
AllServices: s.AllServices,
}
}
func convertEndpoint(endpoint *fnschema.Endpoint, localPort uint32, stack *fnschema.Stack) *storage.Endpoint {
if endpoint == nil {
return nil
}
entry := stack.GetServer(fnschema.PackageName(endpoint.EndpointOwner))
serverName := ""
if entry != nil {
serverName = entry.Server.Name
}
endpointType, _ := convertEndpointType(endpoint.Type)
result := &storage.Endpoint{
Type: endpointType,
ServiceName: endpoint.ServiceName,
EndpointOwner: endpoint.EndpointOwner,
Ports: endpoint.Ports,
AllocatedName: endpoint.AllocatedName,
ServerOwner: endpoint.ServerOwner,
ServiceLabel: endpoint.ServiceLabel,
LocalPort: localPort,
ServerName: serverName,
}
for _, m := range endpoint.ServiceMetadata {
result.ServiceMetadata = append(result.ServiceMetadata, &storage.Endpoint_ServiceMetadata{
Kind: m.Kind,
Protocol: m.Protocol,
})
}
return result
}
func convertEndpointType(t fnschema.Endpoint_Type) (storage.Endpoint_Type, error) {
switch t {
case fnschema.Endpoint_INGRESS_UNSPECIFIED:
return storage.Endpoint_INGRESS_UNSPECIFIED, nil
case fnschema.Endpoint_PRIVATE:
return storage.Endpoint_PRIVATE, nil
case fnschema.Endpoint_INTERNET_FACING:
return storage.Endpoint_INTERNET_FACING, nil
case fnschema.Endpoint_LOAD_BALANCER:
return storage.Endpoint_LOAD_BALANCER, nil
default:
return storage.Endpoint_INGRESS_UNSPECIFIED, fnerrors.InternalError("unknown endpoint type: %s", t)
}
}
func convertHttpPath(httpPath *fnschema.IngressFragment_IngressHttpPath) *storage.IngressHttpPath {
return &storage.IngressHttpPath{
Path: httpPath.Path,
Kind: httpPath.Kind,
Owner: httpPath.Owner,
Service: httpPath.Service,
ServicePort: httpPath.ServicePort,
}
}
func convertManagedType(managed fnschema.Domain_ManagedType) (storage.Domain_ManagedType, error) {
switch managed {
case fnschema.Domain_MANAGED_UNKNOWN:
return storage.Domain_MANAGED_UNKNOWN, nil
case fnschema.Domain_LOCAL_MANAGED:
return storage.Domain_LOCAL_MANAGED, nil
case fnschema.Domain_CLOUD_MANAGED:
return storage.Domain_CLOUD_MANAGED, nil
case fnschema.Domain_CLOUD_TERMINATION:
return storage.Domain_CLOUD_TERMINATION, nil
case fnschema.Domain_USER_SPECIFIED:
return storage.Domain_USER_SPECIFIED, nil
case fnschema.Domain_USER_SPECIFIED_TLS_MANAGED:
return storage.Domain_USER_SPECIFIED_TLS_MANAGED, nil
default:
return storage.Domain_MANAGED_UNKNOWN, fnerrors.InternalError("unknown domain managed type: %s", managed)
}
}
// Deprecated
func addDeprecatedFields(r *storage.NetworkPlan) {
var nonLocalManaged, nonLocalNonManaged []*filteredDomain
domains := filterAndDedupDomains(r.IngressFragments, nil)
for _, n := range domains {
// Local domains need `ns dev` for port forwarding.
if n.Domain.GetManaged() == storage.Domain_USER_SPECIFIED {
nonLocalNonManaged = append(nonLocalNonManaged, n)
} else if n.Domain.GetManaged() == storage.Domain_CLOUD_MANAGED || n.Domain.GetManaged() == storage.Domain_USER_SPECIFIED_TLS_MANAGED {
nonLocalManaged = append(nonLocalManaged, n)
}
}
r.NonLocalManaged = renderIngressBlock(nonLocalManaged)
r.NonLocalNonManaged = renderIngressBlock(nonLocalNonManaged)
summary := render.NetworkPlanToSummary(r)
services := append(summary.FocusedServices, summary.SupportServices...)
for _, s := range services {
endpoint := &storage.NetworkPlan_Endpoint{
Label: &storage.NetworkPlan_Label{Label: s.Label.Label, ServiceProto: s.Label.ServiceProto},
Focus: s.Focus,
LocalPort: s.LocalPort,
EndpointOwner: s.PackageName,
}
for _, cmd := range s.AccessCmd {
endpoint.AccessCmd = append(endpoint.AccessCmd, &storage.NetworkPlan_AccessCmd{
Cmd: cmd.Cmd, IsManaged: cmd.IsManaged})
}
r.Endpoint = append(r.Endpoint, endpoint)
}
}
// Deprecated
type filteredDomain struct {
Domain *storage.Domain
Endpoints []*storage.Endpoint
}
// Deprecated
func filterAndDedupDomains(fragments []*storage.IngressFragment, filter func(*storage.Domain) bool) []*filteredDomain {
seen := map[string]*filteredDomain{} // Map fqdn:type to schema.
domains := []*filteredDomain{}
for _, frag := range fragments {
d := frag.Domain
if d.GetFqdn() == "" {
continue
}
if filter != nil && !filter(d) {
continue
}
key := fmt.Sprintf("%s:%s", d.GetFqdn(), d.GetManaged())
if _, ok := seen[key]; !ok {
fd := &filteredDomain{Domain: d}
domains = append(domains, fd)
seen[key] = fd
}
if frag.Endpoint != nil {
seen[key].Endpoints = append(seen[key].Endpoints, frag.Endpoint)
}
}
return domains
}
// Deprecated
func renderIngressBlock(fragments []*filteredDomain) []*storage.NetworkPlan_Ingress {
var ingresses []*storage.NetworkPlan_Ingress
for _, n := range fragments {
schema, portLabel, cmd, suffix := domainSchema(n.Domain, 443, n.Endpoints...)
var owners uniquestrings.List
for _, endpoint := range n.Endpoints {
owners.Add(endpoint.ServerOwner)
}
ingresses = append(ingresses, &storage.NetworkPlan_Ingress{
Fqdn: n.Domain.Fqdn,
Schema: schema,
PortLabel: portLabel,
Command: cmd,
Comment: suffix,
PackageOwner: owners.Strings(),
})
}
return ingresses
}
// Deprecated
func domainSchema(domain *storage.Domain, localPort uint, endpoints ...*storage.Endpoint) (string, string, string, string) {
var protocols uniquestrings.List
for _, endpoint := range endpoints {
for _, md := range endpoint.GetServiceMetadata() {
if md.Protocol != "" {
protocols.Add(md.Protocol)
}
}
}
var schema, portLabel, cmd, suffix string
switch len(protocols.Strings()) {
case 0:
schema, portLabel = httpSchema(domain, localPort)
case 1:
if protocols.Strings()[0] == fnschema.ClearTextGrpcProtocol {
schema, portLabel, cmd = grpcSchema(domain.TlsFrontend, localPort)
if !domain.TlsFrontend {
suffix = "not currently working, see #26"
}
} else {
schema, portLabel = httpSchema(domain, localPort)
}
default:
schema = "(multiple: " + strings.Join(protocols.Strings(), ", ") + ") "
}
return schema, portLabel, cmd, suffix
}
// Deprecated
func grpcSchema(tls bool, port uint) (string, string, string) {
if tls {
return "ns tools grpcurl", fmt.Sprintf(":%d", port), " list"
}
return "ns tools grpcurl -plaintext", fmt.Sprintf(":%d", port), " list"
}
// Deprecated
func httpSchema(d *storage.Domain, port uint) (string, string) {
if d.TlsFrontend || port == 443 {
return "https://", checkPort(port, 443)
}
return "http://", checkPort(port, 80)
}
// Deprecated
func checkPort(port, expected uint) string {
if port == expected {
return ""
}
return fmt.Sprintf(":%d", port)
}