-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
362 lines (347 loc) · 10.6 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"fmt"
"github.com/andrew-d/go-termutil"
"io"
"log"
"os"
"path"
"github.com/hashicorp/go-getter"
"github.com/sirupsen/logrus"
"github.com/elek/flekszible/api/v2/processor"
"github.com/elek/flekszible/pkg"
"github.com/elek/flekszible/pkg/operator"
"github.com/urfave/cli"
)
var version string
var commit string
var date string
func main() {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true, ForceQuote: false})
var inputDir, outputDir, imageOverride, namespaceOverride string
var minikube bool
app := cli.NewApp()
app.Name = "Flekszible"
app.Usage = "Generate kubernetes sources files"
app.Description = "Kubernetes resource file manager"
app.Version = fmt.Sprintf("%s (%s, %s)", version, commit, date)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "source, s",
Value: "",
Usage: "Source directory to read the Flekszible definition (default: current dir)",
Destination: &inputDir,
},
cli.StringFlag{
Name: "destination, d",
Value: "",
Usage: "Destination directory to generate the k8s resource (default: current dir)",
Destination: &outputDir,
},
}
app.Action = func(c *cli.Context) error {
if !termutil.Isatty(os.Stdin.Fd()) {
raw, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
return pkg.Kustomize(raw)
}
cli.ShowAppHelpAndExit(c, 1)
return nil
}
app.Commands = []cli.Command{
{
Name: "generate",
Aliases: []string{"g"},
Usage: "Generate kubernetes resources files",
Flags: []cli.Flag{
cli.StringFlag{
Name: "namespace, n",
Value: "",
Usage: "kubernetes namespace override. With empty value (\"\") the current namespace will " +
"be used. With exact value the namespace will be forced (set even if the namespace is not added to the to the k8s resources",
Destination: &namespaceOverride,
},
cli.StringFlag{
Name: "image, i",
Usage: "docker image name override",
Destination: &imageOverride,
},
cli.StringSliceFlag{
Name: "transformations, t",
Usage: "manually defined transformations",
},
cli.StringSliceFlag{
Name: "import, p",
Usage: "Define additional import paths",
},
cli.BoolFlag{
Name: "print",
Usage: "Print out the result to the standard output (same as to use - as the destination'-d -')",
},
cli.BoolFlag{
Name: "minikube",
Usage: "Enable minikube specific defaults (eg. daemonset to statefulset conversion)",
Destination: &minikube,
},
},
Action: func(c *cli.Context) error {
outputDir := findOutputDir(&outputDir, c.Args().Get(0))
if c.Bool("print") {
outputDir = "-"
}
context := processor.CreateRenderContext("k8s", findInputDir(&inputDir, c.Args().Get(0)), outputDir)
context.ImageOverride = imageOverride
context.Namespace = namespaceOverride
return pkg.Run(context, minikube, c.StringSlice("import"), c.StringSlice("transformations"))
},
},
{
Name: "import",
Usage: "Import multiple kubernetes resource from file or stdin and generate output to current dir or stdout.",
ArgsUsage: "[file or -] [output dir]",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "transformations, t",
Usage: "manually defined transformations",
},
cli.BoolFlag{
Name: "helm3",
Usage: "Use only parts between MANIFESTS and NOTES. Useful for `helm install --dryrun` output",
},
cli.BoolFlag{
Name: "print",
Usage: "Print out the result to the standard output (same as to use - as the destination'-d -')",
},
},
Action: func(c *cli.Context) error {
dir, err := os.Getwd()
if err != nil {
return err
}
if c.Bool("print") {
dir = "-"
}
err = pkg.Import(c.Args().Get(0), c.StringSlice("transformations"), dir, c.Bool("helm3"))
if err != nil {
return err
}
return nil
},
},
{
Name: "list",
Usage: "List managed kubernetes resources files.",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(&inputDir, c.Args().Get(0)), findOutputDir(&outputDir, c.Args().Get(0)))
pkg.ListResources(context)
return nil
},
},
{
Name: "tree",
Usage: "List managed resources files and registered transformations.",
ArgsUsage: "[flekszible_dir]",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "transformations, t",
Usage: "manually defined transformations",
},
},
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(&inputDir, c.Args().Get(0)), findOutputDir(&outputDir, c.Args().Get(0)))
err := context.Init()
if err != nil {
return err
}
err = context.AddAdHocTransformations(c.StringSlice("transformations"))
if err != nil {
return err
}
pkg.Tree(context)
return nil
},
},
{
Name: "clean",
Usage: "Delete yaml files from the destination directories",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Delete all yaml files from destination directories",
},
},
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(&inputDir, c.Args().Get(0)), findOutputDir(&outputDir, c.Args().Get(0)))
return pkg.Cleanup(context, c.Bool("all"))
},
},
appCommands(&inputDir, &outputDir),
sourceCommands(&inputDir, &outputDir),
transformationCommands(&inputDir, &outputDir),
admissionCommands(),
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func admissionCommands() cli.Command {
return cli.Command{
Name: "operator",
Usage: "Start Kubernetes operator (Mutating webhook endpoint)",
ArgsUsage: "Directory of the Flekszible definition",
Action: func(c *cli.Context) error {
return operator.StartServer(c.Args().First())
},
}
}
func appCommands(inputDir *string, outputDir *string) cli.Command {
return cli.Command{
Name: "app",
Usage: "Manage importable dirs/applications.",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List all the referenced/imported flekszible directory.",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
pkg.ListApp(context)
return nil
},
},
{
Name: "search",
Usage: "Search for available importable apps/dirs from the active sources",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
return pkg.SearchComponent(context)
},
},
{
Name: "add",
Usage: "Add (import) new app to the flekszible.yaml.",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, ""), findOutputDir(outputDir, c.Args().Get(0)))
pkg.AddApp(context, *inputDir, c.Args().First())
return nil
},
},
},
}
}
func sourceCommands(inputDir, outputDir *string) cli.Command {
return cli.Command{
Name: "source",
Usage: "Manage sources of the importable applications.",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List registered sources (directories / repositories where other directories are imported from)",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
pkg.ListSources(context)
return nil
},
},
{
Name: "search",
Usage: "Search for the available source repositories (github repositories with flekszible:topic).",
Action: func(c *cli.Context) error {
pkg.SearchSource()
return nil
},
},
{
Name: "add",
Usage: "Register source to your Flekszible descriptor file",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
return pkg.AddSource(context, findInputDir(inputDir, ""), c.Args().First())
},
},
{
Name: "update",
Usage: "Force update to remote repositories",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
return pkg.UpdateSource(context, findInputDir(inputDir, ""), c.Args().First())
},
},
},
}
}
func transformationCommands(inputDir, outputDir *string) cli.Command {
return cli.Command{
Name: "transformation",
Aliases: []string{"trafo", "definitions"},
Usage: "Show available transformation types/definitions",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"search"},
Usage: "List available transformation types.",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, c.Args().Get(0)), findOutputDir(outputDir, c.Args().Get(0)))
return pkg.ListProcessor(context)
},
},
{
Name: "info",
Aliases: []string{"show"},
Usage: "Show details information / help about a specific transformation type",
ArgsUsage: "[flekszible_dir]",
Action: func(c *cli.Context) error {
context := processor.CreateRenderContext("k8s", findInputDir(inputDir, ""), findOutputDir(outputDir, c.Args().Get(0)))
return pkg.ShowProcessor(context, c.Args().First())
},
},
},
}
}
func findInputDir(argument *string, inputDirFromArg string) string {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
if len(inputDirFromArg) > 0 {
return inputDirFromArg
}
if *argument != "" {
//input dir is specificed
if _, err := os.Stat(*argument); err == nil {
return *argument
} else {
//no such file, assuming it's an url
workDir := path.Join(pwd, ".flekszible-source")
err := getter.Get(workDir, *argument)
if err != nil {
panic(err)
}
return workDir
}
}
return pwd
}
func findOutputDir(argument *string, outputDirFromArg string) string {
if len(outputDirFromArg) > 0 {
return outputDirFromArg
}
if *argument != "" {
return *argument
}
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
return pwd
}