Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/cluster-api-simple-test' into cl…
Browse files Browse the repository at this point in the history
…uster-api-simple-test
  • Loading branch information
maciaszczykm committed Sep 14, 2023
2 parents 728f6e1 + b31119a commit 7746051
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 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
42 changes: 34 additions & 8 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ import (
"path/filepath"

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

"github.com/pluralsh/plural/pkg/api"
"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)
clusterName := "bootstrap"
p := cluster.NewProvider()
n, _ := p.ListNodes(clusterName)
return len(n) > 0
}

// getBootstrapSteps returns list of steps to run during cluster bootstrap.
func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step, error) {
man, err := manifest.FetchProject()
Expand All @@ -37,7 +45,25 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
return nil, err
}

clusterBackup := backup.NewCAPIBackup(man.Cluster)

return []*Step{
{
Name: "Destroy provider cluster",
Execute: func(_ []string) error {
return nil
},
Confirm: "Existing cluster found. Would you like to try and destroy the provider cluster?", // TODO: improve message
Skip: true, // TODO add check if bootstrap cluster exists and contains cluster CRD in non-deleting state

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

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)
},
{
Name: "Destroy local bootstrap cluster",
Execute: func(_ []string) error {
return nil
},
Confirm: "Existing bootstrap cluster found. Would you like to destroy it first?", // TODO: improve message
Skip: true, // TODO add check if bootstrap cluster exists and cluster CRD is in deleting state
},
{
Name: "Create local bootstrap cluster",
Args: []string{"plural", "bootstrap", "cluster", "create", "bootstrap", "--skip-if-exists"},
Expand All @@ -57,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 @@ -69,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 @@ -113,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 @@ -163,11 +189,11 @@ func getBootstrapSteps(runPlural ActionFunc, additionalFlags []string) ([]*Step,
Retries: 2,
},
{
Name: "Destroy local cluster",
Name: "Destroy local bootstrap cluster",
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
9 changes: 9 additions & 0 deletions pkg/bootstrap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"golang.org/x/oauth2/google"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -202,6 +203,14 @@ func ExecuteSteps(steps []*Step) error {

filteredSteps := FilterSteps(steps)
for i, step := range filteredSteps {
if len(step.Confirm) > 0 {
res := true
prompt := &survey.Confirm{Message: step.Confirm}
if err := survey.AskOne(prompt, &res, survey.WithValidator(survey.Required)); err != nil || !res {
continue
}
}

utils.Highlight("[%d/%d] %s\n", i+1, len(filteredSteps), step.Name)

if step.SkipFunc != nil && step.SkipFunc() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/bootstrap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type Step struct {
Retries int
OnError HookFunc
OnAfter HookFunc
Confirm string
}

// Bootstrap is a representation of existing cluster to be migrated to Cluster API.
// This data is fetched from provider with migrator tool.
// See github.com/pluralsh/cluster-api-migration for more details.
type Bootstrap struct {
ClusterAPICluster *api.Values `json:"cluster-api-cluster"`
}
}
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 7746051

Please sign in to comment.