Skip to content

Commit

Permalink
Set up core-infra stack (#585)
Browse files Browse the repository at this point in the history
* Set up core-infra stack

This will add a service context for the management cluster, but could also provision other infra like dns, etc

* better cloud up repo cleanup
  • Loading branch information
michaeljguarino authored Dec 30, 2024
1 parent c1f4442 commit fa9c3fd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/client/plural.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Plural) HandleInit(c *cli.Context) error {
if err != nil {
return err
}
if proj.Network.PluralDns {
if proj.Network != nil && proj.Network.PluralDns {
if err := p.Client.CreateDomain(proj.Network.Subdomain); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/up/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (ctx *Context) Deploy(commit func() error) error {
utils.Highlight("\nSetting up gitops management...\n")

if err := runAll([]terraformCmd{
{dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}},
{dir: "./terraform/apps", cmd: "init", args: []string{"-upgrade"}},
{dir: "./terraform/apps", cmd: "apply", args: []string{"-auto-approve"}, retries: 1},
}); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions pkg/up/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (ctx *Context) Generate() (dir string, err error) {
copies := []templatePair{
{from: ctx.path("terraform/modules/clusters"), to: "terraform/modules/clusters"},
{from: ctx.path(fmt.Sprintf("terraform/clouds/%s", prov)), to: "terraform/mgmt/cluster"},
{from: ctx.path(fmt.Sprintf("terraform/core-infra/%s", prov)), to: "terraform/core-infra"},
{from: ctx.path("setup"), to: "bootstrap"},
{from: ctx.path("templates"), to: "templates"},
{from: ctx.path("resources"), to: "resources"},
Expand Down Expand Up @@ -136,6 +137,28 @@ func (ctx *Context) afterSetup() error {
}
}

redacts := []templatePair{
{from: "./terraform/mgmt/provider.tf"},
}

for _, redact := range redacts {
if err := ctx.redact(redact.from); err != nil {
return err
}
}

uncomments := []templatePair{
{from: "./terraform/mgmt/cluster/eks.tf"},
}

for _, uncomment := range uncomments {
if utils.Exists(uncomment.from) {
if err := ctx.uncomment(uncomment.from); err != nil {
return err
}
}
}

return nil
}

Expand Down
14 changes: 13 additions & 1 deletion pkg/up/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (ctx *Context) Prune() error {
if ctx.Cloud {
return nil
return ctx.pruneCloud()
}

utils.Highlight("\nCleaning up unneeded resources...\n\n")
Expand Down Expand Up @@ -42,6 +42,18 @@ func (ctx *Context) Prune() error {
return git.Sync(repoRoot, "Post-setup resource cleanup", true)
}

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

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

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
Expand Down
19 changes: 19 additions & 0 deletions pkg/up/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package up

import (
"bytes"
"regexp"
"text/template"
"time"

Expand All @@ -10,6 +11,24 @@ import (
"github.com/pluralsh/polly/retry"
)

func (ctx *Context) redact(file string) error {
buf, err := utils.ReadFile(file)
if err != nil {
return err
}
re := regexp.MustCompile(`(?msU)# BEGIN REMOVE(.*)# END REMOVE`)
return utils.WriteFile(file, []byte(re.ReplaceAllString(buf, "")))
}

func (ctx *Context) uncomment(file string) error {
buf, err := utils.ReadFile(file)
if err != nil {
return err
}
re := regexp.MustCompile(`# UNCOMMENT\s+`)
return utils.WriteFile(file, []byte(re.ReplaceAllString(buf, "")))
}

func (ctx *Context) templateFrom(file, to string) error {
buf, err := utils.ReadFile(file)
if err != nil {
Expand Down

0 comments on commit fa9c3fd

Please sign in to comment.