Skip to content

Commit fa0552b

Browse files
author
Chanwit Kaewkasi
authored
Merge pull request #448 from weaveworks/tfctl-replan
implement replan command
2 parents cc84e06 + ac6444a commit fa0552b

File tree

15 files changed

+1589
-217
lines changed

15 files changed

+1589
-217
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ FROM golang:1.19 as builder
44
RUN apt-get update && apt-get install -y unzip
55

66
WORKDIR /workspace
7-
# Copy API and it's go module
7+
# Copy API and its Go module
88
COPY api/ api/
9+
# Copy tfctl and its Go module
10+
COPY tfctl/ tfctl/
911

1012
# Copy the Go Modules manifests
1113
COPY go.mod go.mod

cmd/tfctl/main.go

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,20 @@ func newRootCommand() *cobra.Command {
5353
// bind flags to config
5454
config.BindPFlags(rootCmd.PersistentFlags())
5555

56-
rootCmd.AddCommand(buildVersionCmd(app))
57-
rootCmd.AddCommand(buildPlanGroup(app))
56+
rootCmd.AddCommand(buildCreateCmd(app))
57+
rootCmd.AddCommand(buildDeleteCmd(app))
58+
rootCmd.AddCommand(buildForceUnlockCmd(app))
5859
rootCmd.AddCommand(buildInstallCmd(app))
59-
rootCmd.AddCommand(buildUninstallCmd(app))
6060
rootCmd.AddCommand(buildReconcileCmd(app))
61-
rootCmd.AddCommand(buildSuspendCmd(app))
61+
rootCmd.AddCommand(buildApprovePlanCmd(app))
62+
rootCmd.AddCommand(buildReplanCmd(app))
6263
rootCmd.AddCommand(buildResumeCmd(app))
64+
rootCmd.AddCommand(buildSuspendCmd(app))
65+
rootCmd.AddCommand(buildUninstallCmd(app))
66+
rootCmd.AddCommand(buildVersionCmd(app))
67+
6368
rootCmd.AddCommand(buildGetGroup(app))
64-
rootCmd.AddCommand(buildDeleteCmd(app))
65-
rootCmd.AddCommand(buildCreateCmd(app))
66-
rootCmd.AddCommand(buildForceUnlockCmd(app))
69+
rootCmd.AddCommand(buildShowGroup(app))
6770

6871
return rootCmd
6972
}
@@ -172,48 +175,51 @@ func buildResumeCmd(app *tfctl.CLI) *cobra.Command {
172175
}
173176
}
174177

