Skip to content

Commit

Permalink
Create management cluster using controller behind feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mitalipaygude committed Nov 30, 2023
1 parent 59789da commit 8beb6e9
Show file tree
Hide file tree
Showing 10 changed files with 606 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ docs/.hugo_build.lock
*.log
pkg/executables/TestDeployTemplate*
pkg/files/config
*.yaml
25 changes: 25 additions & 0 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/aws/eks-anywhere/pkg/validations/createvalidations"
"github.com/aws/eks-anywhere/pkg/workflow/management"
"github.com/aws/eks-anywhere/pkg/workflows"
m "github.com/aws/eks-anywhere/pkg/workflows/management"
)

type createClusterOptions struct {
Expand Down Expand Up @@ -252,6 +253,30 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
}

err = wflw.Run(ctx)
} else if clusterSpec.Cluster.IsSelfManaged() && features.UseControllerViaCLIWorkflow().IsActive() {

fmt.Println("Using the new workflow using the controller for management cluster create")

deps, err = factory.
WithClusterApplier().
Build(ctx)

if err != nil {
return err
}
createMgmtCluster := m.NewCreateManagement(
deps.Bootstrapper,
deps.Provider,
deps.ClusterManager,
deps.GitOpsFlux,
deps.Writer,
deps.EksdInstaller,
deps.PackageInstaller,
deps.ClusterApplier,
)

err = createMgmtCluster.Run(ctx, clusterSpec, createValidations, cc.forceClean)

} else {
err = createCluster.Run(ctx, clusterSpec, createValidations, cc.forceClean)
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/v1alpha1/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ func (r *Cluster) ValidateCreate() error {

var allErrs field.ErrorList

if !r.IsReconcilePaused() && r.IsSelfManaged() {
return apierrors.NewBadRequest("creating new cluster on existing cluster is not supported for self managed clusters")
}

if r.Spec.EtcdEncryption != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.etcdEncryption"), r.Spec.EtcdEncryption, "etcdEncryption is not supported during cluster creation"))
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/clustermanager/cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,47 @@ func (c *ClusterManager) CreateWorkloadCluster(ctx context.Context, managementCl
return workloadCluster, nil
}

// GetWorkloadCluster gets workload cluster.
func (c *ClusterManager) GetWorkloadCluster(ctx context.Context, managementCluster *types.Cluster, clusterSpec *cluster.Spec, provider providers.Provider) (*types.Cluster, error) {
clusterName := clusterSpec.Cluster.Name

workloadCluster := &types.Cluster{
Name: clusterName,
ExistingManagement: managementCluster.ExistingManagement,
}

logger.V(3).Info("Waiting for workload kubeconfig generation", "cluster", clusterName)

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

if err := c.getWorkloadClusterKubeconfig(ctx, clusterName, managementCluster, &buf); err != nil {
return nil, fmt.Errorf("waiting for workload kubeconfig: %v", err)
}

rawKubeconfig := buf.Bytes()

// The Docker provider wants to update the kubeconfig to patch the server address before
// we write it to disk. This is to ensure we can communicate with the cluster even when
// hosted inside a Docker Desktop VM.
if err := provider.UpdateKubeConfig(&rawKubeconfig, clusterName); err != nil {
return nil, err
}

kubeconfigFile, err := c.writer.Write(
kubeconfig.FormatWorkloadClusterKubeconfigFilename(clusterName),
rawKubeconfig,
filewriter.PersistentFile,
filewriter.Permission0600,
)
if err != nil {
return nil, fmt.Errorf("writing workload kubeconfig: %v", err)
}
workloadCluster.KubeconfigFile = kubeconfigFile

return workloadCluster, nil
}

func (c *ClusterManager) waitUntilControlPlaneAvailable(
ctx context.Context,
clusterSpec *cluster.Spec,
Expand Down Expand Up @@ -1167,6 +1208,37 @@ func (c *ClusterManager) CreateEKSAResources(ctx context.Context, cluster *types
return c.ApplyReleases(ctx, clusterSpec, cluster)
}

// CreateEKSAReleaseBundle applies the eks-a release bundle to the cluster.
func (c *ClusterManager) CreateEKSAReleaseBundle(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec) error {
if clusterSpec.Cluster.Namespace != "" {
if err := c.clusterClient.CreateNamespaceIfNotPresent(ctx, cluster.KubeconfigFile, clusterSpec.Cluster.Namespace); err != nil {
return err
}
}

if err := c.ApplyBundles(ctx, clusterSpec, cluster); err != nil {
return err
}
return c.ApplyReleases(ctx, clusterSpec, cluster)
}

// ApplyEKSASpec applies the eks-a cluster specs (cluster, datacenterconfig, machine configs, etc.).
func (c *ClusterManager) ApplyEKSASpec(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec,
datacenterConfig providers.DatacenterConfig, machineConfigs []providers.MachineConfig,
) error {
resourcesSpec, err := clustermarshaller.MarshalClusterSpec(clusterSpec, datacenterConfig, machineConfigs)
if err != nil {
return err
}
logger.V(4).Info("Applying eksa yaml resources to cluster")
logger.V(6).Info(string(resourcesSpec))
if err = c.applyResource(ctx, cluster, resourcesSpec); err != nil {
return err
}

return nil
}

func (c *ClusterManager) ApplyBundles(ctx context.Context, clusterSpec *cluster.Spec, cluster *types.Cluster) error {
bundleObj, err := yaml.Marshal(clusterSpec.Bundles)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func (p *vsphereProvider) createSecret(ctx context.Context, cluster *types.Clust
}

func (p *vsphereProvider) PreCAPIInstallOnBootstrap(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec) error {
return nil
return p.UpdateSecrets(ctx, cluster, nil)
}

func (p *vsphereProvider) PostBootstrapSetup(ctx context.Context, clusterConfig *v1alpha1.Cluster, cluster *types.Cluster) error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ClusterManager interface {
BackupCAPIWaitForInfrastructure(ctx context.Context, cluster *types.Cluster, managementStatePath, clusterName string) error
MoveCAPI(ctx context.Context, from, to *types.Cluster, clusterName string, clusterSpec *cluster.Spec, checkers ...types.NodeReadyChecker) error
CreateWorkloadCluster(ctx context.Context, managementCluster *types.Cluster, clusterSpec *cluster.Spec, provider providers.Provider) (*types.Cluster, error)
GetWorkloadCluster(ctx context.Context, managementCluster *types.Cluster, clusterSpec *cluster.Spec, provider providers.Provider) (*types.Cluster, error)
PauseCAPIWorkloadClusters(ctx context.Context, managementCluster *types.Cluster) error
ResumeCAPIWorkloadClusters(ctx context.Context, managementCluster *types.Cluster) error
RunPostCreateWorkloadCluster(ctx context.Context, managementCluster, workloadCluster *types.Cluster, clusterSpec *cluster.Spec) error
Expand All @@ -34,6 +35,8 @@ type ClusterManager interface {
InstallCustomComponents(ctx context.Context, clusterSpec *cluster.Spec, cluster *types.Cluster, provider providers.Provider) error
CreateEKSANamespace(ctx context.Context, cluster *types.Cluster) error
CreateEKSAResources(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec, datacenterConfig providers.DatacenterConfig, machineConfigs []providers.MachineConfig) error
CreateEKSAReleaseBundle(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec) error
ApplyEKSASpec(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec, datacenterConfig providers.DatacenterConfig, machineConfigs []providers.MachineConfig) error
ApplyBundles(ctx context.Context, clusterSpec *cluster.Spec, cluster *types.Cluster) error
ApplyReleases(ctx context.Context, clusterSpec *cluster.Spec, cluster *types.Cluster) error
PauseEKSAControllerReconcile(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec, provider providers.Provider) error
Expand Down
75 changes: 75 additions & 0 deletions pkg/workflows/management/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package management

import (
"context"

"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/task"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/workflows/interfaces"
)

// CreateManagement is a schema for create cluster.
type CreateManagement struct {
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
gitOpsManager interfaces.GitOpsManager
writer filewriter.FileWriter
eksdInstaller interfaces.EksdInstaller
packageInstaller interfaces.PackageInstaller
clusterUpgrader interfaces.ClusterUpgrader
}

// NewCreateManagement builds a new create construct.
func NewCreateManagement(bootstrapper interfaces.Bootstrapper, provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
clusterUpgrade interfaces.ClusterUpgrader,
) *CreateManagement {
return &CreateManagement{
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
writer: writer,
eksdInstaller: eksdInstaller,
packageInstaller: packageInstaller,
clusterUpgrader: clusterUpgrade,
}
}

// Run runs all the create management cluster tasks.
func (c *CreateManagement) Run(ctx context.Context, clusterSpec *cluster.Spec, validator interfaces.Validator, forceCleanup bool) error {
if forceCleanup {
if err := c.bootstrapper.DeleteBootstrapCluster(ctx, &types.Cluster{
Name: clusterSpec.Cluster.Name,
}, constants.Create, forceCleanup); err != nil {
return err
}
}
commandContext := &task.CommandContext{
Bootstrapper: c.bootstrapper,
Provider: c.provider,
ClusterManager: c.clusterManager,
GitOpsManager: c.gitOpsManager,
ClusterSpec: clusterSpec,
Writer: c.writer,
Validations: validator,
EksdInstaller: c.eksdInstaller,
PackageInstaller: c.packageInstaller,
ClusterUpgrader: c.clusterUpgrader,
}

if clusterSpec.ManagementCluster != nil {
commandContext.BootstrapCluster = clusterSpec.ManagementCluster
}

err := task.NewTaskRunner(&setupAndValidateCreate{}, c.writer).RunTask(ctx, commandContext)

return err
}
Loading

0 comments on commit 8beb6e9

Please sign in to comment.