Skip to content

Commit

Permalink
applier and filewriter struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Jan 5, 2024
1 parent 19ecfd7 commit 3ab1169
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
if err != nil {
return err
}

Check warning on line 290 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L280-L290

Added lines #L280 - L290 were not covered by tests

clusCreator := clustermanager.ClusterCreator{
Applier: deps.ClusterApplier,
FS: deps.Writer,
}

createMgmtCluster := m.NewCreate(
deps.Bootstrapper,
deps.Provider,
Expand All @@ -297,6 +303,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
deps.EksdInstaller,
deps.PackageInstaller,
clusterCreate,
clusCreator,
)

err = createMgmtCluster.Run(ctx, clusterSpec, createValidations, cc.forceClean)

Check warning on line 309 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L292-L309

Added lines #L292 - L309 were not covered by tests
Expand Down
101 changes: 101 additions & 0 deletions pkg/clustermanager/cluster_creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package clustermanager

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"

"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/kubeconfig"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
)

type ClusterCreator struct {

Check warning on line 18 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type ClusterCreator should have comment or be unexported (revive)
Applier Applier
// FS is a file system abstraction providing file creation and write capabilities.
FS filewriter.FileWriter
}

func (cc ClusterCreator) GetKubeconfig(ctx context.Context, clusterSpec *cluster.Spec, managementCluster *types.Cluster) ([]byte, error) {

Check warning on line 24 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method ClusterCreator.GetKubeconfig should have comment or be unexported (revive)
kubeconfigSecret := &corev1.Secret{}

err := retrier.New(
cc.Applier.applyClusterTimeout,
retrier.WithRetryPolicy(retrier.BackOffPolicy(cc.Applier.retryBackOff)),
).Retry(func() error {
client, err := cc.Applier.clientFactory.BuildClientFromKubeconfig(managementCluster.KubeconfigFile)
if err != nil {
return err
}

Check warning on line 34 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L24-L34

Added lines #L24 - L34 were not covered by tests

err = client.Get(ctx, fmt.Sprintf("%s-kubeconfig", clusterSpec.Cluster.Name), constants.EksaSystemNamespace, kubeconfigSecret)

if err != nil {
return err
}

Check warning on line 40 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L36-L40

Added lines #L36 - L40 were not covered by tests

return nil

Check warning on line 42 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L42

Added line #L42 was not covered by tests
})

Check failure on line 44 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return nil, err
}

Check warning on line 47 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L45-L47

Added lines #L45 - L47 were not covered by tests

return kubeconfigSecret.Data["value"], nil

Check warning on line 49 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L49

Added line #L49 was not covered by tests
}

func (cc ClusterCreator) CreateSync(ctx context.Context, spec *cluster.Spec, managementCluster *types.Cluster, provider providers.Provider) (*types.Cluster, error) {

Check warning on line 52 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method ClusterCreator.CreateSync should have comment or be unexported (revive)
err := cc.Applier.Run(ctx, spec, *managementCluster)
if err != nil {
return nil, err
}

Check warning on line 56 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L52-L56

Added lines #L52 - L56 were not covered by tests

return cc.getWorkloadCluster(ctx, spec, managementCluster, provider)

Check warning on line 58 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L58

Added line #L58 was not covered by tests
}

func (cc ClusterCreator) getWorkloadCluster(ctx context.Context, clusterSpec *cluster.Spec, management *types.Cluster, provider providers.Provider) (*types.Cluster, error) {
clusterName := clusterSpec.Cluster.Name

workloadCluster := &types.Cluster{
Name: clusterName,
ExistingManagement: management.ExistingManagement,
}

rawkubeconfig, err := cc.GetKubeconfig(ctx, clusterSpec, management)
if err != nil {
return nil, err
}

Check warning on line 72 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L61-L72

Added lines #L61 - L72 were not covered by tests

kubeconfigPath, err := cc.WriteKubeconfig(rawkubeconfig, clusterName, provider)
if err != nil {
return nil, err
}
workloadCluster.KubeconfigFile = kubeconfigPath

return workloadCluster, nil

Check warning on line 80 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L74-L80

Added lines #L74 - L80 were not covered by tests
}

func (cc ClusterCreator) WriteKubeconfig(rawkubeconfig []byte, clusterName string, provider providers.Provider) (string, error) {

Check warning on line 83 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method ClusterCreator.WriteKubeconfig should have comment or be unexported (revive)
err := provider.UpdateKubeConfig(&rawkubeconfig, clusterName)
if err != nil {
return "", err
}

Check warning on line 87 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L83-L87

Added lines #L83 - L87 were not covered by tests

kubeconfigPath, err := cc.FS.Write(
kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName),
rawkubeconfig,
filewriter.PersistentFile,
filewriter.Permission0600,
)

Check failure on line 95 in pkg/clustermanager/cluster_creator.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return "", err
}

Check warning on line 98 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L89-L98

Added lines #L89 - L98 were not covered by tests

return kubeconfigPath, nil

Check warning on line 100 in pkg/clustermanager/cluster_creator.go

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L100

