Skip to content

Commit

Permalink
Add git ops manager task for workload cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
mitalipaygude committed Feb 9, 2024
1 parent d19c5de commit 848abd7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
34 changes: 34 additions & 0 deletions pkg/workflows/workload/create_gitops.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/cluster"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/task"
)

type installGitOpsManagerTask struct{}

func (s *installGitOpsManagerTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Installing GitOps Toolkit on workload cluster")

managementComponents := cluster.ManagementComponentsFromBundles(commandContext.ClusterSpec.Bundles)
err := commandContext.GitOpsManager.InstallGitOps(ctx, commandContext.WorkloadCluster, managementComponents, commandContext.ClusterSpec, commandContext.Provider.DatacenterConfig(commandContext.ClusterSpec), commandContext.Provider.MachineConfigs(commandContext.ClusterSpec))
if err != nil {
logger.MarkFail("Error when installing GitOps toolkits on workload cluster; EKS-A will continue with cluster creation, but GitOps will not be enabled", "error", err)
}
return &writeClusterConfig{}
}

func (s *installGitOpsManagerTask) Name() string {
return "gitops-manager-install"
}

func (s *installGitOpsManagerTask) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil

Check warning on line 29 in pkg/workflows/workload/create_gitops.go

View check run for this annotation

Codecov / codecov/patch

pkg/workflows/workload/create_gitops.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}

func (s *installGitOpsManagerTask) Checkpoint() *task.CompletedTask {
return nil
}
64 changes: 47 additions & 17 deletions pkg/workflows/workload/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ import (
)

type createTestSetup struct {
t *testing.T
clusterManager *mocks.MockClusterManager
gitOpsManager *mocks.MockGitOpsManager
provider *providermocks.MockProvider
writer *writermocks.MockFileWriter
validator *mocks.MockValidator
eksd *mocks.MockEksdInstaller
packageInstaller *mocks.MockPackageInstaller
clusterCreator *mocks.MockClusterCreator
datacenterConfig providers.DatacenterConfig
machineConfigs []providers.MachineConfig
ctx context.Context
clusterSpec *cluster.Spec
workloadCluster *types.Cluster
workload *workload.Create
t *testing.T
clusterManager *mocks.MockClusterManager
gitOpsManager *mocks.MockGitOpsManager
provider *providermocks.MockProvider
writer *writermocks.MockFileWriter
validator *mocks.MockValidator
eksd *mocks.MockEksdInstaller
packageInstaller *mocks.MockPackageInstaller
clusterCreator *mocks.MockClusterCreator
datacenterConfig providers.DatacenterConfig
machineConfigs []providers.MachineConfig
ctx context.Context
clusterSpec *cluster.Spec
workloadCluster *types.Cluster
workload *workload.Create
managementComponents *cluster.ManagementComponents
}

func newCreateTest(t *testing.T) *createTestSetup {
Expand All @@ -56,6 +57,9 @@ func newCreateTest(t *testing.T) *createTestSetup {

validator := mocks.NewMockValidator(mockCtrl)

clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = "test-cluster" })
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

workload := workload.NewCreate(
provider,
clusterManager,
Expand Down Expand Up @@ -89,7 +93,8 @@ func newCreateTest(t *testing.T) *createTestSetup {
s.Cluster.Spec.DatacenterRef.Kind = v1alpha1.VSphereDatacenterKind
s.ManagementCluster = &types.Cluster{Name: "management"}
}),
workloadCluster: &types.Cluster{Name: "workload"},
workloadCluster: &types.Cluster{Name: "workload"},
managementComponents: managementComponents,
}
}

Expand All @@ -100,7 +105,7 @@ func (c *createTestSetup) expectSetup() {
}

func (c *createTestSetup) expectCreateWorkloadCluster(err error) {
c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.clusterSpec.ManagementCluster).Return(nil, err)
c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.clusterSpec.ManagementCluster).Return(c.workloadCluster, err)
}

func (c *createTestSetup) expectWriteWorkloadClusterConfig(err error) {
Expand Down Expand Up @@ -134,6 +139,11 @@ func (c *createTestSetup) expectSaveLogsManagement() {
c.expectWrite()
}

func (c *createTestSetup) expectInstallGitOpsManager(err error) {
c.gitOpsManager.EXPECT().InstallGitOps(
c.ctx, c.workloadCluster, c.managementComponents, c.clusterSpec, c.datacenterConfig, c.machineConfigs).Return(err)
}

func (c *createTestSetup) expectWrite() {
c.writer.EXPECT().Write(gomock.Any(), gomock.Any(), gomock.Any()).Return("", nil)
}
Expand All @@ -147,6 +157,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectInstallGitOpsManager(nil)
test.expectWriteWorkloadClusterConfig(nil)

err := test.run()
Expand Down Expand Up @@ -188,6 +199,24 @@ func TestCreateRunValidateFail(t *testing.T) {
}
}

func TestCreateRunGitOpsConfigFail(t *testing.T) {
features.ClearCache()
os.Setenv(features.UseControllerForCli, "true")
test := newCreateTest(t)
test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectInstallGitOpsManager(fmt.Errorf("Failure"))
test.expectWriteWorkloadClusterConfig(nil)

err := test.run()
if err != nil {
t.Fatalf("Create.Run() err = %v, want err = nil", err)
}
}

func TestCreateRunWriteClusterConfigFail(t *testing.T) {
features.ClearCache()
os.Setenv(features.UseControllerForCli, "true")
Expand All @@ -197,6 +226,7 @@ func TestCreateRunWriteClusterConfigFail(t *testing.T) {
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectInstallGitOpsManager(nil)
test.expectWriteWorkloadClusterConfig(fmt.Errorf("Failure"))
test.expectWrite()

Expand Down
8 changes: 5 additions & 3 deletions pkg/workflows/workload/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ type createCluster struct{}
// Run createCluster performs actions needed to create the management cluster.
func (c *createCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Creating workload cluster")
if _, err := commandContext.ClusterCreator.CreateSync(ctx, commandContext.ClusterSpec, commandContext.ManagementCluster); err != nil {
workloadCluster, err := commandContext.ClusterCreator.CreateSync(ctx, commandContext.ClusterSpec, commandContext.ManagementCluster)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}
commandContext.WorkloadCluster = workloadCluster

return &writeClusterConfig{}
return &installGitOpsManagerTask{}
}

func (c *createCluster) Name() string {
Expand All @@ -32,5 +34,5 @@ func (c *createCluster) Checkpoint() *task.CompletedTask {
}

func (c *createCluster) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return &writeClusterConfig{}, nil
return &installGitOpsManagerTask{}, nil

Check warning on line 37 in pkg/workflows/workload/createcluster.go

View check run for this annotation

Codecov / codecov/patch

pkg/workflows/workload/createcluster.go#L37

Added line #L37 was not covered by tests
}

0 comments on commit 848abd7

Please sign in to comment.