Skip to content

Commit

Permalink
Create cluster namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Feb 13, 2024
1 parent 43ef11f commit b096714
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
5 changes: 5 additions & 0 deletions pkg/clustermanager/cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,11 @@ func (c *ClusterManager) DeletePackageResources(ctx context.Context, managementC
return c.clusterClient.DeletePackageResources(ctx, managementCluster, clusterName)
}

// CreateNamespace creates a namespace on the target cluster if it does not already exist.
func (c *ClusterManager) CreateNamespace(ctx context.Context, targetCluster *types.Cluster, namespace string) error {
return c.clusterClient.CreateNamespaceIfNotPresent(ctx, targetCluster.KubeconfigFile, namespace)
}

func (c *ClusterManager) getUpgraderImagesFromBundle(ctx context.Context, cluster *types.Cluster, cl *cluster.Spec) (*corev1.ConfigMap, error) {
upgraderImages := make(map[string]string)
for _, versionBundle := range cl.Bundles.Spec.VersionsBundles {
Expand Down
1 change: 1 addition & 0 deletions pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ClusterManager interface {
GenerateIamAuthKubeconfig(ctx context.Context, management, workload *types.Cluster, spec *cluster.Spec) error
DeletePackageResources(ctx context.Context, managementCluster *types.Cluster, clusterName string) error
CreateRegistryCredSecret(ctx context.Context, mgmt *types.Cluster) error
CreateNamespace(ctx context.Context, targetCluster *types.Cluster, namespace string) error
}

type GitOpsManager interface {
Expand Down
14 changes: 14 additions & 0 deletions pkg/workflows/interfaces/mocks/clients.go

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

32 changes: 17 additions & 15 deletions pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ func (c *createTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3,
)
}

func (c *createTestSetup) expectCreateWorkload(err1, err2, err3, err4 error) {
func (c *createTestSetup) expectCreateWorkload(err1, err2, err3, err4, err5 error) {
gomock.InOrder(
c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.bootstrapCluster).Return(c.workloadCluster, err1),
c.clusterManager.EXPECT().CreateNamespace(c.ctx, c.bootstrapCluster, c.clusterSpec.Cluster.Namespace).Return(err1),

c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.bootstrapCluster).Return(c.workloadCluster, err2),

c.clusterManager.EXPECT().CreateEKSANamespace(
c.ctx, c.workloadCluster).Return(err2),
c.ctx, c.workloadCluster).Return(err3),

c.clusterManager.EXPECT().InstallCAPI(
c.ctx, c.managementComponents, c.clusterSpec, c.workloadCluster, c.provider).Return(err3),
c.ctx, c.managementComponents, c.clusterSpec, c.workloadCluster, c.provider).Return(err4),

c.provider.EXPECT().UpdateSecrets(
c.ctx, c.workloadCluster, c.clusterSpec).Return(err4),
c.ctx, c.workloadCluster, c.clusterSpec).Return(err5),
)
}

Expand Down Expand Up @@ -259,7 +261,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand Down Expand Up @@ -576,7 +578,7 @@ func TestCreateUpdateSecretsFailure(t *testing.T) {
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectPreflightValidationsToPass()

test.expectCreateWorkload(nil, nil, nil, errors.New("test"))
test.expectCreateWorkload(nil, nil, nil, nil, errors.New("test"))

test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
test.clusterManager.EXPECT().SaveLogsWorkloadCluster(test.ctx, test.provider, test.clusterSpec, test.workloadCluster)
Expand All @@ -596,7 +598,7 @@ func TestCreatePostWorkloadInitFailure(t *testing.T) {
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil, nil)

c.expectInstallResourcesOnManagementTask(fmt.Errorf("test"))

Expand All @@ -618,7 +620,7 @@ func TestCreateMoveCAPIFailure(t *testing.T) {
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil, nil)
c.expectInstallResourcesOnManagementTask(nil)
c.expectPauseReconcile(nil)
c.expectMoveManagement(errors.New("test"))
Expand All @@ -641,7 +643,7 @@ func TestPauseReconcilerFailure(t *testing.T) {
c.expectPreflightValidationsToPass()
c.expectCAPIInstall(nil, nil, nil)
c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil)
c.expectCreateWorkload(nil, nil, nil, nil, nil)
c.expectInstallResourcesOnManagementTask(nil)
c.expectPauseReconcile(errors.New("test"))

Expand All @@ -663,7 +665,7 @@ func TestCreateEKSAWorkloadComponentsFailure(t *testing.T) {
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand All @@ -688,7 +690,7 @@ func TestCreateEKSAWorkloadFailure(t *testing.T) {
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand All @@ -711,7 +713,7 @@ func TestCreateGitOPsFailure(t *testing.T) {
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand Down Expand Up @@ -744,7 +746,7 @@ func TestCreateWriteConfigFailure(t *testing.T) {
test.expectCreateBootstrap()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand Down Expand Up @@ -784,7 +786,7 @@ func TestCreateRunDeleteBootstrapFailure(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectCAPIInstall(nil, nil, nil)
test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil)
test.expectCreateWorkload(nil, nil, nil, nil, nil)
test.expectInstallResourcesOnManagementTask(nil)
test.expectPauseReconcile(nil)
test.expectMoveManagement(nil)
Expand Down
5 changes: 5 additions & 0 deletions pkg/workflows/management/create_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (s *createWorkloadClusterTask) Run(ctx context.Context, commandContext *tas
commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation()
commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)

if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.BootstrapCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}

workloadCluster, err := commandContext.ClusterCreator.CreateSync(ctx, commandContext.ClusterSpec, commandContext.BootstrapCluster)
if err != nil {
commandContext.SetError(err)
Expand Down
13 changes: 7 additions & 6 deletions pkg/workflows/workload/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func (c *createTestSetup) expectSetup() {
c.gitOpsManager.EXPECT().Validations(c.ctx, c.clusterSpec)
}

func (c *createTestSetup) expectCreateWorkloadCluster(err error) {
c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.clusterSpec.ManagementCluster).Return(c.workloadCluster, err)
func (c *createTestSetup) expectCreateWorkloadCluster(err1, err2 error) {
c.clusterManager.EXPECT().CreateNamespace(c.ctx, c.clusterSpec.ManagementCluster, c.clusterSpec.Cluster.Namespace).Return(err1)
c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.clusterSpec.ManagementCluster).Return(c.workloadCluster, err2)
}

func (c *createTestSetup) expectWriteWorkloadClusterConfig(err error) {
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectCreateWorkloadCluster(nil, nil)
test.expectInstallGitOpsManager(nil)
test.expectWriteWorkloadClusterConfig(nil)

Expand All @@ -174,7 +175,7 @@ func TestCreateRunFail(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(fmt.Errorf("Failure"))
test.expectCreateWorkloadCluster(nil, fmt.Errorf("Failure"))
test.expectSaveLogsManagement()

err := test.run()
Expand Down Expand Up @@ -207,7 +208,7 @@ func TestCreateRunGitOpsConfigFail(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectCreateWorkloadCluster(nil, nil)
test.expectInstallGitOpsManager(fmt.Errorf("Failure"))
test.expectWriteWorkloadClusterConfig(nil)

Expand All @@ -225,7 +226,7 @@ func TestCreateRunWriteClusterConfigFail(t *testing.T) {
test.expectPreflightValidationsToPass()
test.expectDatacenterConfig()
test.expectMachineConfigs()
test.expectCreateWorkloadCluster(nil)
test.expectCreateWorkloadCluster(nil, nil)
test.expectInstallGitOpsManager(nil)
test.expectWriteWorkloadClusterConfig(fmt.Errorf("Failure"))
test.expectWrite()
Expand Down
6 changes: 6 additions & 0 deletions pkg/workflows/workload/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ 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.ClusterManager.CreateNamespace(ctx, commandContext.ManagementCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}

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

0 comments on commit b096714

Please sign in to comment.