-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add delete workload using controller workflow * cleanup cluster directory * fix TestDeleteRunSuccess
- Loading branch information
Showing
11 changed files
with
352 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package workload | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/eks-anywhere/pkg/cluster" | ||
"github.com/aws/eks-anywhere/pkg/filewriter" | ||
"github.com/aws/eks-anywhere/pkg/providers" | ||
"github.com/aws/eks-anywhere/pkg/task" | ||
"github.com/aws/eks-anywhere/pkg/types" | ||
"github.com/aws/eks-anywhere/pkg/workflows/interfaces" | ||
) | ||
|
||
// Delete is the workflow that deletes a workload clusters. | ||
type Delete struct { | ||
provider providers.Provider | ||
writer filewriter.FileWriter | ||
clusterManager interfaces.ClusterManager | ||
clusterDeleter interfaces.ClusterDeleter | ||
} | ||
|
||
// NewDelete builds a new delete construct. | ||
func NewDelete(provider providers.Provider, | ||
writer filewriter.FileWriter, | ||
clusterManager interfaces.ClusterManager, | ||
clusterDeleter interfaces.ClusterDeleter, | ||
) *Delete { | ||
return &Delete{ | ||
provider: provider, | ||
writer: writer, | ||
clusterManager: clusterManager, | ||
clusterDeleter: clusterDeleter, | ||
} | ||
} | ||
|
||
// Run executes the tasks to delete a workload cluster. | ||
func (c *Delete) Run(ctx context.Context, workload *types.Cluster, clusterSpec *cluster.Spec) error { | ||
commandContext := &task.CommandContext{ | ||
Provider: c.provider, | ||
Writer: c.writer, | ||
ClusterManager: c.clusterManager, | ||
ClusterSpec: clusterSpec, | ||
ManagementCluster: clusterSpec.ManagementCluster, | ||
WorkloadCluster: workload, | ||
ClusterDeleter: c.clusterDeleter, | ||
} | ||
|
||
return task.NewTaskRunner(&setupAndValidateDelete{}, c.writer).RunTask(ctx, commandContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package workload_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
|
||
"github.com/aws/eks-anywhere/internal/test" | ||
"github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
"github.com/aws/eks-anywhere/pkg/cluster" | ||
"github.com/aws/eks-anywhere/pkg/features" | ||
writermocks "github.com/aws/eks-anywhere/pkg/filewriter/mocks" | ||
"github.com/aws/eks-anywhere/pkg/providers" | ||
providermocks "github.com/aws/eks-anywhere/pkg/providers/mocks" | ||
"github.com/aws/eks-anywhere/pkg/types" | ||
"github.com/aws/eks-anywhere/pkg/workflows/interfaces/mocks" | ||
"github.com/aws/eks-anywhere/pkg/workflows/workload" | ||
) | ||
|
||
type deleteTestSetup struct { | ||
t *testing.T | ||
provider *providermocks.MockProvider | ||
clusterDeleter *mocks.MockClusterDeleter | ||
clusterManager *mocks.MockClusterManager | ||
datacenterConfig providers.DatacenterConfig | ||
machineConfigs []providers.MachineConfig | ||
ctx context.Context | ||
clusterSpec *cluster.Spec | ||
workloadCluster *types.Cluster | ||
workload *workload.Delete | ||
writer *writermocks.MockFileWriter | ||
} | ||
|
||
func newDeleteTest(t *testing.T) *deleteTestSetup { | ||
featureEnvVars := []string{} | ||
mockCtrl := gomock.NewController(t) | ||
provider := providermocks.NewMockProvider(mockCtrl) | ||
writer := writermocks.NewMockFileWriter(mockCtrl) | ||
manager := mocks.NewMockClusterManager(mockCtrl) | ||
|
||
datacenterConfig := &v1alpha1.VSphereDatacenterConfig{} | ||
machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}} | ||
clusterDeleter := mocks.NewMockClusterDeleter(mockCtrl) | ||
|
||
workload := workload.NewDelete( | ||
provider, | ||
writer, | ||
manager, | ||
clusterDeleter, | ||
) | ||
|
||
for _, e := range featureEnvVars { | ||
t.Setenv(e, "true") | ||
} | ||
|
||
return &deleteTestSetup{ | ||
t: t, | ||
provider: provider, | ||
datacenterConfig: datacenterConfig, | ||
machineConfigs: machineConfigs, | ||
workload: workload, | ||
ctx: context.Background(), | ||
clusterDeleter: clusterDeleter, | ||
clusterSpec: test.NewClusterSpec(func(s *cluster.Spec) { | ||
s.Cluster.Name = "workload" | ||
s.Cluster.Spec.DatacenterRef.Kind = v1alpha1.VSphereDatacenterKind | ||
s.ManagementCluster = &types.Cluster{Name: "management"} | ||
}), | ||
workloadCluster: &types.Cluster{Name: "workload"}, | ||
clusterManager: manager, | ||
writer: writer, | ||
} | ||
} | ||
|
||
func (c *deleteTestSetup) expectSetup(err error) { | ||
c.provider.EXPECT().SetupAndValidateDeleteCluster(c.ctx, c.workloadCluster, c.clusterSpec).Return(err) | ||
} | ||
|
||
func (c *deleteTestSetup) expectDeleteWorkloadCluster(err error) { | ||
c.clusterDeleter.EXPECT().Run(c.ctx, c.clusterSpec, *c.clusterSpec.ManagementCluster).Return(err) | ||
} | ||
|
||
func (c *deleteTestSetup) run() error { | ||
return c.workload.Run(c.ctx, c.workloadCluster, c.clusterSpec) | ||
} | ||
|
||
func (c *deleteTestSetup) expectWrite() { | ||
c.writer.EXPECT().Write(gomock.Any(), gomock.Any(), gomock.Any()).Return("", nil) | ||
} | ||
|
||
func (c *deleteTestSetup) expectSaveLogsWorkload() { | ||
c.clusterManager.EXPECT().SaveLogsWorkloadCluster(c.ctx, c.provider, c.clusterSpec, c.workloadCluster) | ||
c.expectWrite() | ||
} | ||
|
||
func (c *deleteTestSetup) expectCleanup() { | ||
c.writer.EXPECT().CleanUp() | ||
} | ||
|
||
func TestDeleteRunSuccess(t *testing.T) { | ||
features.ClearCache() | ||
os.Setenv(features.UseControllerForCli, "true") | ||
test := newDeleteTest(t) | ||
test.expectSetup(nil) | ||
test.expectDeleteWorkloadCluster(nil) | ||
test.expectCleanup() | ||
|
||
err := test.run() | ||
if err != nil { | ||
t.Fatalf("Delete.Run() err = %v, want err = nil", err) | ||
} | ||
} | ||
|
||
func TestDeleteRunFail(t *testing.T) { | ||
features.ClearCache() | ||
os.Setenv(features.UseControllerForCli, "true") | ||
test := newDeleteTest(t) | ||
test.expectSetup(nil) | ||
test.expectDeleteWorkloadCluster(fmt.Errorf("Failure")) | ||
test.expectSaveLogsWorkload() | ||
|
||
err := test.run() | ||
if err == nil { | ||
t.Fatalf("Delete.Run() err = %v, want err = nil", err) | ||
} | ||
} | ||
|
||
func TestDeleteRunFailSetup(t *testing.T) { | ||
features.ClearCache() | ||
os.Setenv(features.UseControllerForCli, "true") | ||
test := newDeleteTest(t) | ||
test.expectSetup(fmt.Errorf("Failure")) | ||
test.expectWrite() | ||
|
||
err := test.run() | ||
if err == nil { | ||
t.Fatalf("Delete.Run() err = %v, want err = nil", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package workload | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/eks-anywhere/pkg/logger" | ||
"github.com/aws/eks-anywhere/pkg/task" | ||
"github.com/aws/eks-anywhere/pkg/workflows" | ||
) | ||
|
||
type deleteWorkloadCluster struct{} | ||
|
||
func (s *deleteWorkloadCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task { | ||
logger.Info("Deleting workload cluster") | ||
err := commandContext.ClusterDeleter.Run(ctx, commandContext.ClusterSpec, *commandContext.ManagementCluster) | ||
if err != nil { | ||
commandContext.SetError(err) | ||
return &workflows.CollectWorkloadClusterDiagnosticsTask{} | ||
} | ||
|
||
return &postDeleteWorkload{} | ||
} | ||
|
||
func (s *deleteWorkloadCluster) Name() string { | ||
return "delete-workload-cluster" | ||
} | ||
|
||
func (s *deleteWorkloadCluster) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) { | ||
return nil, nil | ||
} | ||
|
||
func (s *deleteWorkloadCluster) Checkpoint() *task.CompletedTask { | ||
return nil | ||
} |
Oops, something went wrong.