Skip to content

Commit

Permalink
make kubeconfig reader per provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Jan 8, 2024
1 parent 2c625e5 commit f7ae3a2
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 89 deletions.
11 changes: 3 additions & 8 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
WithPackageInstaller(clusterSpec, cc.installPackages, cc.managementKubeconfig).
WithValidatorClients().
WithCreateClusterDefaulter(createCLIConfig).
WithClusterApplier()
WithClusterApplier().
WithKubeconfigReader(clusterConfig)

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

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L191-L192

Added lines #L191 - L192 were not covered by tests

if cc.timeoutOptions.noTimeouts {
factory.WithNoTimeouts()
Expand Down Expand Up @@ -232,13 +233,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
}
createValidations := createvalidations.New(validationOpts)

clusCreator := clustermanager.ClusterCreator{
Applier: deps.ClusterApplier,
KM: clustermanager.KubeconfigManager{
ClientFactory: deps.UnAuthKubeClient,
FS: deps.Writer,
},
}
clusCreator := clustermanager.NewClusterCreator(deps.ClusterApplier, deps.KubeconfigReader, deps.Writer)

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

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L236-L237

Added lines #L236 - L237 were not covered by tests
if features.UseNewWorkflows().IsActive() {
deps, err = factory.
Expand Down
39 changes: 26 additions & 13 deletions pkg/clustermanager/cluster_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,57 @@ import (
)

type ClusterCreator struct {

Check warning on line 13 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
applier Applier
kubeconfigReader providers.KubeconfigReader
fs filewriter.FileWriter
}

func NewClusterCreator(applier Applier, kubeconfigReader providers.KubeconfigReader, fs filewriter.FileWriter) ClusterCreator {

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

View workflow job for this annotation

GitHub Actions / lint

exported: exported function NewClusterCreator should have comment or be unexported (revive)
return ClusterCreator{
applier: applier,
kubeconfigReader: kubeconfigReader,
fs: fs,
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L19-L24

Added lines #L19 - L24 were not covered by tests
}

// 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)
err := cc.applier.Run(ctx, spec, *managementCluster)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L28-L32

Added lines #L28 - L32 were not covered by tests

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

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#L34

Added line #L34 was not covered by tests
}

// Run exposes the Applier's run.
func (cc ClusterCreator) Run(ctx context.Context, spec *cluster.Spec, managementCluster types.Cluster) error {
return cc.applier.Run(ctx, spec, managementCluster)

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}

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

fh, path, err := cc.KM.FS.Create(
fh, path, err := cc.fs.Create(
kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName),
filewriter.PersistentFile,
filewriter.Permission0600,
)

if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L42-L55

Added lines #L42 - L55 were not covered by tests

err = cc.KM.UpdateKubeconfig(ctx, clusterName, management.KubeconfigFile, provider, fh)
defer fh.Close()

err = cc.kubeconfigReader.WriteKubeconfig(ctx, clusterName, management.KubeconfigFile, fh)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L57-L62

Added lines #L57 - L62 were not covered by tests

defer fh.Close()

workloadCluster.KubeconfigFile = path
cluster.KubeconfigFile = path

return workloadCluster, nil
return cluster, nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/clustermanager/cluster_creator.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}
61 changes: 0 additions & 61 deletions pkg/clustermanager/kubeconfig_manager.go

This file was deleted.

22 changes: 22 additions & 0 deletions pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Dependencies struct {
ExecutableBuilder *executables.ExecutablesBuilder
CreateClusterDefaulter cli.CreateClusterDefaulter
UpgradeClusterDefaulter cli.UpgradeClusterDefaulter
KubeconfigReader providers.KubeconfigReader
}

// KubeClients defines super struct that exposes all behavior.
Expand Down Expand Up @@ -580,6 +581,27 @@ func (f *Factory) WithProvider(clusterConfigFile string, clusterConfig *v1alpha1
return f
}

// WithKubeconfigReader

