-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
282 lines (230 loc) · 7.49 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/chime/mani-diffy/pkg/helm"
"github.com/chime/mani-diffy/pkg/kustomize"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)
const InfiniteDepth = -1
// Renderer is a function that can render an Argo application.
type Renderer func(*v1alpha1.Application, string) error
// PostRenderer is a function that can be called after an Argo application is rendered.
type PostRenderer func(string) error
// Walker walks a directory tree looking for Argo applications and renders them
// using a depth first search.
type Walker struct {
// HelmTemplate is a function that can render an Argo application using Helm
HelmTemplate Renderer
// CopySource is a function that can copy an Argo application to a directory
CopySource Renderer
// PostRender is a function that can be called after an Argo application is rendered.
PostRender PostRenderer
// GenerateHash is used to generate a cache key for an Argo application
GenerateHash func(*v1alpha1.Application) (string, error)
ignoreSuffix string
}
// Walk walks a directory tree looking for Argo applications and renders them
func (w *Walker) Walk(inputPath, outputPath string, maxDepth int, hashes HashStore) error {
visited := make(map[string]bool)
if err := w.walk(inputPath, outputPath, 0, maxDepth, visited, hashes); err != nil {
return err
}
if err := hashes.Save(); err != nil {
return err
}
if maxDepth == InfiniteDepth {
return pruneUnvisited(visited, outputPath)
}
return nil
}
func pruneUnvisited(visited map[string]bool, outputPath string) error {
files, err := os.ReadDir(outputPath)
if err != nil {
return err
}
for _, f := range files {
if !f.IsDir() {
continue
}
path := filepath.Join(outputPath, f.Name())
if visited[path] {
continue
}
if err := os.RemoveAll(path); err != nil {
return err
}
}
return nil
}
func (w *Walker) walk(inputPath, outputPath string, depth, maxDepth int, visited map[string]bool, hashes HashStore) error {
if maxDepth != InfiniteDepth {
// If we've reached the max depth, stop walking
if depth > maxDepth {
return nil
}
}
log.Println("Dropping into", inputPath)
fi, err := os.ReadDir(inputPath)
if err != nil {
return err
}
for _, file := range fi {
if !strings.Contains(file.Name(), ".yaml") {
continue
}
crds, err := helm.Read(filepath.Join(inputPath, file.Name()))
if err != nil {
return err
}
for _, crd := range crds {
if crd.Kind != "Application" {
continue
}
if strings.HasSuffix(crd.ObjectMeta.Name, w.ignoreSuffix) {
continue
}
path := filepath.Join(outputPath, crd.ObjectMeta.Name)
visited[path] = true
hash, err := hashes.Get(crd.ObjectMeta.Name)
// COMPARE HASHES HERE. STEP INTO RENDER IF NO MATCH
if err != nil {
return err
}
hashGenerated, err := w.GenerateHash(crd)
if err != nil {
if errors.Is(err, kustomize.ErrNotSupported) {
continue
}
return err
}
emptyManifest, err := helm.EmptyManifest(filepath.Join(path, "manifest.yaml"))
if err != nil {
return err
}
if hashGenerated != hash || emptyManifest {
log.Printf("No match detected. Render: %s\n", crd.ObjectMeta.Name)
if err := w.Render(crd, path); err != nil {
if errors.Is(err, kustomize.ErrNotSupported) {
continue
}
return err
}
if err := hashes.Add(crd.ObjectMeta.Name, hashGenerated); err != nil {
return err
}
}
if err := w.walk(path, outputPath, depth+1, maxDepth, visited, hashes); err != nil {
return err
}
}
}
return nil
}
func (w *Walker) Render(application *v1alpha1.Application, output string) error {
log.Println("Render", application.ObjectMeta.Name)
var render Renderer
// Figure out which renderer to use
switch {
case application.Spec.Source.Helm != nil:
render = w.HelmTemplate
case application.Spec.Source.Kustomize != nil:
log.Println("WARNING: kustomize not supported")
return kustomize.ErrNotSupported
default:
render = w.CopySource
}
// Make sure the directory is empty before rendering.
if err := os.RemoveAll(output); err != nil {
return err
}
// Render
if err := render(application, output); err != nil {
return err
}
// Call the post renderer to do any post processing
if w.PostRender != nil {
if err := w.PostRender(output); err != nil {
return fmt.Errorf("post render failed: %w", err)
}
}
return nil
}
func HelmTemplate(application *v1alpha1.Application, output string) error {
return helm.Run(application, output, "", "")
}
func CopySource(application *v1alpha1.Application, output string) error {
cmd := exec.Command("cp", "-r", application.Spec.Source.Path+"/.", output)
return cmd.Run()
}
func PostRender(command string) PostRenderer {
return func(output string) error {
cmd := exec.Command(command, output)
cmd.Stderr = os.Stderr
return cmd.Run()
}
}
func main() {
root := flag.String("root", "bootstrap", "Directory to initially look for k8s manifests containing Argo applications. The root of the tree.")
workdir := flag.String("workdir", ".", "Directory to run the command in.")
renderDir := flag.String("output", ".zz.auto-generated", "Path to store the compiled Argo applications.")
maxDepth := flag.Int("max-depth", InfiniteDepth, "Maximum depth for the depth first walk.")
hashStore := flag.String("hash-store", "sumfile", "The hashing backend to use. Can be `sumfile` or `json`.")
hashStrategy := flag.String("hash-strategy", HashStrategyReadWrite, "Whether to read + write, or just read hashes. Can be `readwrite` or `read`.")
ignoreSuffix := flag.String("ignore-suffix", "-ignore", "Suffix used to identify apps to ignore")
skipRenderKey := flag.String("skip-render-key", "do-not-render", "Key to not render")
ignoreValueFile := flag.String("ignore-value-file", "overrides-to-ignore", "Override file to ignore based on filename")
postRenderer := flag.String("post-renderer", "", "When provided, binary will be called after an application is rendered.")
flag.Parse()
// Runs the command in the specified directory
err := os.Chdir(*workdir)
if err != nil {
log.Fatal("Could not set workdir: ", err)
}
start := time.Now()
if err := helm.VerifyRenderDir(*renderDir); err != nil {
log.Fatal(err)
}
h, err := getHashStore(*hashStore, *hashStrategy, *renderDir)
if err != nil {
log.Fatal(err)
}
w := &Walker{
CopySource: CopySource,
HelmTemplate: func(application *v1alpha1.Application, output string) error {
return helm.Run(application, output, *skipRenderKey, *ignoreValueFile)
},
GenerateHash: func(application *v1alpha1.Application) (string, error) {
return helm.GenerateHash(application, *ignoreValueFile)
},
ignoreSuffix: *ignoreSuffix,
}
if *postRenderer != "" {
w.PostRender = PostRender(*postRenderer)
}
if err := w.Walk(*root, *renderDir, *maxDepth, h); err != nil {
log.Fatal(err)
}
log.Printf("mani-diffy took %v to run", time.Since(start))
}
var hashStores = map[string]func(string, string) (HashStore, error){
"sumfile": func(outputPath, hashStrategy string) (HashStore, error) { //nolint:unparam
return NewSumFileStore(outputPath, hashStrategy), nil
},
"json": func(outputPath, hashStrategy string) (HashStore, error) {
return NewJSONHashStore(filepath.Join(outputPath, "hashes.json"), hashStrategy)
},
}
func getHashStore(hashStore, hashStrategy, outputPath string) (HashStore, error) {
if fn, ok := hashStores[hashStore]; ok {
return fn(outputPath, hashStrategy)
}
return nil, fmt.Errorf("Invalid hash store: %v", hashStore)
}