mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-04 07:15:05 -04:00
cli: perform upgrades in-place in Terraform workspace (#2317)
* perform upgrades in-place in terraform workspace Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * update buildfiles Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add iam upgrade apply test Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * update buildfiles Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix linter Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * make config fetcher stubbable Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * change workspace restoring behaviour Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * allow overwriting existing Terraform files Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * allow overwrites of TF variables Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix iam upgrade apply Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix embed directive Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * make loader test less brittle Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * pass upgrade ID to user Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * naming nit Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * use upgradeDir Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * tidy Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> --------- Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
This commit is contained in:
parent
9c54ff06e0
commit
95cf4bdf21
19 changed files with 410 additions and 286 deletions
|
@ -25,39 +25,39 @@ var ErrTerraformWorkspaceDifferentFiles = errors.New("creating cluster: trying t
|
|||
|
||||
//go:embed terraform/*
|
||||
//go:embed terraform/*/.terraform.lock.hcl
|
||||
//go:embed terraform/iam/*/.terraform.lock.hcl
|
||||
var terraformFS embed.FS
|
||||
|
||||
const (
|
||||
noOverwrites overwritePolicy = iota
|
||||
allowOverwrites
|
||||
)
|
||||
|
||||
type overwritePolicy int
|
||||
|
||||
// prepareWorkspace loads the embedded Terraform files,
|
||||
// and writes them into the workspace.
|
||||
func prepareWorkspace(rootDir string, fileHandler file.Handler, workingDir string) error {
|
||||
return terraformCopier(fileHandler, rootDir, workingDir)
|
||||
return terraformCopier(fileHandler, rootDir, workingDir, noOverwrites)
|
||||
}
|
||||
|
||||
// prepareUpgradeWorkspace takes the Terraform state file from the old workspace and the
|
||||
// embedded Terraform files and writes them into the new workspace.
|
||||
func prepareUpgradeWorkspace(rootDir string, fileHandler file.Handler, oldWorkingDir, newWorkingDir, backupDir string) error {
|
||||
// prepareUpgradeWorkspace backs up the old Terraform workspace from workingDir, and
|
||||
// copies the embedded Terraform files into workingDir.
|
||||
func prepareUpgradeWorkspace(rootDir string, fileHandler file.Handler, workingDir, backupDir string) error {
|
||||
// backup old workspace
|
||||
if err := fileHandler.CopyDir(
|
||||
oldWorkingDir,
|
||||
workingDir,
|
||||
backupDir,
|
||||
); err != nil {
|
||||
return fmt.Errorf("backing up old workspace: %w", err)
|
||||
}
|
||||
|
||||
// copy state file
|
||||
if err := fileHandler.CopyFile(
|
||||
filepath.Join(oldWorkingDir, "terraform.tfstate"),
|
||||
filepath.Join(newWorkingDir, "terraform.tfstate"),
|
||||
file.OptMkdirAll,
|
||||
); err != nil {
|
||||
return fmt.Errorf("copying state file: %w", err)
|
||||
}
|
||||
|
||||
return terraformCopier(fileHandler, rootDir, newWorkingDir)
|
||||
return terraformCopier(fileHandler, rootDir, workingDir, allowOverwrites)
|
||||
}
|
||||
|
||||
// terraformCopier copies the embedded Terraform files into the workspace.
|
||||
func terraformCopier(fileHandler file.Handler, rootDir, workingDir string) error {
|
||||
// allowOverwrites allows overwriting existing files in the workspace.
|
||||
func terraformCopier(fileHandler file.Handler, rootDir, workingDir string, overwritePolicy overwritePolicy) error {
|
||||
goEmbedRootDir := filepath.ToSlash(rootDir)
|
||||
return fs.WalkDir(terraformFS, goEmbedRootDir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
|
@ -74,8 +74,15 @@ func terraformCopier(fileHandler file.Handler, rootDir, workingDir string) error
|
|||
}
|
||||
// normalize
|
||||
fileName := strings.Replace(slashpath.Join(workingDir, path), goEmbedRootDir+"/", "", 1)
|
||||
if err := fileHandler.Write(fileName, content, file.OptMkdirAll); errors.Is(err, afero.ErrFileExists) {
|
||||
// If a file already exists, check if it is identical. If yes, continue and don't write anything to disk.
|
||||
opts := []file.Option{
|
||||
file.OptMkdirAll,
|
||||
}
|
||||
if overwritePolicy == allowOverwrites {
|
||||
opts = append(opts, file.OptOverwrite)
|
||||
}
|
||||
if err := fileHandler.Write(fileName, content, opts...); errors.Is(err, afero.ErrFileExists) {
|
||||
// If a file already exists and overwritePolicy is set to noOverwrites,
|
||||
// check if it is identical. If yes, continue and don't write anything to disk.
|
||||
// If no, don't overwrite it and instead throw an error. The affected file could be from a different version,
|
||||
// provider, corrupted or manually modified in general.
|
||||
existingFileContent, err := fileHandler.Read(fileName)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue