From 03bbf258bf52b687bcd16f4d1931502f8762edc1 Mon Sep 17 00:00:00 2001 From: Tanvir Tatla Date: Wed, 17 Jan 2024 13:08:21 -0800 Subject: [PATCH] address pr comments --- pkg/clustermanager/cluster_creator_test.go | 33 ++++++++++++---------- pkg/dependencies/factory.go | 5 +++- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/clustermanager/cluster_creator_test.go b/pkg/clustermanager/cluster_creator_test.go index 8428745b05c07..45c14b87bf51b 100644 --- a/pkg/clustermanager/cluster_creator_test.go +++ b/pkg/clustermanager/cluster_creator_test.go @@ -2,6 +2,7 @@ package clustermanager_test import ( "context" + "fmt" "io" "os" "testing" @@ -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" ) @@ -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", }, @@ -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()) } diff --git a/pkg/dependencies/factory.go b/pkg/dependencies/factory.go index 9337f33feec0c..11fc883c123b7 100644 --- a/pkg/dependencies/factory.go +++ b/pkg/dependencies/factory.go @@ -585,6 +585,10 @@ 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 @@ -592,7 +596,6 @@ func (f *Factory) WithKubeconfigWriter(clusterConfig *v1alpha1.Cluster) *Factory 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