This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
app_config.go
339 lines (288 loc) · 10.1 KB
/
app_config.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package config
import (
"fmt"
"sort"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/function"
"github.com/hashicorp/waypoint/internal/config/dynamic"
"github.com/hashicorp/waypoint/internal/pkg/partial"
pb "github.com/hashicorp/waypoint/pkg/server/gen"
serversort "github.com/hashicorp/waypoint/pkg/server/sort"
)
// genericConfig represents the `config` stanza that can be placed
// both in the app and at the project level.
type genericConfig struct {
// internal are variables which can be seen for templating but are not exposed
// by default to an application or runner.
InternalRaw hcl.Expression `hcl:"internal,optional"`
// env are variables that will be exported into the application or runners
// environment.
EnvRaw hcl.Expression `hcl:"env,optional"`
// file are paths that will be written to disk in the context of the application
// environment.
FileRaw hcl.Expression `hcl:"file,optional"`
// Indicates a signal to send the application when config files change.
FileChangeSignal string `hcl:"file_change_signal,optional"`
// WorkspaceScoped are workspace-scoped config variables.
WorkspaceScoped []*scopedConfig `hcl:"workspace,block"`
// LabelScoped are label-selector-scoped config variables.
LabelScoped []*scopedConfig `hcl:"label,block"`
// Runner are runner-scoped configurations.
Runner *runnerConfig `hcl:"runner,block"`
ctx *hcl.EvalContext // ctx is the context to use when evaluating
scopeFunc func(*pb.ConfigVar) // scopeFunc should set the scope for the config var
}
// runnerConfig is used for the `runner` blocks within `config`.
type runnerConfig struct {
// Same as genericConfig, see there for docs.
InternalRaw hcl.Expression `hcl:"internal,optional"`
EnvRaw hcl.Expression `hcl:"env,optional"`
FileRaw hcl.Expression `hcl:"file,optional"`
WorkspaceScoped []*scopedConfig `hcl:"workspace,block"`
LabelScoped []*scopedConfig `hcl:"label,block"`
}
func (c *runnerConfig) configVars(
ctx *hcl.EvalContext,
scopeFunc func(*pb.ConfigVar),
) ([]*pb.ConfigVar, error) {
if c == nil {
return nil, nil
}
// Setup our scope function
runnerScopeFunc := func(v *pb.ConfigVar) {
// Inherit our parent scope.
scopeFunc(v)
v.Target.Runner = &pb.Ref_Runner{
Target: &pb.Ref_Runner_Any{
Any: &pb.Ref_RunnerAny{},
},
}
}
// Build a generic config since we're most similar to that and that
// lets us share logic across the config var functions without
// copy and pasting.
generic := &genericConfig{
InternalRaw: c.InternalRaw,
EnvRaw: c.EnvRaw,
FileRaw: c.FileRaw,
WorkspaceScoped: c.WorkspaceScoped,
LabelScoped: c.LabelScoped,
scopeFunc: runnerScopeFunc,
}
return generic.configVars(ctx)
}
// scopedConfig is used for the `workspace` and `label`-scoped config blocks
// within genericConfig as a way to further scope configuration.
type scopedConfig struct {
// Scope is the label for the block. This is reused for both workspace
// and label scoped variables so this could be either of those.
Scope string `hcl:",label"`
// Same as genericConfig, see there for docs.
InternalRaw hcl.Expression `hcl:"internal,optional"`
EnvRaw hcl.Expression `hcl:"env,optional"`
FileRaw hcl.Expression `hcl:"file,optional"`
}
// configVars returns the set of ConfigVars ready to be sent to the API server.
//
// scopeFunc must be provided to set the proper scoping on the rendered
// variables since this struct on its own doesn't know.
func (s *scopedConfig) configVars(
ctx *hcl.EvalContext,
scopeFunc func(*pb.ConfigVar),
) ([]*pb.ConfigVar, error) {
// sortVars performs a topological sort of the variables via references, so
// the pairs can be evaluated top to bottom safely.
pairs, err := sortVars(ctx, []sortVarMap{
{Expr: s.EnvRaw, Prefix: "config.env."},
{Expr: s.InternalRaw, Prefix: "config.internal.", Internal: true},
{Expr: s.FileRaw, Prefix: "config.file.", Path: true},
})
if err != nil {
return nil, err
}
return configVars(ctx, pairs, scopeFunc)
}
// ConfigVars returns the set of config vars that can be sent to the API server.
// This will include all the scoped config vars such as workspaces, labels,
// and runners.
func (c *genericConfig) ConfigVars() ([]*pb.ConfigVar, error) {
if c == nil {
return nil, nil
}
// Build our evaluation context for the config vars
ctx := c.ctx
ctx = appendContext(ctx, &hcl.EvalContext{
Functions: dynamic.Register(map[string]function.Function{}),
})
ctx = finalizeContext(ctx)
return c.configVars(ctx)
}
func (c *genericConfig) configVars(
ctx *hcl.EvalContext,
) ([]*pb.ConfigVar, error) {
// We copy ourselves to a scopedConfig so we can share the configVars
// function. Otherwise, the two functions are nearly identical.
rootScope := &scopedConfig{
InternalRaw: c.InternalRaw,
EnvRaw: c.EnvRaw,
FileRaw: c.FileRaw,
}
result, err := rootScope.configVars(ctx, c.scopeFunc)
if err != nil {
return nil, err
}
// Do runners
if c.Runner != nil {
next, err := c.Runner.configVars(ctx, c.scopeFunc)
if err != nil {
return nil, err
}
result = append(result, next...)
}
// Build up our workspace-scoped configs.
for _, wsScope := range c.WorkspaceScoped {
next, err := wsScope.configVars(ctx, func(v *pb.ConfigVar) {
// Always apply our root scope so that if this is a workspace-scoped
// var WITHIN an app-scoped genericConfig, then it gets that target
// too.
c.scopeFunc(v)
// Apply our own filters.
v.Target.Workspace = &pb.Ref_Workspace{Workspace: wsScope.Scope}
})
if err != nil {
return nil, err
}
result = append(result, next...)
}
// Build up our label-scoped configs.
for _, scoped := range c.LabelScoped {
next, err := scoped.configVars(ctx, func(v *pb.ConfigVar) {
// Always apply our root scope so that if this is a label-scoped
// var WITHIN an app-scoped genericConfig, then it gets that target
// too.
c.scopeFunc(v)
// Apply our own filters.
v.Target.LabelSelector = scoped.Scope
})
if err != nil {
return nil, err
}
result = append(result, next...)
}
// Sort our results by name. This helps with deterministic behavior
// in API calls, user output, etc. without forcing all callers to worry
// about sorting.
sort.Sort(serversort.ConfigName(result))
return result, nil
}
// configVars returns the "rendered" list of config vars that are ready to
// be sent to the API server. As inputs, this requires the topologically
// sorted set of config vars (from sortVars) so that ordering is already
// pre-determined.
//
// The scopeFunc can be used to modify the config var and set proper
// targeting and other values. This is called before the value is set.
func configVars(
ctx *hcl.EvalContext,
sortedVars []*analyzedPair,
scopeFunc func(*pb.ConfigVar),
) ([]*pb.ConfigVar, error) {
// We're going to build up the variables as we go along using these 4 maps.
ctx.Variables = map[string]cty.Value{}
var (
env = map[string]cty.Value{}
internal = map[string]cty.Value{}
file = map[string]cty.Value{}
config = map[string]cty.Value{}
)
var result []*pb.ConfigVar
for _, pair := range sortedVars {
key := pair.Name
// Start building our var
var newVar pb.ConfigVar
newVar.Target = &pb.ConfigVar_Target{}
newVar.Name = key
newVar.Internal = pair.Internal
newVar.NameIsPath = pair.Path
scopeFunc(&newVar)
// Decode the value
val, diags := pair.Pair.Value.Value(ctx)
if diags.HasErrors() {
// Ok, we can't read its value right now. Let's do a partial evaluation then.
str, err := partial.EvalExpression(ctx, pair.Pair.Value)
if err != nil {
return nil, err
}
newVar.Value = &pb.ConfigVar_Static{
Static: str,
}
// We don't advertise these variables in the eval context because
// we don't want them to be substituted as strings into other variables.
// If the current variable is referenced by a later variable, we want
// that to be a normal HCL template expansion of the variable reference,
// not the contents. Quick example:
//
// a = "${g} ${s}"
// b = "more: ${a}"
// g = unknown()
// s = "ok"
//
// After running the algorithm, we want b to still be 'more: ${a}', NOT
// 'more: ${g} ok'. The reason being the 2nd one confuses the escaping
// as it appears like it might be data that was returned from a file or
// something.
} else {
switch val.Type() {
case dynamic.Type:
newVar.Value = &pb.ConfigVar_Dynamic{
Dynamic: val.EncapsulatedValue().(*pb.ConfigVar_DynamicVal),
}
default:
// For non-config val types we try to convert it to a string
// as a static value.
var err error
val, err = convert.Convert(val, cty.String)
if err != nil {
return nil, err
}
// We have to escape any HCL we find in the string so that we don't
// evaluate it down-stream.
// First, we need to check if the value is not null, since we allow
// `null` defaults for input variables, and a user may forget to
// provide a value to an input variable
if val.IsNull() {
return nil, fmt.Errorf("could not evaluate %q in app config with `null` value", newVar.Name)
}
newVar.Value = &pb.ConfigVar_Static{
Static: hclEscaper.Replace(val.AsString()),
}
if pair.Internal {
internal[pair.Name] = val
// Because of the nature of the hcl map type, we have to rebuild these
// each time we modify them.
config["internal"] = cty.MapVal(internal)
} else if pair.Path {
file[pair.Name] = val
// Because of the nature of the hcl map type, we have to rebuild these
// each time we modify them.
config["file"] = cty.MapVal(file)
} else {
env[pair.Name] = val
// Because of the nature of the hcl map type, we have to rebuild these
// each time we modify them.
config["env"] = cty.MapVal(env)
}
ctx.Variables["config"] = cty.MapVal(config)
}
}
result = append(result, &newVar)
}
return result, nil
}
var (
// hclEscaper is used to escape HCL in our config values.
hclEscaper = strings.NewReplacer("${", "$${", "%{", "%%{")
)