-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add clustercreator and kubeconfig writer
- Loading branch information
Showing
22 changed files
with
735 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package clustermanager | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/eks-anywhere/pkg/cluster" | ||
"github.com/aws/eks-anywhere/pkg/filewriter" | ||
"github.com/aws/eks-anywhere/pkg/kubeconfig" | ||
"github.com/aws/eks-anywhere/pkg/types" | ||
) | ||
|
||
// ClusterApplier is responsible for applying the cluster spec to the cluster. | ||
type ClusterApplier interface { | ||
Run(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) error | ||
} | ||
|
||
// ClusterCreator is responsible for applying the cluster config and writing the kubeconfig file. | ||
type ClusterCreator struct { | ||
ClusterApplier | ||
kubeconfigWriter kubeconfig.Writer | ||
fs filewriter.FileWriter | ||
} | ||
|
||
// NewClusterCreator creates a ClusterCreator. | ||
func NewClusterCreator(applier ClusterApplier, kubeconfigWriter kubeconfig.Writer, fs filewriter.FileWriter) *ClusterCreator { | ||
return &ClusterCreator{ | ||
ClusterApplier: applier, | ||
kubeconfigWriter: kubeconfigWriter, | ||
fs: fs, | ||
} | ||
} | ||
|
||
// CreateSync creates a workload cluster using the EKS-A controller and returns the types.Cluster object for that cluster. | ||
func (cc ClusterCreator) CreateSync(ctx context.Context, spec *cluster.Spec, managementCluster *types.Cluster) (*types.Cluster, error) { | ||
if err := cc.Run(ctx, spec, *managementCluster); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cc.buildClusterAccess(ctx, spec.Cluster.Name, managementCluster) | ||
} | ||
|
||
func (cc ClusterCreator) buildClusterAccess(ctx context.Context, clusterName string, management *types.Cluster) (*types.Cluster, error) { | ||
cluster := &types.Cluster{ | ||
Name: clusterName, | ||
} | ||
|
||
fsOptions := []filewriter.FileOptionsFunc{filewriter.PersistentFile, filewriter.Permission0600} | ||
fh, path, err := cc.fs.Create( | ||
kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName), | ||
fsOptions..., | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer fh.Close() | ||
|
||
err = cc.kubeconfigWriter.WriteKubeconfig(ctx, clusterName, management.KubeconfigFile, fh) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cluster.KubeconfigFile = path | ||
|
||
return cluster, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package clustermanager_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/utils/pointer" | ||
|
||
"github.com/aws/eks-anywhere/internal/test" | ||
"github.com/aws/eks-anywhere/pkg/cluster" | ||
"github.com/aws/eks-anywhere/pkg/clustermanager" | ||
"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" | ||
) | ||
|
||
type clusterCreatorTest struct { | ||
*WithT | ||
ctx context.Context | ||
spec *cluster.Spec | ||
mgmtCluster *types.Cluster | ||
applier *mocks.MockClusterApplier | ||
writer *mockswriter.MockFileWriter | ||
kubeconfigWriter *mockskubeconfig.MockWriter | ||
} | ||
|
||
func newClusterCreator(t *testing.T) (*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(), | ||
mgmtCluster: &types.Cluster{ | ||
KubeconfigFile: "my-config", | ||
}, | ||
} | ||
|
||
cc := clustermanager.NewClusterCreator(cct.applier, cct.kubeconfigWriter, cct.writer) | ||
|
||
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) expectWriteKubeconfig(ctx context.Context, clusterName, kubeconfig string, w io.Writer) { | ||
cct.kubeconfigWriter.EXPECT().WriteKubeconfig(ctx, clusterName, kubeconfig, 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 TestClusterCreatorCreateSync(t *testing.T) { | ||
clusCreator, tt := newClusterCreator(t) | ||
clusterName := tt.spec.Cluster.Name | ||
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) | ||
_, err := clusCreator.CreateSync(tt.ctx, tt.spec, tt.mgmtCluster) | ||
tt.Expect(err).To(BeNil()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.