mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-12 16:09:39 -05:00
cli: only create Terraform client when needed (#2536)
Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
238a3c222b
commit
5f05810ad7
@ -220,28 +220,28 @@ func runApply(cmd *cobra.Command, _ []string) error {
|
|||||||
|
|
||||||
upgradeID := generateUpgradeID(upgradeCmdKindApply)
|
upgradeID := generateUpgradeID(upgradeCmdKindApply)
|
||||||
upgradeDir := filepath.Join(constants.UpgradeDir, upgradeID)
|
upgradeDir := filepath.Join(constants.UpgradeDir, upgradeID)
|
||||||
clusterUpgrader, err := cloudcmd.NewClusterUpgrader(
|
|
||||||
cmd.Context(),
|
newClusterApplier := func(ctx context.Context) (clusterUpgrader, error) {
|
||||||
constants.TerraformWorkingDir,
|
return cloudcmd.NewClusterUpgrader(
|
||||||
upgradeDir,
|
ctx,
|
||||||
flags.tfLogLevel,
|
constants.TerraformWorkingDir,
|
||||||
fileHandler,
|
upgradeDir,
|
||||||
)
|
flags.tfLogLevel,
|
||||||
if err != nil {
|
fileHandler,
|
||||||
return fmt.Errorf("setting up cluster upgrader: %w", err)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
apply := &applyCmd{
|
apply := &applyCmd{
|
||||||
fileHandler: fileHandler,
|
fileHandler: fileHandler,
|
||||||
flags: flags,
|
flags: flags,
|
||||||
log: log,
|
log: log,
|
||||||
spinner: spinner,
|
spinner: spinner,
|
||||||
merger: &kubeconfigMerger{log: log},
|
merger: &kubeconfigMerger{log: log},
|
||||||
quotaChecker: license.NewClient(),
|
quotaChecker: license.NewClient(),
|
||||||
newHelmClient: newHelmClient,
|
newHelmClient: newHelmClient,
|
||||||
newDialer: newDialer,
|
newDialer: newDialer,
|
||||||
newKubeUpgrader: newKubeUpgrader,
|
newKubeUpgrader: newKubeUpgrader,
|
||||||
clusterUpgrader: clusterUpgrader,
|
newClusterApplier: newClusterApplier,
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(cmd.Context(), time.Hour)
|
ctx, cancel := context.WithTimeout(cmd.Context(), time.Hour)
|
||||||
@ -261,10 +261,10 @@ type applyCmd struct {
|
|||||||
merger configMerger
|
merger configMerger
|
||||||
quotaChecker license.QuotaChecker
|
quotaChecker license.QuotaChecker
|
||||||
|
|
||||||
newHelmClient func(kubeConfigPath string, log debugLog) (helmApplier, error)
|
newHelmClient func(kubeConfigPath string, log debugLog) (helmApplier, error)
|
||||||
newDialer func(validator atls.Validator) *dialer.Dialer
|
newDialer func(validator atls.Validator) *dialer.Dialer
|
||||||
newKubeUpgrader func(io.Writer, string, debugLog) (kubernetesUpgrader, error)
|
newKubeUpgrader func(out io.Writer, kubeConfigPath string, log debugLog) (kubernetesUpgrader, error)
|
||||||
clusterUpgrader clusterUpgrader
|
newClusterApplier func(context.Context) (clusterUpgrader, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -20,7 +20,12 @@ import (
|
|||||||
// runTerraformApply checks if changes to Terraform are required and applies them.
|
// 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 {
|
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")
|
a.log.Debugf("Checking if Terraform migrations are required")
|
||||||
migrationRequired, err := a.planTerraformMigration(cmd, conf)
|
terraformClient, err := a.newClusterApplier(cmd.Context())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("creating Terraform client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
migrationRequired, err := a.planTerraformMigration(cmd, conf, terraformClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("planning Terraform migrations: %w", err)
|
return fmt.Errorf("planning Terraform migrations: %w", err)
|
||||||
}
|
}
|
||||||
@ -31,7 +36,7 @@ func (a *applyCmd) runTerraformApply(cmd *cobra.Command, conf *config.Config, st
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.log.Debugf("Migrating terraform resources for infrastructure changes")
|
a.log.Debugf("Migrating terraform resources for infrastructure changes")
|
||||||
postMigrationInfraState, err := a.migrateTerraform(cmd, conf, upgradeDir)
|
postMigrationInfraState, err := a.migrateTerraform(cmd, conf, terraformClient, upgradeDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("performing Terraform migrations: %w", err)
|
return fmt.Errorf("performing Terraform migrations: %w", err)
|
||||||
}
|
}
|
||||||
@ -53,7 +58,7 @@ func (a *applyCmd) runTerraformApply(cmd *cobra.Command, conf *config.Config, st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// planTerraformMigration checks if the Constellation version the cluster is being upgraded to requires a migration.
|
// planTerraformMigration checks if the Constellation version the cluster is being upgraded to requires a migration.
|
||||||
func (a *applyCmd) planTerraformMigration(cmd *cobra.Command, conf *config.Config) (bool, error) {
|
func (a *applyCmd) planTerraformMigration(cmd *cobra.Command, conf *config.Config, terraformClient clusterUpgrader) (bool, error) {
|
||||||
a.log.Debugf("Planning Terraform migrations")
|
a.log.Debugf("Planning Terraform migrations")
|
||||||
vars, err := cloudcmd.TerraformUpgradeVars(conf)
|
vars, err := cloudcmd.TerraformUpgradeVars(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -73,11 +78,11 @@ func (a *applyCmd) planTerraformMigration(cmd *cobra.Command, conf *config.Confi
|
|||||||
|
|
||||||
a.spinner.Start("Checking for infrastructure changes", false)
|
a.spinner.Start("Checking for infrastructure changes", false)
|
||||||
defer a.spinner.Stop()
|
defer a.spinner.Stop()
|
||||||
return a.clusterUpgrader.PlanClusterUpgrade(cmd.Context(), a.spinner, vars, conf.GetProvider())
|
return terraformClient.PlanClusterUpgrade(cmd.Context(), a.spinner, vars, conf.GetProvider())
|
||||||
}
|
}
|
||||||
|
|
||||||
// migrateTerraform migrates an existing Terraform state and the post-migration infrastructure state is returned.
|
// migrateTerraform migrates an existing Terraform state and the post-migration infrastructure state is returned.
|
||||||
func (a *applyCmd) migrateTerraform(cmd *cobra.Command, conf *config.Config, upgradeDir string) (state.Infrastructure, error) {
|
func (a *applyCmd) migrateTerraform(cmd *cobra.Command, conf *config.Config, terraformClient clusterUpgrader, upgradeDir string) (state.Infrastructure, error) {
|
||||||
// Ask for confirmation first
|
// Ask for confirmation first
|
||||||
cmd.Println("The upgrade requires a migration of Constellation cloud resources by applying an updated Terraform template.")
|
cmd.Println("The upgrade requires a migration of Constellation cloud resources by applying an updated Terraform template.")
|
||||||
if !a.flags.yes {
|
if !a.flags.yes {
|
||||||
@ -89,7 +94,7 @@ func (a *applyCmd) migrateTerraform(cmd *cobra.Command, conf *config.Config, upg
|
|||||||
cmd.Println("Aborting upgrade.")
|
cmd.Println("Aborting upgrade.")
|
||||||
// User doesn't expect to see any changes in his workspace after aborting an "upgrade apply",
|
// User doesn't expect to see any changes in his workspace after aborting an "upgrade apply",
|
||||||
// therefore, roll back to the backed up state.
|
// therefore, roll back to the backed up state.
|
||||||
if err := a.clusterUpgrader.RestoreClusterWorkspace(); err != nil {
|
if err := terraformClient.RestoreClusterWorkspace(); err != nil {
|
||||||
return state.Infrastructure{}, fmt.Errorf(
|
return state.Infrastructure{}, fmt.Errorf(
|
||||||
"restoring Terraform workspace: %w, restore the Terraform workspace manually from %s ",
|
"restoring Terraform workspace: %w, restore the Terraform workspace manually from %s ",
|
||||||
err,
|
err,
|
||||||
@ -102,7 +107,7 @@ func (a *applyCmd) migrateTerraform(cmd *cobra.Command, conf *config.Config, upg
|
|||||||
a.log.Debugf("Applying Terraform migrations")
|
a.log.Debugf("Applying Terraform migrations")
|
||||||
|
|
||||||
a.spinner.Start("Migrating Terraform resources", false)
|
a.spinner.Start("Migrating Terraform resources", false)
|
||||||
infraState, err := a.clusterUpgrader.ApplyClusterUpgrade(cmd.Context(), conf.GetProvider())
|
infraState, err := terraformClient.ApplyClusterUpgrade(cmd.Context(), conf.GetProvider())
|
||||||
a.spinner.Stop()
|
a.spinner.Stop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return state.Infrastructure{}, fmt.Errorf("applying terraform migrations: %w", err)
|
return state.Infrastructure{}, fmt.Errorf("applying terraform migrations: %w", err)
|
||||||
|
@ -277,7 +277,7 @@ func TestInitialize(t *testing.T) {
|
|||||||
getClusterAttestationConfigErr: k8serrors.NewNotFound(schema.GroupResource{}, ""),
|
getClusterAttestationConfigErr: k8serrors.NewNotFound(schema.GroupResource{}, ""),
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
clusterUpgrader: stubTerraformUpgrader{},
|
newClusterApplier: func(ctx context.Context) (clusterUpgrader, error) { return stubTerraformUpgrader{}, nil },
|
||||||
}
|
}
|
||||||
|
|
||||||
err := i.apply(cmd, stubAttestationFetcher{}, "test")
|
err := i.apply(cmd, stubAttestationFetcher{}, "test")
|
||||||
|
@ -265,7 +265,7 @@ func TestUpgradeApply(t *testing.T) {
|
|||||||
newKubeUpgrader: func(_ io.Writer, _ string, _ debugLog) (kubernetesUpgrader, error) {
|
newKubeUpgrader: func(_ io.Writer, _ string, _ debugLog) (kubernetesUpgrader, error) {
|
||||||
return tc.kubeUpgrader, nil
|
return tc.kubeUpgrader, nil
|
||||||
},
|
},
|
||||||
clusterUpgrader: tc.terraformUpgrader,
|
newClusterApplier: func(ctx context.Context) (clusterUpgrader, error) { return tc.terraformUpgrader, nil },
|
||||||
}
|
}
|
||||||
err := upgrader.apply(cmd, stubAttestationFetcher{}, "test")
|
err := upgrader.apply(cmd, stubAttestationFetcher{}, "test")
|
||||||
if tc.wantErr {
|
if tc.wantErr {
|
||||||
|
Loading…
Reference in New Issue
Block a user