Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Jan 17, 2024
1 parent 7016b3b commit 03bbf25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
33 changes: 18 additions & 15 deletions pkg/clustermanager/cluster_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clustermanager_test

import (
"context"
"fmt"
"io"
"os"
"testing"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/aws/eks-anywhere/pkg/clustermanager/mocks"
"github.com/aws/eks-anywhere/pkg/filewriter"
mockswriter "github.com/aws/eks-anywhere/pkg/filewriter/mocks"
"github.com/aws/eks-anywhere/pkg/kubeconfig"
mockskubeconfig "github.com/aws/eks-anywhere/pkg/kubeconfig/mocks"
"github.com/aws/eks-anywhere/pkg/types"
)
Expand All @@ -31,15 +31,17 @@ type clusterCreatorTest struct {
kubeconfigWriter *mockskubeconfig.MockWriter
}

func newClusterCreator(t *testing.T) (*clustermanager.ClusterCreator, *clusterCreatorTest) {
func newClusterCreator(t *testing.T, clusterName string) (*clustermanager.ClusterCreator, *clusterCreatorTest) {
ctrl := gomock.NewController(t)
cct := &clusterCreatorTest{
WithT: NewWithT(t),
applier: mocks.NewMockClusterApplier(ctrl),
writer: mockswriter.NewMockFileWriter(ctrl),
kubeconfigWriter: mockskubeconfig.NewMockWriter(ctrl),
spec: test.NewClusterSpec(),
ctx: context.Background(),
spec: test.NewClusterSpec(func(s *cluster.Spec) {
s.Cluster.Name = clusterName
}),
ctx: context.Background(),
mgmtCluster: &types.Cluster{
KubeconfigFile: "my-config",
},
Expand All @@ -50,26 +52,27 @@ func newClusterCreator(t *testing.T) (*clustermanager.ClusterCreator, *clusterCr
return cc, cct
}

func (cct *clusterCreatorTest) expectFileCreate(clusterName, path string, w io.WriteCloser) {
cct.writer.EXPECT().Create(kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName), gomock.AssignableToTypeOf([]filewriter.FileOptionsFunc{})).Return(w, path, nil)
func (cct *clusterCreatorTest) expectFileCreate(fileName, path string, w io.WriteCloser) {
cct.writer.EXPECT().Create(fileName, gomock.AssignableToTypeOf([]filewriter.FileOptionsFunc{})).Return(w, path, nil)
}

func (cct *clusterCreatorTest) expectWriteKubeconfig(ctx context.Context, clusterName, kubeconfig string, w io.Writer) {
cct.kubeconfigWriter.EXPECT().WriteKubeconfig(ctx, clusterName, kubeconfig, w).Return(nil)
func (cct *clusterCreatorTest) expectWriteKubeconfig(clusterName string, w io.Writer) {
cct.kubeconfigWriter.EXPECT().WriteKubeconfig(cct.ctx, clusterName, cct.mgmtCluster.KubeconfigFile, w).Return(nil)
}

func (cct *clusterCreatorTest) expectApplierRun(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) {
cct.applier.EXPECT().Run(ctx, spec, managementCluster).Return(nil)
func (cct *clusterCreatorTest) expectApplierRun() {
cct.applier.EXPECT().Run(cct.ctx, cct.spec, *cct.mgmtCluster).Return(nil)
}

func TestClusterCreatorCreateSync(t *testing.T) {
clusCreator, tt := newClusterCreator(t)
clusterName := tt.spec.Cluster.Name
clusterName := "testCluster"
clusCreator, tt := newClusterCreator(t, clusterName)
path := "testpath"
writer := os.NewFile(uintptr(*pointer.Uint(0)), "test")
tt.expectApplierRun(tt.ctx, tt.spec, *tt.mgmtCluster)
tt.expectWriteKubeconfig(tt.ctx, clusterName, tt.mgmtCluster.KubeconfigFile, writer)
tt.expectFileCreate(clusterName, path, writer)
tt.expectApplierRun()
tt.expectWriteKubeconfig(clusterName, writer)
fileName := fmt.Sprintf("%s-eks-a-cluster.kubeconfig", clusterName)
tt.expectFileCreate(fileName, path, writer)
_, err := clusCreator.CreateSync(tt.ctx, tt.spec, tt.mgmtCluster)
tt.Expect(err).To(BeNil())
}
5 changes: 4 additions & 1 deletion pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,17 @@ func (f *Factory) WithProvider(clusterConfigFile string, clusterConfig *v1alpha1
// WithKubeconfigWriter adds the KubeconfigReader dependency depending on the provider.
func (f *Factory) WithKubeconfigWriter(clusterConfig *v1alpha1.Cluster) *Factory {
f.WithUnAuthKubeClient()
if clusterConfig.Spec.DatacenterRef.Kind == v1alpha1.DockerDatacenterKind {
f.WithDocker()
}

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.dependencies.KubeconfigWriter != nil {
return nil
}
writer := kubeconfig.NewClusterAPIKubeconfigSecretWriter(f.dependencies.UnAuthKubeClient)
switch clusterConfig.Spec.DatacenterRef.Kind {
case v1alpha1.DockerDatacenterKind:
f.WithDocker()
f.dependencies.KubeconfigWriter = docker.NewKubeconfigWriter(f.dependencies.DockerClient, writer)
default:
f.dependencies.KubeconfigWriter = writer
Expand Down

0 comments on commit 03bbf25

Please sign in to comment.