diff --git a/cmd/eksctl-anywhere/cmd/createcluster.go b/cmd/eksctl-anywhere/cmd/createcluster.go index a8672a5df8bb6..ab0cfbeaf7276 100644 --- a/cmd/eksctl-anywhere/cmd/createcluster.go +++ b/cmd/eksctl-anywhere/cmd/createcluster.go @@ -270,6 +270,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er deps.EksdInstaller, deps.PackageInstaller, deps.ClusterCreator, + deps.UnAuthKubeClient, ) err = createWorkloadCluster.Run(ctx, clusterSpec, createValidations) @@ -278,6 +279,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er createMgmtCluster := management.NewCreate( deps.Bootstrapper, + deps.UnAuthKubeClient, deps.Provider, deps.ClusterManager, deps.GitOpsFlux, diff --git a/pkg/workflows/create_prep.go b/pkg/workflows/create_prep.go new file mode 100644 index 0000000000000..a13b64b24853e --- /dev/null +++ b/pkg/workflows/create_prep.go @@ -0,0 +1,34 @@ +package workflows + +import ( + "context" + + "github.com/aws/eks-anywhere/pkg/workflows/interfaces" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// CreateNamespaceIfNotPresent creates the namespace on the cluster if it does not already exist. +func CreateNamespaceIfNotPresent(ctx context.Context, namespace, kubeconfig string, clientFactory interfaces.ClientFactory) error { + client, err := clientFactory.BuildClientFromKubeconfig(kubeconfig) + if err != nil { + return err + } + + if err := client.Get(ctx, namespace, "", &corev1.Namespace{}); err != nil && !errors.IsNotFound(err) { + return err + } + + ns := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + }, + } + + if err = client.Create(ctx, ns); err != nil { + return err + } + + return nil +} diff --git a/pkg/workflows/create_prep_test.go b/pkg/workflows/create_prep_test.go new file mode 100644 index 0000000000000..94914c616cfb3 --- /dev/null +++ b/pkg/workflows/create_prep_test.go @@ -0,0 +1,106 @@ +package workflows_test + +import ( + "context" + "fmt" + "testing" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + + clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks" + "github.com/aws/eks-anywhere/pkg/workflows" + "github.com/aws/eks-anywhere/pkg/workflows/interfaces/mocks" + "github.com/golang/mock/gomock" +) + +type createPrepTestSetup struct { + t *testing.T + ctx context.Context + client *clientmocks.MockClient + clientFactory *mocks.MockClientFactory +} + +func newCreatePrepTest(t *testing.T) *createPrepTestSetup { + mockCtrl := gomock.NewController(t) + client := clientmocks.NewMockClient(mockCtrl) + clientFactory := mocks.NewMockClientFactory(mockCtrl) + + return &createPrepTestSetup{ + t: t, + ctx: context.Background(), + client: client, + clientFactory: clientFactory, + } +} + +func newNamespace(name string) *corev1.Namespace { + return &corev1.Namespace{ + ObjectMeta: v1.ObjectMeta{ + Name: name, + }, + } +} + +func TestCreateNamespaceNotExists(t *testing.T) { + test := newCreatePrepTest(t) + kubeconfig := "testpath" + namespace := "test-ns" + + test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(errors.NewNotFound(schema.GroupResource{}, "")) + test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(nil) + + err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) + + if err != nil { + t.Fatalf("Expected nil, but got %v", err) + } +} + +func TestCreateNamespaceBuildClientFail(t *testing.T) { + test := newCreatePrepTest(t) + kubeconfig := "testpath" + namespace := "test-ns" + + test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, fmt.Errorf("")) + + err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) + + if err == nil { + t.Fatalf("Expected error, but got nil") + } +} + +func TestCreateNamespaceGetNamespaceFail(t *testing.T) { + test := newCreatePrepTest(t) + kubeconfig := "testpath" + namespace := "test-ns" + + test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(fmt.Errorf("")) + + err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) + + if err == nil { + t.Fatalf("Expected error, but got nil") + } +} + +func TestCreateNamespaceFail(t *testing.T) { + test := newCreatePrepTest(t) + kubeconfig := "testpath" + namespace := "test-ns" + + test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}) + test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(fmt.Errorf("")) + + err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) + + if err == nil { + t.Fatalf("Expected error, but got nil") + } +} diff --git a/pkg/workflows/management/create.go b/pkg/workflows/management/create.go index 9dbc5ecfcc784..9b8e4ab60bb39 100644 --- a/pkg/workflows/management/create.go +++ b/pkg/workflows/management/create.go @@ -13,6 +13,7 @@ import ( // Create is a schema for create cluster. type Create struct { bootstrapper interfaces.Bootstrapper + clientFactory interfaces.ClientFactory provider providers.Provider clusterManager interfaces.ClusterManager gitOpsManager interfaces.GitOpsManager @@ -24,7 +25,8 @@ type Create struct { } // NewCreate builds a new create construct. -func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider, +func NewCreate(bootstrapper interfaces.Bootstrapper, + clientFactory interfaces.ClientFactory, provider providers.Provider, clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager, writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller, packageInstaller interfaces.PackageInstaller, @@ -33,6 +35,7 @@ func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider ) *Create { return &Create{ bootstrapper: bootstrapper, + clientFactory: clientFactory, provider: provider, clusterManager: clusterManager, gitOpsManager: gitOpsManager, @@ -48,6 +51,7 @@ func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator interfaces.Validator) error { commandContext := &task.CommandContext{ Bootstrapper: c.bootstrapper, + ClientFactory: c.clientFactory, Provider: c.provider, ClusterManager: c.clusterManager, GitOpsManager: c.gitOpsManager, diff --git a/pkg/workflows/management/create_install_eksa.go b/pkg/workflows/management/create_install_eksa.go index 1c0b2e36ae48c..6c8b4d61f01fc 100644 --- a/pkg/workflows/management/create_install_eksa.go +++ b/pkg/workflows/management/create_install_eksa.go @@ -50,7 +50,7 @@ func (s *installEksaComponentsOnWorkloadTask) Run(ctx context.Context, commandCo commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version) if commandContext.ClusterSpec.Cluster.Namespace != "" { - if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.WorkloadCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil { + if err := workflows.CreateNamespaceIfNotPresent(ctx, commandContext.ClusterSpec.Cluster.Namespace, commandContext.WorkloadCluster.KubeconfigFile, commandContext.ClientFactory); err != nil { commandContext.SetError(err) return &workflows.CollectMgmtClusterDiagnosticsTask{} } diff --git a/pkg/workflows/management/create_test.go b/pkg/workflows/management/create_test.go index fe85f20220ab9..c6cdb22738e64 100644 --- a/pkg/workflows/management/create_test.go +++ b/pkg/workflows/management/create_test.go @@ -7,6 +7,8 @@ import ( "testing" "github.com/golang/mock/gomock" + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/aws/eks-anywhere/internal/test" "github.com/aws/eks-anywhere/pkg/api/v1alpha1" @@ -44,6 +46,7 @@ type createTestSetup struct { workloadCluster *types.Cluster workflow *management.Create client *clientmocks.MockClient + clientFactory *mocks.MockClientFactory } func newCreateTest(t *testing.T) *createTestSetup { @@ -65,9 +68,11 @@ func newCreateTest(t *testing.T) *createTestSetup { clusterCreator := mocks.NewMockClusterCreator(mockCtrl) validator := mocks.NewMockValidator(mockCtrl) client := clientmocks.NewMockClient(mockCtrl) + clientFactory := mocks.NewMockClientFactory(mockCtrl) workflow := management.NewCreate( bootstrapper, + clientFactory, provider, clusterManager, gitOpsManager, @@ -91,6 +96,7 @@ func newCreateTest(t *testing.T) *createTestSetup { return &createTestSetup{ t: t, bootstrapper: bootstrapper, + clientFactory: clientFactory, clusterManager: clusterManager, gitOpsManager: gitOpsManager, provider: provider, @@ -176,7 +182,7 @@ func (c *createTestSetup) expectInstallEksaComponentsBootstrap(err1, err2, err3, func (c *createTestSetup) expectCreateWorkload(err1, err2, err3, err4, err5, err6 error) { gomock.InOrder( - c.clusterManager.EXPECT().CreateNamespace(c.ctx, c.bootstrapCluster, c.clusterSpec.Cluster.Namespace).Return(err1), + c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, err1), c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.bootstrapCluster).Return(c.workloadCluster, err2), @@ -223,7 +229,7 @@ func (c *createTestSetup) expectInstallEksaComponentsWorkload(err1, err2, err3 e c.eksdInstaller.EXPECT().InstallEksdManifest( c.ctx, c.clusterSpec, c.workloadCluster), - c.clusterManager.EXPECT().CreateNamespace(c.ctx, c.workloadCluster, c.clusterSpec.Cluster.Namespace).Return(err3), + c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, err3), c.clusterCreator.EXPECT().Run(c.ctx, c.clusterSpec, *c.workloadCluster).Return(err2), ) @@ -255,6 +261,12 @@ func (c *createTestSetup) expectWriteClusterConfig() { ) } +func (c *createTestSetup) expectCreateNamespace() { + ns := c.clusterSpec.Cluster.Namespace + c.client.EXPECT().Get(c.ctx, ns, "", &corev1.Namespace{}).MaxTimes(2) + c.client.EXPECT().Create(c.ctx, &corev1.Namespace{ObjectMeta: v1.ObjectMeta{Name: ns}}).MaxTimes(2) +} + func (c *createTestSetup) expectDeleteBootstrap(err error) { c.bootstrapper.EXPECT().DeleteBootstrapCluster(c.ctx, c.bootstrapCluster, gomock.Any(), gomock.Any()).Return(err) } @@ -279,6 +291,7 @@ func TestCreateRunSuccess(t *testing.T) { test.expectWriteClusterConfig() test.expectDeleteBootstrap(nil) test.expectCuratedPackagesInstallation() + test.expectCreateNamespace() err := test.run() if err != nil { @@ -521,7 +534,8 @@ func TestCreateSyncFailure(t *testing.T) { test.expectCAPIInstall(nil, nil, nil) test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) - test.clusterManager.EXPECT().CreateNamespace(test.ctx, test.bootstrapCluster, test.clusterSpec.Cluster.Namespace).Return(nil) + test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.bootstrapCluster.KubeconfigFile).Return(test.client, nil) + test.expectCreateNamespace() 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) test.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", test.clusterSpec.Cluster.Name), gomock.Any()) @@ -540,7 +554,8 @@ func TestCreateEKSANamespaceFailure(t *testing.T) { test.expectCAPIInstall(nil, nil, nil) test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) - test.clusterManager.EXPECT().CreateNamespace(test.ctx, test.bootstrapCluster, test.clusterSpec.Cluster.Namespace).Return(nil) + test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.bootstrapCluster.KubeconfigFile).Return(test.client, nil) + test.expectCreateNamespace() 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")) test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster) @@ -561,7 +576,8 @@ func TestCreateInstallCAPIWorkloadFailure(t *testing.T) { test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) test.expectPreflightValidationsToPass() - test.clusterManager.EXPECT().CreateNamespace(test.ctx, test.bootstrapCluster, test.clusterSpec.Cluster.Namespace).Return(nil) + test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.bootstrapCluster.KubeconfigFile).Return(test.client, nil) + test.expectCreateNamespace() test.clusterCreator.EXPECT().CreateSync( test.ctx, test.clusterSpec, test.bootstrapCluster).Return(test.workloadCluster, nil) @@ -590,6 +606,7 @@ func TestCreateUpdateSecretsFailure(t *testing.T) { test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) test.expectPreflightValidationsToPass() + test.expectCreateNamespace() test.expectCreateWorkload(nil, nil, nil, nil, nil, errors.New("test")) test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster) @@ -611,6 +628,7 @@ func TestCreatePostWorkloadInitFailure(t *testing.T) { c.expectCAPIInstall(nil, nil, nil) c.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) c.expectCreateWorkload(nil, nil, nil, nil, nil, nil) + c.expectCreateNamespace() c.expectInstallResourcesOnManagementTask(fmt.Errorf("test")) @@ -636,6 +654,7 @@ func TestCreateMoveCAPIFailure(t *testing.T) { c.expectInstallResourcesOnManagementTask(nil) c.expectPauseReconcile(nil) c.expectMoveManagement(errors.New("test")) + c.expectCreateNamespace() c.clusterManager.EXPECT().SaveLogsManagementCluster(c.ctx, c.clusterSpec, c.bootstrapCluster) c.clusterManager.EXPECT().SaveLogsWorkloadCluster(c.ctx, c.provider, c.clusterSpec, c.workloadCluster) @@ -658,6 +677,7 @@ func TestPauseReconcilerFailure(t *testing.T) { c.expectCreateWorkload(nil, nil, nil, nil, nil, nil) c.expectInstallResourcesOnManagementTask(nil) c.expectPauseReconcile(errors.New("test")) + c.expectCreateNamespace() c.clusterManager.EXPECT().SaveLogsManagementCluster(c.ctx, c.clusterSpec, c.bootstrapCluster) c.clusterManager.EXPECT().SaveLogsWorkloadCluster(c.ctx, c.provider, c.clusterSpec, c.workloadCluster) @@ -681,6 +701,7 @@ func TestCreateEKSAWorkloadComponentsFailure(t *testing.T) { test.expectInstallResourcesOnManagementTask(nil) test.expectPauseReconcile(nil) test.expectMoveManagement(nil) + test.expectCreateNamespace() test.eksdInstaller.EXPECT().InstallEksdCRDs(test.ctx, test.clusterSpec, test.workloadCluster).Return(fmt.Errorf("test")) @@ -707,6 +728,7 @@ func TestCreateEKSAWorkloadFailure(t *testing.T) { test.expectPauseReconcile(nil) test.expectMoveManagement(nil) test.expectInstallEksaComponentsWorkload(nil, fmt.Errorf("test"), nil) + test.expectCreateNamespace() test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster) @@ -726,6 +748,7 @@ func TestCreateEKSAWorkloadNamespaceFailure(t *testing.T) { test.expectCAPIInstall(nil, nil, nil) test.expectInstallEksaComponentsBootstrap(nil, nil, nil, nil) test.expectCreateWorkload(nil, nil, nil, nil, nil, nil) + test.expectCreateNamespace() test.expectInstallResourcesOnManagementTask(nil) test.expectPauseReconcile(nil) test.expectMoveManagement(nil) @@ -742,7 +765,7 @@ func TestCreateEKSAWorkloadNamespaceFailure(t *testing.T) { test.eksdInstaller.EXPECT().InstallEksdManifest( test.ctx, test.clusterSpec, test.workloadCluster), - test.clusterManager.EXPECT().CreateNamespace(test.ctx, test.workloadCluster, test.clusterSpec.Cluster.Namespace).Return(fmt.Errorf("")), + test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.workloadCluster.KubeconfigFile).Return(test.client, fmt.Errorf("")), ) test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster) @@ -767,6 +790,7 @@ func TestCreateGitOPsFailure(t *testing.T) { test.expectPauseReconcile(nil) test.expectMoveManagement(nil) test.expectInstallEksaComponentsWorkload(nil, nil, nil) + test.expectCreateNamespace() gomock.InOrder( test.provider.EXPECT().DatacenterConfig( @@ -802,6 +826,7 @@ func TestCreateWriteConfigFailure(t *testing.T) { test.expectInstallEksaComponentsWorkload(nil, nil, nil) test.expectInstallGitOpsManager() test.expectPreflightValidationsToPass() + test.expectCreateNamespace() gomock.InOrder( test.provider.EXPECT().DatacenterConfig( @@ -844,6 +869,7 @@ func TestCreateRunDeleteBootstrapFailure(t *testing.T) { test.expectWriteClusterConfig() test.expectDeleteBootstrap(fmt.Errorf("test")) test.expectCuratedPackagesInstallation() + test.expectCreateNamespace() test.writer.EXPECT().Write("test-cluster-checkpoint.yaml", gomock.Any(), gomock.Any()) diff --git a/pkg/workflows/management/create_workload.go b/pkg/workflows/management/create_workload.go index e2e6b4174f829..9093c72d5cc3f 100644 --- a/pkg/workflows/management/create_workload.go +++ b/pkg/workflows/management/create_workload.go @@ -19,7 +19,7 @@ func (s *createWorkloadClusterTask) Run(ctx context.Context, commandContext *tas commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version) if commandContext.ClusterSpec.Cluster.Namespace != "" { - if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.BootstrapCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil { + if err := workflows.CreateNamespaceIfNotPresent(ctx, commandContext.ClusterSpec.Cluster.Namespace, commandContext.BootstrapCluster.KubeconfigFile, commandContext.ClientFactory); err != nil { commandContext.SetError(err) return &workflows.CollectMgmtClusterDiagnosticsTask{} } diff --git a/pkg/workflows/workload/create.go b/pkg/workflows/workload/create.go index 73f6c20e4fe50..219a173a3d12f 100644 --- a/pkg/workflows/workload/create.go +++ b/pkg/workflows/workload/create.go @@ -12,6 +12,7 @@ import ( // Create is the workflow that creates a workload clusters. type Create struct { + clientFactory interfaces.ClientFactory provider providers.Provider clusterManager interfaces.ClusterManager gitOpsManager interfaces.GitOpsManager @@ -28,6 +29,7 @@ func NewCreate(provider providers.Provider, eksdInstaller interfaces.EksdInstaller, packageInstaller interfaces.PackageInstaller, clusterCreator interfaces.ClusterCreator, + clientFactory interfaces.ClientFactory, ) *Create { return &Create{ provider: provider, @@ -37,12 +39,14 @@ func NewCreate(provider providers.Provider, eksdInstaller: eksdInstaller, clusterCreator: clusterCreator, packageInstaller: packageInstaller, + clientFactory: clientFactory, } } // Run executes the tasks to create a workload cluster. func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator interfaces.Validator) error { commandContext := &task.CommandContext{ + ClientFactory: c.clientFactory, Provider: c.provider, ClusterManager: c.clusterManager, GitOpsManager: c.gitOpsManager, diff --git a/pkg/workflows/workload/create_test.go b/pkg/workflows/workload/create_test.go index 2a490e5b4854c..4fd955696c714 100644 --- a/pkg/workflows/workload/create_test.go +++ b/pkg/workflows/workload/create_test.go @@ -8,9 +8,12 @@ import ( "testing" "github.com/golang/mock/gomock" + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/aws/eks-anywhere/internal/test" "github.com/aws/eks-anywhere/pkg/api/v1alpha1" + clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks" "github.com/aws/eks-anywhere/pkg/cluster" "github.com/aws/eks-anywhere/pkg/features" writermocks "github.com/aws/eks-anywhere/pkg/filewriter/mocks" @@ -38,6 +41,8 @@ type createTestSetup struct { workloadCluster *types.Cluster workload *workload.Create managementComponents *cluster.ManagementComponents + client *clientmocks.MockClient + clientFactory *mocks.MockClientFactory } func newCreateTest(t *testing.T) *createTestSetup { @@ -54,6 +59,8 @@ func newCreateTest(t *testing.T) *createTestSetup { datacenterConfig := &v1alpha1.VSphereDatacenterConfig{} machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}} clusterUpgrader := mocks.NewMockClusterCreator(mockCtrl) + client := clientmocks.NewMockClient(mockCtrl) + clientFactory := mocks.NewMockClientFactory(mockCtrl) validator := mocks.NewMockValidator(mockCtrl) @@ -70,6 +77,7 @@ func newCreateTest(t *testing.T) *createTestSetup { eksdInstaller, packageInstaller, clusterUpgrader, + clientFactory, ) for _, e := range featureEnvVars { @@ -98,6 +106,8 @@ func newCreateTest(t *testing.T) *createTestSetup { }), workloadCluster: &types.Cluster{Name: "workload"}, managementComponents: managementComponents, + clientFactory: clientFactory, + client: client, } } @@ -107,9 +117,16 @@ func (c *createTestSetup) expectSetup() { c.gitOpsManager.EXPECT().Validations(c.ctx, c.clusterSpec) } +func (c *createTestSetup) expectCreateNamespace() { + ns := c.clusterSpec.Cluster.Namespace + c.client.EXPECT().Get(c.ctx, ns, "", &corev1.Namespace{}).MaxTimes(2) + c.client.EXPECT().Create(c.ctx, &corev1.Namespace{ObjectMeta: v1.ObjectMeta{Name: ns}}).MaxTimes(2) +} + func (c *createTestSetup) expectCreateWorkloadCluster(err1, err2 error) { - c.clusterManager.EXPECT().CreateNamespace(c.ctx, c.clusterSpec.ManagementCluster, c.clusterSpec.Cluster.Namespace).Return(err1) + c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.clusterSpec.ManagementCluster.KubeconfigFile).Return(c.client, err1) c.clusterCreator.EXPECT().CreateSync(c.ctx, c.clusterSpec, c.clusterSpec.ManagementCluster).Return(c.workloadCluster, err2) + c.expectCreateNamespace() } func (c *createTestSetup) expectWriteWorkloadClusterConfig(err error) { @@ -195,7 +212,7 @@ func TestCreateNamespaceFail(t *testing.T) { test.expectPreflightValidationsToPass() test.expectDatacenterConfig() test.expectMachineConfigs() - test.clusterManager.EXPECT().CreateNamespace(test.ctx, test.clusterSpec.ManagementCluster, test.clusterSpec.Cluster.Namespace).Return(fmt.Errorf("")) + test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.clusterSpec.ManagementCluster.KubeconfigFile).Return(test.client, fmt.Errorf("")) test.expectSaveLogsManagement() err := test.run() diff --git a/pkg/workflows/workload/createcluster.go b/pkg/workflows/workload/createcluster.go index f8028b950c88e..32a60b65e3ba8 100644 --- a/pkg/workflows/workload/createcluster.go +++ b/pkg/workflows/workload/createcluster.go @@ -15,7 +15,7 @@ func (c *createCluster) Run(ctx context.Context, commandContext *task.CommandCon logger.Info("Creating workload cluster") if commandContext.ClusterSpec.Cluster.Namespace != "" { - if err := commandContext.ClusterManager.CreateNamespace(ctx, commandContext.ManagementCluster, commandContext.ClusterSpec.Cluster.Namespace); err != nil { + if err := workflows.CreateNamespaceIfNotPresent(ctx, commandContext.ClusterSpec.Cluster.Namespace, commandContext.ManagementCluster.KubeconfigFile, commandContext.ClientFactory); err != nil { commandContext.SetError(err) return &workflows.CollectMgmtClusterDiagnosticsTask{} }