-
Notifications
You must be signed in to change notification settings - Fork 51
/
helpers.go
81 lines (68 loc) · 2.82 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package helmfiles
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/yaml2s"
"github.com/pkg/errors"
"github.com/roboll/helmfile/pkg/state"
)
type Helmfile struct {
Filepath string
RelativePathToRoot string
}
var pathSeparator = string(os.PathSeparator)
// GatherHelmfiles gathers the helmfiles from the given file
func GatherHelmfiles(helmfile, dir string) ([]Helmfile, error) {
if helmfile == "" {
helmfile = "helmfile.yaml"
}
baseParentHelmfileDir := filepath.Dir(helmfile)
// we need to check if the main helmfile itself is in a subdirectory, if it is then we need to add that to any
// nested subfolders we find so we can correctly reference version stream files
parentHelmfileDepth := 0
if strings.Contains(helmfile, pathSeparator) {
parentHelmfileDepth = len(strings.Split(baseParentHelmfileDir, pathSeparator))
}
helmfile = filepath.Join(dir, helmfile)
helmState := state.HelmState{}
err := yaml2s.LoadFile(helmfile, &helmState)
if err != nil {
return nil, errors.Wrapf(err, "failed to load helmfile %s", helmfile)
}
relativePath := strings.Repeat("../", parentHelmfileDepth)
helmfiles := []Helmfile{
{helmfile, relativePath},
}
parentHelmfileDir := filepath.Dir(helmfile)
for _, nested := range helmState.Helmfiles {
// lets ignore remote helmfiles
if strings.HasPrefix(nested.Path, "git::") {
continue
}
// recursively gather nested helmfiles including their relative path so we can add correct location to version stream values files
// note: unit tests cover this as it is a complex function however they set a test dir with files copied into it,
// when running the resolve command for real the dir is '.' and therefore can take a slightly different route through this
// func. If you change this then its also worth giving a manual test of jx giotops helmfile resolve on a nested helmfile
// and make sure the relative path to version stream values remain the same.
nestedHelmfile, err := GatherHelmfiles(filepath.Join(baseParentHelmfileDir, nested.Path), dir)
if err != nil {
return nil, errors.Wrapf(err, "failed to get nested helmnfiles %s in %s", nested.Path, dir)
}
helmfiles = append(helmfiles, nestedHelmfile...)
nestedHelmfileDepth := len(strings.Split(filepath.Dir(nested.Path), pathSeparator))
relativePath := strings.Repeat("../", parentHelmfileDepth+nestedHelmfileDepth)
fileLocation := filepath.Join(parentHelmfileDir, nested.Path)
exists, err := files.FileExists(fileLocation)
if err != nil {
return nil, errors.Wrapf(err, "failed to check for nested helmfile %s", fileLocation)
}
if !exists {
return nil, fmt.Errorf("failed to find nested helmfile %s", fileLocation)
}
helmfiles = append(helmfiles, Helmfile{fileLocation, relativePath})
}
return helmfiles, nil
}