-
Notifications
You must be signed in to change notification settings - Fork 39
feat: support running k8s autodiscover suite for Beats PRs and local repositories #1115
Changes from all commits
4b3cbcc
60fc387
ba6a90b
fb6b9c0
a5b25f9
f32a1fb
30b5d8d
a89325c
4b49b0b
ca791be
f9b97f8
c2f9b1d
4e7c273
3861cd7
7285dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
|
|
@@ -18,14 +22,28 @@ import ( | |
| messages "github.com/cucumber/messages-go/v10" | ||
| log "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/elastic/e2e-testing/cli/config" | ||
| "github.com/elastic/e2e-testing/internal/common" | ||
| "github.com/elastic/e2e-testing/internal/docker" | ||
| "github.com/elastic/e2e-testing/internal/kubernetes" | ||
| "github.com/elastic/e2e-testing/internal/shell" | ||
| "github.com/elastic/e2e-testing/internal/utils" | ||
| ) | ||
|
|
||
| var beatVersions = map[string]string{} | ||
|
|
||
| const defaultBeatVersion = "8.0.0-SNAPSHOT" | ||
| const defaultEventsWaitTimeout = 120 * time.Second | ||
| const defaultDeployWaitTimeout = 120 * time.Second | ||
|
|
||
| var defaultEventsWaitTimeout = 60 * time.Second | ||
| var defaultDeployWaitTimeout = 60 * time.Second | ||
|
|
||
| func init() { | ||
| // initialise timeout factor | ||
| common.TimeoutFactor = shell.GetEnvInteger("TIMEOUT_FACTOR", common.TimeoutFactor) | ||
|
Comment on lines
+41
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. Do this initialization in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm, but then it's not ensured that the init method will be called in the proper order, right? IIRC Golang is not deterministic when talking about init functions order
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we still want to do so, I'd move this file's init code to the beforeSuite phase.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There are some guarantees in order, for example all the inits in an imported package are executed before continuing, or if a file has multiple inits, they are executed in order of definition. |
||
|
|
||
| defaultEventsWaitTimeout = defaultEventsWaitTimeout * time.Duration(common.TimeoutFactor) | ||
| defaultDeployWaitTimeout = defaultDeployWaitTimeout * time.Duration(common.TimeoutFactor) | ||
| } | ||
|
|
||
| type podsManager struct { | ||
| kubectl kubernetes.Control | ||
|
|
@@ -35,6 +53,11 @@ type podsManager struct { | |
| func (m *podsManager) executeTemplateFor(podName string, writer io.Writer, options []string) error { | ||
| path := filepath.Join("testdata/templates", sanitizeName(podName)+".yml.tmpl") | ||
|
|
||
| err := m.configureDockerImage(podName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| usedOptions := make(map[string]bool) | ||
| funcs := template.FuncMap{ | ||
| "option": func(o string) bool { | ||
|
|
@@ -50,7 +73,7 @@ func (m *podsManager) executeTemplateFor(podName string, writer io.Writer, optio | |
| return utils.GetDockerNamespaceEnvVar("beats") | ||
| }, | ||
| "beats_version": func() string { | ||
| return shell.GetEnv("GITHUB_CHECK_SHA1", shell.GetEnv("BEAT_VERSION", defaultBeatVersion)) | ||
| return beatVersions[podName] | ||
| }, | ||
| "namespace": func() string { | ||
| return m.kubectl.Namespace | ||
|
|
@@ -86,6 +109,64 @@ func (m *podsManager) executeTemplateFor(podName string, writer io.Writer, optio | |
| return nil | ||
| } | ||
|
|
||
| func (m *podsManager) configureDockerImage(podName string) error { | ||
| if podName != "filebeat" && podName != "heartbeat" && podName != "metricbeat" { | ||
| log.Debugf("Not processing custom binaries for pod: %s. Only [filebeat, heartbeat, metricbeat] will be processed", podName) | ||
| return nil | ||
| } | ||
|
|
||
| // we are caching the versions by pod to avoid downloading and loading/tagging the Docker image multiple times | ||
| if beatVersions[podName] != "" { | ||
| log.Tracef("The beat version was already loaded: %s", beatVersions[podName]) | ||
| return nil | ||
| } | ||
|
|
||
| beatVersion := shell.GetEnv("BEAT_VERSION", defaultBeatVersion) | ||
|
|
||
| useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") | ||
| beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") | ||
| if useCISnapshots || beatsLocalPath != "" { | ||
| log.Debugf("Configuring Docker image for %s", podName) | ||
|
|
||
| // this method will detect if the GITHUB_CHECK_SHA1 variable is set | ||
| artifactName := utils.BuildArtifactName(podName, beatVersion, defaultBeatVersion, "linux", "amd64", "tar.gz", true) | ||
|
|
||
| imagePath, err := utils.FetchBeatsBinary(artifactName, podName, beatVersion, defaultBeatVersion, common.TimeoutFactor, true) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // load the TAR file into the docker host as a Docker image | ||
| err = docker.LoadImage(imagePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| beatVersion = beatVersion + "-amd64" | ||
|
|
||
| // tag the image with the proper docker tag, including platform | ||
| err = docker.TagImage( | ||
| "docker.elastic.co/beats/"+podName+":"+defaultBeatVersion, | ||
| "docker.elastic.co/observability-ci/"+podName+":"+beatVersion, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // load PR image into kind | ||
| err = cluster.LoadImage(m.ctx, "docker.elastic.co/observability-ci/"+podName+":"+beatVersion) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| } | ||
|
|
||
| log.Tracef("Caching beat version '%s' for %s", beatVersion, podName) | ||
| beatVersions[podName] = beatVersion | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (m *podsManager) isDeleted(podName string, options []string) error { | ||
| var buf bytes.Buffer | ||
| err := m.executeTemplateFor(podName, &buf, options) | ||
|
|
@@ -388,6 +469,9 @@ func InitializeTestSuite(ctx *godog.TestSuiteContext) { | |
| log.DeferExitHandler(cancel) | ||
|
|
||
| ctx.BeforeSuite(func() { | ||
| // init logger | ||
| config.Init() | ||
|
|
||
| err := cluster.Initialize(suiteContext, "testdata/kind.yml") | ||
| if err != nil { | ||
| log.WithError(err).Fatal("Failed to initialize cluster") | ||
|
|
@@ -409,10 +493,10 @@ func InitializeScenario(ctx *godog.ScenarioContext) { | |
|
|
||
| var kubectl kubernetes.Control | ||
| var pods podsManager | ||
| ctx.BeforeScenario(func(*messages.Pickle) { | ||
| ctx.BeforeScenario(func(p *messages.Pickle) { | ||
| kubectl = cluster.Kubectl().WithNamespace(scenarioCtx, "") | ||
| if kubectl.Namespace != "" { | ||
| log.Debugf("Running scenario in namespace: %s", kubectl.Namespace) | ||
| log.Debugf("Running scenario %s in namespace: %s", p.Name, kubectl.Namespace) | ||
| } | ||
| pods.kubectl = kubectl | ||
| pods.ctx = scenarioCtx | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using test framework default timeout factor of 3 (5 on CI)