Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix container enricher with correct container ids for pods with multiple containers #34516

Conversation

MichaelKatsoulis
Copy link
Contributor

@MichaelKatsoulis MichaelKatsoulis commented Feb 8, 2023

What does this PR do?

This PR fixes a bug in NewContainerMetadataEnricher used by Kubernetes module's container metricset.
In case a pod consists of more than one container, the ecs field container.id was the same for all containers.
This was happening because the meta struct used for both containers was the same and the container.id value was getting overwritten.

Also this PR aligns kubernetes.container.id and container.id of state_container metricset.
So far kubernetes.container.id was not splitted and contained both the runtime and the id (containerd://99781780c8d2eaa7a0333e158c49969b20cdf067f7b00d8b93022b508ee09724)
On the other hand container.id was splitted to 99781780c8d2eaa7a0333e158c49969b20cdf067f7b00d8b93022b508ee09724 leading to confusion.
Now on it will be splitted in all cases.

Why is it important?

This was leading to errors and containers not found.
Issue https://github.com/elastic/observability-dev/issues/4012

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

How to test this PR locally

To test this locally just create an elastic-stack and deploy default kubernetes metricbeat.
Deploy a pod in the k8s cluster with two containers and then in Kibana for that specific pod.name and container metricset, observe the container.ids of the different container names of that pod.

Issues

Screenshots

Before the fix:

before_error

After the fix:

new fixed

Logs

@MichaelKatsoulis MichaelKatsoulis requested a review from a team as a code owner February 8, 2023 09:34
@MichaelKatsoulis MichaelKatsoulis requested review from gsantoro and constanca-m and removed request for a team February 8, 2023 09:34
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Feb 8, 2023
@MichaelKatsoulis MichaelKatsoulis added the Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team label Feb 8, 2023
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Feb 8, 2023
@mergify
Copy link
Contributor

mergify bot commented Feb 8, 2023

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @MichaelKatsoulis? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@gizas
Copy link
Contributor

gizas commented Feb 8, 2023

I think we also need a test for that, because we missed this case.
So a test with 2 containers and reporting names

@@ -314,14 +315,15 @@ func NewContainerMetadataEnricher(
// which is in the form of <container.runtime>://<container.id>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think is time to allign here also with kube_state container.id where container.runtime is not splitted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that is another issue right? Good if we can fix them both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a breaking change. For state_container metricset as far as I see, the container.id is the same form.

Copy link
Contributor

@gizas gizas Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. So it is time maybe to test here both container.ids and align them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does fixing mean? I prefer the splitted container.id

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check below the kubernetes.container.id (vs container.id)

Screenshot 2023-02-08 at 2 45 38 PM

I also prefer the splitted, so to remove it from kubernetes.container.id

Copy link
Contributor Author

@MichaelKatsoulis MichaelKatsoulis Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@ChrsMark ChrsMark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Make sure you add the proper backports for it. Being a bugfix we need to backport it to the versions that the bug exists.

@@ -293,6 +293,7 @@ func NewContainerMetadataEnricher(
podStore, _ := nodeStore.AddPodStore(podId)

for _, container := range append(pod.Spec.Containers, pod.Spec.InitContainers...) {
cmeta := mapstr.M{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@MichaelKatsoulis
Copy link
Contributor Author

Good catch! Make sure you add the proper backports for it. Being a bugfix we need to backport it to the versions that the bug exists.

The addition of container.id ecs fields was introduced in 8.1. So We need to backport till that version

@@ -293,6 +293,7 @@ func NewContainerMetadataEnricher(
podStore, _ := nodeStore.AddPodStore(podId)

for _, container := range append(pod.Spec.Containers, pod.Spec.InitContainers...) {
Copy link
Contributor

@gizas gizas Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we have a for here, but just doublehcecking if we can make a test wit 3 containers. In case we miss sth and compare with kube_state.container dataset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reassure you it works because I tested it!

@elasticmachine
Copy link
Collaborator

elasticmachine commented Feb 8, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-02-09T08:47:53.907+0000

  • Duration: 52 min 24 sec

Test stats 🧪

Test Results
Failed 0
Passed 4279
Skipped 876
Total 5155

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@MichaelKatsoulis
Copy link
Contributor Author

I think we also need a test for that, because we missed this case.
So a test with 2 containers and reporting names

We don't have metricset tests currently that test the enrichers. All the tests we have in the container metricset just test the eventMapping method.
There is one test of the buildMetadataEnricher but even there we pass mock update function and not the one used by NewContainerMetadataEnricher

@gizas
Copy link
Contributor

gizas commented Feb 8, 2023

We don't have metricset tests currently that test the enrichers. All the tests we have in the container metricset just test the eventMapping method. There is one test of the buildMetadataEnricher but even there we pass mock update function and not the one used by NewContainerMetadataEnricher

In order not to delay fix, please merge this. But make sure you add a new story for this in backlog

@ChrsMark
Copy link
Member

ChrsMark commented Feb 9, 2023

I think we also need a test for that, because we missed this case.
So a test with 2 containers and reporting names

We don't have metricset tests currently that test the enrichers. All the tests we have in the container metricset just test the eventMapping method. There is one test of the buildMetadataEnricher but even there we pass mock update function and not the one used by NewContainerMetadataEnricher

Maybe we need to isolate the enricher's functionalities in smaller functions and that would make it easier for writing unit tests for them.

@MichaelKatsoulis
Copy link
Contributor Author

Maybe we need to isolate the enricher's functionalities in smaller functions and that would make it easier for writing unit tests for them.

Yes we need to rethink the code to make it testable. I will open an issue for adding tests.

Copy link
Member

@ChrsMark ChrsMark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a changelog here since it is a bugfix. Make sure you include both issues as you nicely do in the PR's description.

@MichaelKatsoulis MichaelKatsoulis added backport-v8.3.0 Automated backport with mergify backport-v8.1.0 Automated backport with mergify labels Feb 9, 2023
@MichaelKatsoulis MichaelKatsoulis added backport-v8.6.0 Automated backport with mergify backport-v8.7.0 Automated backport with mergify labels Feb 9, 2023
@MichaelKatsoulis MichaelKatsoulis merged commit 4ac4973 into elastic:main Feb 9, 2023
mergify bot pushed a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/state_container.go
#	metricbeat/module/kubernetes/util/kubernetes.go
mergify bot pushed a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
mergify bot pushed a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
mergify bot pushed a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
mergify bot pushed a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)
MichaelKatsoulis added a commit that referenced this pull request Feb 9, 2023
…ple containers (#34516) (#34538)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

Co-authored-by: Michael Katsoulis <[email protected]>
MichaelKatsoulis added a commit that referenced this pull request Feb 10, 2023
…ids for pods with multiple containers (#34537)

* Fix container enricher with correct container ids for pods with multiple containers (#34516)
---------

Co-authored-by: Michael Katsoulis <[email protected]>
@MichaelKatsoulis
Copy link
Contributor Author

@Mergifyio backport 8.2

@mergify
Copy link
Contributor

mergify bot commented Feb 10, 2023

backport 8.2

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Feb 10, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/state_container.go
#	metricbeat/module/kubernetes/util/kubernetes.go
@MichaelKatsoulis
Copy link
Contributor Author

@Mergifyio backport 8.1

mergify bot pushed a commit that referenced this pull request Feb 10, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id

(cherry picked from commit 4ac4973)

# Conflicts:
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.3.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v1.8.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.0.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/test/ksm.v2.7.0.expected
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.4.2.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.5.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.6.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/_meta/testdata/ksm.v2.7.0.plain-expected.json
#	metricbeat/module/kubernetes/state_container/state_container.go
#	metricbeat/module/kubernetes/util/kubernetes.go
@mergify
Copy link
Contributor

mergify bot commented Feb 10, 2023

backport 8.1

✅ Backports have been created

MichaelKatsoulis added a commit that referenced this pull request Feb 10, 2023
…ids for pods with multiple containers (#34536)

* Fix container enricher with correct container ids for pods with multiple containers (#34516)
---------

Co-authored-by: Michael Katsoulis <[email protected]>
MichaelKatsoulis added a commit that referenced this pull request Feb 13, 2023
…ids for pods with multiple containers (#34535)

* Fix container enricher with correct container ids for pods with multiple containers (#34516)
---------

Co-authored-by: Michael Katsoulis <[email protected]>
MichaelKatsoulis added a commit that referenced this pull request Feb 13, 2023
…ids for pods with multiple containers (#34534)

* Fix container enricher with correct container ids for pods with multiple containers (#34516)
---------

Co-authored-by: Michael Katsoulis <[email protected]>
chrisberkhout pushed a commit that referenced this pull request Jun 1, 2023
…ple containers (#34516)

* Fix container enricher with correct container ids for pods with multiple containers
* Align kubernetes.container.id with ecs container.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v8.1.0 Automated backport with mergify backport-v8.2.0 Automated backport with mergify backport-v8.3.0 Automated backport with mergify backport-v8.4.0 Automated backport with mergify backport-v8.5.0 Automated backport with mergify backport-v8.6.0 Automated backport with mergify backport-v8.7.0 Automated backport with mergify bug Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metricbeat on Kubernetes stores incorrect container.id for pods with multiple containers
4 participants