2023-10-24 09:39:18 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/state"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// runTerraformApply checks if changes to Terraform are required and applies them.
|
|
|
|
func (a *applyCmd) runTerraformApply(cmd *cobra.Command, conf *config.Config, stateFile *state.State, upgradeDir string) error {
|
|
|
|
a.log.Debugf("Checking if Terraform migrations are required")
|
2023-10-31 07:46:40 -04:00
|
|
|
terraformClient, removeInstaller, err := a.newInfraApplier(cmd.Context())
|
2023-10-30 07:43:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating Terraform client: %w", err)
|
|
|
|
}
|
2023-10-31 07:46:40 -04:00
|
|
|
defer removeInstaller()
|
2023-10-30 07:43:38 -04:00
|
|
|
|
|
|
|
migrationRequired, err := a.planTerraformMigration(cmd, conf, terraformClient)
|
2023-10-24 09:39:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("planning Terraform migrations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !migrationRequired {
|
|
|
|
a.log.Debugf("No changes to infrastructure required, skipping Terraform migrations")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
a.log.Debugf("Migrating terraform resources for infrastructure changes")
|
2023-10-30 07:43:38 -04:00
|
|
|
postMigrationInfraState, err := a.migrateTerraform(cmd, conf, terraformClient, upgradeDir)
|
2023-10-24 09:39:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("performing Terraform migrations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the pre-upgrade state with the post-migration infrastructure values
|
|
|
|
a.log.Debugf("Updating state file with new infrastructure state")
|
|
|
|
if _, err := stateFile.Merge(
|
|
|
|
// temporary state with post-migration infrastructure values
|
|
|
|
state.New().SetInfrastructure(postMigrationInfraState),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("merging pre-upgrade state with post-migration infrastructure values: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the post-migration state to disk
|
|
|
|
if err := stateFile.WriteToFile(a.fileHandler, constants.StateFilename); err != nil {
|
|
|
|
return fmt.Errorf("writing state file: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// planTerraformMigration checks if the Constellation version the cluster is being upgraded to requires a migration.
|
2023-10-31 07:46:40 -04:00
|
|
|
func (a *applyCmd) planTerraformMigration(cmd *cobra.Command, conf *config.Config, terraformClient cloudApplier) (bool, error) {
|
2023-10-24 09:39:18 -04:00
|
|
|
a.log.Debugf("Planning Terraform migrations")
|
|
|
|
|
|
|
|
// Check if there are any Terraform migrations to apply
|
|
|
|
|
|
|
|
// Add manual migrations here if required
|
|
|
|
//
|
|
|
|
// var manualMigrations []terraform.StateMigration
|
|
|
|
// for _, migration := range manualMigrations {
|
|
|
|
// u.log.Debugf("Adding manual Terraform migration: %s", migration.DisplayName)
|
2023-10-31 07:46:40 -04:00
|
|
|
// u.infraApplier.AddManualStateMigration(migration)
|
2023-10-24 09:39:18 -04:00
|
|
|
// }
|
|
|
|
|
2023-10-30 04:19:35 -04:00
|
|
|
a.spinner.Start("Checking for infrastructure changes", false)
|
|
|
|
defer a.spinner.Stop()
|
2023-10-31 07:46:40 -04:00
|
|
|
return terraformClient.Plan(cmd.Context(), conf)
|
2023-10-24 09:39:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// migrateTerraform migrates an existing Terraform state and the post-migration infrastructure state is returned.
|
2023-10-31 07:46:40 -04:00
|
|
|
func (a *applyCmd) migrateTerraform(cmd *cobra.Command, conf *config.Config, terraformClient cloudApplier, upgradeDir string) (state.Infrastructure, error) {
|
2023-10-24 09:39:18 -04:00
|
|
|
// Ask for confirmation first
|
2023-10-30 04:19:35 -04:00
|
|
|
cmd.Println("The upgrade requires a migration of Constellation cloud resources by applying an updated Terraform template.")
|
2023-10-24 09:39:18 -04:00
|
|
|
if !a.flags.yes {
|
|
|
|
ok, err := askToConfirm(cmd, "Do you want to apply the Terraform migrations?")
|
|
|
|
if err != nil {
|
|
|
|
return state.Infrastructure{}, fmt.Errorf("asking for confirmation: %w", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
cmd.Println("Aborting upgrade.")
|
|
|
|
// User doesn't expect to see any changes in his workspace after aborting an "upgrade apply",
|
|
|
|
// therefore, roll back to the backed up state.
|
2023-10-31 07:46:40 -04:00
|
|
|
if err := terraformClient.RestoreWorkspace(); err != nil {
|
2023-10-24 09:39:18 -04:00
|
|
|
return state.Infrastructure{}, fmt.Errorf(
|
|
|
|
"restoring Terraform workspace: %w, restore the Terraform workspace manually from %s ",
|
|
|
|
err,
|
|
|
|
filepath.Join(upgradeDir, constants.TerraformUpgradeBackupDir),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return state.Infrastructure{}, fmt.Errorf("cluster upgrade aborted by user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.log.Debugf("Applying Terraform migrations")
|
|
|
|
|
|
|
|
a.spinner.Start("Migrating Terraform resources", false)
|
2023-10-31 07:46:40 -04:00
|
|
|
infraState, err := terraformClient.Apply(cmd.Context(), conf.GetProvider(), cloudcmd.WithoutRollbackOnError)
|
2023-10-24 09:39:18 -04:00
|
|
|
a.spinner.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return state.Infrastructure{}, fmt.Errorf("applying terraform migrations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("Infrastructure migrations applied successfully and output written to: %s\n"+
|
|
|
|
"A backup of the pre-upgrade state has been written to: %s\n",
|
|
|
|
a.flags.pathPrefixer.PrefixPrintablePath(constants.StateFilename),
|
|
|
|
a.flags.pathPrefixer.PrefixPrintablePath(filepath.Join(upgradeDir, constants.TerraformUpgradeBackupDir)),
|
|
|
|
)
|
|
|
|
return infraState, nil
|
|
|
|
}
|