From f67823b2a61176de9c758ba2f554f7d2b60bc9e3 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 29 Aug 2023 17:51:43 +0200 Subject: [PATCH 1/2] Inject kibana clients from the command builders --- cmd/testrunner.go | 6 ++++++ internal/benchrunner/runners/system/runner.go | 6 ++++++ internal/servicedeployer/custom_agent.go | 16 ++++---------- internal/servicedeployer/factory.go | 6 +++--- internal/servicedeployer/kubernetes.go | 20 ++++++------------ internal/testrunner/runners/asset/runner.go | 10 ++++----- internal/testrunner/runners/system/runner.go | 21 +++++++------------ internal/testrunner/testrunner.go | 2 ++ 8 files changed, 39 insertions(+), 48 deletions(-) diff --git a/cmd/testrunner.go b/cmd/testrunner.go index 73c33c7cb5..40e57b2911 100644 --- a/cmd/testrunner.go +++ b/cmd/testrunner.go @@ -223,6 +223,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command return err } + kibanaClient, err := stack.NewKibanaClient() + if err != nil { + return fmt.Errorf("can't create Kibana client: %w", err) + } + var results []testrunner.TestResult for _, folder := range testFolders { r, err := testrunner.Run(testType, testrunner.TestOptions{ @@ -231,6 +236,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command PackageRootPath: packageRootPath, GenerateTestResult: generateTestResult, API: esClient.API, + KibanaClient: kibanaClient, DeferCleanup: deferCleanup, ServiceVariant: variantFlag, WithCoverage: testCoverage, diff --git a/internal/benchrunner/runners/system/runner.go b/internal/benchrunner/runners/system/runner.go index e8be30e0c4..2004083e59 100644 --- a/internal/benchrunner/runners/system/runner.go +++ b/internal/benchrunner/runners/system/runner.go @@ -230,6 +230,11 @@ func (r *runner) setUp() error { func (r *runner) run() (report reporters.Reportable, err error) { var service servicedeployer.DeployedService if r.scenario.Corpora.InputService != nil { + stackVersion, err := r.options.KibanaClient.Version() + if err != nil { + return nil, fmt.Errorf("cannot request Kibana version: %w", err) + } + // Setup service. logger.Debug("setting up service...") opts := servicedeployer.FactoryOptions{ @@ -238,6 +243,7 @@ func (r *runner) run() (report reporters.Reportable, err error) { Variant: r.options.Variant, Profile: r.options.Profile, Type: servicedeployer.TypeBench, + StackVersion: stackVersion.Version(), } serviceDeployer, err := servicedeployer.Factory(opts) diff --git a/internal/servicedeployer/custom_agent.go b/internal/servicedeployer/custom_agent.go index 41841c672a..8c3bb61e79 100644 --- a/internal/servicedeployer/custom_agent.go +++ b/internal/servicedeployer/custom_agent.go @@ -34,13 +34,15 @@ var dockerCustomAgentDockerfileContent []byte type CustomAgentDeployer struct { profile *profile.Profile dockerComposeFile string + stackVersion string } // NewCustomAgentDeployer returns a new instance of a deployedCustomAgent. -func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string) (*CustomAgentDeployer, error) { +func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string, stackVersion string) (*CustomAgentDeployer, error) { return &CustomAgentDeployer{ profile: profile, dockerComposeFile: dockerComposeFile, + stackVersion: stackVersion, }, nil } @@ -53,23 +55,13 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err return nil, fmt.Errorf("can't read application configuration: %w", err) } - kibanaClient, err := stack.NewKibanaClient() - if err != nil { - return nil, fmt.Errorf("can't create Kibana client: %w", err) - } - - stackVersion, err := kibanaClient.Version() - if err != nil { - return nil, fmt.Errorf("can't read Kibana injected metadata: %w", err) - } - caCertPath, ok := os.LookupEnv(stack.CACertificateEnv) if !ok { return nil, fmt.Errorf("can't locate CA certificate: %s environment variable not set", stack.CACertificateEnv) } env := append( - appConfig.StackImageRefs(stackVersion.Version()).AsEnv(), + appConfig.StackImageRefs(d.stackVersion).AsEnv(), fmt.Sprintf("%s=%s", serviceLogsDirEnv, inCtxt.Logs.Folder.Local), fmt.Sprintf("%s=%s", localCACertEnv, caCertPath), ) diff --git a/internal/servicedeployer/factory.go b/internal/servicedeployer/factory.go index 00021017be..3d3691dbe8 100644 --- a/internal/servicedeployer/factory.go +++ b/internal/servicedeployer/factory.go @@ -26,6 +26,7 @@ type FactoryOptions struct { DataStreamRootPath string DevDeployDir string Type string + StackVersion string Variant string } @@ -48,7 +49,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) { switch serviceDeployerName { case "k8s": if _, err := os.Stat(serviceDeployerPath); err == nil { - return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath) + return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath, options.StackVersion) } case "docker": dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml") @@ -67,8 +68,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) { if _, err := os.Stat(customAgentCfgYMLPath); err != nil { return nil, fmt.Errorf("can't find expected file custom-agent.yml: %w", err) } - return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath) - + return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath, options.StackVersion) case "tf": if _, err := os.Stat(serviceDeployerPath); err == nil { return NewTerraformServiceDeployer(serviceDeployerPath) diff --git a/internal/servicedeployer/kubernetes.go b/internal/servicedeployer/kubernetes.go index 0c20bd9c32..6d1870eb1a 100644 --- a/internal/servicedeployer/kubernetes.go +++ b/internal/servicedeployer/kubernetes.go @@ -27,6 +27,7 @@ import ( type KubernetesServiceDeployer struct { profile *profile.Profile definitionsDir string + stackVersion string } type kubernetesDeployedService struct { @@ -71,10 +72,11 @@ func (s *kubernetesDeployedService) SetContext(sc ServiceContext) error { var _ DeployedService = new(kubernetesDeployedService) // NewKubernetesServiceDeployer function creates a new instance of KubernetesServiceDeployer. -func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string) (*KubernetesServiceDeployer, error) { +func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string, stackVersion string) (*KubernetesServiceDeployer, error) { return &KubernetesServiceDeployer{ profile: profile, definitionsDir: definitionsPath, + stackVersion: stackVersion, }, nil } @@ -91,7 +93,7 @@ func (ksd KubernetesServiceDeployer) SetUp(ctxt ServiceContext) (DeployedService return nil, fmt.Errorf("can't connect control plane to Elastic stack network: %w", err) } - err = installElasticAgentInCluster() + err = installElasticAgentInCluster(ksd.stackVersion) if err != nil { return nil, fmt.Errorf("can't install Elastic-Agent in the Kubernetes cluster: %w", err) } @@ -145,20 +147,10 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) { return definitionPaths, nil } -func installElasticAgentInCluster() error { +func installElasticAgentInCluster(stackVersion string) error { logger.Debug("install Elastic Agent in the Kubernetes cluster") - kibanaClient, err := stack.NewKibanaClient() - if err != nil { - return fmt.Errorf("can't create Kibana client: %w", err) - } - - stackVersion, err := kibanaClient.Version() - if err != nil { - return fmt.Errorf("can't read Kibana injected metadata: %w", err) - } - - elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion.Version()) + elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion) if err != nil { return fmt.Errorf("can't retrieve Kubernetes file for Elastic Agent: %w", err) } diff --git a/internal/testrunner/runners/asset/runner.go b/internal/testrunner/runners/asset/runner.go index e218410a2b..27ca61c03c 100644 --- a/internal/testrunner/runners/asset/runner.go +++ b/internal/testrunner/runners/asset/runner.go @@ -8,10 +8,10 @@ import ( "fmt" "strings" + "github.com/elastic/elastic-package/internal/kibana" "github.com/elastic/elastic-package/internal/logger" "github.com/elastic/elastic-package/internal/packages" "github.com/elastic/elastic-package/internal/packages/installer" - "github.com/elastic/elastic-package/internal/stack" "github.com/elastic/elastic-package/internal/testrunner" ) @@ -27,6 +27,7 @@ const ( type runner struct { testFolder testrunner.TestFolder packageRootPath string + kibanaClient *kibana.Client // Execution order of following handlers is defined in runner.tearDown() method. removePackageHandler func() error @@ -52,6 +53,7 @@ func (r runner) CanRunPerDataStream() bool { func (r *runner) Run(options testrunner.TestOptions) ([]testrunner.TestResult, error) { r.testFolder = options.TestFolder r.packageRootPath = options.PackageRootPath + r.kibanaClient = options.KibanaClient return r.run() } @@ -76,12 +78,8 @@ func (r *runner) run() ([]testrunner.TestResult, error) { } logger.Debug("installing package...") - kibanaClient, err := stack.NewKibanaClient() - if err != nil { - return result.WithError(fmt.Errorf("could not create kibana client: %w", err)) - } packageInstaller, err := installer.NewForPackage(installer.Options{ - Kibana: kibanaClient, + Kibana: r.kibanaClient, RootPath: r.packageRootPath, SkipValidation: true, }) diff --git a/internal/testrunner/runners/system/runner.go b/internal/testrunner/runners/system/runner.go index de99c6aa6b..ffbb8219d0 100644 --- a/internal/testrunner/runners/system/runner.go +++ b/internal/testrunner/runners/system/runner.go @@ -475,16 +475,11 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext return result.WithError(fmt.Errorf("unable to reload system test case configuration: %w", err)) } - kib, err := stack.NewKibanaClient() - if err != nil { - return result.WithError(fmt.Errorf("can't create Kibana client: %w", err)) - } - // Install the package before creating the policy, so we control exactly what is being // installed. logger.Debug("Installing package...") installer, err := installer.NewForPackage(installer.Options{ - Kibana: kib, + Kibana: r.options.KibanaClient, RootPath: r.options.PackageRootPath, SkipValidation: true, }) @@ -516,13 +511,13 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext Description: fmt.Sprintf("test policy created by elastic-package test system for data stream %s/%s", r.options.TestFolder.Package, r.options.TestFolder.DataStream), Namespace: "ep", } - policy, err := kib.CreatePolicy(p) + policy, err := r.options.KibanaClient.CreatePolicy(p) if err != nil { return result.WithError(fmt.Errorf("could not create test policy: %w", err)) } r.deleteTestPolicyHandler = func() error { logger.Debug("deleting test policy...") - if err := kib.DeletePolicy(*policy); err != nil { + if err := r.options.KibanaClient.DeletePolicy(*policy); err != nil { return fmt.Errorf("error cleaning up test policy: %w", err) } return nil @@ -530,7 +525,7 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext logger.Debug("adding package data stream to test policy...") ds := createPackageDatastream(*policy, *pkgManifest, policyTemplate, *dataStreamManifest, *config) - if err := kib.AddPackageDataStreamToPolicy(ds); err != nil { + if err := r.options.KibanaClient.AddPackageDataStreamToPolicy(ds); err != nil { return result.WithError(fmt.Errorf("could not add data stream config to policy: %w", err)) } @@ -576,7 +571,7 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext return result.WithError(err) } - agents, err := checkEnrolledAgents(kib, ctxt) + agents, err := checkEnrolledAgents(r.options.KibanaClient, ctxt) if err != nil { return result.WithError(fmt.Errorf("can't check enrolled agents: %w", err)) } @@ -589,19 +584,19 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext // Assign policy to agent r.resetAgentPolicyHandler = func() error { logger.Debug("reassigning original policy back to agent...") - if err := kib.AssignPolicyToAgent(agent, origPolicy); err != nil { + if err := r.options.KibanaClient.AssignPolicyToAgent(agent, origPolicy); err != nil { return fmt.Errorf("error reassigning original policy to agent: %w", err) } return nil } - policyWithDataStream, err := kib.GetPolicy(policy.ID) + policyWithDataStream, err := r.options.KibanaClient.GetPolicy(policy.ID) if err != nil { return result.WithError(fmt.Errorf("could not read the policy with data stream: %w", err)) } logger.Debug("assigning package data stream to agent...") - if err := kib.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil { + if err := r.options.KibanaClient.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil { return result.WithError(fmt.Errorf("could not assign policy to agent: %w", err)) } diff --git a/internal/testrunner/testrunner.go b/internal/testrunner/testrunner.go index 7aad921f13..2c03e01341 100644 --- a/internal/testrunner/testrunner.go +++ b/internal/testrunner/testrunner.go @@ -14,6 +14,7 @@ import ( "time" "github.com/elastic/elastic-package/internal/elasticsearch" + "github.com/elastic/elastic-package/internal/kibana" "github.com/elastic/elastic-package/internal/profile" ) @@ -27,6 +28,7 @@ type TestOptions struct { PackageRootPath string GenerateTestResult bool API *elasticsearch.API + KibanaClient *kibana.Client DeferCleanup time.Duration ServiceVariant string From 4a5374a45f0d3534eec3cb218a78a06c27af574d Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 29 Aug 2023 18:35:02 +0200 Subject: [PATCH 2/2] Missing stack version in service deployer --- cmd/service.go | 11 +++++++++++ internal/service/boot.go | 2 ++ internal/testrunner/runners/system/runner.go | 11 +++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/service.go b/cmd/service.go index c095385c01..689574fb79 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -15,6 +15,7 @@ import ( "github.com/elastic/elastic-package/internal/install" "github.com/elastic/elastic-package/internal/packages" "github.com/elastic/elastic-package/internal/service" + "github.com/elastic/elastic-package/internal/stack" "github.com/elastic/elastic-package/internal/testrunner/runners/system" ) @@ -66,6 +67,15 @@ func upCommandAction(cmd *cobra.Command, args []string) error { return err } + kibanaClient, err := stack.NewKibanaClient() + if err != nil { + return fmt.Errorf("cannot create Kibana client: %w", err) + } + stackVersion, err := kibanaClient.Version() + if err != nil { + return fmt.Errorf("cannot request Kibana version: %w", err) + } + _, serviceName := filepath.Split(packageRoot) err = service.BootUp(service.Options{ Profile: profile, @@ -74,6 +84,7 @@ func upCommandAction(cmd *cobra.Command, args []string) error { DevDeployDir: system.DevDeployDir, DataStreamRootPath: dataStreamPath, Variant: variantFlag, + StackVersion: stackVersion.Version(), }) if err != nil { return fmt.Errorf("up command failed: %w", err) diff --git a/internal/service/boot.go b/internal/service/boot.go index b71750d234..7303b3dce0 100644 --- a/internal/service/boot.go +++ b/internal/service/boot.go @@ -26,6 +26,7 @@ type Options struct { PackageRootPath string DevDeployDir string DataStreamRootPath string + StackVersion string Variant string } @@ -39,6 +40,7 @@ func BootUp(options Options) error { DataStreamRootPath: options.DataStreamRootPath, DevDeployDir: options.DevDeployDir, Variant: options.Variant, + StackVersion: options.StackVersion, }) if err != nil { return fmt.Errorf("can't create the service deployer instance: %w", err) diff --git a/internal/testrunner/runners/system/runner.go b/internal/testrunner/runners/system/runner.go index ffbb8219d0..3acfaccd5f 100644 --- a/internal/testrunner/runners/system/runner.go +++ b/internal/testrunner/runners/system/runner.go @@ -201,11 +201,17 @@ func (r *runner) run() (results []testrunner.TestResult, err error) { logger.Debug("Running system tests for package") } + stackVersion, err := r.options.KibanaClient.Version() + if err != nil { + return result.WithError(fmt.Errorf("cannot request Kibana version: %w", err)) + } + devDeployPath, err := servicedeployer.FindDevDeployPath(servicedeployer.FactoryOptions{ Profile: r.options.Profile, PackageRootPath: r.options.PackageRootPath, DataStreamRootPath: dataStreamPath, DevDeployDir: DevDeployDir, + StackVersion: stackVersion.Version(), }) if err != nil { return result.WithError(fmt.Errorf("_dev/deploy directory not found: %w", err)) @@ -224,7 +230,7 @@ func (r *runner) run() (results []testrunner.TestResult, err error) { startTesting := time.Now() for _, cfgFile := range cfgFiles { for _, variantName := range r.selectVariants(variantsFile) { - partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName) + partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName, stackVersion.Version()) results = append(results, partial...) if err != nil { return results, err @@ -253,7 +259,7 @@ func (r *runner) run() (results []testrunner.TestResult, err error) { return results, nil } -func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName string) ([]testrunner.TestResult, error) { +func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName, stackVersion string) ([]testrunner.TestResult, error) { serviceOptions := servicedeployer.FactoryOptions{ Profile: r.options.Profile, PackageRootPath: r.options.PackageRootPath, @@ -261,6 +267,7 @@ func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationMa DevDeployDir: DevDeployDir, Variant: variantName, Type: servicedeployer.TypeTest, + StackVersion: stackVersion, } var ctxt servicedeployer.ServiceContext