Skip to content

Commit

Permalink
use client factory to create namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Feb 14, 2024
1 parent 253ac63 commit e65577d
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions pkg/workflows/create_prep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package workflows

import (
"context"

"github.com/aws/eks-anywhere/pkg/workflows/interfaces"

Check failure on line 6 in pkg/workflows/create_prep.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with -s standard,default,prefix(github.com/aws/eks-anywhere) (gci)
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Check failure on line 9 in pkg/workflows/create_prep.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with -s standard,default,prefix(github.com/aws/eks-anywhere) (gci)
)

// 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
}
106 changes: 106 additions & 0 deletions pkg/workflows/create_prep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package workflows_test

import (
"context"
"fmt"
"testing"

Check failure on line 7 in pkg/workflows/create_prep_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with -s standard,default,prefix(github.com/aws/eks-anywhere) (gci)
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"

Check failure on line 16 in pkg/workflows/create_prep_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with -s standard,default,prefix(github.com/aws/eks-anywhere) (gci)
)

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)

Check failure on line 57 in pkg/workflows/create_prep_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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")
}
}
6 changes: 5 additions & 1 deletion pkg/workflows/management/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/workflows/management/create_install_eksa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
38 changes: 32 additions & 6 deletions pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -91,6 +96,7 @@ func newCreateTest(t *testing.T) *createTestSetup {
return &createTestSetup{
t: t,
bootstrapper: bootstrapper,
clientFactory: clientFactory,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
provider: provider,
Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -279,6 +291,7 @@ func TestCreateRunSuccess(t *testing.T) {
test.expectWriteClusterConfig()
test.expectDeleteBootstrap(nil)
test.expectCuratedPackagesInstallation()
test.expectCreateNamespace()

err := test.run()
if err != nil {
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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"))

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"))

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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())

Expand Down
Loading

0 comments on commit e65577d

Please sign in to comment.