Skip to content

Commit 04085a1

Browse files
Add checkpointing to plural up (#659)
This will allow for some hard-to-make-idempotent operations dedupe along the setup flow
1 parent 842ea58 commit 04085a1

File tree

3 files changed

+86
-28
lines changed

3 files changed

+86
-28
lines changed

pkg/manifest/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ProjectManifest struct {
5757
Region string
5858
Owner *Owner
5959
Network *NetworkConfig
60+
Checkpoint string `yaml:"checkpoint,omitempty"`
6061
AvailabilityZones []string
6162
BucketPrefix string `yaml:"bucketPrefix"`
6263
Context map[string]interface{}

pkg/up/deploy.go

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,52 @@ type terraformCmd struct {
2323
retries int
2424
}
2525

26+
var (
27+
checkpoints = []string{
28+
"init",
29+
"import",
30+
"apply:import",
31+
"commit",
32+
"apps",
33+
"prune:cloud",
34+
"prune:mgmt",
35+
}
36+
37+
priorities = map[string]int{}
38+
)
39+
40+
func init() {
41+
for i, c := range checkpoints {
42+
priorities[c] = i
43+
}
44+
}
45+
46+
func (ctx *Context) runCheckpoint(current, checkpoint string, fn func() error) error {
47+
if current == "" || priorities[checkpoint] > priorities[current] {
48+
err := fn()
49+
if err == nil {
50+
ctx.Manifest.Checkpoint = checkpoint
51+
}
52+
return err
53+
}
54+
55+
utils.Highlight("Skipping checkpoint %s, ran up to %s previously\n", checkpoint, current)
56+
57+
return nil
58+
}
59+
2660
func (ctx *Context) Deploy(commit func() error) error {
2761
if err := ctx.Provider.CreateBucket(); err != nil {
2862
return err
2963
}
3064

31-
if err := runAll([]terraformCmd{
32-
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
33-
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
65+
defer ctx.Manifest.Flush()
66+
67+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "init", func() error {
68+
return runAll([]terraformCmd{
69+
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
70+
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
71+
})
3472
}); err != nil {
3573
return err
3674
}
@@ -41,10 +79,19 @@ func (ctx *Context) Deploy(commit func() error) error {
4179
return err
4280
}
4381

44-
if err := runAll([]terraformCmd{
45-
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
46-
{dir: "./terraform/mgmt", cmd: "import", args: []string{"plural_cluster.mgmt", *ctx.ImportCluster}},
47-
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
82+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "import", func() error {
83+
return runAll([]terraformCmd{
84+
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
85+
{dir: "./terraform/mgmt", cmd: "import", args: []string{"plural_cluster.mgmt", *ctx.ImportCluster}},
86+
})
87+
}); err != nil {
88+
return err
89+
}
90+
91+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "apply:import", func() error {
92+
return runAll([]terraformCmd{
93+
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
94+
})
4895
}); err != nil {
4996
return err
5097
}
@@ -73,16 +120,19 @@ func (ctx *Context) Deploy(commit func() error) error {
73120
}
74121
}
75122

76-
if err := commit(); err != nil {
123+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "commit", func() error {
124+
utils.Highlight("\nSetting up gitops management, first lets commit the changes made up to this point...\n\n")
125+
return commit()
126+
}); err != nil {
77127
return err
78128
}
79129

80-
utils.Highlight("\nSetting up gitops management...\n")
81-
82-
if err := runAll([]terraformCmd{
83-
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}},
84-
{dir: "./terraform/apps", cmd: "init", args: []string{"-upgrade"}},
85-
{dir: "./terraform/apps", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
130+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "apps", func() error {
131+
return runAll([]terraformCmd{
132+
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}},
133+
{dir: "./terraform/apps", cmd: "init", args: []string{"-upgrade"}},
134+
{dir: "./terraform/apps", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
135+
})
86136
}); err != nil {
87137
return err
88138
}

pkg/up/prune.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ import (
1010

1111
func (ctx *Context) Prune() error {
1212
if ctx.Cloud {
13-
return ctx.pruneCloud()
13+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "prune:cloud", func() error {
14+
return ctx.pruneCloud()
15+
}); err != nil {
16+
return err
17+
}
1418
}
1519

16-
utils.Highlight("\nCleaning up unneeded resources...\n\n")
1720
repoRoot, err := git.Root()
1821
if err != nil {
1922
return err
2023
}
2124

22-
toRemove := []string{
23-
"null_resource.console",
24-
"helm_release.certmanager",
25-
"helm_release.flux",
26-
"helm_release.runtime",
27-
"helm_release.console",
28-
}
25+
if err := ctx.runCheckpoint(ctx.Manifest.Checkpoint, "prune:mgmt", func() error {
26+
utils.Highlight("\nCleaning up unneeded resources...\n\n")
2927

30-
for _, field := range toRemove {
31-
if err := stateRm("./terraform/mgmt", field); err != nil {
32-
return err
28+
toRemove := []string{
29+
"null_resource.console",
30+
"helm_release.certmanager",
31+
"helm_release.flux",
32+
"helm_release.runtime",
33+
"helm_release.console",
3334
}
34-
}
3535

36-
if err := os.Remove("./terraform/mgmt/console.tf"); err != nil {
36+
for _, field := range toRemove {
37+
if err := stateRm("./terraform/mgmt", field); err != nil {
38+
return err
39+
}
40+
}
41+
return nil
42+
}); err != nil {
3743
return err
3844
}
3945

46+
_ = os.Remove("./terraform/mgmt/console.tf")
4047
_ = os.RemoveAll("./terraform/apps")
4148

4249
return git.Sync(repoRoot, "Post-setup resource cleanup", true)

0 commit comments

Comments
 (0)