Check failure on line 584 in pkg/dependencies/factory.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func (f *Factory) WithKubeconfigReader(clusterConfig *v1alpha1.Cluster) *Factory {
genericKubeconfigReader := providers.BuildKubeconfigReader(f.dependencies.UnAuthKubeClient, time.Minute, time.Second)
switch clusterConfig.Spec.DatacenterRef.Kind {
case v1alpha1.VSphereDatacenterKind:
f.dependencies.KubeconfigReader = genericKubeconfigReader
case v1alpha1.CloudStackDatacenterKind:
f.dependencies.KubeconfigReader = genericKubeconfigReader
case v1alpha1.DockerDatacenterKind:
f.dependencies.KubeconfigReader = docker.BuildKubeconfigReader(f.dependencies.Provider, f.dependencies.UnAuthKubeClient, time.Minute, time.Second)
case v1alpha1.TinkerbellDatacenterKind:
f.dependencies.KubeconfigReader = genericKubeconfigReader
case v1alpha1.SnowDatacenterKind:
f.dependencies.KubeconfigReader = genericKubeconfigReader
case v1alpha1.NutanixDatacenterKind:
f.dependencies.KubeconfigReader = genericKubeconfigReader

Check warning on line 599 in pkg/dependencies/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/dependencies/factory.go#L585-L599

Added lines #L585 - L599 were not covered by tests
}

return f

Check warning on line 602 in pkg/dependencies/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/dependencies/factory.go#L602

Added line #L602 was not covered by tests
}

