Skip to content

Commit

Permalink
Remove client factory dependency (#7490)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitalipaygude authored Feb 6, 2024
1 parent 44b2242 commit 82c22c5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 113 deletions.
1 change: 0 additions & 1 deletion cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
logger.Info("Using the new workflow using the controller for management cluster create")

createMgmtCluster := management.NewCreate(
deps.ClusterManager.ClientFactory,
deps.Bootstrapper,
deps.Provider,
deps.ClusterManager,
Expand Down
5 changes: 1 addition & 4 deletions pkg/workflows/management/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

// Create is a schema for create cluster.
type Create struct {
clientFactory interfaces.ClientFactory
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
Expand All @@ -25,15 +24,14 @@ type Create struct {
}

// NewCreate builds a new create construct.
func NewCreate(clientFactory interfaces.ClientFactory, bootstrapper interfaces.Bootstrapper, provider providers.Provider,
func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
clusterCreator interfaces.ClusterCreator,
eksaInstaller interfaces.EksaInstaller,
) *Create {
return &Create{
clientFactory: clientFactory,
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
Expand All @@ -59,7 +57,6 @@ func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator i
EksdInstaller: c.eksdInstaller,
PackageInstaller: c.packageInstaller,
ClusterCreator: c.clusterCreator,
ClientFactory: c.clientFactory,
EksaInstaller: c.eksaInstaller,
}

Expand Down
23 changes: 3 additions & 20 deletions pkg/workflows/management/create_install_eksa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package management
import (
"context"

"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/logger"
"github.com/aws/eks-anywhere/pkg/task"
"github.com/aws/eks-anywhere/pkg/types"
Expand Down Expand Up @@ -48,6 +46,9 @@ func (s *installEksaComponentsOnWorkloadTask) Run(ctx context.Context, commandCo
return &workflows.CollectDiagnosticsTask{}
}

commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation()
commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)

logger.Info("Applying cluster spec to workload cluster")
if err = commandContext.ClusterCreator.Run(ctx, commandContext.ClusterSpec, *commandContext.WorkloadCluster); err != nil {
commandContext.SetError(err)
Expand Down Expand Up @@ -93,23 +94,5 @@ func installEKSAComponents(ctx context.Context, commandContext *task.CommandCont
return err
}

client, err := commandContext.ClientFactory.BuildClientFromKubeconfig(targetCluster.KubeconfigFile)
if err != nil {
commandContext.SetError(err)
return err
}

commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation()

commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)
if err := client.ApplyServerSide(ctx,
constants.EKSACLIFieldManager,
commandContext.ClusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
); err != nil {
commandContext.SetError(err)
return err
}

return nil
}
102 changes: 14 additions & 88 deletions pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"github.com/aws/eks-anywhere/internal/test"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/bootstrapper"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/features"
writermocks "github.com/aws/eks-anywhere/pkg/filewriter/mocks"
"github.com/aws/eks-anywhere/pkg/logger"
Expand All @@ -36,7 +34,6 @@ type createTestSetup struct {
validator *mocks.MockValidator
eksdInstaller *mocks.MockEksdInstaller
eksaInstaller *mocks.MockEksaInstaller
clientFactory *mocks.MockClientFactory
clusterCreator *mocks.MockClusterCreator
datacenterConfig providers.DatacenterConfig
machineConfigs []providers.MachineConfig
Expand All @@ -62,7 +59,6 @@ func newCreateTest(t *testing.T) *createTestSetup {
eksaInstaller := mocks.NewMockEksaInstaller(mockCtrl)

packageInstaller := mocks.NewMockPackageInstaller(mockCtrl)
clientFactory := mocks.NewMockClientFactory(mockCtrl)

datacenterConfig := &v1alpha1.VSphereDatacenterConfig{}
machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}}
Expand All @@ -71,7 +67,6 @@ func newCreateTest(t *testing.T) *createTestSetup {
client := clientmocks.NewMockClient(mockCtrl)

workflow := management.NewCreate(
clientFactory,
bootstrapper,
provider,
clusterManager,
Expand Down Expand Up @@ -111,7 +106,6 @@ func newCreateTest(t *testing.T) *createTestSetup {
managementComponents: managementComponents,
clusterSpec: clusterSpec,
client: client,
clientFactory: clientFactory,
}
}

Expand Down Expand Up @@ -155,7 +149,7 @@ func (c *createTestSetup) expectCAPIInstall(err1, err2, err3 error) {
)
}

func (c *createTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3, err4, err5, err6 error) {
func (c *createTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3, err4 error) {
gomock.InOrder(

c.eksdInstaller.EXPECT().InstallEksdCRDs(c.ctx, c.clusterSpec, c.bootstrapCluster).Return(err1),
Expand All @@ -168,12 +162,6 @@ func (c *createTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3,

c.eksdInstaller.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.bootstrapCluster).Return(err4),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(
c.bootstrapCluster.KubeconfigFile).Return(c.client, err5),

c.client.EXPECT().ApplyServerSide(
c.ctx, constants.EKSACLIFieldManager, c.clusterSpec.Cluster, kubernetes.ApplyServerSideOptions{ForceOwnership: true}).Return(err6),
)
}

Expand Down Expand Up @@ -222,12 +210,6 @@ func (c *createTestSetup) expectInstallEksaComponentsWorkload(err1, err2 error)
c.eksdInstaller.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.workloadCluster),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(
c.workloadCluster.KubeconfigFile).Return(c.client, nil),

c.client.EXPECT().ApplyServerSide(
c.ctx, constants.EKSACLIFieldManager, c.clusterSpec.Cluster, kubernetes.ApplyServerSideOptions{ForceOwnership: true}),

c.clusterCreator.EXPECT().Run(c.ctx, c.clusterSpec, *c.workloadCluster).Return(err2),
)
}
Expand Down Expand Up @@ -272,7 +254,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand Down Expand Up @@ -498,69 +480,13 @@ func TestCreateInstallEksdManifestFailure(t *testing.T) {
}
}

