-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (191 loc) · 5.44 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
const (
flagPath = "path"
flagOutputPath = "outputPath"
apiVersionConfigKubernetesIO = "config.kubernetes.io"
annotationIndex = apiVersionConfigKubernetesIO + "/index"
annotationPath = apiVersionConfigKubernetesIO + "/path"
kustomizationFileName = "kustomization.yaml"
)
func main() {
pathOption := "."
outputFileOption := filepath.Join("static", "generated.yaml")
config := configMap{}
resourceList := &framework.ResourceList{FunctionConfig: &config}
cmd := framework.Command(resourceList, func() (err error) {
fmt.Fprintln(os.Stderr, "# Running kustomize", os.Getenv("KUSTOMIZE_VERSION"))
outputFileOption = filepath.Clean(outputFileOption)
copyFromInput := false
kptDir := pathOption
kustomizationDir := filepath.Clean(pathOption)
if !filepath.IsAbs(pathOption) {
// Prepare temp dir to copy from stdin
// when project dir is not mounted
copyFromInput = true
kptDir, err = ioutil.TempDir("", "")
if err != nil {
return err
}
defer os.RemoveAll(kptDir)
kustomizationDir = filepath.Join(kptDir, pathOption)
}
filteredItems := make([]*yaml.RNode, 0, len(resourceList.Items))
for _, item := range resourceList.Items {
meta, err := item.GetMeta()
if err != nil {
return err
}
if meta.Annotations == nil {
continue
}
// Exclude resources from previous output or with absolute or unknown path
itemPath := meta.Annotations[annotationPath]
if itemPath == "" || filepath.IsAbs(itemPath) || itemPath == outputFileOption {
continue
}
filteredItems = append(filteredItems, item)
if copyFromInput {
// Write stdin to temp dir
transformed := item
if filepath.Base(itemPath) == kustomizationFileName {
transformed = yaml.MustParse(transformed.MustString())
err = transformed.PipeE(yaml.FieldSetter{Name: yaml.MetadataField, Value: nil})
if err != nil {
return err
}
}
tmpItemPath := filepath.Join(kptDir, itemPath)
err = writeFile(tmpItemPath, transformed)
if err != nil {
return err
}
}
}
// Render the kustomization
kArgs := kustomizeArgs(kustomizationDir, config.Data)
transformed, err := buildKustomization(outputFileOption, kArgs)
if err != nil {
return err
}
if filepath.IsAbs(outputFileOption) {
// Write file to disk (sink) when absolute output path provided
manifest := ""
for _, o := range transformed {
data, err := o.String()
if err != nil {
return err
}
manifest += "---\n" + data
}
err = ioutil.WriteFile(outputFileOption, []byte(manifest), 0644)
if err != nil {
return err
}
}
// Add rendered resources to output
resourceList.Items = append(filteredItems, transformed...)
return nil
})
cmd.Flags().StringVar(&pathOption, flagPath, pathOption, "path to kustomization")
cmd.Flags().StringVar(&outputFileOption, flagOutputPath, outputFileOption, "output manifest path")
if err := cmd.Execute(); err != nil {
os.Stderr.WriteString("\n")
os.Exit(1)
}
}
type configMap struct {
Data map[string]string
}
func kustomizeArgs(path string, config map[string]string) []string {
a := make([]string, 0, len(config)+2)
a = append(a, "build", path)
for k, v := range config {
if k != flagPath && k != flagOutputPath {
a = append(a, fmt.Sprintf("%s=%s", k, v))
}
}
return a
}
func buildKustomization(outputPath string, kustomizeArgs []string) ([]*yaml.RNode, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command("kustomize", kustomizeArgs...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("kustomize build: %w: %s", err, strings.TrimSpace(stderr.String()))
}
// Parse output
r := []*yaml.RNode{}
d := yaml.NewDecoder(&stdout)
for {
v := yaml.Node{}
o := yaml.NewRNode(&v)
err = d.Decode(&v)
if err != nil {
break
}
err = setKptAnnotations(o, outputPath, len(r))
if err != nil {
break
}
r = append(r, o)
}
if err != nil && err != io.EOF {
return nil, fmt.Errorf("process kustomize output: %w", err)
}
return r, nil
}
func setKptAnnotations(o *yaml.RNode, path string, index int) error {
// Remove annotations field if empty.
// This is required because LookupCreate() doesn't create the MappingNode if it exists but is empty.
err := o.PipeE(yaml.LookupCreate(yaml.MappingNode, yaml.MetadataField), yaml.FieldClearer{Name: yaml.AnnotationsField, IfEmpty: true})
if err != nil {
return err
}
// Add path annotations
lookupAnnotations := yaml.LookupCreate(yaml.MappingNode, yaml.MetadataField, yaml.AnnotationsField)
err = o.PipeE(lookupAnnotations, yaml.FieldSetter{Name: annotationIndex, StringValue: strconv.Itoa(index)})
if err != nil {
return err
}
err = o.PipeE(lookupAnnotations, yaml.FieldSetter{Name: annotationPath, StringValue: path})
if err != nil {
return err
}
return nil
}
func writeFile(file string, transformed *yaml.RNode) (err error) {
data, err := transformed.String()
if err != nil {
return
}
err = os.MkdirAll(filepath.Dir(file), 0755)
if err != nil {
return
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return
}
defer func() {
if e := f.Close(); e != nil && err == nil {
err = e
}
}()
_, err = f.Write([]byte("---\n" + data))
return
}