-
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.
Separate kubeconfig operation from cluster manager
- Loading branch information
Showing
6 changed files
with
136 additions
and
5 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
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 { | ||
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 | ||
} |
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,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 { | ||
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) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
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, | ||
) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return kubeconfigPath, 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
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