Skip to content

Commit f9d7b70

Browse files
authored
fix: filtering values files based off directory filter (#5)
1 parent 7e904f8 commit f9d7b70

4 files changed

Lines changed: 106 additions & 18 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ require (
7474
require (
7575
cloud.google.com/go/compute v1.5.0 // indirect
7676
filippo.io/age v1.0.0 // indirect
77-
github.com/Azure/azure-sdk-for-go v63.3.0+incompatible // indirect
77+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
7878
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
7979
github.com/Azure/go-autorest/autorest v0.11.26 // indirect
8080
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
5151
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
5252
filippo.io/age v1.0.0 h1:V6q14n0mqYU3qKFkZ6oOaF9oXneOviS3ubXsSVBRSzc=
5353
filippo.io/age v1.0.0/go.mod h1:PaX+Si/Sd5G8LgfCwldsSba3H1DDQZhIhFGkhbHaBq8=
54-
github.com/Azure/azure-sdk-for-go v63.3.0+incompatible h1:INepVujzUrmArRZjDLHbtER+FkvCoEwyRCXGqOlmDII=
55-
github.com/Azure/azure-sdk-for-go v63.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
54+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
55+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
5656
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
5757
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
5858
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=

internal/templating/templating.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func LoadValues() error {
3939
}
4040
}
4141

42+
valuesFiles = filterValuesFiles(valuesFiles, util.GetCliContext().String("dir"))
43+
44+
templateValues = TemplateValues{}
45+
4246
for _, valuesFile := range valuesFiles {
4347
log.Trace("Loading secret values file: ", valuesFile)
4448
absoluteSecretPath := path.Join(util.GetRootDir(), valuesFile)
@@ -59,6 +63,42 @@ func LoadValues() error {
5963
return nil
6064
}
6165

66+
func filterValuesFiles(valuesFiles []string, dirLimit string) []string {
67+
dirLimitNormalized := normalizeDirPath(dirLimit)
68+
if dirLimitNormalized == "" {
69+
return valuesFiles
70+
}
71+
72+
filtered := make([]string, 0, len(valuesFiles))
73+
for _, valuesFile := range valuesFiles {
74+
valuesDir := normalizeDirPath(filepath.Dir(valuesFile))
75+
if shouldIncludeValuesFile(valuesDir, dirLimitNormalized) {
76+
filtered = append(filtered, valuesFile)
77+
}
78+
}
79+
return filtered
80+
}
81+
82+
func shouldIncludeValuesFile(valuesDir string, dirLimit string) bool {
83+
if dirLimit == "" {
84+
return true
85+
}
86+
if valuesDir == "" {
87+
return true
88+
}
89+
return strings.HasPrefix(valuesDir, dirLimit) || strings.HasPrefix(dirLimit, valuesDir)
90+
}
91+
92+
func normalizeDirPath(dir string) string {
93+
dir = filepath.ToSlash(strings.TrimSpace(dir))
94+
dir = strings.TrimPrefix(dir, "./")
95+
dir = strings.Trim(dir, "/")
96+
if dir == "" || dir == "." {
97+
return ""
98+
}
99+
return dir + "/"
100+
}
101+
62102
func mergeMaps(a, b map[interface{}]interface{}) map[interface{}]interface{} {
63103
out := make(map[interface{}]interface{}, len(a))
64104
for k, v := range a {

internal/templating/templating_test.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import (
99

1010
func TestMapMerge1(t *testing.T) {
1111
a := map[interface{}]interface{}{
12-
"foo": "bar",
12+
"foo": "bar",
1313
"fizz": "buzz",
1414
}
1515
b := map[interface{}]interface{}{
1616
"fizz": "fizz",
1717
}
1818
c := map[interface{}]interface{}{
19-
"foo": "bar",
19+
"foo": "bar",
2020
"fizz": "fizz",
2121
}
2222
d := mergeMaps(a, b)
23-
assert.Equal(t, c, d, "Maps should be equal")
23+
assert.Equal(t, c, d, "Maps should be equal")
2424
}
2525

2626
func TestMapMerge2(t *testing.T) {
@@ -39,7 +39,7 @@ func TestMapMerge2(t *testing.T) {
3939
"d": "4",
4040
}
4141
d := mergeMaps(a, b)
42-
assert.Equal(t, c, d, "Maps should be equal")
42+
assert.Equal(t, c, d, "Maps should be equal")
4343
}
4444

4545
func TestMapMerge3(t *testing.T) {
@@ -55,7 +55,7 @@ func TestMapMerge3(t *testing.T) {
5555
"b": 42,
5656
}
5757
d := mergeMaps(a, b)
58-
assert.Equal(t, c, d, "Maps should be equal")
58+
assert.Equal(t, c, d, "Maps should be equal")
5959
}
6060

6161
func TestMapMerge4(t *testing.T) {
@@ -71,7 +71,7 @@ func TestMapMerge4(t *testing.T) {
7171
"b": []interface{}{"2", "3"},
7272
}
7373
d := mergeMaps(a, b)
74-
assert.Equal(t, c, d, "Maps should be equal")
74+
assert.Equal(t, c, d, "Maps should be equal")
7575
}
7676

7777
func TestTemplateValuesMerge(t *testing.T) {
@@ -159,17 +159,16 @@ func TestTemplateValuesMerge(t *testing.T) {
159159
assert.Equal(t, expectedMergedTemplateValues, templateValues, "TemplateValues should be equal")
160160
}
161161

162-
163162
func TestValuesFileLoading(t *testing.T) {
164163
c := util.GetDummyCliContext()
165164
util.SetCliContext(c)
166165
util.ComputeRootDir(c)
167-
166+
168167
LoadValues()
169-
168+
170169
valuesSet1 := map[interface{}]interface{}{
171-
"namespace": "gitops-dev",
172-
"stage": "dev",
170+
"namespace": "gitops-dev",
171+
"stage": "dev",
173172
"databaseUsername": "my-very-strong-username",
174173
"databasePassword": "my-very-strong-password",
175174
}
@@ -178,12 +177,61 @@ func TestValuesFileLoading(t *testing.T) {
178177
assert.Equal(t, valuesSet1, mergedValues1, "Values should be equal")
179178

180179
valuesSet2 := map[interface{}]interface{}{
181-
"namespace": "gitops-dev",
182-
"stage": "sub-dev",
180+
"namespace": "gitops-dev",
181+
"stage": "sub-dev",
183182
"databaseUsername": "my-very-strong-username",
184183
"databasePassword": "my-very-strong-password",
185-
"key": "fooo",
184+
"key": "fooo",
186185
}
187186
mergedValues2 := GetValuesForPath("test_assets/subdirectory/subdir-secret.gitops.secret.enc.yml")
188187
assert.Equal(t, valuesSet2, mergedValues2, "Values should be equal")
189-
}
188+
}
189+
190+
func TestFilterValuesFiles(t *testing.T) {
191+
valuesFiles := []string{
192+
"values.gitops.secret.enc.yml",
193+
"foo/values.gitops.secret.enc.yml",
194+
"foo/bar/values.gitops.secret.enc.yml",
195+
"foo/bar/baz/values.gitops.secret.enc.yml",
196+
"foo/other/values.gitops.secret.enc.yml",
197+
}
198+
199+
testCases := []struct {
200+
name string
201+
dirLimit string
202+
expected []string
203+
}{
204+
{
205+
name: "no dir limit returns all",
206+
dirLimit: "",
207+
expected: valuesFiles,
208+
},
209+
{
210+
name: "limit includes ancestors and descendants",
211+
dirLimit: "foo/bar",
212+
expected: []string{
213+
"values.gitops.secret.enc.yml",
214+
"foo/values.gitops.secret.enc.yml",
215+
"foo/bar/values.gitops.secret.enc.yml",
216+
"foo/bar/baz/values.gitops.secret.enc.yml",
217+
},
218+
},
219+
{
220+
name: "limit with trailing slash",
221+
dirLimit: "foo/bar/",
222+
expected: []string{
223+
"values.gitops.secret.enc.yml",
224+
"foo/values.gitops.secret.enc.yml",
225+
"foo/bar/values.gitops.secret.enc.yml",
226+
"foo/bar/baz/values.gitops.secret.enc.yml",
227+
},
228+
},
229+
}
230+
231+
for _, tc := range testCases {
232+
t.Run(tc.name, func(t *testing.T) {
233+
filtered := filterValuesFiles(valuesFiles, tc.dirLimit)
234+
assert.Equal(t, tc.expected, filtered)
235+
})
236+
}
237+
}

0 commit comments

Comments
 (0)