diff --git a/cmd/eksctl-anywhere/cmd/createcluster.go b/cmd/eksctl-anywhere/cmd/createcluster.go index a7398485185ce..6b65e35a11386 100644 --- a/cmd/eksctl-anywhere/cmd/createcluster.go +++ b/cmd/eksctl-anywhere/cmd/createcluster.go @@ -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, }, } diff --git a/pkg/clustermanager/cluster_creator.go b/pkg/clustermanager/cluster_creator.go index 3edacb9a5a912..7ae7d8dec0c32 100644 --- a/pkg/clustermanager/cluster_creator.go +++ b/pkg/clustermanager/cluster_creator.go @@ -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" ) @@ -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, + ) - 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 } diff --git a/pkg/clustermanager/kubeconfig_manager.go b/pkg/clustermanager/kubeconfig_manager.go index 137f841a67a99..39676560902d9 100644 --- a/pkg/clustermanager/kubeconfig_manager.go +++ b/pkg/clustermanager/kubeconfig_manager.go @@ -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 { - 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 @@ -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 }