-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathchart.go
128 lines (117 loc) · 3.68 KB
/
chart.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
package helm
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/arttor/helmify/pkg/cluster"
"github.com/arttor/helmify/pkg/helmify"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)
// NewOutput creates interface to dump processed input to filesystem in Helm chart format.
func NewOutput() helmify.Output {
return &output{}
}
type output struct{}
// Create a helm chart in the current directory:
// chartName/
//
// ├── .helmignore # Contains patterns to ignore when packaging Helm charts.
// ├── Chart.yaml # Information about your chart
// ├── values.yaml # The default values for your templates
// └── templates/ # The template files
// └── _helpers.tp # Helm default template partials
//
// Overwrites existing values.yaml and templates in templates dir on every run.
func (o output) Create(chartDir, chartName string, crd bool, certManagerAsSubchart bool, certManagerVersion string, templates []helmify.Template, filenames []string) error {
err := initChartDir(chartDir, chartName, crd, certManagerAsSubchart, certManagerVersion)
if err != nil {
return err
}
// group templates into files
files := map[string][]helmify.Template{}
values := helmify.Values{}
values[cluster.DomainKey] = cluster.DefaultDomain
for i, template := range templates {
file := files[filenames[i]]
file = append(file, template)
files[filenames[i]] = file
err = values.Merge(template.Values())
if err != nil {
return err
}
}
cDir := filepath.Join(chartDir, chartName)
for filename, tpls := range files {
err = overwriteTemplateFile(filename, cDir, crd, tpls)
if err != nil {
return err
}
}
err = overwriteValuesFile(cDir, values, certManagerAsSubchart)
if err != nil {
return err
}
return nil
}
func overwriteTemplateFile(filename, chartDir string, crd bool, templates []helmify.Template) error {
// pull in crd-dir setting and siphon crds into folder
var subdir string
if strings.Contains(filename, "crd") && crd {
subdir = "crds"
// create "crds" if not exists
if _, err := os.Stat(filepath.Join(chartDir, "crds")); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Join(chartDir, "crds"), 0750)
if err != nil {
return fmt.Errorf("%w: unable create crds dir", err)
}
}
} else {
subdir = "templates"
}
file := filepath.Join(chartDir, subdir, filename)
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("%w: unable to open %s", err, file)
}
defer f.Close()
for i, t := range templates {
logrus.WithField("file", file).Debug("writing a template into")
err = t.Write(f)
if err != nil {
return fmt.Errorf("%w: unable to write into %s", err, file)
}
if i != len(templates)-1 {
_, err = f.Write([]byte("\n---\n"))
if err != nil {
return fmt.Errorf("%w: unable to write into %s", err, file)
}
}
}
logrus.WithField("file", file).Info("overwritten")
return nil
}
func overwriteValuesFile(chartDir string, values helmify.Values, certManagerAsSubchart bool) error {
if certManagerAsSubchart {
_, err := values.Add(true, "certmanager", "installCRDs")
if err != nil {
return fmt.Errorf("%w: unable to add cert-manager.installCRDs", err)
}
_, err = values.Add(true, "certmanager", "enabled")
if err != nil {
return fmt.Errorf("%w: unable to add cert-manager.enabled", err)
}
}
res, err := yaml.Marshal(values)
if err != nil {
return fmt.Errorf("%w: unable to write marshal values.yaml", err)
}
file := filepath.Join(chartDir, "values.yaml")
err = os.WriteFile(file, res, 0600)
if err != nil {
return fmt.Errorf("%w: unable to write values.yaml", err)
}
logrus.WithField("file", file).Info("overwritten")
return nil
}