Skip to content

Commit

Permalink
move capi backup to .plural dir and add multi-cluster backup support
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks committed Sep 14, 2023
1 parent 3ca2833 commit b31119a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
1 change: 0 additions & 1 deletion cmd/plural/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const Gitignore = `/**/.terraform
*.swo
.DS_STORE
.vscode
/bootstrap/capi/
`

// IMPORTANT
Expand Down
16 changes: 9 additions & 7 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pluralsh/plural/pkg/manifest"
"github.com/pluralsh/plural/pkg/provider"
"github.com/pluralsh/plural/pkg/utils"
"github.com/pluralsh/plural/pkg/utils/capi"
"github.com/pluralsh/plural/pkg/utils/backup"
)

func bootstrapClusterExists() bool {

Check failure on line 17 in pkg/bootstrap/bootstrap.go

View workflow job for this annotation

GitHub Actions / Lint

func `bootstrapClusterExists` is unused (unused)
Expand Down Expand Up @@ -45,6 +45,8 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
return nil, err
}

clusterBackup := backup.NewCAPIBackup(man.Cluster)

return []*Step{
{
Name: "Destroy provider cluster",
Expand Down Expand Up @@ -81,7 +83,7 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
Name: "Deploy cluster",
Args: append([]string{"plural", "--bootstrap", "wkspace", "helm", "bootstrap"}, flags...),
Execute: runPlural,
Skip: capi.MoveBackupExists(),
Skip: clusterBackup.Exists(),
},
{
Name: "Restore cluster",
Expand All @@ -93,9 +95,9 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
},
}

return capi.RestoreMoveBackup(options)
return clusterBackup.Restore(options)
},
Skip: !capi.MoveBackupExists(),
Skip: !clusterBackup.Exists(),
},
{
Name: "Wait for cluster",
Expand Down Expand Up @@ -137,9 +139,9 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
},
}

err := capi.SaveMoveBackup(options)
err := clusterBackup.Save(options)
if err != nil {
_ = capi.RemoveStateBackup()
_ = clusterBackup.Remove()
utils.Error("error during saving state backup: %s", err)
}
},
Expand Down Expand Up @@ -191,7 +193,7 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
Args: []string{"plural", "--bootstrap", "bootstrap", "cluster", "delete", "bootstrap"},
Execute: runPlural,
OnAfter: func() {
err := capi.RemoveStateBackup()
err := clusterBackup.Remove()
if err != nil {
utils.Error("error during removing state backup: %s", err)
}
Expand Down
62 changes: 31 additions & 31 deletions pkg/utils/capi/move.go → pkg/utils/backup/capi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package capi
package backup

import (
"fmt"
Expand All @@ -7,75 +7,75 @@ import (

apiclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client"

"github.com/pluralsh/plural/pkg/utils/git"
"github.com/pluralsh/plural/pkg/utils/pathing"
"github.com/pluralsh/plural/pkg/config"
)

var (
clusterStateDir = ""
)
type CAPIBackup struct {
dirPath string
}

func init() {
gitRootDir, err := git.Root()
if err != nil {
panic(err)
func (this CAPIBackup) createDir() {
if this.Exists() {
return
}

clusterStateDir = pathing.SanitizeFilepath(filepath.Join(gitRootDir, "bootstrap", "capi"))
_ = os.MkdirAll(this.dirPath, os.ModePerm)
}

func MoveBackupExists() bool {
_, err := os.Stat(clusterStateDir)
func (this CAPIBackup) Exists() bool {
_, err := os.Stat(this.dirPath)
return !os.IsNotExist(err)
}

func SaveMoveBackup(options apiclient.MoveOptions) error {
func (this CAPIBackup) Save(options apiclient.MoveOptions) error {
client, err := apiclient.New("")
if err != nil {
return err
}

if !MoveBackupExists() {
err = os.Mkdir(clusterStateDir, os.ModePerm)
if err != nil {
return err
}
}

this.createDir()
if len(options.FromKubeconfig.Context) == 0 || len(options.FromKubeconfig.Path) == 0 {
return fmt.Errorf("both FromKubeconfig context and path have to be configured\n")
}

options.ToDirectory = clusterStateDir
options.ToDirectory = this.dirPath
options.Namespace = "bootstrap"

return client.Move(options)
}

func RestoreMoveBackup(options apiclient.MoveOptions) error {
func (this CAPIBackup) Restore(options apiclient.MoveOptions) error {
client, err := apiclient.New("")
if err != nil {
return err
}

if !MoveBackupExists() {
return fmt.Errorf("could not find move backup to restore from")
}

if len(options.ToKubeconfig.Context) == 0 || len(options.ToKubeconfig.Path) == 0 {
return fmt.Errorf("both ToKubeconfig context and path have to be configured\n")
}

options.FromDirectory = clusterStateDir
if !this.Exists() {
return fmt.Errorf("could not find move backup to restore from")
}

options.FromDirectory = this.dirPath
options.Namespace = "bootstrap"

return client.Move(options)
}

func RemoveStateBackup() error {
if !MoveBackupExists() {
func (this CAPIBackup) Remove() error {
if !this.Exists() {
return nil
}

return os.Remove(clusterStateDir)
return os.RemoveAll(this.dirPath)
}

func NewCAPIBackup(cluster string) Backup[apiclient.MoveOptions] {
path, _ := config.PluralDir()

return CAPIBackup{
dirPath: filepath.Join(path, backupsDir, cluster),
}
}
14 changes: 14 additions & 0 deletions pkg/utils/backup/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package backup

type BackupOptions any

const (
backupsDir = "backups"
)

type Backup[T BackupOptions] interface {
Exists() bool
Save(opts T) error
Restore(opts T) error
Remove() error
}

Check failure on line 14 in pkg/utils/backup/types.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)

0 comments on commit b31119a

Please sign in to comment.