This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Kubernetes autodiscover suite #1064
Merged
jsoriano
merged 36 commits into
elastic:master
from
jsoriano:kubernetes-autodiscover-suite
Apr 26, 2021
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
308124f
Initial kubernetes cluster
jsoriano 99d43ef
Skeleton for pods manager
jsoriano b7c5f3b
Complete some scenarios
jsoriano 9659a54
Fix issues with namespace creation
jsoriano 93be1a2
Wait for beats to be running
jsoriano ebab3ac
Implement test for short living cronjob
jsoriano 8a81e31
Remove the cluster is available step
jsoriano 8ac0978
Use more specific steps
jsoriano fc9d440
Recover check for unused options
jsoriano 55b428a
Add kubernetes autodiscover suite to .ci
jsoriano 6afe28b
Merge remote-tracking branch 'origin/master' into kubernetes-autodisc…
jsoriano b5c99eb
Fix imports after merge
jsoriano 128cd16
Delete cluster role binding when deleting namespace
jsoriano f4e343a
Implement scenarios for init and ephemeral containers
jsoriano 8c92fd1
Implement metricbeat scenarios
jsoriano c60ddd7
Fix metricbeat scenario with named ports
jsoriano 9f86699
Implement heartbeat scenario
jsoriano 38469d3
Add heartbeat scenarios
jsoriano 07f910c
Merge remote-tracking branch 'origin/master' into kubernetes-autodisc…
jsoriano 1cee532
Use backoff libraries
jsoriano 6954b9c
Split in different feature files
jsoriano dcd9873
Get namespace and snapshot version
jsoriano 8bd551c
Print kind version when using it to start a cluster
jsoriano 6f8cd44
Install fixed versions of kubectl and kind
jsoriano a644293
Reuse parameters for helm and autodiscover tests
jsoriano aec8258
Add README
jsoriano e8b52f4
Missing quote in readme
jsoriano d8b72ae
Split service availability test in two
jsoriano 24c1b22
Remove test suffix
jsoriano 821124f
Add log messsage when kind cluster is deleted.
jsoriano bffe42e
Run in different workers
jsoriano 7228d01
Fix linting
jsoriano ab92ebe
Merge branch 'master' into kubernetes-autodiscover-suite
mdelapenya b3dd0de
Update e2e/_suites/kubernetes-autodiscover/features/filebeat.feature
adam-stokes c02aae6
Adjust linting
jsoriano 164ae9d
Add UseBackground exception to the pre commit hook too
jsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.ci/scripts/install-kubernetes-autodiscover-test-dependencies.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| ## 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. | ||
|
|
||
| set -euxo pipefail | ||
| # | ||
| # Install the dependencies using the install and test make goals. | ||
| # | ||
| # Parameters: | ||
| # - KIND_VERSION - that's the Kind version which will be installed and enabled. | ||
| # - KUBERNETES_VERSION - that's the Kubernetes version which will be installed and enabled. | ||
| # | ||
|
|
||
| MSG="parameter missing." | ||
| HOME=${HOME:?$MSG} | ||
|
|
||
| KIND_VERSION="v${KIND_VERSION:-"0.10.0"}" | ||
| KUBERNETES_VERSION="${KUBERNETES_VERSION:-"1.18.2"}" | ||
|
|
||
| KUBECTL_CMD="${HOME}/bin/kubectl" | ||
|
|
||
| # Install kind as a Go binary | ||
| GO111MODULE="on" go get sigs.k8s.io/kind@${KIND_VERSION} | ||
|
|
||
| mkdir -p "${HOME}/bin" | ||
|
|
||
| # Install kubectl | ||
| curl -sSLo "${KUBECTL_CMD}" "https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl" | ||
| chmod +x "${KUBECTL_CMD}" | ||
| ${KUBECTL_CMD} version --client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| # Observability Helm charts End-To-End tests | ||
|
|
||
| ## Motivation | ||
|
|
||
| Kubernetes autodiscover is a key feature of any observability solution for this | ||
| orchestrator, resources change dynamically and observers configurations have to | ||
| adapt to these changes. | ||
| Discovery of resources in Kubernetes poses some challenges, there are several | ||
| corner cases that need to be handled, that involve keeping track of changes of | ||
| state that are not always deterministic. This complicates the implementation and | ||
| its testing. With lack of good test coverage and many cases to cover, it is easy to | ||
| introduce regressions even in basic use cases. | ||
| This suite covers a set of use cases that are better tested with real Kubernetes | ||
| implementations. | ||
|
|
||
| ## How do the tests work? | ||
|
|
||
| At the topmost level, the test framework uses a BDD framework written in Go, where | ||
| we set the expected behavior of use cases in a feature file using Gherkin, and | ||
| implementing the steps in Go code. | ||
|
|
||
| `kubectl` is used to configure resources in a Kubernetes cluster. `kind` can be | ||
| used to provide a local cluster. | ||
|
|
||
| The tests will follow this general high-level approach: | ||
|
|
||
| 1. It uses `kubectl` to interact with a Kubernetes cluster. | ||
| 1. If there is no configured Kubernetes cluster in kubectl, a new one | ||
| is deployed using `kind`. If a cluster is created, it is also removed after | ||
| the suite is executed. | ||
| 1. Execute BDD steps representing each scenario. Each scenario is executed in a | ||
| different Kubernetes namespace, so it is easier to cleanup and avoid one | ||
|
jsoriano marked this conversation as resolved.
|
||
| scenarios affecting the others. These namespaces are created and destroyed | ||
| before and after each scenario. | ||
| 1. New scenarios can be configured providing the Gherkin definition and | ||
| templatized Kubernetes manifests. | ||
|
|
||
| ### Adding new scenarios | ||
|
|
||
| Scenarios defined in this suite are based in a sequence of actions and | ||
| expectations defined in the feature files, and in some templates of resources to | ||
| deploy in Kubernetes. Templates are stored in `testdata/templates`, and must | ||
| have the `.yml.tmpl` extension. | ||
|
|
||
| Several of the available steps can be parameterized with template names, these | ||
| names can be written as the name of the template without the extension. Spaces | ||
| in these names are replaced with hyphens. | ||
|
|
||
| There are steps intended to define a desired state for the resources in the | ||
| template, such as the following ones: | ||
| * `"filebeat" is running` deploys the template `filebeat.yml.tmpl` and waits for | ||
| filebeat pods to be running. This step expects some pod to be labeled with | ||
| `k8s-app:filebeat`. | ||
| * `"a service" is deployed` deploys the resources in the template | ||
| `a-service.yml.tmpl`, and continues without expecting any state of the | ||
| deployed resources. | ||
| * `"a pod" is deleted` deletes the resources defined in the template | ||
| `a-pod.yml.tmpl`. | ||
|
|
||
| Any of these steps can be parameterized with an option that can be used to | ||
| select different configuration blocks in the template. For example the following | ||
| step would select the configuration block marked as `monitor annotations` in | ||
| the template: | ||
| ```shell | ||
| `"a service" is deployed with "monitor annotations" | ||
| ``` | ||
|
|
||
| These option blocks can be defined in the template like this: | ||
| ```yaml | ||
| meta: | ||
| annotations: | ||
| {{ if option "monitor annotations" }} | ||
| co.elastic.monitor/type: tcp | ||
| co.elastic.monitor/hosts: "${data.host}:6379" | ||
| {{ end }} | ||
| ``` | ||
|
|
||
| Steps defining expectations are mostly based in checking the events generated by | ||
| the deployed observers. Steps available for that are like the following ones: | ||
| * `"filebeat" collects events with "kubernetes.pod.name:a-pod"` checks that the | ||
| filebeat pod has collected at least one event with the field | ||
| `kubernetes.pod.name` set to a `pod`. | ||
| * `"metricbeat" does not collect events with "kubernetes.pod.name:a-pod" during "30s"` | ||
| expects to have a period of time of 30 seconds without collecting events with | ||
| the given field and value. | ||
|
|
||
| These steps expect to find the events in the `/tmp/beats-events` file in pods marked | ||
| with the label `k8s-app`. | ||
|
|
||
| There are other more specific steps. Examples for them can be found in the | ||
| feature files. | ||
|
|
||
|
|
||
| ### Diagnosing test failures | ||
|
|
||
| The first step in determining the exact failure is to try and reproduce the test run | ||
| locally, ideally using the DEBUG log level to enhance the log output. Once you've | ||
| done that, look at the output from the test run. | ||
|
|
||
| ### Running the tests | ||
|
|
||
| 1. Clone this repository, say into a folder named `e2e-testing`. | ||
|
|
||
| ``` shell | ||
| git clone git@github.com:elastic/e2e-testing.git | ||
| ``` | ||
|
|
||
| 2. Configure the version of the tools you want to test (Optional). | ||
|
|
||
| This is an example of the optional configuration: | ||
|
|
||
| ```shell | ||
| # Depending on the versions used, | ||
| export BEAT_VERSION=7.12.0 # version of beats to use | ||
| export BEATS_USE_CI_SNAPSHOTS=true # to select snapshots built by beats-ci | ||
| export KUBERNETES_VERSION="1.18.2" # version of the cluster to be passed to kind | ||
| ``` | ||
|
|
||
| 3. Install dependencies. | ||
|
|
||
| - Install Kubectl 1.18 or newer | ||
| - Install Kind 0.10.0 or newer | ||
| - Install Go: `https://golang.org/doc/install` _(The CI uses [GVM](https://github.com/andrewkroh/gvm))_ | ||
| - Install godog (from project's root directory): `make -C e2e install-godog` | ||
|
|
||
| 4. Run the tests. | ||
| ```shell | ||
| cd e2e/_suites/kubernetes-autodiscover | ||
| OP_LOG_LEVEL=DEBUG godog | ||
| ``` | ||
|
|
||
| Optionally, you can run only one of the feature files | ||
| ```shell | ||
| cd e2e/_suites/kubernetes-autodiscover | ||
| OP_LOG_LEVEL=DEBUG godog features/filebeat.feature | ||
| ``` | ||
|
|
||
| The tests will take a few minutes to run, spinning up the Kubernetes cluster | ||
| if needed. | ||
|
|
||
| ### Problems with the environment | ||
|
|
||
| If a Kubernetes cluster is pre-configured in kubectl, you can directly use this | ||
| command to investigate the resources deployed in the cluster by the suite. If | ||
| the cluster was deployed by the suite, it will have a randomized name, and will | ||
| use a temporary configuration file for kubectl. | ||
|
|
||
| The name of the cluster can be obtained with `kubectl get clusters`, clusters | ||
| created by this suite will follow the pattern `kind-<random uuid>`. | ||
|
|
||
| The temporary configuration file is logged by the suite at the info level. If a | ||
| cluster is created by the suite, you will see something like this: | ||
| ```shell | ||
| INFO[0000] Kubernetes cluster not available, will start one using kind | ||
| INFO[0000] Using kind v0.10.0 go1.15.7 linux/amd64 | ||
| INFO[0046] Kubeconfig in /tmp/test-252418601/kubeconfig | ||
| ``` | ||
|
|
||
| Then you could use the following command to control the resources with | ||
| `kubectl`: | ||
| ```shell | ||
| kubectl --kubeconfig /tmp/test-252418601/kubeconfig ... | ||
| ``` | ||
|
|
||
| Each scenario creates its own namespace, you can find them with `kubectl get | ||
| ns`, they will follow the pattern `test-<random uuid>`. | ||
|
|
||
| Interrupting the tests with Ctrl-C will leave all resources as they were, you | ||
| can use the previous instructions to investigate problems or access to logs of | ||
| the deployed pods. | ||
|
|
||
| ### I cannot move on | ||
|
|
||
| Please open an issue here: https://github.com/elastic/e2e-testing/issues/new | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.