Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Feb 7, 2024
1 parent b61126a commit 87f7ab4
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/eksctl-anywhere/cmd/deletecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (dc *deleteClusterOptions) deleteCluster(ctx context.Context) error {
deleteWorkload := workload.NewDelete(deps.Provider, deps.Writer, deps.ClusterManager, deps.ClusterDeleter)
err = deleteWorkload.Run(ctx, cluster, clusterSpec)
} else if features.UseControllerViaCLIWorkflow().IsActive() && clusterSpec.Cluster.IsSelfManaged() {
deleteManagement := management.NewDelete(deps.Bootstrapper, deps.Provider, deps.Writer, deps.ClusterManager, deps.ClusterDeleter, deps.GitOpsFlux)
deleteManagement := management.NewDelete(deps.Bootstrapper, deps.Provider, deps.Writer, deps.ClusterManager, deps.GitOpsFlux)
err = deleteManagement.Run(ctx, cluster, clusterSpec)

Check warning on line 165 in cmd/eksctl-anywhere/cmd/deletecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/deletecluster.go#L163-L165

Added lines #L163 - L165 were not covered by tests
} else {
err = deleteCluster.Run(ctx, cluster, clusterSpec, dc.forceCleanup, dc.managementKubeconfig)
Expand Down
36 changes: 0 additions & 36 deletions pkg/clustermanager/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (

"github.com/go-logr/logr"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
)
Expand Down Expand Up @@ -101,37 +99,3 @@ func (a Deleter) Run(ctx context.Context, spec *cluster.Spec, managementCluster

return nil
}

// DeleteCAPICluster deletes the CAPI cluster in the bootstrap cluster and waits
// until the changes are fully reconciled.
func (a Deleter) DeleteCAPICluster(ctx context.Context, cluster string, bootstrapCluster types.Cluster) error {
var client kubernetes.Client
a.log.V(3).Info("Deleting cluster spec")
err := retrier.New(
a.deleteClusterTimeout,
retrier.WithRetryPolicy(retrier.BackOffPolicy(a.retryBackOff)),
).Retry(func() error {
var err error
client, err = a.clientFactory.BuildClientFromKubeconfig(bootstrapCluster.KubeconfigFile)
if err != nil {
return errors.Wrap(err, "building client to delete cluster")
}

capiCluster := &v1beta1.Cluster{}

if err := client.Get(ctx, cluster, constants.EksaSystemNamespace, capiCluster); err != nil {
return errors.Wrapf(err, "getting capi cluster")
}

if err := client.Delete(ctx, capiCluster); err != nil {
return errors.Wrapf(err, "deleting cluster")
}

return nil
})
if err != nil {
return err
}

return nil
}
1 change: 0 additions & 1 deletion pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,4 @@ type EksaInstaller interface {
// ClusterDeleter deletes the cluster.
type ClusterDeleter interface {
Run(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) error
DeleteCAPICluster(ctx context.Context, cluster string, bootstrapCluster types.Cluster) error
}
2 changes: 1 addition & 1 deletion pkg/workflows/management/create_delete_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type deleteBootstrapClusterForDeleteTask struct{}

func (s *deleteBootstrapClusterForDeleteTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Deleting bootstrap cluster")
if err := commandContext.Bootstrapper.DeleteBootstrapCluster(ctx, commandContext.BootstrapCluster, constants.Delete, true); err != nil {
if err := commandContext.Bootstrapper.DeleteBootstrapCluster(ctx, commandContext.BootstrapCluster, constants.Delete, false); err != nil {
commandContext.SetError(err)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/workflows/management/create_install_capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func (s *installCAPIComponentsForDeleteTask) Run(ctx context.Context, commandCon
}

logger.Info("Installing cluster-api providers on bootstrap cluster")
if err := commandContext.ClusterManager.InstallCAPI(ctx, commandContext.ClusterSpec, commandContext.BootstrapCluster, commandContext.Provider); err != nil {
managementComponents := cluster.ManagementComponentsFromBundles(commandContext.ClusterSpec.Bundles)
if err := commandContext.ClusterManager.InstallCAPI(ctx, managementComponents, commandContext.ClusterSpec, commandContext.BootstrapCluster, commandContext.Provider); err != nil {
commandContext.SetError(err)
return &deleteManagementCluster{}
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/workflows/management/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Delete struct {
provider providers.Provider
writer filewriter.FileWriter
clusterManager interfaces.ClusterManager
clusterDeleter interfaces.ClusterDeleter
gitopsManager interfaces.GitOpsManager
}

Expand All @@ -26,15 +25,13 @@ func NewDelete(bootstrapper interfaces.Bootstrapper,
provider providers.Provider,
writer filewriter.FileWriter,
clusterManager interfaces.ClusterManager,
clusterDeleter interfaces.ClusterDeleter,
gitopsManager interfaces.GitOpsManager,
) *Delete {
return &Delete{
bootstrapper: bootstrapper,
provider: provider,
writer: writer,
clusterManager: clusterManager,
clusterDeleter: clusterDeleter,
gitopsManager: gitopsManager,
}
}
Expand All @@ -48,7 +45,6 @@ func (c *Delete) Run(ctx context.Context, workload *types.Cluster, clusterSpec *
ClusterManager: c.clusterManager,
ClusterSpec: clusterSpec,
WorkloadCluster: workload,
ClusterDeleter: c.clusterDeleter,
GitOpsManager: c.gitopsManager,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/workflows/management/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type deleteManagementCluster struct{}

func (s *deleteManagementCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Deleting management cluster")
err := commandContext.ClusterDeleter.DeleteCAPICluster(ctx, commandContext.ClusterSpec.Cluster.Name, *commandContext.BootstrapCluster)
err := commandContext.ClusterManager.DeleteCluster(ctx, commandContext.BootstrapCluster, commandContext.WorkloadCluster, commandContext.Provider, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
Expand Down
Loading

0 comments on commit 87f7ab4

Please sign in to comment.