mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-06 14:04:17 -04:00
fixup! update
This commit is contained in:
parent
ad56360d5f
commit
58a7df489a
4 changed files with 120 additions and 40 deletions
|
@ -183,12 +183,7 @@ func (u *Upgrader) CleanUpTerraformMigrations(fileHandler file.Handler) error {
|
||||||
// If a diff exists, it's being written to the upgrader's output writer. It also returns
|
// If a diff exists, it's being written to the upgrader's output writer. It also returns
|
||||||
// a bool indicating whether a diff exists.
|
// a bool indicating whether a diff exists.
|
||||||
func (u *Upgrader) PlanTerraformMigrations(ctx context.Context, opts upgrade.TerraformUpgradeOptions) (bool, error) {
|
func (u *Upgrader) PlanTerraformMigrations(ctx context.Context, opts upgrade.TerraformUpgradeOptions) (bool, error) {
|
||||||
hasDiff, err := u.tfUpgrader.PlanIAMMigration(ctx, opts.CSP, opts.LogLevel, u.upgradeID)
|
return u.tfUpgrader.PlanTerraformMigrations(ctx, opts, u.upgradeID)
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("planning terraform migrations: %w", err)
|
|
||||||
}
|
|
||||||
return hasDiff, nil
|
|
||||||
// return u.tfUpgrader.PlanTerraformMigrations(ctx, opts, u.upgradeID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyTerraformMigrations applies the migrations planned by PlanTerraformMigrations.
|
// ApplyTerraformMigrations applies the migrations planned by PlanTerraformMigrations.
|
||||||
|
|
111
cli/internal/terraform/iammigrate_test.go
Normal file
111
cli/internal/terraform/iammigrate_test.go
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) Edgeless Systems GmbH
|
||||||
|
|
||||||
|
SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package terraform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIAMMigrate(t *testing.T) {
|
||||||
|
// plan should copy the terraform files to the upgrade directory
|
||||||
|
// create upgrade dir
|
||||||
|
// show plan changes in outWriter
|
||||||
|
upgradeID := "test-upgrade"
|
||||||
|
tfClient, err := New(context.Background(), filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeWorkingDir))
|
||||||
|
require.NoError(t, err)
|
||||||
|
// prep fs
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
file := file.NewHandler(fs)
|
||||||
|
err = file.MkdirAll(constants.TerraformIAMWorkingDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = file.Write(filepath.Join(constants.TerraformIAMWorkingDir, "terraform.tfvars"), []byte("OLD"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = file.Write(filepath.Join(constants.TerraformIAMWorkingDir, "terraform.tfstate"), []byte("OLD"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
tfClient.file = file
|
||||||
|
|
||||||
|
writer := bytes.NewBuffer(nil)
|
||||||
|
fakeTfClient := &tfClientStub{tfClient, upgradeID}
|
||||||
|
sut := NewIAMMigrateCmd(fakeTfClient, upgradeID, cloudprovider.AWS, LogLevelDebug, writer)
|
||||||
|
|
||||||
|
hasDiff, err := sut.Plan(context.Background())
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.False(t, hasDiff)
|
||||||
|
// check that files are copied
|
||||||
|
res, err := fs.Stat(filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformIAMUpgradeWorkingDir, "terraform.tfvars"))
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, res)
|
||||||
|
res, err = fs.Stat(filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformIAMUpgradeWorkingDir, "terraform.tfstate"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, res)
|
||||||
|
|
||||||
|
// apply
|
||||||
|
err = sut.Apply(context.Background(), file)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
// check that files are copied
|
||||||
|
bt, err := file.Read(filepath.Join(constants.TerraformIAMWorkingDir, "terraform.tfvars"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "NEW", string(bt))
|
||||||
|
bt, err = file.Read(filepath.Join(constants.TerraformIAMWorkingDir, "terraform.tfstate"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "NEW", string(bt))
|
||||||
|
|
||||||
|
// upgrade dir should be removed
|
||||||
|
res, err = fs.Stat(filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformIAMUpgradeWorkingDir))
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
type tfClientStub struct {
|
||||||
|
realClient *Client
|
||||||
|
upgradeID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tfClientStub) PrepareIAMUpgradeWorkspace(rootDir, workingDir, newWorkingDir, backupDir string) error {
|
||||||
|
return t.realClient.PrepareIAMUpgradeWorkspace(rootDir, workingDir, newWorkingDir, backupDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tfClientStub) Plan(ctx context.Context, logLevel LogLevel, planFile string) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tfClientStub) ShowPlan(ctx context.Context, logLevel LogLevel, planFile string, outWriter io.Writer) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tfClientStub) CreateIAMConfig(ctx context.Context, csp cloudprovider.Provider, logLevel LogLevel) (IAMOutput, error) {
|
||||||
|
upgradeDir := filepath.Join(constants.UpgradeDir, t.upgradeID, constants.TerraformIAMUpgradeWorkingDir)
|
||||||
|
err := t.realClient.file.Remove(filepath.Join(upgradeDir, "terraform.tfvars"))
|
||||||
|
if err != nil {
|
||||||
|
return IAMOutput{}, err
|
||||||
|
}
|
||||||
|
err = t.realClient.file.Write(filepath.Join(upgradeDir, "terraform.tfvars"), []byte("NEW"))
|
||||||
|
if err != nil {
|
||||||
|
return IAMOutput{}, err
|
||||||
|
}
|
||||||
|
err = t.realClient.file.Remove(filepath.Join(upgradeDir, "terraform.tfstate"))
|
||||||
|
if err != nil {
|
||||||
|
return IAMOutput{}, err
|
||||||
|
}
|
||||||
|
err = t.realClient.file.Write(filepath.Join(upgradeDir, "terraform.tfstate"), []byte("NEW"))
|
||||||
|
if err != nil {
|
||||||
|
return IAMOutput{}, err
|
||||||
|
}
|
||||||
|
return IAMOutput{}, nil
|
||||||
|
}
|
|
@ -8,7 +8,6 @@ import (
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/upgrade"
|
"github.com/edgelesssys/constellation/v2/cli/internal/upgrade"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,12 +23,12 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("setting up terraform upgrader: %w", err))
|
panic(fmt.Errorf("setting up terraform upgrader: %w", err))
|
||||||
}
|
}
|
||||||
diff, err := tfUpgrader.PlanIAMMigration(ctx, upgrade.TerraformUpgradeOptions{
|
//diff, err := tfUpgrader.PlanIAMMigration(ctx, upgrade.TerraformUpgradeOptions{
|
||||||
CSP: cloudprovider.AWS,
|
// CSP: cloudprovider.AWS,
|
||||||
LogLevel: terraform.LogLevelDebug,
|
// LogLevel: terraform.LogLevelDebug,
|
||||||
}, "test")
|
//}, "test")
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
panic(fmt.Errorf("planning terraform migrations: %w", err))
|
// panic(fmt.Errorf("planning terraform migrations: %w", err))
|
||||||
}
|
//}
|
||||||
fmt.Println(diff)
|
//fmt.Println(diff)
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,31 +85,6 @@ func checkFileExists(fileHandler file.Handler, existingFiles *[]string, filename
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *TerraformUpgrader) PlanIAMMigration(ctx context.Context, csp cloudprovider.Provider, logLevel terraform.LogLevel, upgradeID string) (bool, error) {
|
|
||||||
err := u.tf.PrepareIAMUpgradeWorkspace(
|
|
||||||
filepath.Join("terraform", "iam", strings.ToLower(csp.String())),
|
|
||||||
constants.TerraformIAMWorkingDir,
|
|
||||||
filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeWorkingDir),
|
|
||||||
filepath.Join(constants.UpgradeDir, upgradeID, constants.TerraformUpgradeBackupDir),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("preparing terraform workspace: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
hasDiff, err := u.tf.Plan(ctx, logLevel, constants.TerraformUpgradePlanFile)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("terraform plan: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasDiff {
|
|
||||||
if err := u.tf.ShowPlan(ctx, logLevel, constants.TerraformUpgradePlanFile, u.outWriter); err != nil {
|
|
||||||
return false, fmt.Errorf("terraform show plan: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasDiff, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlanTerraformMigrations prepares the upgrade workspace and plans the Terraform migrations for the Constellation upgrade.
|
// PlanTerraformMigrations prepares the upgrade workspace and plans the Terraform migrations for the Constellation upgrade.
|
||||||
// If a diff exists, it's being written to the upgrader's output writer. It also returns
|
// If a diff exists, it's being written to the upgrader's output writer. It also returns
|
||||||
// a bool indicating whether a diff exists.
|
// a bool indicating whether a diff exists.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue