Skip to content

Commit

Permalink
Create management cluster using controller validation task (#7307)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitalipaygude authored Jan 17, 2024
1 parent 413ea04 commit 9d8959c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 12 deletions.
60 changes: 60 additions & 0 deletions pkg/workflows/management/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package management

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/workflows/interfaces"
)

// Create is a schema for create cluster.
type Create struct {
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
gitOpsManager interfaces.GitOpsManager
writer filewriter.FileWriter
eksdInstaller interfaces.EksdInstaller
packageInstaller interfaces.PackageInstaller
clusterCreator interfaces.ClusterCreator
}

// NewCreate builds a new create construct.
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,
) *Create {
return &Create{
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
writer: writer,
eksdInstaller: eksdInstaller,
packageInstaller: packageInstaller,
clusterCreator: clusterCreator,
}
}

// Run runs all the create management cluster tasks.
func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator interfaces.Validator) error {
commandContext := &task.CommandContext{
Bootstrapper: c.bootstrapper,
Provider: c.provider,
ClusterManager: c.clusterManager,
GitOpsManager: c.gitOpsManager,
ClusterSpec: clusterSpec,
Writer: c.writer,
Validations: validator,
EksdInstaller: c.eksdInstaller,
PackageInstaller: c.packageInstaller,
ClusterCreator: c.clusterCreator,
}

return task.NewTaskRunner(&setupAndValidateCreate{}, c.writer).RunTask(ctx, commandContext)
}
117 changes: 117 additions & 0 deletions pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package management_test

import (
"context"
"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/management"
)

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

func newCreateTest(t *testing.T) *createTestSetup {
featureEnvVars := []string{}
featureEnvVars = append(featureEnvVars, features.UseControllerForCli)
mockCtrl := gomock.NewController(t)
bootstrapper := mocks.NewMockBootstrapper(mockCtrl)
clusterManager := mocks.NewMockClusterManager(mockCtrl)
gitOpsManager := mocks.NewMockGitOpsManager(mockCtrl)
provider := providermocks.NewMockProvider(mockCtrl)
writer := writermocks.NewMockFileWriter(mockCtrl)
eksdInstaller := mocks.NewMockEksdInstaller(mockCtrl)
packageInstaller := mocks.NewMockPackageInstaller(mockCtrl)

datacenterConfig := &v1alpha1.VSphereDatacenterConfig{}
machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}}
clusterCreator := mocks.NewMockClusterCreator(mockCtrl)
validator := mocks.NewMockValidator(mockCtrl)

workflow := management.NewCreate(
bootstrapper,
provider,
clusterManager,
gitOpsManager,
writer,
eksdInstaller,
packageInstaller,
clusterCreator,
)

for _, e := range featureEnvVars {
t.Setenv(e, "true")
}

return &createTestSetup{
t: t,
bootstrapper: bootstrapper,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
provider: provider,
writer: writer,
validator: validator,
eksdInstaller: eksdInstaller,
packageInstaller: packageInstaller,
clusterCreator: clusterCreator,
datacenterConfig: datacenterConfig,
machineConfigs: machineConfigs,
workflow: workflow,
ctx: context.Background(),
bootstrapCluster: &types.Cluster{Name: "bootstrap"},
workloadCluster: &types.Cluster{Name: "workload"},
clusterSpec: test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = "cluster-name" }),
}
}

func (c *createTestSetup) expectSetup() {
c.provider.EXPECT().SetupAndValidateCreateCluster(c.ctx, c.clusterSpec)
c.provider.EXPECT().Name()
c.gitOpsManager.EXPECT().Validations(c.ctx, c.clusterSpec)
}

func (c *createTestSetup) run() error {
return c.workflow.Run(c.ctx, c.clusterSpec, c.validator)
}

func (c *createTestSetup) expectPreflightValidationsToPass() {
c.validator.EXPECT().PreflightValidations(c.ctx)
}

func TestCreateRunSuccess(t *testing.T) {
test := newCreateTest(t)
test.expectSetup()
test.expectPreflightValidationsToPass()

err := test.run()
if err != nil {
t.Fatalf("Create.Run() err = %v, want err = nil", err)
}
}
4 changes: 2 additions & 2 deletions pkg/workflows/management/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (c *Upgrade) Run(ctx context.Context, clusterSpec *cluster.Spec, management
ClusterUpgrader: c.clusterUpgrader,
}
if features.IsActive(features.CheckpointEnabled()) {
return task.NewTaskRunner(&setupAndValidate{}, c.writer, task.WithCheckpointFile()).RunTask(ctx, commandContext)
return task.NewTaskRunner(&setupAndValidateUpgrade{}, c.writer, task.WithCheckpointFile()).RunTask(ctx, commandContext)
}

return task.NewTaskRunner(&setupAndValidate{}, c.writer).RunTask(ctx, commandContext)
return task.NewTaskRunner(&setupAndValidateUpgrade{}, c.writer).RunTask(ctx, commandContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func TestRunnerHappyPath(t *testing.T) {

clusterSpec := test.NewClusterSpec()
managementCluster := &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
}

ctx := context.Background()
Expand Down Expand Up @@ -132,8 +132,8 @@ func TestRunnerStopsWhenValidationFailed(t *testing.T) {

clusterSpec := test.NewClusterSpec()
managementCluster := &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
}

ctx := context.Background()
Expand Down
55 changes: 49 additions & 6 deletions pkg/workflows/management/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,53 @@ import (
"github.com/aws/eks-anywhere/pkg/validations"
)

type setupAndValidate struct{}
type setupAndValidateCreate struct{}

// setupAndValidateCreate implementation

func (s *setupAndValidateCreate) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Performing setup and validations")
runner := validations.NewRunner()
runner.Register(s.providerValidation(ctx, commandContext)...)
runner.Register(commandContext.GitOpsManager.Validations(ctx, commandContext.ClusterSpec)...)
runner.Register(commandContext.Validations.PreflightValidations(ctx)...)

err := runner.Run()
if err != nil {
commandContext.SetError(err)
return nil
}

return nil
}

func (s *setupAndValidateCreate) providerValidation(ctx context.Context, commandContext *task.CommandContext) []validations.Validation {
return []validations.Validation{
func() *validations.ValidationResult {
return &validations.ValidationResult{
Name: fmt.Sprintf("%s Provider setup is valid", commandContext.Provider.Name()),
Err: commandContext.Provider.SetupAndValidateCreateCluster(ctx, commandContext.ClusterSpec),
}
},
}
}

func (s *setupAndValidateCreate) Name() string {
return "setup-validate"
}

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

func (s *setupAndValidateCreate) Checkpoint() *task.CompletedTask {
return nil
}

type setupAndValidateUpgrade struct{}

// Run setupAndValidate validates management cluster before upgrade process starts.
func (s *setupAndValidate) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
func (s *setupAndValidateUpgrade) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Performing setup and validations")
currentSpec, err := commandContext.ClusterManager.GetCurrentClusterSpec(ctx, commandContext.ManagementCluster, commandContext.ClusterSpec.Cluster.Name)
if err != nil {
Expand All @@ -33,7 +76,7 @@ func (s *setupAndValidate) Run(ctx context.Context, commandContext *task.Command
return &updateSecrets{}
}

func (s *setupAndValidate) providerValidation(ctx context.Context, commandContext *task.CommandContext) []validations.Validation {
func (s *setupAndValidateUpgrade) providerValidation(ctx context.Context, commandContext *task.CommandContext) []validations.Validation {
return []validations.Validation{
func() *validations.ValidationResult {
return &validations.ValidationResult{
Expand All @@ -44,11 +87,11 @@ func (s *setupAndValidate) providerValidation(ctx context.Context, commandContex
}
}

func (s *setupAndValidate) Name() string {
func (s *setupAndValidateUpgrade) Name() string {
return "setup-and-validate"
}

func (s *setupAndValidate) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
func (s *setupAndValidateUpgrade) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
if err := commandContext.Provider.SetupAndValidateUpgradeCluster(ctx, commandContext.ManagementCluster, commandContext.ClusterSpec, commandContext.CurrentClusterSpec); err != nil {
commandContext.SetError(err)
return nil, err
Expand All @@ -63,7 +106,7 @@ func (s *setupAndValidate) Restore(ctx context.Context, commandContext *task.Com
return &updateSecrets{}, nil
}

func (s *setupAndValidate) Checkpoint() *task.CompletedTask {
func (s *setupAndValidateUpgrade) Checkpoint() *task.CompletedTask {
return &task.CompletedTask{
Checkpoint: nil,
}
Expand Down

0 comments on commit 9d8959c

Please sign in to comment.