-
Notifications
You must be signed in to change notification settings - Fork 9
/
backendconfig.go
137 lines (117 loc) · 4.2 KB
/
backendconfig.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
// 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.
package binary
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
// Generates a "backends.fn.js" with ingress addresses for required backends.
func generateBackendsConfig(ctx context.Context, loc pkggraph.Location, backends []*schema.NodejsBuild_Backend, ingressFragments compute.Computable[[]*schema.IngressFragment], placeholder bool) ([]byte, error) {
var fragments []*schema.IngressFragment
if ingressFragments != nil {
var err error
fragments, err = compute.GetValue(ctx, ingressFragments)
if err != nil {
return nil, fnerrors.InternalError("failed to build a nodejs app while waiting on ingress computation: %w", err)
}
}
backendsMap := map[string]*BackendDefinition{}
for _, backend := range backends {
if placeholder {
backendsMap[backend.Name] = &BackendDefinition{
Managed: "placeholder",
}
} else {
backendDef, err := resolveBackend(loc, backend.Service, fragments)
if err != nil {
return nil, err
}
backendsMap[backend.Name] = backendDef
}
}
return GenerateBackendConfFromMap(ctx, backendsMap, placeholder)
}
type BackendDefinition struct {
Managed string `json:"managed,omitempty"`
Unmanaged []string `json:"unmanaged,omitempty"`
}
func GenerateBackendConfFromMap(ctx context.Context, backends map[string]*BackendDefinition, placeholder bool) ([]byte, error) {
var b bytes.Buffer
fmt.Fprintln(&b, "// This is an automatically generated file.")
if placeholder {
fmt.Fprintln(&b, "//")
fmt.Fprintln(&b, "// This placeholder file exists as a convenience. The actual values are")
fmt.Fprintln(&b, "// resolved at build time, when the build is bound to an environment")
fmt.Fprintln(&b, "// and the server dependencies can be introspected.")
fmt.Fprintln(&b, "//")
fmt.Fprintln(&b, "// Each backend will have a list of URLs, separated by foundation-managed")
fmt.Fprintln(&b, "// domains, and user-specified. E.g.")
fmt.Fprintln(&b, "//")
fmt.Fprintln(&b, "// export const Backends = {")
fmt.Fprintln(&b, "// apiBackend: {")
fmt.Fprintln(&b, "// managed: 'foobar.prod.org.nscloud.dev'")
fmt.Fprintln(&b, "// unmanaged: ['foobar.myorg.com']")
fmt.Fprintln(&b, "// }")
fmt.Fprintln(&b, "// }")
fmt.Fprintln(&b, "//")
fmt.Fprintln(&b, "//")
}
fmt.Fprintln(&b)
fmt.Fprint(&b, "export const Backends = ")
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
if err := enc.Encode(backends); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func resolveBackend(loc pkggraph.Location, serviceRef *schema.PackageRef, fragments []*schema.IngressFragment) (*BackendDefinition, error) {
var matching []*schema.IngressFragment
for _, fragment := range fragments {
if fragment.GetOwner() != serviceRef.PackageName {
continue
}
if fragment.GetEndpoint().GetServiceName() != serviceRef.Name {
continue
}
matching = append(matching, fragment)
}
if len(matching) == 0 {
var matches []string
for _, r := range [][2]string{
{"endpoint_owner", serviceRef.PackageName},
{"service_name", serviceRef.Name},
} {
if r[1] != "" {
matches = append(matches, fmt.Sprintf("%s=%q", r[0], r[1]))
}
}
return nil, fnerrors.UserError(loc, "no ingress matches %s, perhaps you're missing `ingress: internetFacing: true`",
strings.Join(matches, " "))
}
bd := &BackendDefinition{}
for _, fragment := range matching {
d := fragment.Domain
if d.Managed == schema.Domain_LOCAL_MANAGED {
bd.Managed = fmt.Sprintf("http://%s:%d", d.Fqdn, runtime.LocalIngressPort)
} else if d.Managed == schema.Domain_CLOUD_MANAGED || d.Managed == schema.Domain_CLOUD_TERMINATION {
if d.TlsFrontend {
bd.Managed = fmt.Sprintf("https://%s", d.Fqdn)
} else {
bd.Managed = fmt.Sprintf("http://%s", d.Fqdn)
}
} else {
bd.Unmanaged = append(bd.Unmanaged, d.Fqdn)
}
}
return bd, nil
}