From bf6c9164e337de4058dfbff2bb0a7950c664589d Mon Sep 17 00:00:00 2001 From: Mitali Paygude Date: Wed, 14 Feb 2024 11:39:13 -0800 Subject: [PATCH] Create namespace if specified (#7575) --- .../management/create_install_eksa.go | 8 ++-- pkg/workflows/management/create_test.go | 39 +++++++++++-------- pkg/workflows/management/create_workload.go | 8 ++-- pkg/workflows/workload/create_test.go | 5 ++- pkg/workflows/workload/createcluster.go | 8 ++-- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/pkg/workflows/management/create_install_eksa.go b/pkg/workflows/management/create_install_eksa.go index 9971ea426b3c..1c0b2e36ae48 100644 --- a/pkg/workflows/management/create_install_eksa.go +++ b/pkg/workflows/management/create_install_eksa.go @@ -49,9 +49,11 @@ func (s *installEksaComponentsOnWorkloadTask) Run(ctx context.Context, commandCo commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation() commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version) - if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.WorkloadCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil { - commandContext.SetError(err) - return &workflows.CollectMgmtClusterDiagnosticsTask{} + if commandContext.ClusterSpec.Cluster.Namespace != "" { + if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.WorkloadCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil { + commandContext.SetError(err) + return &workflows.CollectMgmtClusterDiagnosticsTask{} + } } logger.Info("Applying cluster spec to workload cluster") diff --git a/pkg/workflows/management/create_test.go b/pkg/workflows/management/create_test.go index 1143f9070cd1..2065d4f726b3 100644 --- a/pkg/workflows/management/create_test.go +++ b/pkg/workflows/management/create_test.go @@ -82,26 +82,31 @@ func newCreateTest(t *testing.T) *createTestSetup { t.Setenv(e, "true") } - clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = "test-cluster" }) + clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { + s.Cluster.Name = "test-cluster" + s.Cluster.Namespace = "test-ns" + }) managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles) return &createTestSetup{ - t: t, - bootstrapper: bootstrapper, - clusterManager: clusterManager, - gitOpsManager: gitOpsManager, - provider: provider, - writer: writer, - validator: validator, - eksdInstaller: eksdInstaller, - eksaInstaller: eksaInstaller, - packageInstaller: packageInstaller, - clusterCreator: clusterCreator, - datacenterConfig: datacenterConfig, - machineConfigs: machineConfigs, - workflow: workflow, - ctx: context.Background(), - bootstrapCluster: &types.Cluster{Name: "test-cluster"}, + t: t, + bootstrapper: bootstrapper, + clusterManager: clusterManager, + gitOpsManager: gitOpsManager, + provider: provider, + writer: writer, + validator: validator, + eksdInstaller: eksdInstaller, + eksaInstaller: eksaInstaller, + packageInstaller: packageInstaller, + clusterCreator: clusterCreator, + datacenterConfig: datacenterConfig, + machineConfigs: machineConfigs, + workflow: workflow, + ctx: context.Background(), + bootstrapCluster: &types.Cluster{ + Name: "test-cluster", + }, workloadCluster: &types.Cluster{}, managementComponents: managementComponents, clusterSpec: clusterSpec, diff --git a/pkg/workflows/management/create_workload.go b/pkg/workflows/management/create_workload.go index b469d5a04bae..2ef1645b88a2 100644 --- a/pkg/workflows/management/create_workload.go +++ b/pkg/workflows/management/create_workload.go @@ -18,9 +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{} + if commandContext.ClusterSpec.Cluster.Namespace != "" { + 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) diff --git a/pkg/workflows/workload/create_test.go b/pkg/workflows/workload/create_test.go index 1e52319c294b..2a490e5b4854 100644 --- a/pkg/workflows/workload/create_test.go +++ b/pkg/workflows/workload/create_test.go @@ -57,7 +57,9 @@ func newCreateTest(t *testing.T) *createTestSetup { validator := mocks.NewMockValidator(mockCtrl) - clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = "test-cluster" }) + clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { + s.Cluster.Name = "test-cluster" + }) managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles) workload := workload.NewCreate( @@ -90,6 +92,7 @@ func newCreateTest(t *testing.T) *createTestSetup { clusterCreator: clusterUpgrader, clusterSpec: test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = "workload" + s.Cluster.Namespace = "test-ns" s.Cluster.Spec.DatacenterRef.Kind = v1alpha1.VSphereDatacenterKind s.ManagementCluster = &types.Cluster{Name: "management"} }), diff --git a/pkg/workflows/workload/createcluster.go b/pkg/workflows/workload/createcluster.go index 2a08beda8586..f8028b950c88 100644 --- a/pkg/workflows/workload/createcluster.go +++ b/pkg/workflows/workload/createcluster.go @@ -14,9 +14,11 @@ type createCluster struct{} 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{} + if commandContext.ClusterSpec.Cluster.Namespace != "" { + 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)