Skip to content

Commit

Permalink
Unify delete workload (#7417)
Browse files Browse the repository at this point in the history
* Add delete workload using controller workflow

* cleanup cluster directory

* fix TestDeleteRunSuccess
  • Loading branch information
tatlat authored Feb 5, 2024
1 parent 3672cc4 commit 3555997
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ mocks: ## Generate mocks
${MOCKGEN} -destination=pkg/bootstrapper/mocks/bootstrapper.go -package=mocks "github.com/aws/eks-anywhere/pkg/bootstrapper" ClusterClient
${MOCKGEN} -destination=pkg/git/providers/github/mocks/github.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/providers/github" GithubClient
${MOCKGEN} -destination=pkg/git/mocks/git.go -package=mocks "github.com/aws/eks-anywhere/pkg/git" Client,ProviderClient
${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,ClientFactory,EksaInstaller
${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,ClientFactory,EksaInstaller,ClusterDeleter
${MOCKGEN} -destination=pkg/git/gogithub/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gogithub" Client
${MOCKGEN} -destination=pkg/git/gitclient/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gitclient" GoGit
${MOCKGEN} -destination=pkg/validations/mocks/docker.go -package=mocks "github.com/aws/eks-anywhere/pkg/validations" DockerExecutable
Expand Down
10 changes: 9 additions & 1 deletion cmd/eksctl-anywhere/cmd/deletecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"github.com/spf13/cobra"

"github.com/aws/eks-anywhere/pkg/dependencies"
"github.com/aws/eks-anywhere/pkg/features"
"github.com/aws/eks-anywhere/pkg/kubeconfig"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers/tinkerbell/hardware"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/validations"
"github.com/aws/eks-anywhere/pkg/workflows"
"github.com/aws/eks-anywhere/pkg/workflows/workload"
)

type deleteClusterOptions struct {
Expand Down Expand Up @@ -121,6 +123,7 @@ func (dc *deleteClusterOptions) deleteCluster(ctx context.Context) error {
WithGitOpsFlux(clusterSpec.Cluster, clusterSpec.FluxConfig, cliConfig).
WithWriter().
WithDeleteClusterDefaulter(deleteCLIConfig).
WithClusterDeleter().
Build(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -153,7 +156,12 @@ func (dc *deleteClusterOptions) deleteCluster(ctx context.Context) error {
}
}

err = deleteCluster.Run(ctx, cluster, clusterSpec, dc.forceCleanup, dc.managementKubeconfig)
if features.UseControllerViaCLIWorkflow().IsActive() && clusterSpec.Cluster.IsManaged() {
deleteWorkload := workload.NewDelete(deps.Provider, deps.Writer, deps.ClusterManager, deps.ClusterDeleter)
err = deleteWorkload.Run(ctx, cluster, clusterSpec)
} else {
err = deleteCluster.Run(ctx, cluster, clusterSpec, dc.forceCleanup, dc.managementKubeconfig)
}
cleanup(deps, &err)
return err
}
1 change: 1 addition & 0 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type CommandContext struct {
EksdUpgrader interfaces.EksdUpgrader
ClusterUpgrader interfaces.ClusterUpgrader
ClusterCreator interfaces.ClusterCreator
ClusterDeleter interfaces.ClusterDeleter
CAPIManager interfaces.CAPIManager
ClusterSpec *cluster.Spec
CurrentClusterSpec *cluster.Spec
Expand Down
12 changes: 12 additions & 0 deletions pkg/workflows/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,28 @@ func (s *CollectDiagnosticsTask) Checkpoint() *task.CompletedTask {

// CollectWorkloadClusterDiagnosticsTask implementation

// Run starts collecting the logs for workload cluster diagnostics.
func (s *CollectWorkloadClusterDiagnosticsTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("collecting workload cluster diagnostics")
_ = commandContext.ClusterManager.SaveLogsWorkloadCluster(ctx, commandContext.Provider, commandContext.ClusterSpec, commandContext.WorkloadCluster)
return nil
}

// Name returns the name of CollectWorkloadClusterDiagnosticsTask.
func (s *CollectWorkloadClusterDiagnosticsTask) Name() string {
return "collect-workload-cluster-diagnostics"
}

// Restore restores from CollectWorkloadClusterDiagnosticsTask.
func (s *CollectWorkloadClusterDiagnosticsTask) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}

// Checkpoint sets a checkpoint at CollectWorkloadClusterDiagnosticsTask.
func (s *CollectWorkloadClusterDiagnosticsTask) Checkpoint() *task.CompletedTask {
return nil
}

// CollectMgmtClusterDiagnosticsTask implementation

func (s *CollectMgmtClusterDiagnosticsTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
Expand Down
5 changes: 5 additions & 0 deletions pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ type ClusterCreator interface {
type EksaInstaller interface {
Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, managementComponents *cluster.ManagementComponents, spec *cluster.Spec) error
}

// ClusterDeleter deletes the cluster.
type ClusterDeleter interface {
Run(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) error
}
39 changes: 38 additions & 1 deletion pkg/workflows/interfaces/mocks/clients.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions pkg/workflows/workload/delete.go
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)
}
142 changes: 142 additions & 0 deletions pkg/workflows/workload/delete_test.go
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)
}
}
34 changes: 34 additions & 0 deletions pkg/workflows/workload/deletecluster.go
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
}
Loading

0 comments on commit 3555997

Please sign in to comment.