Skip to content

Commit

Permalink
Change EKSA upgrader to use management components (aws#7400)
Browse files Browse the repository at this point in the history
* use management components to install and upgrade eksa components

* fix conflicts with aws#7401 install EKSA components on bootstrap task
  • Loading branch information
cxbrowne1207 authored Jan 29, 2024
1 parent fb6909f commit e72b325
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 265 deletions.
21 changes: 18 additions & 3 deletions cmd/eksctl-anywhere/cmd/upgradeplancluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/cobra"

"github.com/aws/eks-anywhere/pkg/cluster"
capiupgrader "github.com/aws/eks-anywhere/pkg/clusterapi"
eksaupgrader "github.com/aws/eks-anywhere/pkg/clustermanager"
"github.com/aws/eks-anywhere/pkg/dependencies"
Expand Down Expand Up @@ -89,10 +90,24 @@ func (uc *upgradeClusterOptions) upgradePlanCluster(ctx context.Context) error {
return err
}

componentChangeDiffs := eksaupgrader.EksaChangeDiff(currentSpec, newClusterSpec)
if componentChangeDiffs == nil {
componentChangeDiffs = &types.ChangeDiff{}
componentChangeDiffs := &types.ChangeDiff{}

if newClusterSpec.Cluster.IsSelfManaged() {
client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(managementCluster.KubeconfigFile)
if err != nil {
return err
}

currentManagementComponents, err := cluster.GetManagementComponents(ctx, client, currentSpec.Cluster)
if err != nil {
return err
}

newManagementComponents := cluster.ManagementComponentsFromBundles(newClusterSpec.Bundles)
componentChangeDiffs.Append(eksaupgrader.EksaChangeDiff(currentManagementComponents, newManagementComponents))

}

componentChangeDiffs.Append(fluxupgrader.FluxChangeDiff(currentSpec, newClusterSpec))
componentChangeDiffs.Append(capiupgrader.CapiChangeDiff(currentSpec, newClusterSpec, deps.Provider))
componentChangeDiffs.Append(cilium.ChangeDiff(currentSpec, newClusterSpec))
Expand Down
13 changes: 7 additions & 6 deletions pkg/clustermanager/cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ type AwsIamAuth interface {

// EKSAComponents allows to manage the eks-a components installation in a cluster.
type EKSAComponents interface {
Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error
Upgrade(ctx context.Context, log logr.Logger, cluster *types.Cluster, currentSpec, newSpec *cluster.Spec) (*types.ChangeDiff, error)
Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, managementComponents *cluster.ManagementComponents, spec *cluster.Spec) error
Upgrade(ctx context.Context, log logr.Logger, cluster *types.Cluster, currentManagementComponents, newManagementComponents *cluster.ManagementComponents, newSpec *cluster.Spec) (*types.ChangeDiff, error)
}

type ClusterManagerOpt func(*ClusterManager)
Expand Down Expand Up @@ -1087,8 +1087,9 @@ func (c *ClusterManager) removeOldWorkerNodeGroups(ctx context.Context, workload
return nil
}

func (c *ClusterManager) InstallCustomComponents(ctx context.Context, clusterSpec *cluster.Spec, cluster *types.Cluster, provider providers.Provider) error {
if err := c.eksaComponents.Install(ctx, logger.Get(), cluster, clusterSpec); err != nil {
// InstallCustomComponents installs the eks-a components in a cluster.
func (c *ClusterManager) InstallCustomComponents(ctx context.Context, managementComponents *cluster.ManagementComponents, clusterSpec *cluster.Spec, cluster *types.Cluster, provider providers.Provider) error {
if err := c.eksaComponents.Install(ctx, logger.Get(), cluster, managementComponents, clusterSpec); err != nil {
return err
}

Expand All @@ -1097,8 +1098,8 @@ func (c *ClusterManager) InstallCustomComponents(ctx context.Context, clusterSpe
}

// Upgrade updates the eksa components in a cluster according to a Spec.
func (c *ClusterManager) Upgrade(ctx context.Context, cluster *types.Cluster, currentSpec, newSpec *cluster.Spec) (*types.ChangeDiff, error) {
return c.eksaComponents.Upgrade(ctx, logger.Get(), cluster, currentSpec, newSpec)
func (c *ClusterManager) Upgrade(ctx context.Context, cluster *types.Cluster, currentManagementComponents, newManagementComponents *cluster.ManagementComponents, newSpec *cluster.Spec) (*types.ChangeDiff, error) {
return c.eksaComponents.Upgrade(ctx, logger.Get(), cluster, currentManagementComponents, newManagementComponents, newSpec)
}

func (c *ClusterManager) CreateEKSANamespace(ctx context.Context, cluster *types.Cluster) error {
Expand Down
33 changes: 18 additions & 15 deletions pkg/clustermanager/cluster_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2514,19 +2514,19 @@ func TestClusterManagerInstallCustomComponentsSuccess(t *testing.T) {
features.ClearCache()
tt := newTest(t)

tt.mocks.eksaComponents.EXPECT().Install(tt.ctx, logger.Get(), tt.cluster, tt.clusterSpec)
tt.mocks.eksaComponents.EXPECT().Install(tt.ctx, logger.Get(), tt.cluster, tt.managementComponents, tt.clusterSpec)
tt.mocks.provider.EXPECT().InstallCustomProviderComponents(tt.ctx, tt.cluster.KubeconfigFile)
if err := tt.clusterManager.InstallCustomComponents(tt.ctx, tt.clusterSpec, tt.cluster, tt.mocks.provider); err != nil {
if err := tt.clusterManager.InstallCustomComponents(tt.ctx, tt.managementComponents, tt.clusterSpec, tt.cluster, tt.mocks.provider); err != nil {
t.Errorf("ClusterManager.InstallCustomComponents() error = %v, wantErr nil", err)
}
}

func TestClusterManagerInstallCustomComponentsErrorInstalling(t *testing.T) {
tt := newTest(t, clustermanager.WithRetrier(retrier.NewWithMaxRetries(2, 0)))

tt.mocks.eksaComponents.EXPECT().Install(tt.ctx, logger.Get(), tt.cluster, tt.clusterSpec).Return(errors.New("error from apply"))
tt.mocks.eksaComponents.EXPECT().Install(tt.ctx, logger.Get(), tt.cluster, tt.managementComponents, tt.clusterSpec).Return(errors.New("error from apply"))

if err := tt.clusterManager.InstallCustomComponents(tt.ctx, tt.clusterSpec, tt.cluster, nil); err == nil {
if err := tt.clusterManager.InstallCustomComponents(tt.ctx, tt.managementComponents, tt.clusterSpec, tt.cluster, nil); err == nil {
t.Error("ClusterManager.InstallCustomComponents() error = nil, wantErr not nil")
}
}
Expand Down Expand Up @@ -2791,12 +2791,13 @@ func TestClusterManagerClusterSpecChangedAWSIamConfigChanged(t *testing.T) {

type testSetup struct {
*WithT
clusterManager *clustermanager.ClusterManager
mocks *clusterManagerMocks
ctx context.Context
clusterSpec *cluster.Spec
cluster *types.Cluster
clusterName string
clusterManager *clustermanager.ClusterManager
mocks *clusterManagerMocks
ctx context.Context
managementComponents *cluster.ManagementComponents
clusterSpec *cluster.Spec
cluster *types.Cluster
clusterName string
}

func (tt *testSetup) expectPauseClusterReconciliation() *gomock.Call {
Expand Down Expand Up @@ -2824,12 +2825,14 @@ func (tt *testSetup) expectPauseClusterReconciliation() *gomock.Call {
func newTest(t *testing.T, opts ...clustermanager.ClusterManagerOpt) *testSetup {
c, m := newClusterManager(t, opts...)
clusterName := "cluster-name"
clusterSpec := test.NewClusterSpec()
return &testSetup{
WithT: NewWithT(t),
clusterManager: c,
mocks: m,
ctx: context.Background(),
clusterSpec: test.NewClusterSpec(),
WithT: NewWithT(t),
clusterManager: c,
mocks: m,
ctx: context.Background(),
managementComponents: cluster.ManagementComponentsFromBundles(clusterSpec.Bundles),
clusterSpec: clusterSpec,
cluster: &types.Cluster{
Name: clusterName,
},
Expand Down
42 changes: 18 additions & 24 deletions pkg/clustermanager/eksa_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func WithEKSAInstallerNoTimeouts() EKSAInstallerOpt {
}

// Install configures and applies eks-a components in a cluster accordingly to a spec.
func (i *EKSAInstaller) Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error {
if err := i.createEKSAComponents(ctx, log, cluster, spec); err != nil {
func (i *EKSAInstaller) Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, managementComponents *cluster.ManagementComponents, spec *cluster.Spec) error {
if err := i.createEKSAComponents(ctx, log, cluster, managementComponents, spec); err != nil {
return fmt.Errorf("applying EKSA components: %v", err)
}

Expand All @@ -76,33 +76,31 @@ func (i *EKSAInstaller) Install(ctx context.Context, log logr.Logger, cluster *t

// Upgrade re-installs the eksa components in a cluster if the VersionBundle defined in the
// new spec has a different eks-a components version. Workload clusters are ignored.
func (i *EKSAInstaller) Upgrade(ctx context.Context, log logr.Logger, c *types.Cluster, currentSpec, newSpec *cluster.Spec) (*types.ChangeDiff, error) {
func (i *EKSAInstaller) Upgrade(ctx context.Context, log logr.Logger, c *types.Cluster, currentManagementComponents, newManagementComponents *cluster.ManagementComponents, newSpec *cluster.Spec) (*types.ChangeDiff, error) {
log.V(1).Info("Checking for EKS-A components upgrade")
if !newSpec.Cluster.IsSelfManaged() {
log.V(1).Info("Skipping EKS-A components upgrade, not a self-managed cluster")
return nil, nil
}
changeDiff := EksaChangeDiff(currentSpec, newSpec)
changeDiff := EksaChangeDiff(currentManagementComponents, newManagementComponents)
if changeDiff == nil {
log.V(1).Info("Nothing to upgrade for controller and CRDs")
return nil, nil
}
log.V(1).Info("Starting EKS-A components upgrade")
oldVersionsBundle := currentSpec.RootVersionsBundle()
newVersionsBundle := newSpec.RootVersionsBundle()
oldVersion := oldVersionsBundle.Eksa.Version
newVersion := newVersionsBundle.Eksa.Version
if err := i.createEKSAComponents(ctx, log, c, newSpec); err != nil {
oldVersion := currentManagementComponents.Eksa.Version
newVersion := newManagementComponents.Eksa.Version
if err := i.createEKSAComponents(ctx, log, c, newManagementComponents, newSpec); err != nil {
return nil, fmt.Errorf("upgrading EKS-A components from version %v to version %v: %v", oldVersion, newVersion, err)
}

return changeDiff, nil
}

// createEKSAComponents creates eksa components and applies the objects to the cluster.
func (i *EKSAInstaller) createEKSAComponents(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error {
func (i *EKSAInstaller) createEKSAComponents(ctx context.Context, log logr.Logger, cluster *types.Cluster, managementComponents *cluster.ManagementComponents, spec *cluster.Spec) error {
generator := EKSAComponentGenerator{log: log, reader: i.reader}
components, err := generator.buildEKSAComponentsSpec(spec)
components, err := generator.buildEKSAComponentsSpec(managementComponents, spec)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,8 +160,8 @@ type EKSAComponentGenerator struct {
reader manifests.FileReader
}

func (g *EKSAComponentGenerator) buildEKSAComponentsSpec(spec *cluster.Spec) (*eksaComponents, error) {
components, err := g.parseEKSAComponentsSpec(spec)
func (g *EKSAComponentGenerator) buildEKSAComponentsSpec(managamentComponents *cluster.ManagementComponents, spec *cluster.Spec) (*eksaComponents, error) {
components, err := g.parseEKSAComponentsSpec(managamentComponents)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -225,9 +223,8 @@ func fullLifeCycleControllerForProvider(cluster *anywherev1.Cluster) bool {
cluster.Spec.DatacenterRef.Kind == anywherev1.CloudStackDatacenterKind
}

func (g *EKSAComponentGenerator) parseEKSAComponentsSpec(spec *cluster.Spec) (*eksaComponents, error) {
bundle := spec.RootVersionsBundle()
componentsManifest, err := bundles.ReadManifest(g.reader, bundle.Eksa.Components)
func (g *EKSAComponentGenerator) parseEKSAComponentsSpec(managementComponents *cluster.ManagementComponents) (*eksaComponents, error) {
componentsManifest, err := bundles.ReadManifest(g.reader, managementComponents.Eksa.Components)
if err != nil {
return nil, fmt.Errorf("loading manifest for eksa components: %v", err)
}
Expand Down Expand Up @@ -273,17 +270,14 @@ func (c *eksaComponents) BuildFromParsed(lookup yamlutil.ObjectLookup) error {
}

// EksaChangeDiff computes the version diff in eksa components between two specs.
func EksaChangeDiff(currentSpec, newSpec *cluster.Spec) *types.ChangeDiff {
oldVersionsBundle := currentSpec.RootVersionsBundle()
newVersionsBundle := newSpec.RootVersionsBundle()

if oldVersionsBundle.Eksa.Version != newVersionsBundle.Eksa.Version {
func EksaChangeDiff(currentManagementComponents, newManagementComponents *cluster.ManagementComponents) *types.ChangeDiff {
if currentManagementComponents.Eksa.Version != newManagementComponents.Eksa.Version {
return &types.ChangeDiff{
ComponentReports: []types.ComponentChangeDiff{
{
ComponentName: "EKS-A",
NewVersion: newVersionsBundle.Eksa.Version,
OldVersion: oldVersionsBundle.Eksa.Version,
ComponentName: "EKS-A Management",
NewVersion: newManagementComponents.Eksa.Version,
OldVersion: currentManagementComponents.Eksa.Version,
},
},
}
Expand Down
Loading

0 comments on commit e72b325

Please sign in to comment.