Skip to content

Commit

Permalink
Prune unneeded resources post plural up (#523)
Browse files Browse the repository at this point in the history
We use helm_releases and set up a largely unnecessary `apps` stack as part of `plural up`.  We can just eliminate them to save complexity once the management cluster is up, and it will also make it easier to move the management cluster into a sack seamlessly.
  • Loading branch information
michaeljguarino authored Jun 30, 2024
1 parent 70f9395 commit 16cf84c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
2 changes: 0 additions & 2 deletions cmd/plural/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func (p *Plural) handleUp(c *cli.Context) error {
return err
}

defer ctx.Cleanup()

if err := ctx.Backfill(); err != nil {
return err
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/up/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func (ctx *Context) Deploy(commit func() error) error {
}

if err := runAll([]terraformCmd{
{dir: "./clusters", cmd: "init", args: []string{"-upgrade"}},
{dir: "./clusters", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
}); err != nil {
return err
}

stateCmd := &terraformCmd{dir: "./clusters"}
stateCmd := &terraformCmd{dir: "./terraform/mgmt"}
outs, err := stateCmd.outputs()
if err != nil {
return err
Expand All @@ -56,16 +56,20 @@ func (ctx *Context) Deploy(commit func() error) error {

utils.Highlight("\nSetting up gitops management...\n")

return runAll([]terraformCmd{
{dir: "./apps/terraform", cmd: "init", args: []string{"-upgrade"}},
{dir: "./apps/terraform", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
})
if err := runAll([]terraformCmd{
{dir: "./terraform/apps", cmd: "init", args: []string{"-upgrade"}},
{dir: "./terraform/apps", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
}); err != nil {
return err
}

return ctx.Prune()
}

func (ctx *Context) Destroy() error {
return runAll([]terraformCmd{
{dir: "./clusters", cmd: "init", args: []string{"-upgrade"}},
{dir: "./clusters", cmd: "destroy", args: []string{"-auto-approve"}, retries: 2},
{dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}},
{dir: "./terraform/mgmt", cmd: "destroy", args: []string{"-auto-approve"}, retries: 2},
})
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/up/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (ctx *Context) Cleanup() {

func (ctx *Context) Generate() error {
if !utils.Exists("./bootstrap") {
if err := git.BranchedSubmodule("https://github.com/pluralsh/bootstrap.git", "stacks-support"); err != nil {
if err := git.BranchedSubmodule("https://github.com/pluralsh/bootstrap.git", "refactored-up"); err != nil {
return err
}
}
Expand All @@ -31,11 +31,11 @@ func (ctx *Context) Generate() error {
{from: "./bootstrap/charts/runtime/values.yaml.tpl", to: "./helm-values/runtime.yaml", overwrite: true},
{from: "./bootstrap/helm/certmanager.yaml", to: "./helm-values/certmanager.yaml", overwrite: true},
{from: "./bootstrap/helm/flux.yaml", to: "./helm-values/flux.yaml", overwrite: true},
{from: fmt.Sprintf("./bootstrap/templates/providers/bootstrap/%s.tf", prov), to: "clusters/provider.tf"},
{from: fmt.Sprintf("./bootstrap/templates/setup/providers/%s.tf", prov), to: "clusters/mgmt.tf"},
{from: "./bootstrap/templates/setup/console.tf", to: "clusters/console.tf"},
{from: fmt.Sprintf("./bootstrap/templates/providers/apps/%s.tf", prov), to: "apps/terraform/provider.tf"},
{from: "./bootstrap/templates/setup/cd.tf", to: "apps/terraform/cd.tf"},
{from: fmt.Sprintf("./bootstrap/templates/providers/bootstrap/%s.tf", prov), to: "terraform/mgmt/provider.tf"},
{from: fmt.Sprintf("./bootstrap/templates/setup/providers/%s.tf", prov), to: "terraform/mgmt/mgmt.tf"},
{from: "./bootstrap/templates/setup/console.tf", to: "terraform/mgmt/console.tf"},
{from: fmt.Sprintf("./bootstrap/templates/providers/apps/%s.tf", prov), to: "terraform/apps/provider.tf"},
{from: "./bootstrap/templates/setup/cd.tf", to: "terraform/apps/cd.tf"},
{from: "./bootstrap/README.md", to: "README.md", overwrite: true},
}

Expand All @@ -52,7 +52,7 @@ func (ctx *Context) Generate() error {

copies := []templatePair{
{from: "./bootstrap/terraform/modules/clusters", to: "terraform/modules/clusters"},
{from: fmt.Sprintf("./bootstrap/terraform/clouds/%s", prov), to: "terraform/modules/mgmt"},
{from: fmt.Sprintf("./bootstrap/terraform/clouds/%s", prov), to: "terraform/mgmt/cluster"},
{from: "./bootstrap/apps/repositories", to: "apps/repositories"},
{from: "./bootstrap/apps/services", to: "apps/services"},
{from: "./bootstrap/templates", to: "templates"},
Expand Down
48 changes: 48 additions & 0 deletions pkg/up/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package up

import (
"os"
"os/exec"

"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/pluralsh/plural-cli/pkg/utils/git"
)

func (ctx *Context) Prune() error {
utils.Highlight("\nCleaning up unneeded resources...\n\n")
repoRoot, err := git.Root()
if err != nil {
return err
}

toRemove := []string{
"null_resource.console",
"helm_release.certmanager",
"helm_release.flux",
"helm_release.runtime",
"helm_release.console",
}

for _, field := range toRemove {
if err := stateRm("./terraform/mgmt", field); err != nil {
return err
}
}

if err := os.Remove("./terraform/mgmt/console.tf"); err != nil {
return err
}

_ = os.RemoveAll("./terraform/apps")
ctx.Cleanup()

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

func stateRm(dir, field string) error {
cmd := exec.Command("terraform", "state", "rm", field)
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

0 comments on commit 16cf84c

Please sign in to comment.