From 47db5fa689de6fd5ede8570cab833eaf31d0a213 Mon Sep 17 00:00:00 2001 From: Mitali Paygude Date: Mon, 22 Jan 2024 16:54:27 -0800 Subject: [PATCH] fixes --- Makefile | 2 +- pkg/clustermanager/eksa_installer.go | 71 ++++++++------ pkg/workflows/interfaces/interfaces.go | 11 ++- pkg/workflows/interfaces/mocks/clients.go | 95 +++++++++---------- .../management/create_install_eksa.go | 23 +---- 5 files changed, 98 insertions(+), 104 deletions(-) diff --git a/Makefile b/Makefile index 3490c67943d7f..27c569fca703b 100644 --- a/Makefile +++ b/Makefile @@ -543,7 +543,7 @@ mocks: ## Generate mocks ${MOCKGEN} -destination=pkg/bootstrapper/mocks/bootstrapper.go -package=mocks "github.com/aws/eks-anywhere/pkg/bootstrapper" ClusterClient ${MOCKGEN} -destination=pkg/git/providers/github/mocks/github.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/providers/github" GithubClient ${MOCKGEN} -destination=pkg/git/mocks/git.go -package=mocks "github.com/aws/eks-anywhere/pkg/git" Client,ProviderClient - ${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,EksaInstaller + ${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,ClientFactory,EksaInstaller ${MOCKGEN} -destination=pkg/git/gogithub/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gogithub" Client ${MOCKGEN} -destination=pkg/git/gitclient/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gitclient" GoGit ${MOCKGEN} -destination=pkg/validations/mocks/docker.go -package=mocks "github.com/aws/eks-anywhere/pkg/validations" DockerExecutable diff --git a/pkg/clustermanager/eksa_installer.go b/pkg/clustermanager/eksa_installer.go index 5e8e1db66a5fe..86ca0e8b7880c 100644 --- a/pkg/clustermanager/eksa_installer.go +++ b/pkg/clustermanager/eksa_installer.go @@ -59,26 +59,20 @@ 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 { - generator := EKSAComponentGenerator{log: log, reader: i.reader} - components, err := generator.buildEKSAComponentsSpec(spec) + + err := i.createEKSAComponents(ctx, log, cluster, spec) if err != nil { - return err - } - - objs := make([]runtime.Object, 0, len(components.rest)+1) - objs = append(objs, components.deployment) - for _, o := range components.rest { - objs = append(objs, o) + return fmt.Errorf("applying EKSA components: %v", err) } - for _, o := range objs { - if err = i.client.Apply(ctx, cluster.KubeconfigFile, o); err != nil { - return fmt.Errorf("applying eksa components: %v", err) - } + err = i.applyBundles(ctx, log, cluster, spec) + if err != nil { + return fmt.Errorf("applying EKSA bundles: %v", err) } - if err := i.client.WaitForDeployment(ctx, cluster, i.deploymentWaitTimeout.String(), "Available", constants.EksaControllerManagerDeployment, constants.EksaSystemNamespace); err != nil { - return fmt.Errorf("waiting for eksa-controller-manager: %v", err) + err = i.applyReleases(ctx, log, cluster, spec) + if err != nil { + return fmt.Errorf("applying EKSA releases: %v", err) } return nil @@ -102,15 +96,41 @@ func (i *EKSAInstaller) Upgrade(ctx context.Context, log logr.Logger, c *types.C newVersionsBundle := newSpec.RootVersionsBundle() oldVersion := oldVersionsBundle.Eksa.Version newVersion := newVersionsBundle.Eksa.Version - if err := i.Install(ctx, log, c, newSpec); err != nil { + if err := i.createEKSAComponents(ctx, log, c, newSpec); err != nil { return nil, fmt.Errorf("upgrading EKS-A components from version %v to version %v: %v", oldVersion, newVersion, err) } return changeDiff, nil } -// ApplyBundles applies the bundles to the cluster. -func (i *EKSAInstaller) ApplyBundles(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error { +// 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 { + generator := EKSAComponentGenerator{log: log, reader: i.reader} + components, err := generator.buildEKSAComponentsSpec(spec) + if err != nil { + return err + } + + objs := make([]runtime.Object, 0, len(components.rest)+1) + objs = append(objs, components.deployment) + for _, o := range components.rest { + objs = append(objs, o) + } + + for _, o := range objs { + if err = i.client.Apply(ctx, cluster.KubeconfigFile, o); err != nil { + return fmt.Errorf("applying eksa components: %v", err) + } + } + + if err := i.client.WaitForDeployment(ctx, cluster, i.deploymentWaitTimeout.String(), "Available", constants.EksaControllerManagerDeployment, constants.EksaSystemNamespace); err != nil { + return fmt.Errorf("waiting for eksa-controller-manager: %v", err) + } + + return nil +} +// applyBundles applies the bundles to the cluster. +func (i *EKSAInstaller) applyBundles(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error { bundleObj, err := yaml.Marshal(spec.Bundles) if err != nil { return fmt.Errorf("outputting bundle yaml: %v", err) @@ -124,8 +144,8 @@ func (i *EKSAInstaller) ApplyBundles(ctx context.Context, log logr.Logger, clust return nil } -// ApplyReleases applies the releases to the cluster. -func (i *EKSAInstaller) ApplyReleases(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error { +// applyReleases applies the releases to the cluster. +func (i *EKSAInstaller) applyReleases(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error { releaseObj, err := yaml.Marshal(spec.EKSARelease) if err != nil { return fmt.Errorf("outputting release yaml: %v", err) @@ -139,17 +159,6 @@ func (i *EKSAInstaller) ApplyReleases(ctx context.Context, log logr.Logger, clus return nil } -// CreateNamespaceIfNotPresent creates the cluster namespace if it doesn not exist. -func (i *EKSAInstaller) CreateNamespaceIfNotPresent(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error { - if spec.Cluster.Namespace != "" { - if err := i.client.CreateNamespaceIfNotPresent(ctx, cluster.KubeconfigFile, spec.Cluster.Namespace); err != nil { - return err - } - } - - return nil -} - // EKSAComponentGenerator generates and configures eks-a components. type EKSAComponentGenerator struct { log logr.Logger diff --git a/pkg/workflows/interfaces/interfaces.go b/pkg/workflows/interfaces/interfaces.go index 937091ad96745..a334083031b26 100644 --- a/pkg/workflows/interfaces/interfaces.go +++ b/pkg/workflows/interfaces/interfaces.go @@ -6,6 +6,7 @@ import ( "github.com/go-logr/logr" "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/constants" "github.com/aws/eks-anywhere/pkg/providers" @@ -13,6 +14,12 @@ import ( "github.com/aws/eks-anywhere/pkg/validations" ) +// ClientFactory builds Kubernetes clients. +type ClientFactory interface { + // BuildClientFromKubeconfig builds a Kubernetes client from a kubeconfig file. + BuildClientFromKubeconfig(kubeconfigPath string) (kubernetes.Client, error) +} + type Bootstrapper interface { CreateBootstrapCluster(ctx context.Context, clusterSpec *cluster.Spec, opts ...bootstrapper.BootstrapClusterOption) (*types.Cluster, error) DeleteBootstrapCluster(context.Context, *types.Cluster, constants.Operation, bool) error @@ -97,7 +104,5 @@ type ClusterCreator interface { // EksaInstaller exposes the EKSA installer methods. type EksaInstaller interface { - ApplyBundles(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error - ApplyReleases(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error - CreateNamespaceIfNotPresent(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error + Install(ctx context.Context, log logr.Logger, cluster *types.Cluster, spec *cluster.Spec) error } diff --git a/pkg/workflows/interfaces/mocks/clients.go b/pkg/workflows/interfaces/mocks/clients.go index b122450683ef7..a9ac4c13b0ea7 100644 --- a/pkg/workflows/interfaces/mocks/clients.go +++ b/pkg/workflows/interfaces/mocks/clients.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/eks-anywhere/pkg/workflows/interfaces (interfaces: Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,EksaInstaller) +// Source: github.com/aws/eks-anywhere/pkg/workflows/interfaces (interfaces: Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,ClientFactory,EksaInstaller) // Package mocks is a generated GoMock package. package mocks @@ -9,6 +9,7 @@ import ( reflect "reflect" bootstrapper "github.com/aws/eks-anywhere/pkg/bootstrapper" + kubernetes "github.com/aws/eks-anywhere/pkg/clients/kubernetes" cluster "github.com/aws/eks-anywhere/pkg/cluster" constants "github.com/aws/eks-anywhere/pkg/constants" providers "github.com/aws/eks-anywhere/pkg/providers" @@ -182,20 +183,6 @@ func (mr *MockClusterManagerMockRecorder) CreateEKSANamespace(arg0, arg1 interfa return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateEKSANamespace", reflect.TypeOf((*MockClusterManager)(nil).CreateEKSANamespace), arg0, arg1) } -// CreateEKSAReleaseBundle mocks base method. -func (m *MockClusterManager) CreateEKSAReleaseBundle(arg0 context.Context, arg1 *types.Cluster, arg2 *cluster.Spec) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateEKSAReleaseBundle", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// CreateEKSAReleaseBundle indicates an expected call of CreateEKSAReleaseBundle. -func (mr *MockClusterManagerMockRecorder) CreateEKSAReleaseBundle(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateEKSAReleaseBundle", reflect.TypeOf((*MockClusterManager)(nil).CreateEKSAReleaseBundle), arg0, arg1, arg2) -} - // CreateEKSAResources mocks base method. func (m *MockClusterManager) CreateEKSAResources(arg0 context.Context, arg1 *types.Cluster, arg2 *cluster.Spec, arg3 providers.DatacenterConfig, arg4 []providers.MachineConfig) error { m.ctrl.T.Helper() @@ -980,6 +967,44 @@ func (mr *MockClusterCreatorMockRecorder) Run(arg0, arg1, arg2 interface{}) *gom return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Run", reflect.TypeOf((*MockClusterCreator)(nil).Run), arg0, arg1, arg2) } +// MockClientFactory is a mock of ClientFactory interface. +type MockClientFactory struct { + ctrl *gomock.Controller + recorder *MockClientFactoryMockRecorder +} + +// MockClientFactoryMockRecorder is the mock recorder for MockClientFactory. +type MockClientFactoryMockRecorder struct { + mock *MockClientFactory +} + +// NewMockClientFactory creates a new mock instance. +func NewMockClientFactory(ctrl *gomock.Controller) *MockClientFactory { + mock := &MockClientFactory{ctrl: ctrl} + mock.recorder = &MockClientFactoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockClientFactory) EXPECT() *MockClientFactoryMockRecorder { + return m.recorder +} + +// BuildClientFromKubeconfig mocks base method. +func (m *MockClientFactory) BuildClientFromKubeconfig(arg0 string) (kubernetes.Client, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "BuildClientFromKubeconfig", arg0) + ret0, _ := ret[0].(kubernetes.Client) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// BuildClientFromKubeconfig indicates an expected call of BuildClientFromKubeconfig. +func (mr *MockClientFactoryMockRecorder) BuildClientFromKubeconfig(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BuildClientFromKubeconfig", reflect.TypeOf((*MockClientFactory)(nil).BuildClientFromKubeconfig), arg0) +} + // MockEksaInstaller is a mock of EksaInstaller interface. type MockEksaInstaller struct { ctrl *gomock.Controller @@ -1003,44 +1028,16 @@ func (m *MockEksaInstaller) EXPECT() *MockEksaInstallerMockRecorder { return m.recorder } -// ApplyBundles mocks base method. -func (m *MockEksaInstaller) ApplyBundles(arg0 context.Context, arg1 logr.Logger, arg2 *types.Cluster, arg3 *cluster.Spec) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ApplyBundles", arg0, arg1, arg2, arg3) - ret0, _ := ret[0].(error) - return ret0 -} - -// ApplyBundles indicates an expected call of ApplyBundles. -func (mr *MockEksaInstallerMockRecorder) ApplyBundles(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyBundles", reflect.TypeOf((*MockEksaInstaller)(nil).ApplyBundles), arg0, arg1, arg2, arg3) -} - -// ApplyReleases mocks base method. -func (m *MockEksaInstaller) ApplyReleases(arg0 context.Context, arg1 logr.Logger, arg2 *types.Cluster, arg3 *cluster.Spec) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ApplyReleases", arg0, arg1, arg2, arg3) - ret0, _ := ret[0].(error) - return ret0 -} - -// ApplyReleases indicates an expected call of ApplyReleases. -func (mr *MockEksaInstallerMockRecorder) ApplyReleases(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyReleases", reflect.TypeOf((*MockEksaInstaller)(nil).ApplyReleases), arg0, arg1, arg2, arg3) -} - -// CreateNamespaceIfNotPresent mocks base method. -func (m *MockEksaInstaller) CreateNamespaceIfNotPresent(arg0 context.Context, arg1 logr.Logger, arg2 *types.Cluster, arg3 *cluster.Spec) error { +// Install mocks base method. +func (m *MockEksaInstaller) Install(arg0 context.Context, arg1 logr.Logger, arg2 *types.Cluster, arg3 *cluster.Spec) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateNamespaceIfNotPresent", arg0, arg1, arg2, arg3) + ret := m.ctrl.Call(m, "Install", arg0, arg1, arg2, arg3) ret0, _ := ret[0].(error) return ret0 } -// CreateNamespaceIfNotPresent indicates an expected call of CreateNamespaceIfNotPresent. -func (mr *MockEksaInstallerMockRecorder) CreateNamespaceIfNotPresent(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { +// Install indicates an expected call of Install. +func (mr *MockEksaInstallerMockRecorder) Install(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateNamespaceIfNotPresent", reflect.TypeOf((*MockEksaInstaller)(nil).CreateNamespaceIfNotPresent), arg0, arg1, arg2, arg3) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Install", reflect.TypeOf((*MockEksaInstaller)(nil).Install), arg0, arg1, arg2, arg3) } diff --git a/pkg/workflows/management/create_install_eksa.go b/pkg/workflows/management/create_install_eksa.go index 818429adb2639..01a578e3530ed 100644 --- a/pkg/workflows/management/create_install_eksa.go +++ b/pkg/workflows/management/create_install_eksa.go @@ -35,15 +35,9 @@ func (s *installEksaComponentsOnBootstrapTask) Checkpoint() *task.CompletedTask } func installEKSAComponents(ctx context.Context, commandContext *task.CommandContext, targetCluster *types.Cluster) error { - logger.Info("Installing EKS-A custom components (CRD and controller)") - err := commandContext.ClusterManager.InstallCustomComponents(ctx, commandContext.ClusterSpec, targetCluster, commandContext.Provider) - if err != nil { - commandContext.SetError(err) - return err - } logger.Info("Installing EKS-D components") - err = commandContext.EksdInstaller.InstallEksdCRDs(ctx, commandContext.ClusterSpec, targetCluster) + err := commandContext.EksdInstaller.InstallEksdCRDs(ctx, commandContext.ClusterSpec, targetCluster) if err != nil { commandContext.SetError(err) return err @@ -51,19 +45,8 @@ func installEKSAComponents(ctx context.Context, commandContext *task.CommandCont logger.Info("Creating EKS-A CRDs instances") - err = commandContext.EksaInstaller.CreateNamespaceIfNotPresent(ctx, logger.Get(), targetCluster, commandContext.ClusterSpec) - if err != nil { - commandContext.SetError(err) - return err - } - - err = commandContext.EksaInstaller.ApplyBundles(ctx, logger.Get(), targetCluster, commandContext.ClusterSpec) - if err != nil { - commandContext.SetError(err) - return err - } - - err = commandContext.EksaInstaller.ApplyReleases(ctx, logger.Get(), targetCluster, commandContext.ClusterSpec) + logger.Info("Installing EKS-A custom components (CRD and controller)") + err = commandContext.ClusterManager.InstallCustomComponents(ctx, commandContext.ClusterSpec, targetCluster, commandContext.Provider) if err != nil { commandContext.SetError(err) return err