Skip to content

Commit 6777c33

Browse files
committed
Respect ignoreMissingValueFiles when generating hashes
Follow-up for #31 We were not checking for `ignoreMissingValueFiles` when generating file hashes.
1 parent a601c74 commit 6777c33

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

pkg/helm/helm.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"os/exec"
1414
"path"
1515
"path/filepath"
16-
"regexp"
1716
"sort"
1817
"strings"
1918
"sync"
@@ -54,12 +53,7 @@ func buildParams(payload *v1alpha1.Application, ignoreValueFile string) (string,
5453

5554
}
5655
for i := 0; i < len(helmFiles); i++ {
57-
isExplicitlyIgnored := ignoreValueFile != "" && strings.Contains(helmFiles[i], ignoreValueFile)
58-
isMissingAndShouldBeIgnored :=
59-
!fileExists(path.Join(payload.Spec.Source.Path, helmFiles[i])) &&
60-
payload.Spec.Source.Helm.IgnoreMissingValueFiles
61-
62-
if !isExplicitlyIgnored && !isMissingAndShouldBeIgnored {
56+
if !ignoredValueFile(payload, helmFiles[i], ignoreValueFile) {
6357
fileValues += fmt.Sprintf("%s,", helmFiles[i])
6458
}
6559
}
@@ -219,11 +213,9 @@ func GenerateHash(crd *v1alpha1.Application, ignoreValueFile string) (string, er
219213
if crd.Spec.Source.Helm != nil && len(crd.Spec.Source.Helm.ValueFiles) > 0 {
220214
oHash := sha256.New()
221215
overrideFiles := crd.Spec.Source.Helm.ValueFiles
222-
matchDots := regexp.MustCompile(`\.\.\/`)
223216
for i := 0; i < len(overrideFiles); i++ {
224-
if ignoreValueFile == "" || !strings.Contains(overrideFiles[i], ignoreValueFile) {
225-
trimmedFilename := matchDots.ReplaceAllString(overrideFiles[i], "")
226-
oHashReturned, err := generalHashFunction(trimmedFilename)
217+
if !ignoredValueFile(crd, overrideFiles[i], ignoreValueFile) {
218+
oHashReturned, err := generalHashFunction(path.Join(crd.Spec.Source.Path, overrideFiles[i]))
227219
if err != nil {
228220
return "", err
229221
}
@@ -437,6 +429,15 @@ func Read(inputCRD string) ([]*v1alpha1.Application, error) {
437429
return crdSpecs, nil
438430
}
439431

432+
func ignoredValueFile(crd *v1alpha1.Application, f string, ignoreValueFile string) bool {
433+
isExplicitlyIgnored := ignoreValueFile != "" && strings.Contains(f, ignoreValueFile)
434+
isMissingAndShouldBeIgnored :=
435+
!fileExists(path.Join(crd.Spec.Source.Path, f)) &&
436+
crd.Spec.Source.Helm.IgnoreMissingValueFiles
437+
438+
return isExplicitlyIgnored || isMissingAndShouldBeIgnored
439+
}
440+
440441
func fileExists(path string) bool {
441442
_, err := os.Stat(path)
442443
return !os.IsNotExist(err)

pkg/helm/helm_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ kind: Application
224224
}
225225

226226
for _, tt := range testFiles {
227+
tt := tt
227228
t.Run(tt.name, func(t *testing.T) {
228229
t.Parallel()
229230
hash, err := generalHashFunction(tt.file) //nolint:govet
@@ -326,6 +327,54 @@ kind: Application
326327
}
327328
})
328329

330+
t.Run("GenerateHash", func(t *testing.T) {
331+
testFiles := []struct {
332+
name string
333+
file string
334+
ignoreValueFile string
335+
hash string
336+
}{
337+
{
338+
name: "Generating hash for an Application",
339+
file: "crdData_testfile_3.yaml",
340+
ignoreValueFile: "",
341+
hash: "7ce0306f218c7147b388dbb1ec2fa78389e44ebd11ca31b208e085b82158d787",
342+
},
343+
{
344+
name: "Generate hash for an application with ignoreMissingValueFiles and a missing value file",
345+
file: "crdData_testfile_4.yaml",
346+
ignoreValueFile: "",
347+
hash: "349f7da89b1c47362663daedb6fcc28980d017613a9cb9da7d53bb852467fa6c",
348+
},
349+
}
350+
351+
for _, tt := range testFiles {
352+
tt := tt
353+
t.Run(tt.name, func(t *testing.T) {
354+
t.Parallel()
355+
356+
data, err := Read(tt.file)
357+
if err != nil {
358+
t.Error(err)
359+
}
360+
if len(data) != 1 {
361+
t.Fatal("This test can work only with one Application at a time")
362+
}
363+
crd := data[0]
364+
365+
got, err := GenerateHash(crd, tt.ignoreValueFile)
366+
if err != nil {
367+
t.Error(err)
368+
}
369+
370+
want := tt.hash //nolint:govet
371+
if got != want {
372+
t.Errorf("Failed to generate a correct hash on an overrides. got: %s want %s", got, want)
373+
}
374+
})
375+
}
376+
})
377+
329378
t.Run("IsMissingDependencyErr", func(t *testing.T) {
330379
templateErrors := []struct {
331380
name string

0 commit comments

Comments
 (0)