Skip to content

Commit

Permalink
Add upgrade management components cmd (#7238)
Browse files Browse the repository at this point in the history
  • Loading branch information
d8660091 authored Jan 3, 2024
1 parent 6eee9ff commit 7b80274
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 13 deletions.
96 changes: 96 additions & 0 deletions cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/aws/eks-anywhere/cmd/eksctl-anywhere/cmd/aflag"
"github.com/aws/eks-anywhere/pkg/dependencies"
"github.com/aws/eks-anywhere/pkg/kubeconfig"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/workflows/management"
)

type upgradeManagementComponentsOptions struct {
clusterOptions
}

var umco = &upgradeManagementComponentsOptions{}

func init() {
flagSet := upgradeManagementComponentsCmd.Flags()
aflag.String(aflag.ClusterConfig, &umco.fileName, flagSet)
aflag.String(aflag.BundleOverride, &umco.bundlesOverride, flagSet)
}

var upgradeManagementComponentsCmd = &cobra.Command{
Use: "management-components",
Short: "Upgrade management components in a management cluster",
Long: "The term 'management components' encompasses all Kubernetes controllers and their CRDs present in the management cluster that are responsible for reconciling your EKS Anywhere (EKS-A) cluster. This command is specifically designed to facilitate the upgrade of these management components. Post this upgrade, the cluster itself can be upgraded by updating the 'eksaRelease' field in your eksa cluster object.",
PreRunE: bindFlagsToViper,
SilenceUsage: true,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

clusterSpec, err := newClusterSpec(clusterOptions{
fileName: umco.fileName,
})
if err != nil {
return err
}
if !clusterSpec.Cluster.IsSelfManaged() {
return fmt.Errorf("cluster %s doesn't contain management components to be upgraded", clusterSpec.Cluster.Name)
}

cliConfig := buildCliConfig(clusterSpec)
dirs, err := uc.directoriesToMount(clusterSpec, cliConfig)
if err != nil {
return err
}

factory := dependencies.ForSpec(clusterSpec).WithExecutableMountDirs(dirs...).
WithBootstrapper().
WithCliConfig(cliConfig).
WithClusterManager(clusterSpec.Cluster, nil).
WithClusterApplier().
WithProvider(umco.fileName, clusterSpec.Cluster, false, "", false, "", nil, nil).
WithGitOpsFlux(clusterSpec.Cluster, clusterSpec.FluxConfig, cliConfig).
WithWriter().
WithCAPIManager().
WithEksdUpgrader().
WithEksdInstaller().
WithKubectl().
WithValidatorClients()

deps, err := factory.Build(ctx)
if err != nil {
return err
}
defer close(cmd.Context(), deps)

runner := management.NewUpgradeManagementComponentsRunner(
deps.Provider,
deps.CAPIManager,
deps.ClusterManager,
deps.GitOpsFlux,
deps.Writer,
deps.EksdUpgrader,
deps.EksdInstaller,
)

managementCluster := &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
ExistingManagement: clusterSpec.Cluster.IsSelfManaged(),
}

validator := management.NewUMCValidator(managementCluster, deps.Kubectl)
return runner.Run(ctx, clusterSpec, managementCluster, validator)
},
}

func init() {
upgradeCmd.AddCommand(upgradeManagementComponentsCmd)
}
26 changes: 17 additions & 9 deletions pkg/workflows/management/core_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ func (s *ensureEtcdCAPIComponentsExist) Checkpoint() *task.CompletedTask {
}
}

func (s *ensureEtcdCAPIComponentsExist) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
func (s *ensureEtcdCAPIComponentsExist) Restore(_ context.Context, _ *task.CommandContext, _ *task.CompletedTask) (task.Task, error) {
return &pauseGitOpsReconcile{}, nil
}

type upgradeCoreComponents struct {
UpgradeChangeDiff *types.ChangeDiff
}

// Run upgradeCoreComponents upgrades pre cluster upgrade components.
func (s *upgradeCoreComponents) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
func runUpgradeCoreComponents(ctx context.Context, commandContext *task.CommandContext) error {
logger.Info("Upgrading core components")

err := commandContext.Provider.PreCoreComponentsUpgrade(
Expand All @@ -50,41 +49,50 @@ func (s *upgradeCoreComponents) Run(ctx context.Context, commandContext *task.Co
)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}

changeDiff, err := commandContext.CAPIManager.Upgrade(ctx, commandContext.ManagementCluster, commandContext.Provider, commandContext.CurrentClusterSpec, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}
commandContext.UpgradeChangeDiff.Append(changeDiff)

if err = commandContext.GitOpsManager.Install(ctx, commandContext.ManagementCluster, commandContext.CurrentClusterSpec, commandContext.ClusterSpec); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}

changeDiff, err = commandContext.GitOpsManager.Upgrade(ctx, commandContext.ManagementCluster, commandContext.CurrentClusterSpec, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}
commandContext.UpgradeChangeDiff.Append(changeDiff)

changeDiff, err = commandContext.ClusterManager.Upgrade(ctx, commandContext.ManagementCluster, commandContext.CurrentClusterSpec, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}
commandContext.UpgradeChangeDiff.Append(changeDiff)

changeDiff, err = commandContext.EksdUpgrader.Upgrade(ctx, commandContext.ManagementCluster, commandContext.CurrentClusterSpec, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}
commandContext.UpgradeChangeDiff.Append(changeDiff)

return nil
}

// Run upgradeCoreComponents upgrades pre cluster upgrade components.
func (s *upgradeCoreComponents) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if err := runUpgradeCoreComponents(ctx, commandContext); err != nil {
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}
s.UpgradeChangeDiff = commandContext.UpgradeChangeDiff

return &preClusterUpgrade{}
Expand Down
15 changes: 11 additions & 4 deletions pkg/workflows/management/install_new_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ import (

type installNewComponents struct{}

// Run installNewComponents performs actions needed to upgrade the management cluster.
func (s *installNewComponents) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
func runInstallNewComponents(ctx context.Context, commandContext *task.CommandContext) error {
if err := commandContext.ClusterManager.ApplyBundles(ctx, commandContext.ClusterSpec, commandContext.ManagementCluster); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}

if err := commandContext.ClusterManager.ApplyReleases(ctx, commandContext.ClusterSpec, commandContext.ManagementCluster); err != nil {
commandContext.SetError(err)
return &workflows.CollectMgmtClusterDiagnosticsTask{}
return err
}

err := commandContext.EksdInstaller.InstallEksdManifest(ctx, commandContext.ClusterSpec, commandContext.ManagementCluster)
if err != nil {
commandContext.SetError(err)
return err
}
return nil
}

// Run installNewComponents performs actions needed to upgrade the management cluster.
func (s *installNewComponents) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if err := runInstallNewComponents(ctx, commandContext); err != nil {
return &workflows.CollectMgmtClusterDiagnosticsTask{}
}
return &upgradeCluster{}
Expand Down
Loading

0 comments on commit 7b80274

Please sign in to comment.