mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
AB#2532 Dont clean up workspace if rollback fails (#360)
* Dont clean up workspace if rollback fails * Remove dependency on CSP from terminate Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
1f8eba37c8
commit
e66cb84d6e
@ -10,10 +10,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type terraformClient interface {
|
type terraformClient interface {
|
||||||
CreateCluster(ctx context.Context, name string, input terraform.Variables) (string, error)
|
CreateCluster(ctx context.Context, provider cloudprovider.Provider, name string, input terraform.Variables) (string, error)
|
||||||
DestroyCluster(ctx context.Context) error
|
DestroyCluster(ctx context.Context) error
|
||||||
CleanUpWorkspace() error
|
CleanUpWorkspace() error
|
||||||
RemoveInstaller()
|
RemoveInstaller()
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
||||||
"go.uber.org/goleak"
|
"go.uber.org/goleak"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ type stubTerraformClient struct {
|
|||||||
cleanUpWorkspaceErr error
|
cleanUpWorkspaceErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *stubTerraformClient) CreateCluster(ctx context.Context, name string, input terraform.Variables) (string, error) {
|
func (c *stubTerraformClient) CreateCluster(ctx context.Context, provider cloudprovider.Provider, name string, input terraform.Variables) (string, error) {
|
||||||
return c.ip, c.createClusterErr
|
return c.ip, c.createClusterErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
// Creator creates cloud resources.
|
// Creator creates cloud resources.
|
||||||
type Creator struct {
|
type Creator struct {
|
||||||
out io.Writer
|
out io.Writer
|
||||||
newTerraformClient func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error)
|
newTerraformClient func(ctx context.Context) (terraformClient, error)
|
||||||
newLibvirtRunner func() libvirtRunner
|
newLibvirtRunner func() libvirtRunner
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,8 +33,8 @@ type Creator struct {
|
|||||||
func NewCreator(out io.Writer) *Creator {
|
func NewCreator(out io.Writer) *Creator {
|
||||||
return &Creator{
|
return &Creator{
|
||||||
out: out,
|
out: out,
|
||||||
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
||||||
return terraform.New(ctx, provider)
|
return terraform.New(ctx)
|
||||||
},
|
},
|
||||||
newLibvirtRunner: func() libvirtRunner {
|
newLibvirtRunner: func() libvirtRunner {
|
||||||
return libvirt.New()
|
return libvirt.New()
|
||||||
@ -51,21 +51,21 @@ func (c *Creator) Create(ctx context.Context, provider cloudprovider.Provider, c
|
|||||||
if os.Getenv("CONSTELLATION_AWS_DEV") != "1" {
|
if os.Getenv("CONSTELLATION_AWS_DEV") != "1" {
|
||||||
return clusterid.File{}, fmt.Errorf("AWS isn't supported yet")
|
return clusterid.File{}, fmt.Errorf("AWS isn't supported yet")
|
||||||
}
|
}
|
||||||
cl, err := c.newTerraformClient(ctx, provider)
|
cl, err := c.newTerraformClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
defer cl.RemoveInstaller()
|
defer cl.RemoveInstaller()
|
||||||
return c.createAWS(ctx, cl, config, name, insType, controlPlaneCount, workerCount)
|
return c.createAWS(ctx, cl, config, name, insType, controlPlaneCount, workerCount)
|
||||||
case cloudprovider.GCP:
|
case cloudprovider.GCP:
|
||||||
cl, err := c.newTerraformClient(ctx, provider)
|
cl, err := c.newTerraformClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
defer cl.RemoveInstaller()
|
defer cl.RemoveInstaller()
|
||||||
return c.createGCP(ctx, cl, config, name, insType, controlPlaneCount, workerCount)
|
return c.createGCP(ctx, cl, config, name, insType, controlPlaneCount, workerCount)
|
||||||
case cloudprovider.Azure:
|
case cloudprovider.Azure:
|
||||||
cl, err := c.newTerraformClient(ctx, provider)
|
cl, err := c.newTerraformClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ func (c *Creator) Create(ctx context.Context, provider cloudprovider.Provider, c
|
|||||||
if runtime.GOARCH != "amd64" || runtime.GOOS != "linux" {
|
if runtime.GOARCH != "amd64" || runtime.GOOS != "linux" {
|
||||||
return clusterid.File{}, fmt.Errorf("creation of a QEMU based Constellation is not supported for %s/%s", runtime.GOOS, runtime.GOARCH)
|
return clusterid.File{}, fmt.Errorf("creation of a QEMU based Constellation is not supported for %s/%s", runtime.GOOS, runtime.GOARCH)
|
||||||
}
|
}
|
||||||
cl, err := c.newTerraformClient(ctx, provider)
|
cl, err := c.newTerraformClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ func (c *Creator) createAWS(ctx context.Context, cl terraformClient, config *con
|
|||||||
Debug: config.IsDebugCluster(),
|
Debug: config.IsDebugCluster(),
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := cl.CreateCluster(ctx, name, vars)
|
ip, err := cl.CreateCluster(ctx, cloudprovider.AWS, name, vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func (c *Creator) createGCP(ctx context.Context, cl terraformClient, config *con
|
|||||||
Debug: config.IsDebugCluster(),
|
Debug: config.IsDebugCluster(),
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := cl.CreateCluster(ctx, name, &vars)
|
ip, err := cl.CreateCluster(ctx, cloudprovider.GCP, name, &vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
@ -177,7 +177,7 @@ func (c *Creator) createAzure(ctx context.Context, cl terraformClient, config *c
|
|||||||
|
|
||||||
vars = normalizeAzureURIs(vars)
|
vars = normalizeAzureURIs(vars)
|
||||||
|
|
||||||
ip, err := cl.CreateCluster(ctx, name, &vars)
|
ip, err := cl.CreateCluster(ctx, cloudprovider.Azure, name, &vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
@ -258,7 +258,7 @@ func (c *Creator) createQEMU(ctx context.Context, cl terraformClient, lv libvirt
|
|||||||
Firmware: config.Provider.QEMU.Firmware,
|
Firmware: config.Provider.QEMU.Firmware,
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := cl.CreateCluster(ctx, name, &vars)
|
ip, err := cl.CreateCluster(ctx, cloudprovider.QEMU, name, &vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return clusterid.File{}, err
|
return clusterid.File{}, err
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ func TestCreator(t *testing.T) {
|
|||||||
|
|
||||||
creator := &Creator{
|
creator := &Creator{
|
||||||
out: &bytes.Buffer{},
|
out: &bytes.Buffer{},
|
||||||
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
||||||
return tc.tfClient, tc.newTfClientErr
|
return tc.tfClient, tc.newTfClientErr
|
||||||
},
|
},
|
||||||
newLibvirtRunner: func() libvirtRunner {
|
newLibvirtRunner: func() libvirtRunner {
|
||||||
|
@ -41,7 +41,9 @@ type rollbackerTerraform struct {
|
|||||||
func (r *rollbackerTerraform) rollback(ctx context.Context) error {
|
func (r *rollbackerTerraform) rollback(ctx context.Context) error {
|
||||||
var err error
|
var err error
|
||||||
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
||||||
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
if err == nil {
|
||||||
|
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +56,8 @@ func (r *rollbackerQEMU) rollback(ctx context.Context) error {
|
|||||||
var err error
|
var err error
|
||||||
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
err = multierr.Append(err, r.client.DestroyCluster(ctx))
|
||||||
err = multierr.Append(err, r.libvirt.Stop(ctx))
|
err = multierr.Append(err, r.libvirt.Stop(ctx))
|
||||||
err = multierr.Append(err, r.client.CleanUpWorkspace())
|
if err == nil {
|
||||||
|
err = r.client.CleanUpWorkspace()
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
112
cli/internal/cloudcmd/rollback_test.go
Normal file
112
cli/internal/cloudcmd/rollback_test.go
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) Edgeless Systems GmbH
|
||||||
|
|
||||||
|
SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cloudcmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRollbackTerraform(t *testing.T) {
|
||||||
|
someErr := errors.New("failed")
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
tfClient *stubTerraformClient
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
"success": {
|
||||||
|
tfClient: &stubTerraformClient{},
|
||||||
|
},
|
||||||
|
"destroy cluster error": {
|
||||||
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"clean up workspace error": {
|
||||||
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
rollbacker := &rollbackerTerraform{
|
||||||
|
client: tc.tfClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := rollbacker.rollback(context.Background())
|
||||||
|
if tc.wantErr {
|
||||||
|
assert.Error(err)
|
||||||
|
if tc.tfClient.cleanUpWorkspaceErr == nil {
|
||||||
|
assert.False(tc.tfClient.cleanUpWorkspaceCalled)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.True(tc.tfClient.destroyClusterCalled)
|
||||||
|
assert.True(tc.tfClient.cleanUpWorkspaceCalled)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRollbackQEMU(t *testing.T) {
|
||||||
|
someErr := errors.New("failed")
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
libvirt *stubLibvirtRunner
|
||||||
|
tfClient *stubTerraformClient
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
"success": {
|
||||||
|
libvirt: &stubLibvirtRunner{},
|
||||||
|
tfClient: &stubTerraformClient{},
|
||||||
|
},
|
||||||
|
"stop libvirt error": {
|
||||||
|
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
||||||
|
tfClient: &stubTerraformClient{},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"destroy cluster error": {
|
||||||
|
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
||||||
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"clean up workspace error": {
|
||||||
|
libvirt: &stubLibvirtRunner{},
|
||||||
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
rollbacker := &rollbackerQEMU{
|
||||||
|
libvirt: tc.libvirt,
|
||||||
|
client: tc.tfClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := rollbacker.rollback(context.Background())
|
||||||
|
if tc.wantErr {
|
||||||
|
assert.Error(err)
|
||||||
|
if tc.tfClient.cleanUpWorkspaceErr == nil {
|
||||||
|
assert.False(tc.tfClient.cleanUpWorkspaceCalled)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.True(tc.libvirt.stopCalled)
|
||||||
|
assert.True(tc.tfClient.destroyClusterCalled)
|
||||||
|
assert.True(tc.tfClient.cleanUpWorkspaceCalled)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -8,24 +8,22 @@ package cloudcmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
|
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Terminator deletes cloud provider resources.
|
// Terminator deletes cloud provider resources.
|
||||||
type Terminator struct {
|
type Terminator struct {
|
||||||
newTerraformClient func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error)
|
newTerraformClient func(ctx context.Context) (terraformClient, error)
|
||||||
newLibvirtRunner func() libvirtRunner
|
newLibvirtRunner func() libvirtRunner
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTerminator create a new cloud terminator.
|
// NewTerminator create a new cloud terminator.
|
||||||
func NewTerminator() *Terminator {
|
func NewTerminator() *Terminator {
|
||||||
return &Terminator{
|
return &Terminator{
|
||||||
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
||||||
return terraform.New(ctx, provider)
|
return terraform.New(ctx)
|
||||||
},
|
},
|
||||||
newLibvirtRunner: func() libvirtRunner {
|
newLibvirtRunner: func() libvirtRunner {
|
||||||
return libvirt.New()
|
return libvirt.New()
|
||||||
@ -34,21 +32,14 @@ func NewTerminator() *Terminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Terminate deletes the could provider resources.
|
// Terminate deletes the could provider resources.
|
||||||
func (t *Terminator) Terminate(ctx context.Context, provider cloudprovider.Provider) (retErr error) {
|
func (t *Terminator) Terminate(ctx context.Context) (retErr error) {
|
||||||
if provider == cloudprovider.Unknown {
|
defer func() {
|
||||||
return errors.New("unknown cloud provider")
|
if retErr == nil {
|
||||||
}
|
retErr = t.newLibvirtRunner().Stop(ctx)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if provider == cloudprovider.QEMU {
|
cl, err := t.newTerraformClient(ctx)
|
||||||
libvirt := t.newLibvirtRunner()
|
|
||||||
defer func() {
|
|
||||||
if retErr == nil {
|
|
||||||
retErr = libvirt.Stop(ctx)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
cl, err := t.newTerraformClient(ctx, provider)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,54 +21,32 @@ func TestTerminator(t *testing.T) {
|
|||||||
tfClient terraformClient
|
tfClient terraformClient
|
||||||
newTfClientErr error
|
newTfClientErr error
|
||||||
libvirt *stubLibvirtRunner
|
libvirt *stubLibvirtRunner
|
||||||
provider cloudprovider.Provider
|
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
"gcp": {
|
"gcp": {
|
||||||
|
libvirt: &stubLibvirtRunner{},
|
||||||
tfClient: &stubTerraformClient{},
|
tfClient: &stubTerraformClient{},
|
||||||
provider: cloudprovider.GCP,
|
|
||||||
},
|
},
|
||||||
"gcp newTfClientErr": {
|
"newTfClientErr": {
|
||||||
|
libvirt: &stubLibvirtRunner{},
|
||||||
newTfClientErr: someErr,
|
newTfClientErr: someErr,
|
||||||
provider: cloudprovider.GCP,
|
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
"gcp destroy cluster error": {
|
"destroy cluster error": {
|
||||||
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
|
||||||
provider: cloudprovider.GCP,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
"gcp clean up workspace error": {
|
|
||||||
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
|
||||||
provider: cloudprovider.GCP,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
"qemu": {
|
|
||||||
tfClient: &stubTerraformClient{},
|
|
||||||
libvirt: &stubLibvirtRunner{},
|
|
||||||
provider: cloudprovider.QEMU,
|
|
||||||
},
|
|
||||||
"qemu destroy cluster error": {
|
|
||||||
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
||||||
libvirt: &stubLibvirtRunner{},
|
libvirt: &stubLibvirtRunner{},
|
||||||
provider: cloudprovider.QEMU,
|
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
"qemu clean up workspace error": {
|
"clean up workspace error": {
|
||||||
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
||||||
libvirt: &stubLibvirtRunner{},
|
libvirt: &stubLibvirtRunner{},
|
||||||
provider: cloudprovider.QEMU,
|
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
"qemu stop libvirt error": {
|
"qemu stop libvirt error": {
|
||||||
tfClient: &stubTerraformClient{},
|
tfClient: &stubTerraformClient{},
|
||||||
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
||||||
provider: cloudprovider.QEMU,
|
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
"unknown cloud provider": {
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
@ -77,7 +54,7 @@ func TestTerminator(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
terminator := &Terminator{
|
terminator := &Terminator{
|
||||||
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
||||||
return tc.tfClient, tc.newTfClientErr
|
return tc.tfClient, tc.newTfClientErr
|
||||||
},
|
},
|
||||||
newLibvirtRunner: func() libvirtRunner {
|
newLibvirtRunner: func() libvirtRunner {
|
||||||
@ -85,19 +62,17 @@ func TestTerminator(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := terminator.Terminate(context.Background(), tc.provider)
|
err := terminator.Terminate(context.Background())
|
||||||
|
|
||||||
if tc.wantErr {
|
if tc.wantErr {
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
} else {
|
return
|
||||||
assert.NoError(err)
|
|
||||||
cl := tc.tfClient.(*stubTerraformClient)
|
|
||||||
assert.True(cl.destroyClusterCalled)
|
|
||||||
assert.True(cl.removeInstallerCalled)
|
|
||||||
if tc.provider == cloudprovider.QEMU {
|
|
||||||
assert.True(tc.libvirt.stopCalled)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
assert.NoError(err)
|
||||||
|
cl := tc.tfClient.(*stubTerraformClient)
|
||||||
|
assert.True(cl.destroyClusterCalled)
|
||||||
|
assert.True(cl.removeInstallerCalled)
|
||||||
|
assert.True(tc.libvirt.stopCalled)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,5 +25,5 @@ type cloudCreator interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type cloudTerminator interface {
|
type cloudTerminator interface {
|
||||||
Terminate(context.Context, cloudprovider.Provider) error
|
Terminate(context.Context) error
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ type stubCloudTerminator struct {
|
|||||||
terminateErr error
|
terminateErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *stubCloudTerminator) Terminate(context.Context, cloudprovider.Provider) error {
|
func (c *stubCloudTerminator) Terminate(context.Context) error {
|
||||||
c.called = true
|
c.called = true
|
||||||
return c.terminateErr
|
return c.terminateErr
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ import (
|
|||||||
"go.uber.org/multierr"
|
"go.uber.org/multierr"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
||||||
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/file"
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
||||||
)
|
)
|
||||||
@ -45,13 +44,8 @@ func runTerminate(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
func terminate(cmd *cobra.Command, terminator cloudTerminator, fileHandler file.Handler, spinner spinnerInterf,
|
func terminate(cmd *cobra.Command, terminator cloudTerminator, fileHandler file.Handler, spinner spinnerInterf,
|
||||||
) error {
|
) error {
|
||||||
var idFile clusterid.File
|
|
||||||
if err := fileHandler.ReadJSON(constants.ClusterIDsFileName, &idFile); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
spinner.Start("Terminating", false)
|
spinner.Start("Terminating", false)
|
||||||
err := terminator.Terminate(cmd.Context(), idFile.CloudProvider)
|
err := terminator.Terminate(cmd.Context())
|
||||||
spinner.Stop()
|
spinner.Stop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("terminating Constellation cluster: %w", err)
|
return fmt.Errorf("terminating Constellation cluster: %w", err)
|
||||||
|
@ -83,7 +83,7 @@ func TestTerminate(t *testing.T) {
|
|||||||
terminator: &stubCloudTerminator{terminateErr: someErr},
|
terminator: &stubCloudTerminator{terminateErr: someErr},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
"missing id file": {
|
"missing id file does not error": {
|
||||||
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
|
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
|
||||||
setupFs: func(require *require.Assertions, idFile clusterid.File) afero.Fs {
|
setupFs: func(require *require.Assertions, idFile clusterid.File) afero.Fs {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
@ -92,7 +92,6 @@ func TestTerminate(t *testing.T) {
|
|||||||
return fs
|
return fs
|
||||||
},
|
},
|
||||||
terminator: &stubCloudTerminator{},
|
terminator: &stubCloudTerminator{},
|
||||||
wantErr: true,
|
|
||||||
},
|
},
|
||||||
"remove file fails": {
|
"remove file fails": {
|
||||||
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
|
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
|
||||||
|
@ -45,15 +45,21 @@ func prepareWorkspace(fileHandler file.Handler, provider cloudprovider.Provider)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cleanUpWorkspace removes files that were loaded into the workspace.
|
// cleanUpWorkspace removes files that were loaded into the workspace.
|
||||||
func cleanUpWorkspace(fileHandler file.Handler, provider cloudprovider.Provider) error {
|
func cleanUpWorkspace(fileHandler file.Handler) error {
|
||||||
rootDir := path.Join("terraform", strings.ToLower(provider.String()))
|
// try to remove any terraform files in the workspace
|
||||||
return fs.WalkDir(terraformFS, rootDir, func(path string, d fs.DirEntry, err error) error {
|
for _, csp := range []string{"aws", "azure", "gcp", "qemu"} {
|
||||||
if err != nil {
|
rootDir := path.Join("terraform", csp)
|
||||||
|
if err := fs.WalkDir(terraformFS, rootDir, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fileName := strings.TrimPrefix(path, rootDir+"/")
|
||||||
|
return ignoreFileNotFoundErr(fileHandler.RemoveAll(fileName))
|
||||||
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fileName := strings.TrimPrefix(path, rootDir+"/")
|
}
|
||||||
return ignoreFileNotFoundErr(fileHandler.RemoveAll(fileName))
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignoreFileNotFoundErr ignores the error if it is a file not found error.
|
// ignoreFileNotFoundErr ignores the error if it is a file not found error.
|
||||||
|
@ -63,7 +63,7 @@ func TestLoader(t *testing.T) {
|
|||||||
|
|
||||||
checkFiles(t, file, func(err error) { assert.NoError(err) }, tc.fileList)
|
checkFiles(t, file, func(err error) { assert.NoError(err) }, tc.fileList)
|
||||||
|
|
||||||
err = cleanUpWorkspace(file, tc.provider)
|
err = cleanUpWorkspace(file)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
checkFiles(t, file, func(err error) { assert.ErrorIs(err, fs.ErrNotExist) }, tc.fileList)
|
checkFiles(t, file, func(err error) { assert.ErrorIs(err, fs.ErrNotExist) }, tc.fileList)
|
||||||
|
@ -32,14 +32,12 @@ const (
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
tf tfInterface
|
tf tfInterface
|
||||||
|
|
||||||
provider cloudprovider.Provider
|
|
||||||
|
|
||||||
file file.Handler
|
file file.Handler
|
||||||
remove func()
|
remove func()
|
||||||
}
|
}
|
||||||
|
|
||||||
// New sets up a new Client for Terraform.
|
// New sets up a new Client for Terraform.
|
||||||
func New(ctx context.Context, provider cloudprovider.Provider) (*Client, error) {
|
func New(ctx context.Context) (*Client, error) {
|
||||||
tf, remove, err := GetExecutable(ctx, ".")
|
tf, remove, err := GetExecutable(ctx, ".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -48,16 +46,17 @@ func New(ctx context.Context, provider cloudprovider.Provider) (*Client, error)
|
|||||||
file := file.NewHandler(afero.NewOsFs())
|
file := file.NewHandler(afero.NewOsFs())
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
tf: tf,
|
tf: tf,
|
||||||
provider: provider,
|
remove: remove,
|
||||||
remove: remove,
|
file: file,
|
||||||
file: file,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateCluster creates a Constellation cluster using Terraform.
|
// CreateCluster creates a Constellation cluster using Terraform.
|
||||||
func (c *Client) CreateCluster(ctx context.Context, name string, vars Variables) (string, error) {
|
func (c *Client) CreateCluster(
|
||||||
if err := prepareWorkspace(c.file, c.provider); err != nil {
|
ctx context.Context, provider cloudprovider.Provider, name string, vars Variables,
|
||||||
|
) (string, error) {
|
||||||
|
if err := prepareWorkspace(c.file, provider); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +101,7 @@ func (c *Client) RemoveInstaller() {
|
|||||||
|
|
||||||
// CleanUpWorkspace removes terraform files from the current directory.
|
// CleanUpWorkspace removes terraform files from the current directory.
|
||||||
func (c *Client) CleanUpWorkspace() error {
|
func (c *Client) CleanUpWorkspace() error {
|
||||||
if err := cleanUpWorkspace(c.file, c.provider); err != nil {
|
if err := cleanUpWorkspace(c.file); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,12 +122,11 @@ func TestCreateCluster(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
provider: tc.provider,
|
tf: tc.tf,
|
||||||
tf: tc.tf,
|
file: file.NewHandler(tc.fs),
|
||||||
file: file.NewHandler(tc.fs),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := c.CreateCluster(context.Background(), "test", tc.vars)
|
ip, err := c.CreateCluster(context.Background(), tc.provider, "test", tc.vars)
|
||||||
|
|
||||||
if tc.wantErr {
|
if tc.wantErr {
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
@ -160,8 +159,7 @@ func TestDestroyInstances(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
provider: cloudprovider.QEMU,
|
tf: tc.tf,
|
||||||
tf: tc.tf,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.DestroyCluster(context.Background())
|
err := c.DestroyCluster(context.Background())
|
||||||
@ -207,9 +205,8 @@ func TestCleanupWorkspace(t *testing.T) {
|
|||||||
require.NoError(tc.prepareFS(file))
|
require.NoError(tc.prepareFS(file))
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
provider: tc.provider,
|
file: file,
|
||||||
file: file,
|
tf: &stubTerraform{},
|
||||||
tf: &stubTerraform{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.CleanUpWorkspace()
|
err := c.CleanUpWorkspace()
|
||||||
|
Loading…
Reference in New Issue
Block a user