-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
134 lines (124 loc) · 2.98 KB
/
template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package magekubernetes
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/sh" // sh contains helpful utility functions, like RunV
"gopkg.in/yaml.v3"
)
func renderTemplate(app ArgoCDApp) (string, error) {
if app.Spec.Source.Helm.ReleaseName != "" {
return renderHelm(app.Spec.Source)
} else if isKustomizeDir(app.Spec.Source.Path) {
return renderKustomize(app.Spec.Source.Path)
}
return app.Spec.Source.Path, nil
}
func renderHelm(source ArgoCDAppSource) (string, error) {
dir, err := tempDir()
if err != nil {
return "", err
}
pwd, err := os.Getwd()
if err != nil {
return "", err
}
err = os.Chdir(source.Path)
if err != nil {
return "", err
}
// temporary fix until https://github.com/helm/helm/issues/7214 is fixed
// again
err = addHelmRepos("./")
if err != nil {
return "", err
}
fmt.Println("rendering helm templates to: " + dir)
err = sh.Run("helm", "dependency", "build")
if err != nil {
return "", err
}
err = sh.Run("helm", "template",
"-f", strings.Join(source.Helm.ValueFiles, ","),
"--output-dir", dir,
".")
if err != nil {
return "", err
}
err = os.Chdir(pwd)
if err != nil {
return "", err
}
return dir, nil
}
func renderKustomize(path string) (string, error) {
dir, err := tempDir()
if err != nil {
return "", err
}
fmt.Println("rendering kustomize templates: " + dir)
err = sh.Run("kustomize", "build", path, "--output", dir)
if err != nil {
return "", err
}
return dir, nil
}
// Render templates to an temporary directory. Using a comma sep string here because
// mg. can only have int, str and bools as arguments
func renderTemplates() (string, error) {
var files []string
repo, err := repoURL()
if err != nil {
return "", err
}
apps, err := getArgoCDDeployments(repo)
if err != nil {
return "", err
}
for _, trackedDeployment := range apps {
templates, err := renderTemplate(trackedDeployment)
if err != nil {
return "", err
}
tackedFiles, err := listFilesInDirectory(templates)
if err != nil {
return "", err
}
files = append(files, tackedFiles...)
}
return strings.Join(files, ","), nil
}
func addHelmRepos(path string) error {
var chart HelmChart
chartfile, err := os.ReadFile(filepath.Join(path, "Chart.yaml"))
if err != nil {
return err
}
err = yaml.Unmarshal([]byte(chartfile), &chart)
if err != nil {
return err
}
for _, dep := range chart.Dependencies {
if strings.HasPrefix(dep.Repository, "oci://") {
fmt.Println("skipping repo add for oci repository: " + dep.Repository)
continue
}
err := sh.Run("helm", "repo", "add", dep.Name, dep.Repository)
if err != nil {
return err
}
}
return nil
}
// HelmChart contains all metadata of an helm chart
type HelmChart struct {
Dependencies []HelmDependency `yaml:"dependencies"`
}
// HelmDependency contains a dependency of a helmchart
type HelmDependency struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Repository string `yaml:"repository"`
Alias string `yaml:"alias"`
}