func (f *Factory) WithDocker() *Factory {
f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.dependencies.DockerClient != nil {
Expand Down
65 changes: 65 additions & 0 deletions pkg/providers/docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package docker

import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"os"
"regexp"
"time"

etcdv1 "github.com/aws/etcdadm-controller/api/v1beta1"
corev1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/bootstrapper"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clusterapi"
"github.com/aws/eks-anywhere/pkg/config"
Expand All @@ -24,6 +29,7 @@ import (
"github.com/aws/eks-anywhere/pkg/providers/common"
"github.com/aws/eks-anywhere/pkg/registrymirror"
"github.com/aws/eks-anywhere/pkg/registrymirror/containerd"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/templater"
"github.com/aws/eks-anywhere/pkg/types"
releasev1alpha1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
Expand Down Expand Up @@ -52,6 +58,14 @@ type provider struct {
templateBuilder *DockerTemplateBuilder
}

// DockerKubeconfigReader reads the kubeconfig secret on a cluster and copies the contents to a writer.
type DockerKubeconfigReader struct {

Check warning on line 62 in pkg/providers/docker/docker.go

View workflow job for this annotation

GitHub Actions / lint

exported: type name will be used as docker.DockerKubeconfigReader by other packages, and that stutters; consider calling this KubeconfigReader (revive)
provider providers.Provider
client *kubernetes.UnAuthClient
timeout time.Duration
backoff time.Duration
}

func (p *provider) InstallCustomProviderComponents(ctx context.Context, kubeconfigFile string) error {
return nil
}
Expand Down Expand Up @@ -531,6 +545,57 @@ func (p *provider) UpdateKubeConfig(content *[]byte, clusterName string) error {
}
}

// BuildKubeconfigReader creates a KubeconfigReader.
func BuildKubeconfigReader(provider providers.Provider, unauthClient *kubernetes.UnAuthClient, timeout time.Duration, backoff time.Duration) providers.KubeconfigReader {
return DockerKubeconfigReader{
client: unauthClient,
timeout: timeout,
backoff: backoff,
provider: provider,
}

Check warning on line 555 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L549-L555

Added lines #L549 - L555 were not covered by tests
}

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

err := retrier.New(
kr.timeout,
retrier.WithRetryPolicy(retrier.BackOffPolicy(kr.backoff)),
).Retry(func() error {
client, err := kr.client.BuildClientFromKubeconfig(kubeconfigPath)
if err != nil {
return err
}

Check warning on line 569 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L559-L569

Added lines #L559 - L569 were not covered by tests

err = client.Get(ctx, fmt.Sprintf("%s-kubeconfig", clusterName), constants.EksaSystemNamespace, kubeconfigSecret)
if err != nil {
return err
}

Check warning on line 574 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L571-L574

Added lines #L571 - L574 were not covered by tests

if err != nil {
return err
}

Check warning on line 578 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L576-L578

Added lines #L576 - L578 were not covered by tests

return nil

Check warning on line 580 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L580

Added line #L580 was not covered by tests
})

Check failure on line 582 in pkg/providers/docker/docker.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 585 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L583-L585

Added lines #L583 - L585 were not covered by tests

rawkubeconfig := kubeconfigSecret.Data["value"]
if err := kr.provider.UpdateKubeConfig(&rawkubeconfig, clusterName); err != nil {
return err
}

Check warning on line 590 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L587-L590

Added lines #L587 - L590 were not covered by tests

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

Check warning on line 594 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L592-L594

Added lines #L592 - L594 were not covered by tests

return nil

Check warning on line 596 in pkg/providers/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/docker/docker.go#L596

Added line #L596 was not covered by tests
}

// this is required for docker provider.
func getUpdatedKubeConfigContent(content *[]byte, dockerLbPort string) {
mc := regexp.MustCompile("server:.*")
Expand Down
66 changes: 66 additions & 0 deletions pkg/providers/kubeconfig_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package providers

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/retrier"
)

// GenericKubeconfigReader reads the kubeconfig secret on a cluster and copies the contents to a writer.
type GenericKubeconfigReader struct {
client *kubernetes.UnAuthClient
timeout time.Duration
backoff time.Duration
}

// BuildKubeconfigReader creates a KubeconfigReader.
func BuildKubeconfigReader(unauthClient *kubernetes.UnAuthClient, timeout time.Duration, backoff time.Duration) KubeconfigReader {
return GenericKubeconfigReader{
client: unauthClient,
timeout: timeout,
backoff: backoff,
}

Check warning on line 30 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L25-L30

Added lines #L25 - L30 were not covered by tests
}

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

err := retrier.New(
kr.timeout,
retrier.WithRetryPolicy(retrier.BackOffPolicy(kr.backoff)),
).Retry(func() error {
client, err := kr.client.BuildClientFromKubeconfig(kubeconfigPath)
if err != nil {
return err
}

Check warning on line 44 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L34-L44

Added lines #L34 - L44 were not covered by tests

err = client.Get(ctx, fmt.Sprintf("%s-kubeconfig", clusterName), constants.EksaSystemNamespace, kubeconfigSecret)
if err != nil {
return err
}

Check warning on line 49 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L46-L49

Added lines #L46 - L49 were not covered by tests

if err != nil {
return err
}

Check warning on line 53 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L51-L53

Added lines #L51 - L53 were not covered by tests

return nil

Check warning on line 55 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L55

Added line #L55 was not covered by tests
})
if err != nil {
return err
}

Check warning on line 59 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L57-L59

Added lines #L57 - L59 were not covered by tests

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

Check warning on line 63 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L61-L63

Added lines #L61 - L63 were not covered by tests

return nil

Check warning on line 65 in pkg/providers/kubeconfig_reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/kubeconfig_reader.go#L65

Added line #L65 was not covered by tests
}
6 changes: 6 additions & 0 deletions pkg/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package providers

import (
"context"
"io"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/bootstrapper"
Expand Down Expand Up @@ -66,3 +67,8 @@ type MachineConfig interface {
GetNamespace() string
GetName() string
}

// KubeconfigReader reads the kubeconfig secret on a cluster and copies the contents to a writer.
type KubeconfigReader interface {
WriteKubeconfig(ctx context.Context, clusterName, kubeconfig string, w io.Writer) error
}
3 changes: 1 addition & 2 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -38,7 +37,7 @@ type CommandContext struct {
PackageInstaller interfaces.PackageInstaller
EksdUpgrader interfaces.EksdUpgrader
ClusterUpgrader interfaces.ClusterUpgrader
ClusterCreator clustermanager.ClusterCreator
ClusterCreator interfaces.ClusterCreator
CAPIManager interfaces.CAPIManager
ClusterSpec *cluster.Spec
CurrentClusterSpec *cluster.Spec
Expand Down
Loading

0 comments on commit f7ae3a2

Please sign in to comment.