-
Notifications
You must be signed in to change notification settings - Fork 230
/
base_builder.go
264 lines (210 loc) · 6.51 KB
/
base_builder.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
package rtconfgen
import (
"fmt"
"time"
"github.com/cockroachdb/errors"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"encr.dev/pkg/fns"
"encr.dev/pkg/option"
meta "encr.dev/proto/encore/parser/meta/v1"
runtimev1 "encr.dev/proto/encore/runtime/v1"
)
type ResourceID interface {
comparable
fmt.Stringer
}
type Builder struct {
Infra *InfraBuilder
rs *resourceSet
env *runtimev1.Environment
encore *runtimev1.EncorePlatform
obs *runtimev1.Observability
// Any errors encountered during the build process.
err error
// authMethods are the service auth methods to use for authenticating to this deployment.
authMethods []*runtimev1.ServiceAuth
// defaultGracefulShutdown is the graceful shutdown behavior to use by default,
// unless a deployment overrides it.
defaultGracefulShutdown *runtimev1.GracefulShutdown
defaultDeployID string
defaultDeployedAt time.Time
deployments map[string]*Deployment
}
func NewBuilder() *Builder {
rs := new(resourceSet)
b := &Builder{
Infra: newInfraBuilder(rs),
rs: rs,
obs: &runtimev1.Observability{},
deployments: make(map[string]*Deployment),
}
return b
}
func (b *Builder) Env(env *runtimev1.Environment) *Builder {
b.env = env
return b
}
func (b *Builder) EncorePlatform(encore *runtimev1.EncorePlatform) *Builder {
b.encore = encore
return b
}
func (b *Builder) DefaultGracefulShutdown(s *runtimev1.GracefulShutdown) *Builder {
b.defaultGracefulShutdown = s
return b
}
func (b *Builder) AuthMethods(m []*runtimev1.ServiceAuth) *Builder {
b.authMethods = m
return b
}
func (b *Builder) DeployID(id string) *Builder {
b.defaultDeployID = id
return b
}
func (b *Builder) DeployedAt(t time.Time) *Builder {
b.defaultDeployedAt = t
return b
}
func (b *Builder) TracingProvider(p *runtimev1.TracingProvider) {
b.TracingProviderFn(p.Rid, tofn(p))
}
func (b *Builder) TracingProviderFn(rid string, fn func() *runtimev1.TracingProvider) {
addResFunc(&b.obs.Tracing, b.rs, rid, fn)
}
func (b *Builder) MetricsProvider(p *runtimev1.MetricsProvider) {
b.MetricsProviderFn(p.Rid, tofn(p))
}
func (b *Builder) MetricsProviderFn(rid string, fn func() *runtimev1.MetricsProvider) {
addResFunc(&b.obs.Metrics, b.rs, rid, fn)
}
func (b *Builder) LogsProvider(p *runtimev1.LogsProvider) {
b.LogsProviderFn(p.Rid, tofn(p))
}
func (b *Builder) LogsProviderFn(rid string, fn func() *runtimev1.LogsProvider) {
addResFunc(&b.obs.Logs, b.rs, rid, fn)
}
func (b *Builder) Deployment(rid string) *Deployment {
if d, ok := b.deployments[rid]; ok {
return d
}
d := &Deployment{b: b, rid: rid}
b.deployments[rid] = d
return d
}
type Deployment struct {
b *Builder
rid string
deployID option.Option[string]
deployedAt option.Option[time.Time]
reduceWith option.Option[*meta.Data]
dynamicExperiments []string
// The graceful shutdown behavior to use for this deployment.
// If None, uses the default graceful shutdown behavior.
gracefulShutdown option.Option[*runtimev1.GracefulShutdown]
// The service-discovery configuration for this deployment.
sd *runtimev1.ServiceDiscovery
// The base URL for reaching this deployment from another service.
svc2svcBaseURL string
hostedGateways []string
hostedServices []string
}
// DeployID sets the deploy id.
func (d *Deployment) DeployID(id string) *Deployment {
d.deployID = option.Some(id)
return d
}
// OverrideDeployedAt sets the time of deploy.
func (d *Deployment) OverrideDeployedAt(t time.Time) *Deployment {
d.deployedAt = option.Some(t)
return d
}
func (d *Deployment) DynamicExperiments(experiments []string) *Deployment {
d.dynamicExperiments = experiments
return d
}
// HostsServices adds the given service names as being hosted by this deployment.
// It appends and doesn't overwrite any existing hosted services.
func (d *Deployment) HostsServices(names ...string) *Deployment {
d.hostedServices = append(d.hostedServices, names...)
return d
}
// HostsGateways adds the given gateway names as being hosted by this deployment.
// It appends and doesn't overwrite any existing hosted gateways.
func (d *Deployment) HostsGateways(names ...string) *Deployment {
d.hostedGateways = append(d.hostedGateways, names...)
return d
}
// OverrideGracefulShutdown sets the graceful shutdown behavior for this specific deployment.
// To set a default graceful shutdown shared for all deployments, use [Builder.DefaultGracefulShutdown] instead.
func (d *Deployment) OverrideGracefulShutdown(s *runtimev1.GracefulShutdown) *Deployment {
d.gracefulShutdown = option.Some(s)
return d
}
func (d *Deployment) ServiceDiscovery(sd *runtimev1.ServiceDiscovery) *Deployment {
d.sd = sd
return d
}
func (d *Deployment) ReduceWithMeta(md *meta.Data) *Deployment {
d.reduceWith = option.Some(md)
return d
}
func (d *Deployment) BuildRuntimeConfig() (*runtimev1.RuntimeConfig, error) {
b := d.b
infra, err := b.Infra.get()
if err != nil {
return nil, err
}
if reduced, ok := d.reduceWith.Get(); ok {
infra = reduceForServices(infra, reduced, d.hostedServices)
}
graceful := d.gracefulShutdown.GetOrElse(d.b.defaultGracefulShutdown)
var hostedServices []*runtimev1.HostedService
for _, svcName := range d.hostedServices {
hostedServices = append(hostedServices, &runtimev1.HostedService{
Name: svcName,
})
}
gatewaysByName := make(map[string]*runtimev1.Gateway)
for _, gw := range infra.Resources.Gateways {
gatewaysByName[gw.EncoreName] = gw
}
gatewayRids := fns.Map(d.hostedGateways, func(name string) string {
gw, ok := gatewaysByName[name]
if !ok {
b.setErrf("gateway %q not found", name)
return ""
}
return gw.Rid
})
deploy := &runtimev1.Deployment{
HostedGateways: gatewayRids,
HostedServices: hostedServices,
ServiceDiscovery: d.sd,
GracefulShutdown: graceful,
DynamicExperiments: d.dynamicExperiments,
Observability: b.obs,
AuthMethods: d.b.authMethods,
DeployId: d.deployID.GetOrElse(b.defaultDeployID),
DeployedAt: timestamppb.New(d.deployedAt.GetOrElse(b.defaultDeployedAt)),
}
cfg := &runtimev1.RuntimeConfig{
Environment: b.env,
EncorePlatform: b.encore,
Infra: infra,
Deployment: deploy,
}
// Deep-clone the protobuf to avoid subsequent mutations from modifying this one.
cfg = cloneProto(cfg)
return cfg, b.err
}
func (b *Builder) setErr(err error) {
if b.err == nil {
b.err = err
}
}
func (b *Builder) setErrf(format string, args ...any) {
b.setErr(errors.Newf(format, args...))
}
func cloneProto[M proto.Message](m M) M {
return proto.Clone(m).(M)
}