Skip to content

Commit

Permalink
Add semver validation for required bootstrap tf/helm modules on migra…
Browse files Browse the repository at this point in the history
…tion (#445)

There are now some requirements for performing a migration tied to our helm/tf.  This will at least guarantee they're installed at migrate time.
  • Loading branch information
michaeljguarino authored Sep 13, 2023
1 parent 8958bdf commit 68e6844
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/plural/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pluralsh/plural/pkg/api"
"github.com/pluralsh/plural/pkg/bootstrap"
"github.com/pluralsh/plural/pkg/bootstrap/aws"
"github.com/pluralsh/plural/pkg/bootstrap/validation"
"github.com/pluralsh/plural/pkg/cluster"
"github.com/pluralsh/plural/pkg/config"
"github.com/pluralsh/plural/pkg/exp"
Expand Down Expand Up @@ -84,7 +85,7 @@ func (p *Plural) clusterCommands() []cli.Command {
{
Name: "migrate",
Usage: "migrate to Cluster API",
Action: latestVersion(rooted(initKubeconfig(handleMigration))),
Action: latestVersion(rooted(initKubeconfig(p.handleMigration))),
Category: "Publishing",
Hidden: !exp.IsFeatureEnabled(exp.EXP_PLURAL_CAPI),
},
Expand All @@ -96,7 +97,12 @@ func (p *Plural) clusterCommands() []cli.Command {
}
}

func handleMigration(_ *cli.Context) error {
func (p *Plural) handleMigration(_ *cli.Context) error {
p.InitPluralClient()
if err := validation.ValidateMigration(p); err != nil {
return err
}

project, err := manifest.FetchProject()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/Azure/azure-storage-blob-go v0.15.0
github.com/Azure/azure-workload-identity v1.1.0
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/Yamashou/gqlgenc v0.14.0
github.com/aws/aws-sdk-go-v2 v1.18.0
Expand Down
69 changes: 69 additions & 0 deletions pkg/bootstrap/validation/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package validation

import (
"fmt"

"github.com/Masterminds/semver"
"github.com/pluralsh/plural/pkg/api"
)

var (
tfRequirements = map[string]string{
"aws-bootstrap": ">= 0.1.53",
"gcp-bootstrap": ">= 0.2.24",
"azure-bootstrap": ">= 0.2.0",
}
helmRequirements = map[string]string{
"bootstrap": ">= 0.8.72",
}
)

func ValidateMigration(client api.Client) error {
repo, err := client.GetRepository("bootstrap")
if err != nil {
return err
}

charts, tfs, err := client.GetPackageInstallations(repo.Id)
if err != nil {
return err
}
chartsByName, tfsByName := map[string]*api.ChartInstallation{}, map[string]*api.TerraformInstallation{}
for _, chart := range charts {
chartsByName[chart.Chart.Name] = chart
}
for _, tf := range tfs {
tfsByName[tf.Terraform.Name] = tf
}

for name, req := range tfRequirements {
if tf, ok := tfsByName[name]; ok {
if !testSemver(req, tf.Version.Version) {
return fmt.Errorf("You must have installed the %s terraform module at least at version %s to run cluster migration, your version is %s", name, req, tf.Version.Version)
}
}
}

for name, req := range helmRequirements {
if chart, ok := chartsByName[name]; ok {
if !testSemver(req, chart.Version.Version) {
return fmt.Errorf("You must have installed the %s helm chart at least at version %s to run cluster migration, your version is %s", name, req, chart.Version.Version)
}
}
}

return nil
}

func testSemver(constraint, vsn string) bool {
c, err := semver.NewConstraint(constraint)
if err != nil {
return false
}
v, err := semver.NewVersion(vsn)
if err != nil {
return false
}

return c.Check(v)
}

0 comments on commit 68e6844

Please sign in to comment.