forked from helmfile/chartify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kustomize.go
221 lines (195 loc) · 5.53 KB
/
kustomize.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package chartify
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v3"
)
type KustomizeOpts struct {
Images []KustomizeImage `yaml:"images"`
NamePrefix string `yaml:"namePrefix"`
NameSuffix string `yaml:"nameSuffix"`
Namespace string `yaml:"namespace"`
}
type KustomizeImage struct {
Name string `yaml:"name"`
NewName string `yaml:"newName"`
NewTag string `yaml:"newTag"`
Digest string `yaml:"digest"`
}
func (img KustomizeImage) String() string {
res := img.Name
if img.NewName != "" {
res = res + "=" + img.NewName
}
if img.NewTag != "" {
res = res + ":" + img.NewTag
}
if img.Digest != "" {
res = res + "@" + img.Digest
}
return res
}
type KustomizeBuildOpts struct {
ValuesFiles []string
SetValues []string
SetFlags []string
EnableAlphaPlugins bool
Namespace string
HelmBinary string
}
func (o *KustomizeBuildOpts) SetKustomizeBuildOption(opts *KustomizeBuildOpts) error {
*opts = *o
return nil
}
type KustomizeBuildOption interface {
SetKustomizeBuildOption(opts *KustomizeBuildOpts) error
}
func (r *Runner) KustomizeBuild(srcDir string, tempDir string, opts ...KustomizeBuildOption) (string, error) {
kustomizeOpts := KustomizeOpts{}
u := &KustomizeBuildOpts{}
for i := range opts {
if err := opts[i].SetKustomizeBuildOption(u); err != nil {
return "", err
}
}
for _, f := range u.ValuesFiles {
valsFileContent, err := r.ReadFile(f)
if err != nil {
return "", err
}
if err := yaml.Unmarshal(valsFileContent, &kustomizeOpts); err != nil {
return "", err
}
}
if u.Namespace != "" {
kustomizeOpts.Namespace = u.Namespace
}
if len(u.SetValues) > 0 || len(u.SetFlags) > 0 {
panic("--set is not yet supported for kustomize-based apps! Use -f/--values flag instead.")
}
prevDir, err := os.Getwd()
if err != nil {
return "", err
}
evaluatedPath, err := filepath.EvalSymlinks(tempDir)
if err != nil {
return "", err
}
var absoluteSrcPath string
if filepath.IsAbs(srcDir) {
absoluteSrcPath = srcDir
} else {
absoluteSrcPath = path.Join(prevDir, srcDir)
}
relPath, err := filepath.Rel(evaluatedPath, absoluteSrcPath)
if err != nil {
return "", err
}
baseFile := []byte("bases:\n- " + relPath + "\n")
kustomizationPath := path.Join(tempDir, "kustomization.yaml")
if err := r.WriteFile(kustomizationPath, baseFile, 0644); err != nil {
return "", err
}
if len(kustomizeOpts.Images) > 0 {
args := []string{"edit", "set", "image"}
for _, image := range kustomizeOpts.Images {
args = append(args, image.String())
}
_, err := r.runInDir(tempDir, r.kustomizeBin(), args...)
if err != nil {
return "", err
}
}
if kustomizeOpts.NamePrefix != "" {
_, err := r.runInDir(tempDir, r.kustomizeBin(), "edit", "set", "nameprefix", kustomizeOpts.NamePrefix)
if err != nil {
fmt.Println(err)
return "", err
}
}
if kustomizeOpts.NameSuffix != "" {
// "--" is there to avoid `namesuffix -acme` to fail due to `-a` being considered as a flag
_, err := r.runInDir(tempDir, r.kustomizeBin(), "edit", "set", "namesuffix", "--", kustomizeOpts.NameSuffix)
if err != nil {
return "", err
}
}
if kustomizeOpts.Namespace != "" {
_, err := r.runInDir(tempDir, r.kustomizeBin(), "edit", "set", "namespace", kustomizeOpts.Namespace)
if err != nil {
return "", err
}
}
outputFile := filepath.Join(tempDir, "templates", "kustomized.yaml")
kustomizeArgs := []string{"-o", outputFile, "build"}
if u.EnableAlphaPlugins {
f, err := r.kustomizeEnableAlphaPluginsFlag()
if err != nil {
return "", err
}
kustomizeArgs = append(kustomizeArgs, f)
}
f, err := r.kustomizeLoadRestrictionsNoneFlag()
if err != nil {
return "", err
}
kustomizeArgs = append(kustomizeArgs, f, "--enable-helm")
if u.HelmBinary != "" {
kustomizeArgs = append(kustomizeArgs, "--helm-command="+u.HelmBinary)
}
out, err := r.runInDir(tempDir, r.kustomizeBin(), append(kustomizeArgs, tempDir)...)
if err != nil {
return "", err
}
fmt.Println(out)
if err := os.RemoveAll(kustomizationPath); err != nil {
return "", fmt.Errorf("removing unnecessary kustomization.yaml after build: %v", err)
}
return outputFile, nil
}
// kustomizeVersion returns the kustomize binary version.
func (r *Runner) kustomizeVersion() (*semver.Version, error) {
versionInfo, err := r.run(r.kustomizeBin(), "version")
if err != nil {
return nil, err
}
vi, err := FindSemVerInfo(versionInfo)
if err != nil {
return nil, err
}
version, err := semver.NewVersion(vi)
if err != nil {
return nil, err
}
return version, nil
}
// kustomizeEnableAlphaPluginsFlag returns the kustomize binary alpha plugin argument.
// Above Kustomize v3, it is `--enable-alpha-plugins`.
// Below Kustomize v3 (including v3), it is `--enable_alpha_plugins`.
func (r *Runner) kustomizeEnableAlphaPluginsFlag() (string, error) {
version, err := r.kustomizeVersion()
if err != nil {
return "", err
}
if version.Major() > 3 {
return "--enable-alpha-plugins", nil
}
return "--enable_alpha_plugins", nil
}
// kustomizeLoadRestrictionsNoneFlag returns the kustomize loading files from outside
// the root argument.
// Above Kustomize v3, it is `--load-restrictor=LoadRestrictionsNone`.
// Below Kustomize v3 (including v3), it is `--load_restrictor=none`.
func (r *Runner) kustomizeLoadRestrictionsNoneFlag() (string, error) {
version, err := r.kustomizeVersion()
if err != nil {
return "", err
}
if version.Major() > 3 {
return "--load-restrictor=LoadRestrictionsNone", nil
}
return "--load_restrictor=none", nil
}