Skip to content

Commit 4c447ff

Browse files
committed
fix: import variable resolving
1 parent 12530f9 commit 4c447ff

7 files changed

Lines changed: 28 additions & 33 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func (r *RawConfig) GetEnv(name string) string {
402402
}
403403

404404
varName := "${" + name + "}"
405-
out, err := r.Resolver.FillVariables(r.Ctx, varName)
405+
out, err := r.Resolver.FillVariables(r.Ctx, varName, true)
406406
if err == nil {
407407
value := fmt.Sprintf("%v", out)
408408
if value != varName && value != "" {

pkg/devspace/config/loader/imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ResolveImports(ctx context.Context, resolver variable.Resolver, basePath st
4646
return nil, errors.Errorf("Version is missing in devspace.yaml")
4747
}
4848

49-
rawImportsInterface, err := resolver.FillVariablesInclude(ctx, rawImports, []string{"/imports/**"})
49+
rawImportsInterface, err := resolver.FillVariablesInclude(ctx, rawImports, true, []string{"/imports/**"})
5050
if err != nil {
5151
return nil, err
5252
}

pkg/devspace/config/loader/loader.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,6 @@ func prepareProfiles(ctx context.Context, config map[string]interface{}, resolve
506506
profileMap["replace"] = replace
507507
}
508508

509-
// Resolve strategicMerge field
510-
if profileMap["strategicMerge"] != nil {
511-
strategicMerge, err := resolve(ctx, profileMap["strategicMerge"], resolver)
512-
if err != nil {
513-
return nil, err
514-
}
515-
profileMap["strategicMerge"] = strategicMerge
516-
}
517-
518509
// Validate that the profile doesn't use forbidden expressions
519510
err = validateProfile(profileMap)
520511
if err != nil {
@@ -536,7 +527,7 @@ func resolve(ctx context.Context, data interface{}, resolver variable.Resolver)
536527
}
537528

538529
// find and fill variables
539-
return resolver.FillVariables(ctx, data)
530+
return resolver.FillVariables(ctx, data, true)
540531
}
541532

542533
func (l *configLoader) applyProfiles(ctx context.Context, data map[string]interface{}, options *ConfigOptions, resolver variable.Resolver, log log.Logger) (map[string]interface{}, error) {

pkg/devspace/config/loader/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func fillVariablesExcludeAndParse(ctx context.Context, resolver variable.Resolve
126126

127127
func fillVariablesAndParse(ctx context.Context, resolver variable.Resolver, preparedConfig map[string]interface{}, log log.Logger, excludedPaths ...string) (*latest.Config, map[string]interface{}, error) {
128128
// fill in variables and expressions
129-
preparedConfigInterface, err := resolver.FillVariablesExclude(ctx, preparedConfig, excludedPaths)
129+
preparedConfigInterface, err := resolver.FillVariablesExclude(ctx, preparedConfig, false, excludedPaths)
130130
if err != nil {
131131
return nil, nil, err
132132
}

pkg/devspace/config/loader/variable/resolver.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (r *resolver) replaceString(ctx context.Context, str string) (interface{},
117117
})
118118
}
119119

120-
func (r *resolver) findVariablesInclude(haystack interface{}, include []*regexp.Regexp) ([]*latest.Variable, error) {
120+
func (r *resolver) findVariables(haystack interface{}, skipUnused bool, include []*regexp.Regexp) ([]*latest.Variable, error) {
121121
// find out what vars are really used
122122
varsUsed := map[string]bool{}
123123
switch t := haystack.(type) {
@@ -144,10 +144,13 @@ func (r *resolver) findVariablesInclude(haystack interface{}, include []*regexp.
144144
}
145145
}
146146

147-
// add always resolve variables
148-
for _, v := range r.vars {
149-
if v.AlwaysResolve == nil || *v.AlwaysResolve {
150-
varsUsed[v.Name] = true
147+
// add always resolve variables. If skip unused is true, we don't
148+
// resolve by default and instead only resolve the actually found variables
149+
if !skipUnused {
150+
for _, v := range r.vars {
151+
if v.AlwaysResolve == nil || *v.AlwaysResolve {
152+
varsUsed[v.Name] = true
153+
}
151154
}
152155
}
153156

@@ -162,7 +165,7 @@ func (r *resolver) findVariablesInclude(haystack interface{}, include []*regexp.
162165
}
163166

164167
func (r *resolver) FindVariables(haystack interface{}) ([]*latest.Variable, error) {
165-
return r.findVariablesInclude(haystack, nil)
168+
return r.findVariables(haystack, false, nil)
166169
}
167170

168171
func (r *resolver) getVariableDefinition(name string) *latest.Variable {
@@ -223,6 +226,7 @@ func (r *resolver) orderVariables(vars map[string]bool) ([]*latest.Variable, err
223226
for i, j := 0, len(retVars)-1; i < j; i, j = i+1, j-1 {
224227
retVars[i], retVars[j] = retVars[j], retVars[i]
225228
}
229+
226230
return retVars, nil
227231
}
228232

@@ -257,7 +261,7 @@ func (r *resolver) insertVariableGraph(g *graph.Graph, node *latest.Variable) er
257261
return nil
258262
}
259263

260-
func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{}, includedPaths []string) (interface{}, error) {
264+
func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{}, skipUnused bool, includedPaths []string) (interface{}, error) {
261265
paths := []*regexp.Regexp{}
262266
for _, path := range includedPaths {
263267
path = strings.ReplaceAll(path, "**", ".+")
@@ -272,7 +276,7 @@ func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{
272276
}
273277

274278
// fill variables
275-
preparedConfigInterface, err := r.findAndFillVariables(ctx, haystack, nil, paths)
279+
preparedConfigInterface, err := r.findAndFillVariables(ctx, haystack, skipUnused, nil, paths)
276280
if err != nil {
277281
return nil, err
278282
}
@@ -284,10 +288,10 @@ func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{
284288
}
285289

286290
// fill in variables again
287-
return r.findAndFillVariables(ctx, preparedConfigInterface, nil, paths)
291+
return r.findAndFillVariables(ctx, preparedConfigInterface, skipUnused, nil, paths)
288292
}
289293

290-
func (r *resolver) FillVariablesExclude(ctx context.Context, haystack interface{}, excludedPaths []string) (interface{}, error) {
294+
func (r *resolver) FillVariablesExclude(ctx context.Context, haystack interface{}, skipUnused bool, excludedPaths []string) (interface{}, error) {
291295
paths := []*regexp.Regexp{}
292296
for _, path := range excludedPaths {
293297
path = strings.ReplaceAll(path, "**", ".+")
@@ -302,7 +306,7 @@ func (r *resolver) FillVariablesExclude(ctx context.Context, haystack interface{
302306
}
303307

304308
// fill variables
305-
preparedConfigInterface, err := r.findAndFillVariables(ctx, haystack, paths, nil)
309+
preparedConfigInterface, err := r.findAndFillVariables(ctx, haystack, skipUnused, paths, nil)
306310
if err != nil {
307311
return nil, err
308312
}
@@ -314,15 +318,15 @@ func (r *resolver) FillVariablesExclude(ctx context.Context, haystack interface{
314318
}
315319

316320
// fill in variables again
317-
return r.findAndFillVariables(ctx, preparedConfigInterface, paths, nil)
321+
return r.findAndFillVariables(ctx, preparedConfigInterface, skipUnused, paths, nil)
318322
}
319323

320-
func (r *resolver) FillVariables(ctx context.Context, haystack interface{}) (interface{}, error) {
321-
return r.FillVariablesExclude(ctx, haystack, nil)
324+
func (r *resolver) FillVariables(ctx context.Context, haystack interface{}, skipUnused bool) (interface{}, error) {
325+
return r.FillVariablesExclude(ctx, haystack, skipUnused, nil)
322326
}
323327

324-
func (r *resolver) findAndFillVariables(ctx context.Context, haystack interface{}, exclude, include []*regexp.Regexp) (interface{}, error) {
325-
varsUsed, err := r.findVariablesInclude(haystack, include)
328+
func (r *resolver) findAndFillVariables(ctx context.Context, haystack interface{}, skipUnused bool, exclude, include []*regexp.Regexp) (interface{}, error) {
329+
varsUsed, err := r.findVariables(haystack, skipUnused, include)
326330
if err != nil {
327331
return nil, err
328332
}

pkg/devspace/config/loader/variable/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ type Resolver interface {
3333
FindVariables(haystack interface{}) ([]*latest.Variable, error)
3434

3535
// FillVariables finds the used variables first and then fills in those in the haystack
36-
FillVariables(ctx context.Context, haystack interface{}) (interface{}, error)
36+
FillVariables(ctx context.Context, haystack interface{}, skipUnused bool) (interface{}, error)
3737

3838
// FillVariablesExclude finds the used variables first and then fills in those that do not match the excluded paths in the haystack
39-
FillVariablesExclude(ctx context.Context, haystack interface{}, excluded []string) (interface{}, error)
39+
FillVariablesExclude(ctx context.Context, haystack interface{}, skipUnused bool, excluded []string) (interface{}, error)
4040

4141
// FillVariablesInclude finds the used variables first and then fills in those that match the included paths in the haystack
42-
FillVariablesInclude(ctx context.Context, haystack interface{}, included []string) (interface{}, error)
42+
FillVariablesInclude(ctx context.Context, haystack interface{}, skipUnused bool, included []string) (interface{}, error)
4343

4444
// ResolvedVariables returns the internal memory cache of the resolver with all resolved variables
4545
ResolvedVariables() map[string]interface{}

pkg/devspace/config/versions/versions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func sanitizeMatchExpression(expression string) string {
349349
}
350350

351351
func resolveVariableValue(ctx context.Context, name string, resolver variable.Resolver) (string, error) {
352-
val, err := resolver.FillVariables(ctx, "${"+name+"}")
352+
val, err := resolver.FillVariables(ctx, "${"+name+"}", true)
353353
if err != nil {
354354
return "", err
355355
}

0 commit comments

Comments
 (0)