Skip to content

Commit

Permalink
change how kubeconfig is written to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Jan 6, 2024
1 parent 65dcb88 commit 2c625e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
clusCreator := clustermanager.ClusterCreator{
Applier: deps.ClusterApplier,
KM: clustermanager.KubeconfigManager{
Client: deps.UnAuthKubeClient.KubeconfigClient(mgmt.KubeconfigFile),
FS: deps.Writer,
ClientFactory: deps.UnAuthKubeClient,
FS: deps.Writer,
},
}

Expand Down
18 changes: 12 additions & 6 deletions pkg/clustermanager/cluster_creator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package clustermanager

import (
"bytes"
"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/providers"
"github.com/aws/eks-anywhere/pkg/types"
)
Expand All @@ -30,19 +31,24 @@ func (cc ClusterCreator) getWorkloadCluster(ctx context.Context, clusterName str
ExistingManagement: management.ExistingManagement,
}

// Use a buffer to cache the kubeconfig.
var buf bytes.Buffer
fh, path, err := cc.KM.FS.Create(
kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName),
filewriter.PersistentFile,
filewriter.Permission0600,
)

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

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
err := cc.KM.GetKubeconfig(ctx, clusterName, &buf)
if err != nil {
return nil, err
}

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

defer fh.Close()

workloadCluster.KubeconfigFile = path

return workloadCluster, nil
}
42 changes: 15 additions & 27 deletions pkg/clustermanager/kubeconfig_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ import (

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 18 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
ClientFactory ClientFactory
// 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 {
// UpdateKubeconfig retrieves the contents of the specified cluster's kubeconfig from a secret and copies it to an io.Writer.
func (km KubeconfigManager) UpdateKubeconfig(ctx context.Context, clusterName, kubeconfig string, provider providers.Provider, 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)
client, err := km.ClientFactory.BuildClientFromKubeconfig(kubeconfig)
if err != nil {
return err
}

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

if err != nil {
return err
Expand All @@ -44,30 +47,15 @@ func (km KubeconfigManager) GetKubeconfig(ctx context.Context, clusterName strin
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)
rawkubeconfig := kubeconfigSecret.Data["value"]
err = provider.UpdateKubeConfig(&rawkubeconfig, clusterName)
if err != nil {
return "", err
return err
}

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

if err != nil {
return "", err
if _, err := io.Copy(w, bytes.NewReader(rawkubeconfig)); err != nil {
return err
}

return kubeconfigPath, nil
return err
}

0 comments on commit 2c625e5

Please sign in to comment.