User-friendlier errors

This commit is contained in:
Nils Hanke 2022-11-16 16:33:51 +01:00 committed by Nils Hanke
parent e1d8926395
commit 6e5895f200
3 changed files with 31 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import (
"io/fs" "io/fs"
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd" "github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider" "github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config" "github.com/edgelesssys/constellation/v2/internal/config"
"github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/constants"
@ -126,7 +127,7 @@ func create(cmd *cobra.Command, creator cloudCreator, fileHandler file.Handler,
idFile, err := creator.Create(cmd.Context(), provider, conf, flags.name, instanceType, flags.controllerCount, flags.workerCount) idFile, err := creator.Create(cmd.Context(), provider, conf, flags.name, instanceType, flags.controllerCount, flags.workerCount)
spinner.Stop() spinner.Stop()
if err != nil { if err != nil {
return err return translateCreateErrors(cmd, err)
} }
if err := fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone); err != nil { if err := fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone); err != nil {
@ -209,6 +210,27 @@ func checkDirClean(fileHandler file.Handler) error {
return nil return nil
} }
func translateCreateErrors(cmd *cobra.Command, err error) error {
switch {
case errors.Is(err, terraform.ErrTerraformWorkspaceDifferentFiles):
cmd.PrintErrln("\nYour current working directory contains an existing Terraform workspace which does not match the expected state.")
cmd.PrintErrln("This can be due to a mix up between providers, versions or an otherwise corrupted workspace.")
cmd.PrintErrln("Before creating a new cluster, try \"constellation terminate\".")
cmd.PrintErrf("If this does not work, either move or delete the directory %q.\n", constants.TerraformWorkingDir)
cmd.PrintErrln("Please only delete the directory if you made sure that all created cloud resources have been terminated.")
return err
case errors.Is(err, terraform.ErrTerraformWorkspaceExistsWithDifferentVariables):
cmd.PrintErrln("\nYour current working directory contains an existing Terraform workspace which was initiated with different input variables.")
cmd.PrintErrln("This can be the case if you have tried to create a cluster before with different options which did not complete, or the workspace is corrupted.")
cmd.PrintErrln("Before creating a new cluster, try \"constellation terminate\".")
cmd.PrintErrf("If this does not work, either move or delete the directory %q.\n", constants.TerraformWorkingDir)
cmd.PrintErrln("Please only delete the directory if you made sure that all created cloud resources have been terminated.")
return err
default:
return err
}
}
func must(err error) { func must(err error) {
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -10,7 +10,6 @@ import (
"bytes" "bytes"
"embed" "embed"
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"path" "path"
"path/filepath" "path/filepath"
@ -21,6 +20,9 @@ import (
"github.com/spf13/afero" "github.com/spf13/afero"
) )
// ErrTerraformWorkspaceDifferentFiles is returned when a re-used existing Terraform workspace has different files than the ones to be extracted (e.g. due to a version mix-up or incomplete writes).
var ErrTerraformWorkspaceDifferentFiles = errors.New("creating cluster: trying to overwrite an existing Terraform file with a different version")
//go:embed terraform/* //go:embed terraform/*
//go:embed terraform/*/.terraform.lock.hcl //go:embed terraform/*/.terraform.lock.hcl
var terraformFS embed.FS var terraformFS embed.FS
@ -53,7 +55,7 @@ func prepareWorkspace(fileHandler file.Handler, provider cloudprovider.Provider,
} }
if !bytes.Equal(content, existingFileContent) { if !bytes.Equal(content, existingFileContent) {
return fmt.Errorf("trying to overwrite existing Terraform file with different version") return ErrTerraformWorkspaceDifferentFiles
} }
return nil return nil
} else if err != nil { } else if err != nil {

View File

@ -29,6 +29,9 @@ const (
terraformVarsFile = "terraform.tfvars" terraformVarsFile = "terraform.tfvars"
) )
// ErrTerraformWorkspaceExistsWithDifferentVariables is returned when existing Terraform files differ from the version the CLI wants to extract.
var ErrTerraformWorkspaceExistsWithDifferentVariables = errors.New("creating cluster: a Terraform workspace already exists with different variables")
// Client manages interaction with Terraform. // Client manages interaction with Terraform.
type Client struct { type Client struct {
tf tfInterface tf tfInterface
@ -162,7 +165,7 @@ func (c *Client) writeVars(vars Variables) error {
return err return err
} }
if vars.String() != string(varsContent) { if vars.String() != string(varsContent) {
return errors.New("creating cluster: workspace already exists with different variables") return ErrTerraformWorkspaceExistsWithDifferentVariables
} }
} else if err != nil { } else if err != nil {
return err return err