Added line #L100 was not covered by tests
}
2 changes: 2 additions & 0 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clustermanager"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers"
Expand Down Expand Up @@ -39,6 +40,7 @@ type CommandContext struct {
EksdUpgrader interfaces.EksdUpgrader
ClusterUpgrader interfaces.ClusterUpgrader
ClusterCreate structs.ClusterCreate
ClusterCreator clustermanager.ClusterCreator
CAPIManager interfaces.CAPIManager
ClusterSpec *cluster.Spec
CurrentClusterSpec *cluster.Spec
Expand Down
2 changes: 1 addition & 1 deletion pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type ClusterCreator interface {
Run(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) error
}

// Cluster
// Cluster represents a workload cluster to be created.
type Cluster interface {
WriteKubeconfig(ctx context.Context, w io.Writer, management *types.Cluster) error
WaitUntilControlPlaneAvailable(ctx context.Context, management *types.Cluster) error
Expand Down
5 changes: 5 additions & 0 deletions pkg/workflows/management/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clustermanager"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/task"
Expand All @@ -21,6 +22,7 @@ type Create struct {
eksdInstaller interfaces.EksdInstaller
packageInstaller interfaces.PackageInstaller
clusterCreate structs.ClusterCreate
clusterCreator clustermanager.ClusterCreator
}

// NewCreate builds a new create construct.
Expand All @@ -29,6 +31,7 @@ func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider
writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
clusterCreate structs.ClusterCreate,
clusterCreator clustermanager.ClusterCreator,
) *Create {
return &Create{
bootstrapper: bootstrapper,
Expand All @@ -39,6 +42,7 @@ func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider
eksdInstaller: eksdInstaller,
packageInstaller: packageInstaller,
clusterCreate: clusterCreate,
clusterCreator: clusterCreator,
}

Check warning on line 46 in pkg/workflows/management/create.go

View check run for this annotation

Codecov / codecov/patch

pkg/workflows/management/create.go#L35-L46

Added lines #L35 - L46 were not covered by tests
}

Expand All @@ -55,6 +59,7 @@ func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator i
EksdInstaller: c.eksdInstaller,
PackageInstaller: c.packageInstaller,
ClusterCreate: c.clusterCreate,
ClusterCreator: c.clusterCreator,
}

return task.NewTaskRunner(&setupAndValidateCreate{}, c.writer).RunTask(ctx, commandContext)

Check warning on line 65 in pkg/workflows/management/create.go

View check run for this annotation

Codecov / codecov/patch

pkg/workflows/management/create.go#L50-L65

Added lines #L50 - L65 were not covered by tests
Expand Down
18 changes: 12 additions & 6 deletions pkg/workflows/management/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ func (s *createWorkloadClusterTask) Run(ctx context.Context, commandContext *tas
commandContext.ClusterSpec.Cluster.AddManagedByCLIAnnotation()

logger.Info("Applying cluster spec to bootstrap cluster")
if err := commandContext.ClusterCreate.ClusterCreator.Run(ctx, commandContext.ClusterSpec, *commandContext.BootstrapCluster); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}

workloadCluster, err := commandContext.ClusterCreate.GetWorkloadCluster(ctx, commandContext.ClusterSpec, *&commandContext.BootstrapCluster, commandContext.Provider)
// if err := commandContext.ClusterCreate.ClusterCreator.Run(ctx, commandContext.ClusterSpec, *commandContext.BootstrapCluster); err != nil {
// commandContext.SetError(err)
// return &workflows.CollectMgmtClusterDiagnosticsTask{}
// }

// workloadCluster, err := commandContext.ClusterCreate.GetWorkloadCluster(ctx, commandContext.ClusterSpec, *&commandContext.BootstrapCluster, commandContext.Provider)
// if err != nil {
// commandContext.SetError(err)
// return &workflows.CollectDiagnosticsTask{}
// }

workloadCluster, err := commandContext.ClusterCreator.CreateSync(ctx, commandContext.ClusterSpec, commandContext.BootstrapCluster, commandContext.Provider)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectDiagnosticsTask{}
Expand Down
3 changes: 2 additions & 1 deletion pkg/workflows/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/aws/eks-anywhere/pkg/workflows/interfaces"
)

// ClusterCreate creates a Kubernetes conformant cluster that is immediately usable for simple workloads.
// ClusterCreate creates a Kubernetes cluster that is immediately usable for simple workloads.
type ClusterCreate struct {
Cluster interfaces.Cluster
ClusterCreator interfaces.ClusterCreator
Expand All @@ -22,6 +22,7 @@ type ClusterCreate struct {
FS filewriter.FileWriter
}

// GetWorkloadCluster returns the types.Cluster object for a newly created workload cluster.
func (cc ClusterCreate) GetWorkloadCluster(ctx context.Context, clusterSpec *cluster.Spec, management *types.Cluster, provider providers.Provider) (*types.Cluster, error) {
clusterName := clusterSpec.Cluster.Name

Expand Down

0 comments on commit 3ab1169

Please sign in to comment.