175-
func buildPlanGroup(app *tfctl.CLI) *cobra.Command {
178+
func buildShowGroup(app *tfctl.CLI) *cobra.Command {
176179
cmd := &cobra.Command{
177-
Use: "plan",
178-
Short: "Plan a Terraform configuration",
180+
Use: "show",
181+
Short: "Show a Terraform configuration",
179182
}
180-
cmd.AddCommand(buildPlanShowCmd(app))
181-
cmd.AddCommand(buildPlanApproveCmd(app))
183+
cmd.AddCommand(buildShowPlanCmd(app))
182184
return cmd
183185
}
184186

185-
var planShowExamples = `
187+
var showPlanExamples = `
186188
# Show the plan for a Terraform resource
187-
tfctl plan show my-resource
189+
tfctl show plan my-resource
188190
`
189191

190-
func buildPlanShowCmd(app *tfctl.CLI) *cobra.Command {
192+
func buildShowPlanCmd(app *tfctl.CLI) *cobra.Command {
191193
return &cobra.Command{
192-
Use: "show NAME",
194+
Use: "plan NAME",
193195
Short: "Show pending Terraform plan",
194-
Example: strings.Trim(planShowExamples, "\n"),
196+
Example: strings.Trim(showPlanExamples, "\n"),
195197
Args: cobra.ExactArgs(1),
196198
RunE: func(cmd *cobra.Command, args []string) error {
197199
return app.ShowPlan(os.Stdout, args[0])
198200
},
199201
}
200202
}
201203

202-
var planApproveExamples = `
204+
var approvePlanExamples = `
203205
# Approve the plan for a Terraform resource
204-
tfctl plan approve my-resource
206+
tfctl approve my-resource -f manifests/my-resource.yaml
205207
`
206208

207-
func buildPlanApproveCmd(app *tfctl.CLI) *cobra.Command {
208-
return &cobra.Command{
209+
func buildApprovePlanCmd(app *tfctl.CLI) *cobra.Command {
210+
approvePlan := &cobra.Command{
209211
Use: "approve NAME",
210212
Short: "Approve pending Terraform plan",
211-
Example: strings.Trim(planApproveExamples, "\n"),
213+
Example: strings.Trim(approvePlanExamples, "\n"),
212214
Args: cobra.ExactArgs(1),
213215
RunE: func(cmd *cobra.Command, args []string) error {
214-
return app.ApprovePlan(os.Stdout, args[0])
216+
return app.ApprovePlan(os.Stdout, args[0], viper.GetString("filename"))
215217
},
216218
}
219+
220+
approvePlan.Flags().StringP("filename", "f", "", "YAML file to approve.")
221+
viper.BindPFlags(approvePlan.Flags())
222+
return approvePlan
217223
}
218224

219225
var getExamples = `
@@ -302,14 +308,6 @@ func buildCreateCmd(app *tfctl.CLI) *cobra.Command {
302308
return create
303309
}
304310

305-
func configureDefaultNamespace() {
306-
*kubeconfigArgs.Namespace = defaultNamespace
307-
fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE")
308-
if fromEnv != "" {
309-
kubeconfigArgs.Namespace = &fromEnv
310-
}
311-
}
312-
313311
var forceUnlockExample = `
314312
# Unlock Terraform resource "aws-security-group" with lock id "f2ab685b-f84d-ac0b-a125-378a22877e8d" in the default namespace
315313
tfctl force-unlock aws-security-group -n default --lock-id="f2ab685b-f84d-ac0b-a125-378a22877e8d"
@@ -333,3 +331,29 @@ func buildForceUnlockCmd(app *tfctl.CLI) *cobra.Command {
333331
viper.BindPFlags(forceUnlock.Flags())
334332
return forceUnlock
335333
}
334+
335+
var replanExamples = `
336+
# Replan a Terraform resource
337+
tfctl -n default replan my-resource
338+
`
339+
340+
func buildReplanCmd(app *tfctl.CLI) *cobra.Command {
341+
replan := &cobra.Command{
342+
Use: "replan",
343+
Short: "Replan a Terraform resource",
344+
Example: strings.Trim(replanExamples, "\n"),
345+
Args: cobra.ExactArgs(1),
346+
RunE: func(cmd *cobra.Command, args []string) error {
347+
return app.Replan(os.Stdout, args[0])
348+
},
349+
}
350+
return replan
351+
}
352+
353+
func configureDefaultNamespace() {
354+
*kubeconfigArgs.Namespace = defaultNamespace
355+
fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE")
356+
if fromEnv != "" {
357+
kubeconfigArgs.Namespace = &fromEnv
358+
}
359+
}

go.mod

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.19
44

55
replace github.com/weaveworks/tf-controller/api => ./api
66

7+
replace github.com/weaveworks/tf-controller/tfctl => ./tfctl
8+
79
require (
810
github.com/Masterminds/sprig/v3 v3.2.2
911
github.com/aws/aws-sdk-go-v2 v1.16.11
@@ -15,41 +17,36 @@ require (
1517
github.com/elgohr/go-localstack v0.0.0-20220812012220-cd041bfe1b37
1618
github.com/fluxcd/pkg/apis/meta v0.17.0
1719
github.com/fluxcd/pkg/runtime v0.14.1
18-
github.com/fluxcd/pkg/ssa v0.15.2
1920
github.com/fluxcd/pkg/untar v0.1.0
2021
github.com/fluxcd/source-controller/api v0.31.0
2122
github.com/go-logr/logr v1.2.3
2223
github.com/google/uuid v1.3.0
2324
github.com/hashicorp/go-cleanhttp v0.5.2
2425
github.com/hashicorp/go-retryablehttp v0.7.1
25-
github.com/hashicorp/go-version v1.4.0
26-
github.com/hashicorp/hc-install v0.3.1
2726
github.com/hashicorp/terraform-exec v0.16.1
2827
github.com/hashicorp/terraform-json v0.13.0
29-
github.com/olekukonko/tablewriter v0.0.5
30-
github.com/onsi/gomega v1.20.1
28+
github.com/onsi/gomega v1.24.0
3129
github.com/pkg/errors v0.9.1
32-
github.com/spf13/cobra v1.4.0
30+
github.com/spf13/cobra v1.6.1
3331
github.com/spf13/pflag v1.0.5
34-
github.com/spf13/viper v1.11.0
35-
github.com/theckman/yacspin v0.13.12
32+
github.com/spf13/viper v1.13.0
3633
github.com/weaveworks/tf-controller/api v0.0.0-00010101000000-000000000000
34+
github.com/weaveworks/tf-controller/tfctl v0.0.0-00010101000000-000000000000
3735
github.com/zclconf/go-cty v1.10.0
3836
google.golang.org/grpc v1.47.0
3937
google.golang.org/protobuf v1.28.0
40-
k8s.io/api v0.25.2
38+
k8s.io/api v0.25.3
4139
k8s.io/apiextensions-apiserver v0.25.2
4240
k8s.io/apimachinery v0.25.3
4341
k8s.io/cli-runtime v0.25.2
44-
k8s.io/client-go v0.25.2
45-
sigs.k8s.io/cli-utils v0.29.4
42+
k8s.io/client-go v0.25.3
43+
sigs.k8s.io/cli-utils v0.33.0
4644
sigs.k8s.io/controller-runtime v0.13.0
4745
sigs.k8s.io/kustomize/kyaml v0.13.9
48-
sigs.k8s.io/yaml v1.3.0
4946
)
5047

5148
require (
52-
cloud.google.com/go/compute v1.5.0 // indirect
49+
cloud.google.com/go/compute v1.6.1 // indirect
5350
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
5451
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
5552
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
@@ -91,6 +88,7 @@ require (
9188
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
9289
github.com/fatih/color v1.13.0 // indirect
9390
github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect
91+
github.com/fluxcd/pkg/ssa v0.21.0 // indirect
9492
github.com/fsnotify/fsnotify v1.5.4 // indirect
9593
github.com/go-errors/errors v1.0.1 // indirect
9694
github.com/go-logr/zapr v1.2.3 // indirect
@@ -103,16 +101,16 @@ require (
103101
github.com/golang/protobuf v1.5.2 // indirect
104102
github.com/google/btree v1.0.1 // indirect
105103
github.com/google/gnostic v0.5.7-v3refs // indirect
106-
github.com/google/go-cmp v0.5.8 // indirect
104+
github.com/google/go-cmp v0.5.9 // indirect
107105
github.com/google/gofuzz v1.2.0 // indirect
108106
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
109107
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
110-
github.com/hashicorp/errwrap v1.0.0 // indirect
111-
github.com/hashicorp/go-multierror v1.1.1 // indirect
108+
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b // indirect
109+
github.com/hashicorp/go-version v1.6.0 // indirect
112110
github.com/hashicorp/hcl v1.0.0 // indirect
113111
github.com/huandu/xstrings v1.3.1 // indirect
114112
github.com/imdario/mergo v0.3.12 // indirect
115-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
113+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
116114
github.com/jmespath/go-jmespath v0.4.0 // indirect
117115
github.com/josharian/intern v1.0.0 // indirect
118116
github.com/json-iterator/go v1.1.12 // indirect
@@ -126,62 +124,63 @@ require (
126124
github.com/maxbrunsfeld/counterfeiter/v6 v6.5.0 // indirect
127125
github.com/mitchellh/copystructure v1.2.0 // indirect
128126
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
129-
github.com/mitchellh/mapstructure v1.4.3 // indirect
127+
github.com/mitchellh/mapstructure v1.5.0 // indirect
130128
github.com/mitchellh/reflectwalk v1.0.2 // indirect
131129
github.com/moby/spdystream v0.2.0 // indirect
132130
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
133131
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
134132
github.com/modern-go/reflect2 v1.0.2 // indirect
135133
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
136134
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
135+
github.com/olekukonko/tablewriter v0.0.5 // indirect
137136
github.com/opencontainers/go-digest v1.0.0 // indirect
138137
github.com/opencontainers/image-spec v1.0.2 // indirect
139-
github.com/pelletier/go-toml v1.9.4 // indirect
140-
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
138+
github.com/pelletier/go-toml v1.9.5 // indirect
139+
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
141140
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
142141
github.com/prometheus/client_golang v1.12.2 // indirect
143142
github.com/prometheus/client_model v0.2.0 // indirect
144143
github.com/prometheus/common v0.32.1 // indirect
145144
github.com/prometheus/procfs v0.7.3 // indirect
146145
github.com/rivo/uniseg v0.2.0 // indirect
147-
github.com/rogpeppe/go-internal v1.8.1 // indirect
148146
github.com/russross/blackfriday v1.5.2 // indirect
149147
github.com/shopspring/decimal v1.2.0 // indirect
150148
github.com/sirupsen/logrus v1.9.0 // indirect
151149
github.com/spf13/afero v1.8.2 // indirect
152-
github.com/spf13/cast v1.4.1 // indirect
150+
github.com/spf13/cast v1.5.0 // indirect
153151
github.com/spf13/jwalterweatherman v1.1.0 // indirect
154-
github.com/stretchr/objx v0.4.0 // indirect
155-
github.com/subosito/gotenv v1.2.0 // indirect
152+
github.com/subosito/gotenv v1.4.1 // indirect
153+
github.com/theckman/yacspin v0.13.12 // indirect
156154
github.com/xlab/treeprint v1.1.0 // indirect
157155
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
158156
go.uber.org/atomic v1.7.0 // indirect
159157
go.uber.org/multierr v1.6.0 // indirect
160158
go.uber.org/zap v1.21.0 // indirect
161159
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
162160
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
163-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
161+
golang.org/x/net v0.1.0 // indirect
164162
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
165-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
166-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
167-
golang.org/x/text v0.3.7 // indirect
163+
golang.org/x/sys v0.1.0 // indirect
164+
golang.org/x/term v0.1.0 // indirect
165+
golang.org/x/text v0.4.0 // indirect
168166
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
169167
golang.org/x/tools v0.1.12 // indirect
170168
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
171169
google.golang.org/appengine v1.6.7 // indirect
172-
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
170+
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
173171
gopkg.in/inf.v0 v0.9.1 // indirect
174-
gopkg.in/ini.v1 v1.66.4 // indirect
172+
gopkg.in/ini.v1 v1.67.0 // indirect
175173
gopkg.in/yaml.v2 v2.4.0 // indirect
176174
gopkg.in/yaml.v3 v3.0.1 // indirect
177175
k8s.io/component-base v0.25.2 // indirect
178176
k8s.io/klog/v2 v2.70.1 // indirect
179177
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
180-
k8s.io/kubectl v0.23.2 // indirect
178+
k8s.io/kubectl v0.24.0 // indirect
181179
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
182180
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
183181
sigs.k8s.io/kustomize/api v0.12.1 // indirect
184182
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
183+
sigs.k8s.io/yaml v1.3.0 // indirect
185184
)
186185

187186
replace (

0 commit comments

Comments
 (0)