Skip to content

Commit

Permalink
Separate kubeconfig operation from cluster manager
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Jan 5, 2024
1 parent 7c8b62f commit 65dcb88
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 5 deletions.
10 changes: 9 additions & 1 deletion cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
}
createValidations := createvalidations.New(validationOpts)

clusCreator := clustermanager.ClusterCreator{
Applier: deps.ClusterApplier,
KM: clustermanager.KubeconfigManager{
Client: deps.UnAuthKubeClient.KubeconfigClient(mgmt.KubeconfigFile),
FS: deps.Writer,
},
}

if features.UseNewWorkflows().IsActive() {
deps, err = factory.
WithCNIInstaller(clusterSpec, deps.Provider).
Expand Down Expand Up @@ -263,9 +271,9 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
deps.ClusterManager,
deps.GitOpsFlux,
deps.Writer,
deps.ClusterApplier,
deps.EksdInstaller,
deps.PackageInstaller,
clusCreator,
)
err = createWorkloadCluster.Run(ctx, clusterSpec, createValidations)

Expand Down
48 changes: 48 additions & 0 deletions pkg/clustermanager/cluster_creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package clustermanager

import (
"bytes"
"context"

"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/types"
)

type ClusterCreator struct {

Check warning on line 12 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
KM KubeconfigManager
}

// 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, provider providers.Provider) (*types.Cluster, error) {
err := cc.Applier.Run(ctx, spec, *managementCluster)
if err != nil {
return nil, err
}

return cc.getWorkloadCluster(ctx, spec.Cluster.Name, managementCluster, provider)
}

func (cc ClusterCreator) getWorkloadCluster(ctx context.Context, clusterName string, management *types.Cluster, provider providers.Provider) (*types.Cluster, error) {
workloadCluster := &types.Cluster{
Name: clusterName,
ExistingManagement: management.ExistingManagement,
}

// Use a buffer to cache the kubeconfig.
var buf bytes.Buffer

err := cc.KM.GetKubeconfig(ctx, clusterName, &buf)
if err != nil {
return nil, err
}

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

return workloadCluster, nil
}
73 changes: 73 additions & 0 deletions pkg/clustermanager/kubeconfig_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package clustermanager

import (
"bytes"
"context"
"fmt"
"io"
"time"

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

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"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"
)

type KubeconfigManager struct {

Check warning on line 20 in pkg/clustermanager/kubeconfig_manager.go

View workflow job for this annotation

GitHub Actions / lint

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

// GetKubeconfig retrieves the contents of the specified cluster's kubeconfig from a secret and copies it to an io.Writer.
func (km KubeconfigManager) GetKubeconfig(ctx context.Context, clusterName string, w io.Writer) error {
kubeconfigSecret := &corev1.Secret{}

err := retrier.New(
time.Minute,
retrier.WithRetryPolicy(retrier.BackOffPolicy(time.Second)),
).Retry(func() error {
err := km.Client.Get(ctx, fmt.Sprintf("%s-kubeconfig", clusterName), constants.EksaSystemNamespace, kubeconfigSecret)

Check failure on line 35 in pkg/clustermanager/kubeconfig_manager.go

View workflow job for this annotation

GitHub Actions / lint

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

return nil
})

Check failure on line 42 in pkg/clustermanager/kubeconfig_manager.go

View workflow job for this annotation

GitHub Actions / lint

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

if _, err := io.Copy(w, bytes.NewReader(kubeconfigSecret.Data["value"])); err != nil {
return err
}

return nil
}

// WriteKubeconfig takes a raw binary kubeconfig in memory, writes the kubeconfig to a file on disk, and returns the path of the kubeconfig file.
func (km KubeconfigManager) WriteKubeconfig(rawkubeconfig []byte, clusterName string, provider providers.Provider) (string, error) {
err := provider.UpdateKubeConfig(&rawkubeconfig, clusterName)
if err != nil {
return "", err
}

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

Check failure on line 67 in pkg/clustermanager/kubeconfig_manager.go

View workflow job for this annotation

GitHub Actions / lint

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

return kubeconfigPath, nil
}
3 changes: 2 additions & 1 deletion 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 @@ -37,7 +38,7 @@ type CommandContext struct {
PackageInstaller interfaces.PackageInstaller
EksdUpgrader interfaces.EksdUpgrader
ClusterUpgrader interfaces.ClusterUpgrader
ClusterCreator interfaces.ClusterCreator
ClusterCreator clustermanager.ClusterCreator
CAPIManager interfaces.CAPIManager
ClusterSpec *cluster.Spec
CurrentClusterSpec *cluster.Spec
Expand Down
5 changes: 3 additions & 2 deletions pkg/workflows/workload/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 @@ -17,17 +18,17 @@ type Create struct {
gitOpsManager interfaces.GitOpsManager
writer filewriter.FileWriter
eksdInstaller interfaces.EksdInstaller
clusterCreator interfaces.ClusterCreator
clusterCreator clustermanager.ClusterCreator
packageInstaller interfaces.PackageInstaller
}

// NewCreate builds a new create construct.
func NewCreate(provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter,
clusterCreator interfaces.ClusterCreator,
eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
clusterCreator clustermanager.ClusterCreator,
) *Create {
return &Create{
provider: provider,
Expand Down
2 changes: 1 addition & 1 deletion pkg/workflows/workload/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type createCluster struct{}
// Run createCluster performs actions needed to create the management cluster.
func (c *createCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Creating workload cluster")
if err := commandContext.ClusterCreator.Run(ctx, commandContext.ClusterSpec, *commandContext.ManagementCluster); err != nil {
if err := commandContext.ClusterCreator.Applier.Run(ctx, commandContext.ClusterSpec, *commandContext.ManagementCluster); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}
Expand Down

0 comments on commit 65dcb88

Please sign in to comment.