-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathmain.go
414 lines (351 loc) · 11 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
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
406
407
408
409
410
411
412
413
414
package main
import (
"errors"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/flux-iac/tofu-controller/tfctl"
)
var (
// BuildSHA is the tfctl version
BuildSHA string
// BuildVersion is the tfctl build version
BuildVersion string
)
var defaultNamespace = "flux-system"
var kubeconfigArgs = genericclioptions.NewConfigFlags(false)
func main() {
cmd := newRootCommand()
cobra.CheckErr(cmd.Execute())
}
func newRootCommand() *cobra.Command {
app := tfctl.New(BuildSHA, BuildVersion)
config := viper.New()
rootCmd := &cobra.Command{
Use: "tfctl",
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return app.Init(kubeconfigArgs, config)
},
}
configureDefaultNamespace()
// flags
rootCmd.PersistentFlags().String("terraform", "/usr/bin/terraform", "The location of the terraform binary.")
kubeconfigArgs.AddFlags(rootCmd.PersistentFlags())
// bind flags to config
config.BindPFlags(rootCmd.PersistentFlags())
rootCmd.AddCommand(buildCreateCmd(app))
rootCmd.AddCommand(buildDeleteCmd(app))
rootCmd.AddCommand(buildForceUnlockCmd(app))
rootCmd.AddCommand(buildInstallCmd(app))
rootCmd.AddCommand(buildReconcileCmd(app))
rootCmd.AddCommand(buildApprovePlanCmd(app))
rootCmd.AddCommand(buildReplanCmd(app))
rootCmd.AddCommand(buildResumeCmd(app))
rootCmd.AddCommand(buildSuspendCmd(app))
rootCmd.AddCommand(buildUninstallCmd(app))
rootCmd.AddCommand(buildVersionCmd(app))
rootCmd.AddCommand(buildGetGroup(app))
rootCmd.AddCommand(buildShowGroup(app))
rootCmd.AddCommand(buildBreakTheGlassCmd(app))
rootCmd.AddCommand(buildLogsCmd())
return rootCmd
}
func buildVersionCmd(app *tfctl.CLI) *cobra.Command {
install := &cobra.Command{
Use: "version",
Short: "Prints tf-controller and tfctl version information",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Version(os.Stdout)
},
}
install.Flags().String("version", "", "The version of tf-controller to install.")
viper.BindPFlag("version", install.Flags().Lookup("version"))
return install
}
var installExamples = `
# Install the Terraform controller
tfctl install --namespace=flux-system
# Generate the Terraform controller manifests and print them to stdout
tfctl install --namespace=flux-system --export
# Install a specific version of the Terraform controller
tfctl install --namespace=flux-system --version=v0.9.3
`
func buildInstallCmd(app *tfctl.CLI) *cobra.Command {
install := &cobra.Command{
Use: "install",
Short: "Install the tf-controller",
Long: "Install the tf-controller resources in the specified namespace",
Example: strings.Trim(installExamples, "\n"),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Install(os.Stdout, viper.GetString("version"), viper.GetBool("export"))
},
}
install.Flags().String("version", "", "The version of tf-controller to install.")
install.Flags().Bool("export", false, "Print installation manifests to stdout")
viper.BindPFlags(install.Flags())
return install
}
func buildUninstallCmd(app *tfctl.CLI) *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Uninstall the tf-controller",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Uninstall(os.Stdout)
},
}
}
var reconcileExamples = `
# Reconcile a Terraform resource
tfctl reconcile --namespace=default my-resource
`
func buildReconcileCmd(app *tfctl.CLI) *cobra.Command {
return &cobra.Command{
Use: "reconcile NAME",
Short: "Trigger a reconcile of the provided resource",
Example: strings.Trim(reconcileExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Reconcile(os.Stdout, args[0])
},
}
}
var suspendExamples = `
# Suspend reconciliation for a Terraform resource
tfctl suspend my-resource
# Suspend reconciliation for all Terraform resources
tfctl suspend --all
`
func buildSuspendCmd(app *tfctl.CLI) *cobra.Command {
suspend := &cobra.Command{
Use: "suspend NAME",
Short: "Suspend reconciliation for the provided resource",
Example: strings.Trim(suspendExamples, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
resource := ""
if !all {
if len(args) == 0 {
return errors.New("resource name required")
}
resource = args[0]
}
return app.Suspend(os.Stdout, resource)
},
}
suspend.Flags().BoolP("all", "A", false, "Suspend reconciliation for all resources")
return suspend
}
var resumeExamples = `
# Resume reconciliation for a Terraform resource
tfctl resume my-resource
# Resume reconciliation for all Terraform resources
tfctl resume --all
`
func buildResumeCmd(app *tfctl.CLI) *cobra.Command {
resume := &cobra.Command{
Use: "resume NAME",
Short: "Resume reconciliation for the provided resource",
Example: strings.Trim(resumeExamples, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
resource := ""
if !all {
if len(args) == 0 {
return errors.New("resource name required")
}
resource = args[0]
}
return app.Resume(os.Stdout, resource)
},
}
resume.Flags().BoolP("all", "A", false, "Resume reconciliation for all resources")
return resume
}
func buildShowGroup(app *tfctl.CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "Show a Terraform configuration",
}
cmd.AddCommand(buildShowPlanCmd(app))
return cmd
}
var showPlanExamples = `
# Show the plan for a Terraform resource
tfctl show plan my-resource
`
func buildShowPlanCmd(app *tfctl.CLI) *cobra.Command {
return &cobra.Command{
Use: "plan NAME",
Short: "Show pending Terraform plan",
Example: strings.Trim(showPlanExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.ShowPlan(os.Stdout, args[0])
},
}
}
var approvePlanExamples = `
# Approve the plan for a Terraform resource
tfctl approve my-resource -f manifests/my-resource.yaml
`
func buildApprovePlanCmd(app *tfctl.CLI) *cobra.Command {
approvePlan := &cobra.Command{
Use: "approve NAME",
Short: "Approve pending Terraform plan",
Example: strings.Trim(approvePlanExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.ApprovePlan(os.Stdout, args[0], viper.GetString("filename"))
},
}
approvePlan.Flags().StringP("filename", "f", "", "YAML file to approve.")
viper.BindPFlags(approvePlan.Flags())
return approvePlan
}
var getExamples = `
# List all Terraform resources in the given namespace
tfctl get --namespace=default
`
func buildGetGroup(app *tfctl.CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get Terraform resources",
Example: strings.Trim(getExamples, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Get(os.Stdout)
},
}
cmd.AddCommand(buildGetTerraformCmd(app))
return cmd
}
var getTerraformExamples = `
# Show a specific Terraform resource
tfctl get my-resource
`
func buildGetTerraformCmd(app *tfctl.CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "get NAME",
Short: "Get a Terraform resource",
Example: strings.Trim(getTerraformExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.GetTerraform(os.Stdout, args[0])
},
}
return cmd
}
var deleteExamples = `
# Delete a Terraform resource
tfctl delete my-resource
`
func buildDeleteCmd(app *tfctl.CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "delete NAME",
Short: "Delete a Terraform resource",
Example: strings.Trim(getTerraformExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.DeleteTerraform(os.Stdout, args[0])
},
}
return cmd
}
var createExamples = `
# Create a Terraform resource in the default namespace
tfctl create -n default my-resource --source GitRepository/my-project --path ./terraform --interval 15m
# Generate a Terraform resource manifest
tfctl create -n default my-resource --source GitRepository/my-project --path ./terraform --interval 15m --export
`
func buildCreateCmd(app *tfctl.CLI) *cobra.Command {
create := &cobra.Command{
Use: "create NAME",
Short: "Create a Terraform resource",
Example: strings.Trim(createExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Create(os.Stdout,
args[0],
viper.GetString("namespace"),
viper.GetString("path"),
viper.GetString("source"),
viper.GetString("interval"),
viper.GetBool("export"))
},
}
create.Flags().String("path", "", "")
create.Flags().String("source", "", "")
create.Flags().String("interval", "", "")
create.Flags().Bool("export", false, "Print generated Terraform resource to stdout")
viper.BindPFlags(create.Flags())
return create
}
var forceUnlockExample = `
# Unlock Terraform resource "aws-security-group" with lock id "f2ab685b-f84d-ac0b-a125-378a22877e8d" in the default namespace
tfctl force-unlock aws-security-group -n default --lock-id="f2ab685b-f84d-ac0b-a125-378a22877e8d"
`
func buildForceUnlockCmd(app *tfctl.CLI) *cobra.Command {
forceUnlock := &cobra.Command{
Use: "force-unlock",
Short: "Force unlock a locked Terraform State",
Example: strings.Trim(forceUnlockExample, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.ForceUnlock(
os.Stdout,
args[0],
viper.GetString("lock-id"),
)
},
}
forceUnlock.Flags().String("lock-id", "", "Set the lock-id that currently holds the lock of the terraform state e.g. f2ab685b-f84d-ac0b-a125-378a22877e8d")
viper.BindPFlags(forceUnlock.Flags())
return forceUnlock
}
var replanExamples = `
# Replan a Terraform resource
tfctl -n default replan my-resource
`
func buildReplanCmd(app *tfctl.CLI) *cobra.Command {
replan := &cobra.Command{
Use: "replan",
Short: "Replan a Terraform resource",
Example: strings.Trim(replanExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Replan(cmd.Context(), os.Stdout, args[0])
},
}
return replan
}
func buildBreakTheGlassCmd(app *tfctl.CLI) *cobra.Command {
breakTheGlass := &cobra.Command{
Use: "break-glass",
Aliases: []string{"break-the-glass", "bg", "btg"},
Short: "Break the glass",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.BreakTheGlass(cmd.Context(), os.Stdout, args[0])
},
}
return breakTheGlass
}
func configureDefaultNamespace() {
*kubeconfigArgs.Namespace = defaultNamespace
fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE")
if fromEnv != "" {
kubeconfigArgs.Namespace = &fromEnv
}
}