Skip to content

Commit 77075f4

Browse files
committed
fix: parent directory values files
1 parent f9d7b70 commit 77075f4

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

internal/templating/templating.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,27 @@ func LoadValues() error {
6464
}
6565

6666
func filterValuesFiles(valuesFiles []string, dirLimit string) []string {
67+
// If dirLimit points to a secret file, extract its directory
68+
if strings.HasSuffix(dirLimit, ".gitops.secret.enc.yaml") || strings.HasSuffix(dirLimit, ".gitops.secret.enc.yml") {
69+
dirLimit = filepath.Dir(dirLimit)
70+
log.Debugf("Converted file-based dir limit to directory: %q", dirLimit)
71+
}
72+
6773
dirLimitNormalized := normalizeDirPath(dirLimit)
74+
log.Debugf("Filtering values files with dirLimit=%q normalized=%q", dirLimit, dirLimitNormalized)
6875
if dirLimitNormalized == "" {
6976
return valuesFiles
7077
}
7178

7279
filtered := make([]string, 0, len(valuesFiles))
7380
for _, valuesFile := range valuesFiles {
7481
valuesDir := normalizeDirPath(filepath.Dir(valuesFile))
75-
if shouldIncludeValuesFile(valuesDir, dirLimitNormalized) {
82+
include := shouldIncludeValuesFile(valuesDir, dirLimitNormalized)
83+
log.Debugf("Evaluating values file %q dir=%q normalizedDir=%q include=%v", valuesFile, filepath.Dir(valuesFile), valuesDir, include)
84+
if include {
7685
filtered = append(filtered, valuesFile)
86+
} else {
87+
log.Debugf("Excluding values file %q due to dir limit %q", valuesFile, dirLimitNormalized)
7788
}
7889
}
7990
return filtered
@@ -124,26 +135,34 @@ func (t TemplateValues) merge() {
124135
sort.SliceStable(t, func(i, j int) bool {
125136
return len(strings.Split(t[i].Path, "/")) < len(strings.Split(t[j].Path, "/"))
126137
})
138+
order := make([]string, len(t))
139+
for idx, tv := range t {
140+
order[idx] = tv.Path
141+
}
142+
log.Debugf("TemplateValues merge order: %v", order)
127143
for i, templateValue := range t {
128-
merged := false
129-
for j, templateValue2 := range t {
130-
if j >= i {
131-
break
132-
}
133-
134-
if strings.HasPrefix(templateValue.Path, templateValue2.Path) {
135-
merged = true
136-
templateValue.MergedValues = mergeMaps(templateValue2.MergedValues, templateValue.Values)
144+
var bestParent *TemplateValuesPath
145+
for j := i - 1; j >= 0; j-- {
146+
candidate := t[j]
147+
log.Debugf("Considering ancestor candidate %q for child %q", candidate.Path, templateValue.Path)
148+
if strings.HasPrefix(templateValue.Path, candidate.Path) {
149+
bestParent = candidate
137150
break
138151
}
139152
}
140-
if !merged {
153+
154+
if bestParent != nil {
155+
log.Debugf("Merging ancestor %q into %q", bestParent.Path, templateValue.Path)
156+
templateValue.MergedValues = mergeMaps(bestParent.MergedValues, templateValue.Values)
157+
} else {
158+
log.Debugf("No ancestor found for %q, using own values only", templateValue.Path)
141159
templateValue.MergedValues = templateValue.Values
142160
}
143161
}
144162
}
145163

146164
func GetValuesForPath(path string) map[interface{}]interface{} {
165+
log.Debugf("Resolving values for secret path %q", path)
147166
if !loaded {
148167
err := LoadValues()
149168
if err != nil {
@@ -154,17 +173,19 @@ func GetValuesForPath(path string) map[interface{}]interface{} {
154173
usedPath := ""
155174
maxPathLength := 0
156175
for _, templateValue := range templateValues {
176+
log.Debugf("Considering values prefix %q for path %q", templateValue.Path, path)
157177
if strings.HasPrefix(path, templateValue.Path) && len(templateValue.Path) > maxPathLength {
158178
maxPathLength = len(templateValue.Path)
159179
usedPath = templateValue.Path
160180
values = templateValue.MergedValues
181+
log.Debugf("Selecting values prefix %q for path %q", templateValue.Path, path)
161182
}
162183
}
163184

164185
if usedPath != "" {
165-
log.Tracef("Using values from %s for path %s", usedPath, path)
186+
log.Debugf("Using values from %s for path %s", usedPath, path)
166187
} else {
167-
log.Tracef("No values found for path %s", path)
188+
log.Debugf("No values found for path %s", path)
168189
}
169190

170191
return values

internal/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetSecretFiles() ([]string, error) {
4949
}
5050

5151
if secretFileRegex.MatchString(path) {
52-
log.Debug("Found secret file: ", path)
52+
log.Trace("Found secret file: ", path)
5353
relativePath, err := filepath.Rel(GetRootDir(), path)
5454
if err != nil {
5555
log.Error("An error occurred while getting the relative path of the secret file")

0 commit comments

Comments
 (0)