-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathroot.go
405 lines (371 loc) · 11.9 KB
/
root.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2020 Fairwinds
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
package cmd
import (
"flag"
"fmt"
"os"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
"k8s.io/klog/v2"
"github.com/fairwindsops/reckoner/pkg/course"
"github.com/fairwindsops/reckoner/pkg/reckoner"
)
var (
version string
versionCommit string
courseSchema []byte
// runAll contains the boolean flag to install all the releases
runAll bool
// dryRun contains the boolean flag to run as dry run
dryRun bool
// courseFile is the name and path of the course.yml file
courseFile string
// onlyRun contains the list of releases to install
onlyRun []string
// createNamespaces contains the boolean flag to create namespaces
createNamespaces bool
// continueOnError contains the boolean flag to continue processing releases even if one errors
continueOnError bool
// inPlaceConvert contains the boolean flag to update the course file in place
inPlaceConvert bool
// noColor contains the boolean flag to disable color output
noColor bool
// importNamespace is the namespace that contains the release to be imported
importNamespace string
// importRelease is the name of the release to be imported
importRelease string
// importRepository is the helm repository for the imported release
importRepository string
// additionalHelmArgs is a list of arguments to add to all helm commands
additionalHelmArgs []string
// templateOutputDir
templateOutputDir string
)
func init() {
rootCmd.PersistentFlags().BoolVarP(&runAll, "run-all", "a", false, "Install every release in the course file")
rootCmd.PersistentFlags().StringSliceVarP(&onlyRun, "only", "o", nil, "Only install this list of releases. Can be passed multiple times.")
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Implies helm --dry-run --debug and skips any hooks")
rootCmd.PersistentFlags().BoolVar(&createNamespaces, "create-namespaces", true, "If true, allow reckoner to create namespaces.")
rootCmd.PersistentFlags().BoolVar(&noColor, "no-color", false, "If true, don't colorize output.")
rootCmd.PersistentFlags().StringSliceVar(&additionalHelmArgs, "helm-args", nil, "Additional arguments to pass to helm commands. Can be passed multiple times. used more than once. WARNING: Setting this will completely override any helm_args in the course.")
plotCmd.PersistentFlags().BoolVar(&continueOnError, "continue-on-error", false, "If true, continue to plot releases even if one or more has errors.")
updateCmd.PersistentFlags().BoolVar(&continueOnError, "continue-on-error", false, "If true, continue to update releases even if one or more has errors.")
diffCmd.PersistentFlags().BoolVar(&continueOnError, "continue-on-error", false, "If true, continue to diff releases even if one or more has errors.")
convertCmd.Flags().BoolVarP(&inPlaceConvert, "in-place", "i", false, "If specified, will update the file in place, otherwise outputs to stdout.")
importCmd.Flags().StringVar(&importNamespace, "namespace", "", "Namespace that contains the release to be imported.")
importCmd.Flags().StringVar(&importRelease, "release_name", "", "The name of the release to import.")
importCmd.Flags().StringVar(&importRepository, "repository", "", "The helm repository for the imported release.")
templateCmd.Flags().StringVar(&templateOutputDir, "output-dir", "", "path to the base output directory (eg, ~/myproject/manifests)")
// templateCmd.PersistentFlags().BoolVar(&continueOnError, "continue-on-error", false, "If true, continue to template releases even if one or more has errors.")
rootCmd.AddCommand(
plotCmd,
convertCmd,
templateCmd,
diffCmd,
lintCmd,
getManifestsCmd,
updateCmd,
importCmd,
updateReckonerCmd,
versionCmd,
)
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("v"))
color.Output = colorable.NewColorableStderr()
}
var rootCmd = &cobra.Command{
Use: "reckoner",
Short: "reckoner",
Long: `A tool to maintain your helm installations as code.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("You must specify a sub-command.")
err := cmd.Help()
if err != nil {
klog.Error(err)
}
os.Exit(1)
},
}
var plotCmd = &cobra.Command{
Use: "plot",
Short: "plot <course file>",
Long: "Runs a helm install on a release or several releases.",
PreRunE: validateCobraArgs,
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: dryRun,
CreateNamespaces: createNamespaces,
ContinueOnError: continueOnError,
Releases: onlyRun,
}
err := client.Init(courseFile, true)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
err = client.Plot()
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
if client.Errors > 0 {
os.Exit(1)
}
},
}
var templateCmd = &cobra.Command{
Use: "template",
Short: "template <course file>",
Long: "Templates a helm chart for a release or several releases. Automatically sets --create-namespaces=false --dry-run=true",
PreRunE: validateCobraArgs,
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: true,
CreateNamespaces: false,
ContinueOnError: continueOnError,
Releases: onlyRun,
}
err := client.Init(courseFile, false)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
tmpl, err := client.TemplateAll(templateOutputDir)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
if len(templateOutputDir) < 1 {
fmt.Println(tmpl)
}
},
}
var getManifestsCmd = &cobra.Command{
Use: "get-manifests",
Short: "get-manifests <course file>",
Long: "Gets the manifests currently in the cluster.",
PreRunE: validateCobraArgs,
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: true,
CreateNamespaces: false,
ContinueOnError: false,
Releases: onlyRun,
}
err := client.Init(courseFile, true)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
manifests, err := client.GetManifests()
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
fmt.Print(manifests)
},
}
var diffCmd = &cobra.Command{
Use: "diff",
Short: "diff <course file>",
Long: "Diffs the currently defined release and the one in the cluster",
PreRunE: validateCobraArgs,
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: true,
CreateNamespaces: false,
ContinueOnError: continueOnError,
Releases: onlyRun,
}
err := client.Init(courseFile, true)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
if err := client.UpdateHelmRepos(); err != nil {
color.Red(err.Error())
os.Exit(1)
}
err = client.Diff()
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
if client.Errors > 0 {
os.Exit(1)
}
},
}
var lintCmd = &cobra.Command{
Use: "lint",
Short: "lint <course file>",
Long: "Lints the course file. Checks for structure and valid yaml.",
PreRunE: func(cmd *cobra.Command, args []string) error {
runAll = true
return validateCobraArgs(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: true,
CreateNamespaces: false,
ContinueOnError: false,
Releases: onlyRun,
}
err := client.Init(courseFile, false)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
color.Green("No schema validation errors found in course file: %s", courseFile)
},
}
var convertCmd = &cobra.Command{
Use: "convert",
Short: "convert <course file>",
Long: "Converts a course file from the v1 schema to v2 schema (reckoner v6.0.0+)",
PreRunE: func(cmd *cobra.Command, args []string) error {
runAll = true
return validateCobraArgs(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
course.ParseEnv = false
newCourse, err := course.OpenCourseFile(courseFile, courseSchema)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
w := os.Stdout
if inPlaceConvert {
f, err := os.OpenFile(courseFile, os.O_RDWR, 0644)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
defer f.Close()
err = f.Truncate(0)
if err != nil {
color.Red("failed to truncate course file \"%s\": %s", courseFile, err)
os.Exit(1)
}
w = f
}
e := yaml.NewEncoder(w)
defer e.Close()
// We prefer 2 spaces in yaml
e.SetIndent(2)
err = e.Encode(newCourse)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
},
}
var updateCmd = &cobra.Command{
Use: "update",
Short: "update <course file>",
Long: "Only install/upgrade a release if there are changes.",
PreRunE: validateCobraArgs,
Run: func(cmd *cobra.Command, args []string) {
client := reckoner.Client{
ReckonerVersion: version,
Schema: courseSchema,
HelmArgs: additionalHelmArgs,
DryRun: dryRun,
CreateNamespaces: createNamespaces,
ContinueOnError: continueOnError,
Releases: onlyRun,
}
err := client.Init(courseFile, true)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
err = client.Update()
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
if client.Errors > 0 {
os.Exit(1)
}
},
}
var importCmd = &cobra.Command{
Use: "import",
Short: "import",
Long: "Outputs a release block that can be used to import the specified release.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if importNamespace == "" || importRelease == "" || importRepository == "" {
return fmt.Errorf("import requires --namespace, --release, and --repository")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
color.Yellow("Import is experimental and may be unreliable. Double check all output. Output assumes course schema v2.")
out, err := reckoner.ImportOutput(importRelease, importNamespace, importRepository)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
fmt.Println(out)
},
}
// getCourseFilePath returns the path to a course YAML file. This does not guarantee the file exists.
// The order of precedence is as follows (higher overrides lower)
// - command-line argument
// - environment variable
// - default value (nothing above was specified)
func getCourseFilePath(args []string) string {
// attempt to get the course file from environment variable
courseFile = os.Getenv("RECKONER_COURSE_FILE") // environment variable override
// if the environment variable was unset or zero-length,
// try default value of 'course.yml'
if len(courseFile) == 0 {
courseFile = "course.yml" // default course file
}
// allow cli argument to override everything else
if len(args) > 0 { // an argument was passed
courseFile = args[0] // use it
}
return courseFile
}
// Execute the stuff
func Execute(VERSION string, COMMIT string, schema []byte) {
version = VERSION
versionCommit = COMMIT
courseSchema = schema
if err := rootCmd.Execute(); err != nil {
klog.Error(err)
os.Exit(1)
}
}