func TestCreateBuildClientFailure(t *testing.T) {
c := newCreateTest(t)
c.expectSetup()
c.expectCreateBootstrap()
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)

err := errors.New("test")

c.eksdInstaller.EXPECT().InstallEksdCRDs(c.ctx, c.clusterSpec, c.bootstrapCluster)

c.eksaInstaller.EXPECT().Install(
c.ctx, logger.Get(), c.bootstrapCluster, c.managementComponents, c.clusterSpec)

c.provider.EXPECT().InstallCustomProviderComponents(
c.ctx, c.bootstrapCluster.KubeconfigFile)

c.eksdInstaller.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.bootstrapCluster)

c.clientFactory.EXPECT().BuildClientFromKubeconfig(
c.bootstrapCluster.KubeconfigFile).Return(nil, err)

c.clusterManager.EXPECT().SaveLogsManagementCluster(c.ctx, c.clusterSpec, c.bootstrapCluster)
c.clusterManager.EXPECT().SaveLogsWorkloadCluster(c.ctx, c.provider, c.clusterSpec, nil)

c.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", c.clusterSpec.Cluster.Name), gomock.Any())

err = c.run()
if err == nil {
t.Fatalf("Create.Run() expected to return an error %v", err)
}
}

func TestCreateApplyServersideFailure(t *testing.T) {
c := newCreateTest(t)
c.expectSetup()
c.expectCreateBootstrap()
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)

err := errors.New("test")

c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, err)

c.clusterManager.EXPECT().SaveLogsManagementCluster(c.ctx, c.clusterSpec, c.bootstrapCluster)
c.clusterManager.EXPECT().SaveLogsWorkloadCluster(c.ctx, c.provider, c.clusterSpec, nil)

c.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", c.clusterSpec.Cluster.Name), gomock.Any())

err = c.run()
if err == nil {
t.Fatalf("Create.Run() expected to return an error %v", err)
}
}

func TestCreateSyncFailure(t *testing.T) {
test := newCreateTest(t)
test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)

test.clusterCreator.EXPECT().CreateSync(test.ctx, test.clusterSpec, test.bootstrapCluster).Return(nil, errors.New("test"))
test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
Expand All @@ -578,7 +504,7 @@ func TestCreateEKSANamespaceFailure(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)

test.clusterCreator.EXPECT().CreateSync(test.ctx, test.clusterSpec, test.bootstrapCluster).Return(test.workloadCluster, nil)
test.clusterManager.EXPECT().CreateEKSANamespace(test.ctx, test.workloadCluster).Return(errors.New("test"))
Expand All @@ -597,7 +523,7 @@ func TestCreateInstallCAPIWorkloadFailure(t *testing.T) {
test.expectSetup()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectPreflightValidationsToPass()

test.clusterCreator.EXPECT().CreateSync(
Expand Down Expand Up @@ -625,7 +551,7 @@ func TestCreateUpdateSecretsFailure(t *testing.T) {
test.expectSetup()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectPreflightValidationsToPass()

test.expectCreateWorkload(nil, nil, nil, errors.New("test"))
Expand All @@ -647,7 +573,7 @@ func TestCreatePostWorkloadInitFailure(t *testing.T) {
c.expectCreateBootstrap()
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)

c.expectInstallResourcesOnManagementTask(fmt.Errorf("test"))
Expand All @@ -669,7 +595,7 @@ func TestCreateMoveCAPIFailure(t *testing.T) {
c.expectCreateBootstrap()
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)
c.expectInstallResourcesOnManagementTask(nil)
c.expectPauseReconcile(nil)
Expand All @@ -692,7 +618,7 @@ func TestPauseReconcilerFailure(t *testing.T) {
c.expectCreateBootstrap()
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)
c.expectInstallResourcesOnManagementTask(nil)
c.expectPauseReconcile(errors.New("test"))
Expand All @@ -714,7 +640,7 @@ func TestCreateEKSAWorkloadComponentsFailure(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand All @@ -739,7 +665,7 @@ func TestCreateEKSAWorkloadFailure(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand All @@ -762,7 +688,7 @@ func TestCreateGitOPsFailure(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand Down Expand Up @@ -792,7 +718,7 @@ func TestCreateWriteConfigFailure(t *testing.T) {
test.expectSetup()
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand Down Expand Up @@ -832,7 +758,7 @@ func TestCreateRunDeleteBootstrapFailure(t *testing.T) {
test.expectCreateBootstrap()
test.expectPreflightValidationsToPass()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
Expand Down
3 changes: 3 additions & 0 deletions pkg/workflows/management/create_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type createWorkloadClusterTask struct{}
func (s *createWorkloadClusterTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Creating new workload cluster")

commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation()
commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)

workloadCluster, err := commandContext.ClusterCreator.CreateSync(ctx, commandContext.ClusterSpec, commandContext.BootstrapCluster)
if err != nil {
commandContext.SetError(err)
Expand Down

0 comments on commit 82c22c5

Please sign in to comment.