-
Notifications
You must be signed in to change notification settings - Fork 9
/
compute.go
58 lines (46 loc) · 1.83 KB
/
compute.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
// 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 startup
import (
"context"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func ComputeConfig(ctx context.Context, env pkggraph.Context, serverStartupPlan *schema.StartupPlan, deps []*planning.ParsedNode, info pkggraph.StartupInputs) (*schema.BinaryConfig, error) {
computed := &schema.BinaryConfig{}
// For each already loaded configuration, unify the startup args to produce the final startup configuration.
for _, dep := range deps {
if err := loadStartupPlan(ctx, env, dep, info, computed); err != nil {
return nil, fnerrors.Wrapf(dep.Package.Location, err, "computing startup config")
}
}
if err := mergePlan(serverStartupPlan, computed); err != nil {
return nil, err
}
return computed, nil
}
func loadStartupPlan(ctx context.Context, env pkggraph.Context, dep *planning.ParsedNode, info pkggraph.StartupInputs, merged *schema.BinaryConfig) error {
plan, err := dep.ProvisionPlan.Startup.EvalStartup(ctx, env, info, dep.Allocations)
if err != nil {
return fnerrors.Wrap(dep.Package.Location, err)
}
return mergePlan(plan, merged)
}
func mergePlan(plan *schema.StartupPlan, merged *schema.BinaryConfig) error {
merged.Args = append(merged.Args, plan.Args...)
// XXX O(n^2)
var errs []error
for _, entry := range plan.Env {
var err error
merged.Env, err = runtime.SetEnv(merged.Env, entry)
if err != nil {
errs = append(errs, err)
}
}
return multierr.New(errs...)
}