|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/loft-sh/devspace/pkg/devspace/config" |
| 6 | + "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression" |
| 7 | + "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/legacy" |
| 8 | + "github.com/loft-sh/devspace/pkg/devspace/dependency/types" |
| 9 | + "github.com/loft-sh/devspace/pkg/devspace/deploy/deployer/kubectl/walk" |
| 10 | + "github.com/loft-sh/devspace/pkg/devspace/imageselector" |
| 11 | + varspkg "github.com/loft-sh/devspace/pkg/util/vars" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +func varMatchFn(key, value string) bool { |
| 17 | + return true |
| 18 | +} |
| 19 | + |
| 20 | +// NewRuntimeResolver creates a new resolver that caches resolved variables in memory and in the provided cache |
| 21 | +func NewRuntimeResolver(enableLegacyHelpers bool) RuntimeResolver { |
| 22 | + return &runtimeResolver{ |
| 23 | + enableLegacyHelpers: enableLegacyHelpers, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +type runtimeResolver struct { |
| 28 | + enableLegacyHelpers bool |
| 29 | +} |
| 30 | + |
| 31 | +func (r *runtimeResolver) FillRuntimeVariablesAsString(haystack interface{}, config config.Config, dependencies []types.Dependency) (string, error) { |
| 32 | + out, err := r.FillRuntimeVariables(haystack, config, dependencies) |
| 33 | + if err != nil { |
| 34 | + return "", err |
| 35 | + } |
| 36 | + |
| 37 | + return fmt.Sprintf("%v", out), nil |
| 38 | +} |
| 39 | + |
| 40 | +func (r *runtimeResolver) FillRuntimeVariablesAsImageSelector(haystack interface{}, config config.Config, dependencies []types.Dependency) (*imageselector.ImageSelector, error) { |
| 41 | + out, err := r.FillRuntimeVariablesAsString(haystack, config, dependencies) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + return &imageselector.ImageSelector{ |
| 47 | + Image: out, |
| 48 | + }, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (r *runtimeResolver) FillRuntimeVariablesWithRebuild(haystack interface{}, config config.Config, dependencies []types.Dependency, builtImages map[string]string) (bool, interface{}, error) { |
| 52 | + shouldRebuild, haystack, err := r.fillVariables(haystack, config, dependencies, builtImages) |
| 53 | + if err != nil { |
| 54 | + return false, nil, err |
| 55 | + } |
| 56 | + |
| 57 | + // resolve expressions |
| 58 | + haystack, err = expression.ResolveAllExpressions(haystack, filepath.Dir(config.Path()), nil) |
| 59 | + if err != nil { |
| 60 | + return false, nil, err |
| 61 | + } |
| 62 | + |
| 63 | + return shouldRebuild, haystack, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (r *runtimeResolver) FillRuntimeVariables(haystack interface{}, config config.Config, dependencies []types.Dependency) (interface{}, error) { |
| 67 | + _, out, err := r.FillRuntimeVariablesWithRebuild(haystack, config, dependencies, map[string]string{}) |
| 68 | + return out, err |
| 69 | +} |
| 70 | + |
| 71 | +func (r *runtimeResolver) fillVariables(haystack interface{}, config config.Config, dependencies []types.Dependency, builtImages map[string]string) (bool, interface{}, error) { |
| 72 | + switch t := haystack.(type) { |
| 73 | + case string: |
| 74 | + return r.replaceString(t, config, dependencies, builtImages) |
| 75 | + case map[interface{}]interface{}: |
| 76 | + shouldRebuild := false |
| 77 | + err := walk.Walk(t, varMatchFn, func(path, value string) (interface{}, error) { |
| 78 | + rebuild, val, err := r.replaceString(value, config, dependencies, builtImages) |
| 79 | + shouldRebuild = shouldRebuild || rebuild |
| 80 | + return val, err |
| 81 | + }) |
| 82 | + return shouldRebuild, t, err |
| 83 | + } |
| 84 | + |
| 85 | + return false, nil, fmt.Errorf("unrecognized haystack type: %#v", haystack) |
| 86 | +} |
| 87 | + |
| 88 | +func (r *runtimeResolver) replaceString(str string, config config.Config, dependencies []types.Dependency, builtImages map[string]string) (bool, interface{}, error) { |
| 89 | + shouldRebuild := false |
| 90 | + if r.enableLegacyHelpers { |
| 91 | + rebuild, val, err := legacy.Replace(str, config, dependencies, map[string]string{}) |
| 92 | + if err != nil { |
| 93 | + return false, "", err |
| 94 | + } |
| 95 | + |
| 96 | + shouldRebuild = shouldRebuild || rebuild |
| 97 | + str = fmt.Sprintf("%v", val) |
| 98 | + } |
| 99 | + |
| 100 | + value, err := varspkg.ParseString(str, func(v string) (interface{}, error) { |
| 101 | + rebuild, val, err := r.resolve(v, config, dependencies, builtImages) |
| 102 | + if err != nil { |
| 103 | + return "", err |
| 104 | + } |
| 105 | + |
| 106 | + shouldRebuild = shouldRebuild || rebuild |
| 107 | + return val, nil |
| 108 | + }) |
| 109 | + return shouldRebuild, value, err |
| 110 | +} |
| 111 | + |
| 112 | +func (r *runtimeResolver) resolve(name string, config config.Config, dependencies []types.Dependency, builtImages map[string]string) (bool, interface{}, error) { |
| 113 | + name = strings.TrimSpace(name) |
| 114 | + |
| 115 | + // check if in vars already |
| 116 | + v, ok := config.Variables()[name] |
| 117 | + if ok { |
| 118 | + return false, v, nil |
| 119 | + } |
| 120 | + |
| 121 | + // fill the variable if not found |
| 122 | + shouldRebuild, value, err := r.fillRuntimeVariable(name, config, dependencies, builtImages) |
| 123 | + if err != nil { |
| 124 | + return false, nil, err |
| 125 | + } |
| 126 | + |
| 127 | + return shouldRebuild, value, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (r *runtimeResolver) fillRuntimeVariable(name string, config config.Config, dependencies []types.Dependency, builtImages map[string]string) (bool, interface{}, error) { |
| 131 | + // is runtime variable |
| 132 | + if strings.HasPrefix(name, "runtime.") { |
| 133 | + return NewRuntimeVariable(name, config, dependencies, builtImages).Load() |
| 134 | + } |
| 135 | + |
| 136 | + return false, nil, fmt.Errorf("cannot resolve variable %s as it is not a runtime variable and apparently wasn't resolved earlier") |
| 137 | +} |
0 commit comments