Skip to content

Commit c114a7b

Browse files
authored
Inject kibana clients from the command builders (#1418)
1 parent 60ee154 commit c114a7b

File tree

10 files changed

+61
-50
lines changed

10 files changed

+61
-50
lines changed

cmd/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/elastic/elastic-package/internal/install"
1616
"github.com/elastic/elastic-package/internal/packages"
1717
"github.com/elastic/elastic-package/internal/service"
18+
"github.com/elastic/elastic-package/internal/stack"
1819
"github.com/elastic/elastic-package/internal/testrunner/runners/system"
1920
)
2021

@@ -67,6 +68,15 @@ func upCommandAction(cmd *cobra.Command, args []string) error {
6768
return err
6869
}
6970

71+
kibanaClient, err := stack.NewKibanaClient()
72+
if err != nil {
73+
return fmt.Errorf("cannot create Kibana client: %w", err)
74+
}
75+
stackVersion, err := kibanaClient.Version()
76+
if err != nil {
77+
return fmt.Errorf("cannot request Kibana version: %w", err)
78+
}
79+
7080
_, serviceName := filepath.Split(packageRoot)
7181
err = service.BootUp(service.Options{
7282
Profile: profile,
@@ -75,6 +85,7 @@ func upCommandAction(cmd *cobra.Command, args []string) error {
7585
DevDeployDir: system.DevDeployDir,
7686
DataStreamRootPath: dataStreamPath,
7787
Variant: variantFlag,
88+
StackVersion: stackVersion.Version(),
7889
})
7990
if err != nil {
8091
return fmt.Errorf("up command failed: %w", err)

cmd/testrunner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
224224
return err
225225
}
226226

227+
kibanaClient, err := stack.NewKibanaClient()
228+
if err != nil {
229+
return fmt.Errorf("can't create Kibana client: %w", err)
230+
}
231+
227232
var results []testrunner.TestResult
228233
for _, folder := range testFolders {
229234
r, err := testrunner.Run(testType, testrunner.TestOptions{
@@ -232,6 +237,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
232237
PackageRootPath: packageRootPath,
233238
GenerateTestResult: generateTestResult,
234239
API: esClient.API,
240+
KibanaClient: kibanaClient,
235241
DeferCleanup: deferCleanup,
236242
ServiceVariant: variantFlag,
237243
WithCoverage: testCoverage,

internal/benchrunner/runners/system/runner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ func (r *runner) setUp() error {
230230
func (r *runner) run() (report reporters.Reportable, err error) {
231231
var service servicedeployer.DeployedService
232232
if r.scenario.Corpora.InputService != nil {
233+
stackVersion, err := r.options.KibanaClient.Version()
234+
if err != nil {
235+
return nil, fmt.Errorf("cannot request Kibana version: %w", err)
236+
}
237+
233238
// Setup service.
234239
logger.Debug("setting up service...")
235240
opts := servicedeployer.FactoryOptions{
@@ -238,6 +243,7 @@ func (r *runner) run() (report reporters.Reportable, err error) {
238243
Variant: r.options.Variant,
239244
Profile: r.options.Profile,
240245
Type: servicedeployer.TypeBench,
246+
StackVersion: stackVersion.Version(),
241247
}
242248
serviceDeployer, err := servicedeployer.Factory(opts)
243249

internal/service/boot.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Options struct {
2626
PackageRootPath string
2727
DevDeployDir string
2828
DataStreamRootPath string
29+
StackVersion string
2930

3031
Variant string
3132
}
@@ -39,6 +40,7 @@ func BootUp(options Options) error {
3940
DataStreamRootPath: options.DataStreamRootPath,
4041
DevDeployDir: options.DevDeployDir,
4142
Variant: options.Variant,
43+
StackVersion: options.StackVersion,
4244
})
4345
if err != nil {
4446
return fmt.Errorf("can't create the service deployer instance: %w", err)

internal/servicedeployer/custom_agent.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ var dockerCustomAgentDockerfileContent []byte
3434
type CustomAgentDeployer struct {
3535
profile *profile.Profile
3636
dockerComposeFile string
37+
stackVersion string
3738
}
3839

3940
// NewCustomAgentDeployer returns a new instance of a deployedCustomAgent.
40-
func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string) (*CustomAgentDeployer, error) {
41+
func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string, stackVersion string) (*CustomAgentDeployer, error) {
4142
return &CustomAgentDeployer{
4243
profile: profile,
4344
dockerComposeFile: dockerComposeFile,
45+
stackVersion: stackVersion,
4446
}, nil
4547
}
4648

@@ -53,23 +55,13 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err
5355
return nil, fmt.Errorf("can't read application configuration: %w", err)
5456
}
5557

56-
kibanaClient, err := stack.NewKibanaClient()
57-
if err != nil {
58-
return nil, fmt.Errorf("can't create Kibana client: %w", err)
59-
}
60-
61-
stackVersion, err := kibanaClient.Version()
62-
if err != nil {
63-
return nil, fmt.Errorf("can't read Kibana injected metadata: %w", err)
64-
}
65-
6658
caCertPath, ok := os.LookupEnv(stack.CACertificateEnv)
6759
if !ok {
6860
return nil, fmt.Errorf("can't locate CA certificate: %s environment variable not set", stack.CACertificateEnv)
6961
}
7062

7163
env := append(
72-
appConfig.StackImageRefs(stackVersion.Version()).AsEnv(),
64+
appConfig.StackImageRefs(d.stackVersion).AsEnv(),
7365
fmt.Sprintf("%s=%s", serviceLogsDirEnv, inCtxt.Logs.Folder.Local),
7466
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
7567
)

internal/servicedeployer/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type FactoryOptions struct {
2626
DataStreamRootPath string
2727
DevDeployDir string
2828
Type string
29+
StackVersion string
2930

3031
Variant string
3132
}
@@ -48,7 +49,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
4849
switch serviceDeployerName {
4950
case "k8s":
5051
if _, err := os.Stat(serviceDeployerPath); err == nil {
51-
return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath)
52+
return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath, options.StackVersion)
5253
}
5354
case "docker":
5455
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
@@ -67,8 +68,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
6768
if _, err := os.Stat(customAgentCfgYMLPath); err != nil {
6869
return nil, fmt.Errorf("can't find expected file custom-agent.yml: %w", err)
6970
}
70-
return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath)
71-
71+
return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath, options.StackVersion)
7272
case "tf":
7373
if _, err := os.Stat(serviceDeployerPath); err == nil {
7474
return NewTerraformServiceDeployer(serviceDeployerPath)

internal/servicedeployer/kubernetes.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
type KubernetesServiceDeployer struct {
2828
profile *profile.Profile
2929
definitionsDir string
30+
stackVersion string
3031
}
3132

3233
type kubernetesDeployedService struct {
@@ -71,10 +72,11 @@ func (s *kubernetesDeployedService) SetContext(sc ServiceContext) error {
7172
var _ DeployedService = new(kubernetesDeployedService)
7273

7374
// NewKubernetesServiceDeployer function creates a new instance of KubernetesServiceDeployer.
74-
func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string) (*KubernetesServiceDeployer, error) {
75+
func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string, stackVersion string) (*KubernetesServiceDeployer, error) {
7576
return &KubernetesServiceDeployer{
7677
profile: profile,
7778
definitionsDir: definitionsPath,
79+
stackVersion: stackVersion,
7880
}, nil
7981
}
8082

@@ -91,7 +93,7 @@ func (ksd KubernetesServiceDeployer) SetUp(ctxt ServiceContext) (DeployedService
9193
return nil, fmt.Errorf("can't connect control plane to Elastic stack network: %w", err)
9294
}
9395

94-
err = installElasticAgentInCluster()
96+
err = installElasticAgentInCluster(ksd.stackVersion)
9597
if err != nil {
9698
return nil, fmt.Errorf("can't install Elastic-Agent in the Kubernetes cluster: %w", err)
9799
}
@@ -145,20 +147,10 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) {
145147
return definitionPaths, nil
146148
}
147149

148-
func installElasticAgentInCluster() error {
150+
func installElasticAgentInCluster(stackVersion string) error {
149151
logger.Debug("install Elastic Agent in the Kubernetes cluster")
150152

151-
kibanaClient, err := stack.NewKibanaClient()
152-
if err != nil {
153-
return fmt.Errorf("can't create Kibana client: %w", err)
154-
}
155-
156-
stackVersion, err := kibanaClient.Version()
157-
if err != nil {
158-
return fmt.Errorf("can't read Kibana injected metadata: %w", err)
159-
}
160-
161-
elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion.Version())
153+
elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion)
162154
if err != nil {
163155
return fmt.Errorf("can't retrieve Kubernetes file for Elastic Agent: %w", err)
164156
}

internal/testrunner/runners/asset/runner.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"fmt"
99
"strings"
1010

11+
"github.com/elastic/elastic-package/internal/kibana"
1112
"github.com/elastic/elastic-package/internal/logger"
1213
"github.com/elastic/elastic-package/internal/packages"
1314
"github.com/elastic/elastic-package/internal/packages/installer"
14-
"github.com/elastic/elastic-package/internal/stack"
1515
"github.com/elastic/elastic-package/internal/testrunner"
1616
)
1717

@@ -27,6 +27,7 @@ const (
2727
type runner struct {
2828
testFolder testrunner.TestFolder
2929
packageRootPath string
30+
kibanaClient *kibana.Client
3031

3132
// Execution order of following handlers is defined in runner.tearDown() method.
3233
removePackageHandler func() error
@@ -52,6 +53,7 @@ func (r runner) CanRunPerDataStream() bool {
5253
func (r *runner) Run(options testrunner.TestOptions) ([]testrunner.TestResult, error) {
5354
r.testFolder = options.TestFolder
5455
r.packageRootPath = options.PackageRootPath
56+
r.kibanaClient = options.KibanaClient
5557

5658
return r.run()
5759
}
@@ -76,12 +78,8 @@ func (r *runner) run() ([]testrunner.TestResult, error) {
7678
}
7779

7880
logger.Debug("installing package...")
79-
kibanaClient, err := stack.NewKibanaClient()
80-
if err != nil {
81-
return result.WithError(fmt.Errorf("could not create kibana client: %w", err))
82-
}
8381
packageInstaller, err := installer.NewForPackage(installer.Options{
84-
Kibana: kibanaClient,
82+
Kibana: r.kibanaClient,
8583
RootPath: r.packageRootPath,
8684
SkipValidation: true,
8785
})

internal/testrunner/runners/system/runner.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,17 @@ func (r *runner) run() (results []testrunner.TestResult, err error) {
201201
logger.Debug("Running system tests for package")
202202
}
203203

204+
stackVersion, err := r.options.KibanaClient.Version()
205+
if err != nil {
206+
return result.WithError(fmt.Errorf("cannot request Kibana version: %w", err))
207+
}
208+
204209
devDeployPath, err := servicedeployer.FindDevDeployPath(servicedeployer.FactoryOptions{
205210
Profile: r.options.Profile,
206211
PackageRootPath: r.options.PackageRootPath,
207212
DataStreamRootPath: dataStreamPath,
208213
DevDeployDir: DevDeployDir,
214+
StackVersion: stackVersion.Version(),
209215
})
210216
if err != nil {
211217
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) {
224230
startTesting := time.Now()
225231
for _, cfgFile := range cfgFiles {
226232
for _, variantName := range r.selectVariants(variantsFile) {
227-
partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName)
233+
partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName, stackVersion.Version())
228234
results = append(results, partial...)
229235
if err != nil {
230236
return results, err
@@ -253,14 +259,15 @@ func (r *runner) run() (results []testrunner.TestResult, err error) {
253259
return results, nil
254260
}
255261

256-
func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName string) ([]testrunner.TestResult, error) {
262+
func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName, stackVersion string) ([]testrunner.TestResult, error) {
257263
serviceOptions := servicedeployer.FactoryOptions{
258264
Profile: r.options.Profile,
259265
PackageRootPath: r.options.PackageRootPath,
260266
DataStreamRootPath: dataStreamPath,
261267
DevDeployDir: DevDeployDir,
262268
Variant: variantName,
263269
Type: servicedeployer.TypeTest,
270+
StackVersion: stackVersion,
264271
}
265272

266273
var ctxt servicedeployer.ServiceContext
@@ -475,16 +482,11 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
475482
return result.WithError(fmt.Errorf("unable to reload system test case configuration: %w", err))
476483
}
477484

478-
kib, err := stack.NewKibanaClient()
479-
if err != nil {
480-
return result.WithError(fmt.Errorf("can't create Kibana client: %w", err))
481-
}
482-
483485
// Install the package before creating the policy, so we control exactly what is being
484486
// installed.
485487
logger.Debug("Installing package...")
486488
installer, err := installer.NewForPackage(installer.Options{
487-
Kibana: kib,
489+
Kibana: r.options.KibanaClient,
488490
RootPath: r.options.PackageRootPath,
489491
SkipValidation: true,
490492
})
@@ -516,21 +518,21 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
516518
Description: fmt.Sprintf("test policy created by elastic-package test system for data stream %s/%s", r.options.TestFolder.Package, r.options.TestFolder.DataStream),
517519
Namespace: "ep",
518520
}
519-
policy, err := kib.CreatePolicy(p)
521+
policy, err := r.options.KibanaClient.CreatePolicy(p)
520522
if err != nil {
521523
return result.WithError(fmt.Errorf("could not create test policy: %w", err))
522524
}
523525
r.deleteTestPolicyHandler = func() error {
524526
logger.Debug("deleting test policy...")
525-
if err := kib.DeletePolicy(*policy); err != nil {
527+
if err := r.options.KibanaClient.DeletePolicy(*policy); err != nil {
526528
return fmt.Errorf("error cleaning up test policy: %w", err)
527529
}
528530
return nil
529531
}
530532

531533
logger.Debug("adding package data stream to test policy...")
532534
ds := createPackageDatastream(*policy, *pkgManifest, policyTemplate, *dataStreamManifest, *config)
533-
if err := kib.AddPackageDataStreamToPolicy(ds); err != nil {
535+
if err := r.options.KibanaClient.AddPackageDataStreamToPolicy(ds); err != nil {
534536
return result.WithError(fmt.Errorf("could not add data stream config to policy: %w", err))
535537
}
536538

@@ -576,7 +578,7 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
576578
return result.WithError(err)
577579
}
578580

579-
agents, err := checkEnrolledAgents(kib, ctxt)
581+
agents, err := checkEnrolledAgents(r.options.KibanaClient, ctxt)
580582
if err != nil {
581583
return result.WithError(fmt.Errorf("can't check enrolled agents: %w", err))
582584
}
@@ -589,19 +591,19 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
589591
// Assign policy to agent
590592
r.resetAgentPolicyHandler = func() error {
591593
logger.Debug("reassigning original policy back to agent...")
592-
if err := kib.AssignPolicyToAgent(agent, origPolicy); err != nil {
594+
if err := r.options.KibanaClient.AssignPolicyToAgent(agent, origPolicy); err != nil {
593595
return fmt.Errorf("error reassigning original policy to agent: %w", err)
594596
}
595597
return nil
596598
}
597599

598-
policyWithDataStream, err := kib.GetPolicy(policy.ID)
600+
policyWithDataStream, err := r.options.KibanaClient.GetPolicy(policy.ID)
599601
if err != nil {
600602
return result.WithError(fmt.Errorf("could not read the policy with data stream: %w", err))
601603
}
602604

603605
logger.Debug("assigning package data stream to agent...")
604-
if err := kib.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil {
606+
if err := r.options.KibanaClient.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil {
605607
return result.WithError(fmt.Errorf("could not assign policy to agent: %w", err))
606608
}
607609

internal/testrunner/testrunner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/elastic/elastic-package/internal/elasticsearch"
17+
"github.com/elastic/elastic-package/internal/kibana"
1718
"github.com/elastic/elastic-package/internal/profile"
1819
)
1920

@@ -27,6 +28,7 @@ type TestOptions struct {
2728
PackageRootPath string
2829
GenerateTestResult bool
2930
API *elasticsearch.API
31+
KibanaClient *kibana.Client
3032

3133
DeferCleanup time.Duration
3234
ServiceVariant string

0 commit comments

Comments
 (0)