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

[Hints Support] Add default host #3575

Merged
merged 5 commits into from
Oct 19, 2023

Conversation

constanca-m
Copy link
Contributor

@constanca-m constanca-m commented Oct 10, 2023

What does this PR do?

Given a pod with containers, it adds co.elastic.hints.<container-name>/host: '${kubernetes.pod.ip}:<container-port>' to the annotations for each container.

Why is it important?

The user no longer needs to specify the host for the hints, which until now was a mandatory step.

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/fragments using the changelog tool
  • I have added an integration test or an E2E test

How to test this PR locally

  1. Clone this branch.
  2. Follow the steps in the README file to create the EA and add it to Kubernetes.
  3. Deploy a pod with at least one container and the required annotation co.elastic.hints/package and check if the results can be seen in ES.

Related issues

Results

Context: We have a nginx container with two pods: one in port 80, nginx-1, and another in port 81, nginx-2.

The manifest file for that would be something like this.
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    co.elastic.hints/package: nginx
spec:
  volumes:
    - name: config
      configMap:
        name: nginx-conf
  containers:
    - name: nginx-1
      image: nginx
      ports:
        - containerPort: 80
    - name: nginx-2
      image: nginx
      ports:
        - containerPort: 81
      volumeMounts:
        - name: config
          mountPath: /etc/nginx/conf.d

Situation 1: Two pods and no annotation for host

If we have annotations only for the package:

  annotations:
    co.elastic.hints/package: nginx

Then the defaults for the host will be used.

Inspecting the output of the inspect command (elastic-agent inspect -v --variables --variables-wait 2s) results in this:

...
- data_stream.namespace: default
  id: <...>.nginx-1 <--------------------------- Our container nginx-1
  name: nginx/metrics-nginx
  ...
  streams:
    - data_stream:
        dataset: nginx.stubstatus
        type: metrics
      hosts:
        - 10.244.0.6:80 <---------------------------- Host of nginx-1
...
- data_stream.namespace: default
  id: <...>.nginx-2  <--------------------------- Our container nginx-2
  name: nginx/metrics-nginx
  ...
  streams:
    - data_stream:
        dataset: nginx.stubstatus
        type: metrics
      hosts:
        - 10.244.0.6:81 <---------------------------- Host of nginx-2
...

Checking the unique count of the containers has this output:
Screenshot from 2023-10-09 17-28-36

Situation 2: Two pods and only one host annotation for one pod

The annotations would be like this:

  annotations:
    co.elastic.hints/package: nginx
    co.elastic.hints.nginx-1/host: '${kubernetes.pod.ip}:80'

The result is the same as situation 1, as the host for nginx-2 will be the default ('${kubernetes.pod.ip}: + port).

Situation 3: Two pods and one top level annotation

The annotations would be like this:

  annotations:
    co.elastic.hints/package: nginx
    co.elastic.hints/host: '${kubernetes.pod.ip}:82'

In this case, we are using a port that is not even possible (there is no nginx pod with port 82), to make sure the host works.

The output is the exact same as situation 1. The following happened in our code:

  1. Checked the mappings for each container.
  2. There was no default host defined for any of the containers.
  3. Then add the default host for each container.

@constanca-m constanca-m added enhancement New feature or request Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team labels Oct 10, 2023
@constanca-m constanca-m self-assigned this Oct 10, 2023
@constanca-m constanca-m requested a review from a team as a code owner October 10, 2023 07:26
@mergify
Copy link
Contributor

mergify bot commented Oct 10, 2023

This pull request does not have a backport label. Could you fix it @constanca-m? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

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

NOTE: backport-skip has been added to this pull request.

@elasticmachine
Copy link
Contributor

elasticmachine commented Oct 10, 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-10-10T13:01:28.018+0000

  • Duration: 25 min 53 sec

Test stats 🧪

Test Results
Failed 0
Passed 6545
Skipped 59
Total 6604

💚 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.

  • run integration tests : Run the Elastic Agent Integration tests.

  • run end-to-end tests : Generate the packages and run the E2E Tests.

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

@elasticmachine
Copy link
Contributor

elasticmachine commented Oct 10, 2023

🌐 Coverage report

Name Metrics % (covered/total) Diff
Packages 98.795% (82/83) 👍
Files 67.114% (200/298) 👍
Classes 65.766% (365/555) 👍
Methods 52.99% (1152/2174) 👎 -0.043
Lines 38.577% (13163/34121) 👎 -0.013
Conditionals 100.0% (0/0) 💚

@constanca-m
Copy link
Contributor Author

/test

},
"enabled": true,
"host": "127.0.0.5:6379",
"stream": "stderr",
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the stream here? If not shall we remove it from all tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need it, I just wanted to make sure that adding the default host was not overwriting any of the hints.

@gizas
Copy link
Contributor

gizas commented Oct 10, 2023

@constanca-m you should create a fragment to pass the CI failure.
As per https://github.com/elastic/elastic-agent#changelog

elastic-agent-changelog-tool new <some title>

@constanca-m constanca-m requested a review from a team as a code owner October 10, 2023 11:05
@ChrsMark
Copy link
Member

I think in the description the term "pod" is mixed with the term "container". For example co.elastic.hints.<pod-name>/host: '${kubernetes.pod.ip}:<container-port>' should be co.elastic.hints.<container-name>/host: '${kubernetes.pod.ip}:<container-port>' right? Same in the manual testing examples' tittles.

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.

Nice addition!

I have left some comments to consider as improvements. I have not tested the patch manually but maybe it would worth it to introduce some manual testing phase for such PRs (since it seems that e2e is not used any more?).

internal/pkg/composable/providers/kubernetes/hints.go Outdated Show resolved Hide resolved
internal/pkg/composable/providers/kubernetes/hints.go Outdated Show resolved Hide resolved
internal/pkg/composable/providers/kubernetes/hints.go Outdated Show resolved Hide resolved
internal/pkg/composable/providers/kubernetes/hints.go Outdated Show resolved Hide resolved
@pierrehilbert pierrehilbert added the Team:Elastic-Agent Label for the Agent team label Oct 10, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@elastic-sonarqube
Copy link

@constanca-m constanca-m merged commit 9196320 into elastic:main Oct 19, 2023
11 checks passed
@constanca-m constanca-m deleted the add-default-host branch October 19, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-skip enhancement New feature or request Team:Cloudnative-Monitoring Label for the Cloud Native Monitoring team Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove the need to define host in hints autodiscovery by using proper default values
6 participants