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

[Processor: add_cloud_metadata] Use AWS client to get instance metadata and EKS cluster name #35182

Merged
merged 32 commits into from
May 31, 2023

Conversation

tetianakravchenko
Copy link
Contributor

@tetianakravchenko tetianakravchenko commented Apr 24, 2023

What does this PR do?

  • Use AWS sdk instead of http calls
  • Get Identity document to set the same metadata as before
  • use EC2 describe tags call to get instance tags, that contains eks:cluster-name tag

NOTE: existing issue:

Why is it important?

Add cluster name metadata for EKS clusters, similar to the GKE

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.

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

when used configuration:

processors:
      - add_cloud_metadata:
          providers:
            - "aws"

Screenshot 2023-04-24 at 10 34 22

Logs

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Apr 24, 2023
@mergify
Copy link
Contributor

mergify bot commented Apr 24, 2023

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @tetianakravchenko? 🙏.
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

@elasticmachine
Copy link
Collaborator

elasticmachine commented Apr 24, 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-05-31T07:26:24.750+0000

  • Duration: 46 min 54 sec

❕ Flaky test report

No test was executed to be analysed.

🤖 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!)

@tetianakravchenko tetianakravchenko requested review from a team, gizas and ChrsMark and removed request for a team April 27, 2023 08:23
logger.Warnf("error when read token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
logger.Debugf("error loading AWS default configuration: %s.", err)
result.err = errors.Wrapf(err, "failed loading AWS default configuration")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result.err = errors.Wrapf(err, "failed loading AWS default configuration")
result.err = fmt.Errorf("%w failed loading AWS default configuration", error)

"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/pkg/errors"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just import fmt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done - 0241f22

instanceIdentity, err := awsClient.GetInstanceIdentityDocument(context.TODO(), &imds.GetInstanceIdentityDocumentInput{})
if err != nil {
logger.Debugf("error fetching EC2 Identity Document: %s.", err)
result.err = errors.Wrapf(err, "failed fetching EC2 Identity Document.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above with fmt.errorf

if err != nil {
logger.Warnf("error when reading token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
logger.Debugf("error fetching cluster name metadata: %s.", err)
Copy link
Contributor

@gizas gizas Apr 27, 2023

Choose a reason for hiding this comment

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

This can be Warnf level and not debug?

Copy link
Contributor

@gizas gizas May 9, 2023

Choose a reason for hiding this comment

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

@tetianakravchenko only minor if you think that we need to expose this to warnings or to errors , as it is quite important to print this by default, or repeat what you do in line

result.err = errors.Wrapf(err, "failed fetching EC2 Identity Document.")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gizas thank you for pointing it out! From my understanding it should be fine to use it this way, http fetcher uses this definition as well -

result.err = errors.Wrapf(err, "failed to create http request for %v", f.provider)

Copy link
Contributor

Choose a reason for hiding this comment

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

My comment has to do more with the level error vs debug vs warn. I would say not to use debug as it would hide any messages unless you raise the log level

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code is outdated - was replaced to Warn in 0241f22

Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
@mergify
Copy link
Contributor

mergify bot commented May 15, 2023

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b eks-cluster-name upstream/eks-cluster-name
git merge upstream/main
git push upstream eks-cluster-name

@tetianakravchenko
Copy link
Contributor Author

hey @kaiyan-sheng as I pushed some changes after your approve, could you please have a look/approve again?

@tetianakravchenko
Copy link
Contributor Author

@pierrehilbert @fearful-symmetry could you please review this PR and help with merging it?

I can't merge it, since I am missing the code owners review

Screenshot 2023-05-15 at 20 24 48

@tetianakravchenko
Copy link
Contributor Author

Hey @elastic/elastic-agent-data-plane, could you help with this PR? I can't merge it because Waiting on code owner review from elastic/elastic-agent-data-plane. #35182 (comment)

@MichaelKatsoulis
Copy link
Contributor

@tetianakravchenko have you tested that on AWS Fargate ?

@tetianakravchenko
Copy link
Contributor Author

@tetianakravchenko have you tested that on AWS Fargate ?

@MichaelKatsoulis no, only on the standard EKS setup. Do you think it is a blocker for this PR?

@MichaelKatsoulis
Copy link
Contributor

@MichaelKatsoulis no, only on the standard EKS setup. Do you think it is a blocker for this PR?

Not a blocker, I was just interested on what happens in that case. I would expect to not get the cluster name.
It could be worthwhile testing to ensure that it doesn't cause anything undesired.

@pierrehilbert pierrehilbert added the Team:Elastic-Agent Label for the Agent team label May 19, 2023
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label May 19, 2023
if err != nil {
logger.Warnf("error when read token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
logger.Debugf("error loading AWS default configuration: %s.", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an error message, is there a reason the log level is debug? Should be Errorf.

Copy link
Contributor Author

@tetianakravchenko tetianakravchenko May 30, 2023

Choose a reason for hiding this comment

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

add_cloud_metadata tries to fetch the metadata from all the available cloud providers, so if running on GCP - fetchMetadata for AWS will still run and in this case it might will be confusing to get errors on GCP regarding the AWS configuration. I've changed it to Warnf as it was done before - 19ab154

@mergify
Copy link
Contributor

mergify bot commented May 26, 2023

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b eks-cluster-name upstream/eks-cluster-name
git merge upstream/main
git push upstream eks-cluster-name

@fearful-symmetry
Copy link
Contributor

Not sure why github set me as the code owner here, since I don't have a lot of context for the cloud processors. Tried to give a bit of feedback.

@tetianakravchenko
Copy link
Contributor Author

tetianakravchenko commented May 30, 2023

Not sure why github set me as the code owner here, since I don't have a lot of context for the cloud processors. Tried to give a bit of feedback.

@fearful-symmetry I believe it is mainly due to adding new dependency

Signed-off-by: Tetiana Kravchenko <[email protected]>
Signed-off-by: Tetiana Kravchenko <[email protected]>
@mergify
Copy link
Contributor

mergify bot commented May 30, 2023

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b eks-cluster-name upstream/eks-cluster-name
git merge upstream/main
git push upstream eks-cluster-name

@tetianakravchenko
Copy link
Contributor Author

/test

@tetianakravchenko tetianakravchenko merged commit c40fa0a into elastic:main May 31, 2023
chrisberkhout pushed a commit that referenced this pull request Jun 1, 2023
…ta and EKS cluster name (#35182)

* add generic metadata fetcher

Signed-off-by: Tetiana Kravchenko <[email protected]>

* merge main

Signed-off-by: Tetiana Kravchenko <[email protected]>

* clean up

Signed-off-by: Tetiana Kravchenko <[email protected]>

* move tagDescribe to different func

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add tests for add_cloud_metadata

Signed-off-by: Tetiana Kravchenko <[email protected]>

* Tiltfile: fix docker_registry, use more generic value

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add notice file

Signed-off-by: Tetiana Kravchenko <[email protected]>

* fix tests - add former test cases; fix linter issues

Signed-off-by: Tetiana Kravchenko <[email protected]>

* handle correctly result.err

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add generic metadata fetcher

Signed-off-by: Tetiana Kravchenko <[email protected]>

* merge main

Signed-off-by: Tetiana Kravchenko <[email protected]>

* clean up

Signed-off-by: Tetiana Kravchenko <[email protected]>

* move tagDescribe to different func

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add tests for add_cloud_metadata

Signed-off-by: Tetiana Kravchenko <[email protected]>

* Tiltfile: fix docker_registry, use more generic value

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add notice file

Signed-off-by: Tetiana Kravchenko <[email protected]>

* fix tests - add former test cases; fix linter issues

Signed-off-by: Tetiana Kravchenko <[email protected]>

* handle correctly result.err

Signed-off-by: Tetiana Kravchenko <[email protected]>

* address reviews

Signed-off-by: Tetiana Kravchenko <[email protected]>

* Update dev-tools/kubernetes/Tiltfile

Co-authored-by: kaiyan-sheng <[email protected]>

* fix the types.TagDescription struct

Signed-off-by: Tetiana Kravchenko <[email protected]>

* remove not used variable; fix types.TagDescription struct

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add a changelog record

Signed-off-by: Tetiana Kravchenko <[email protected]>

* change Debugf to Warnf

Signed-off-by: Tetiana Kravchenko <[email protected]>

---------

Signed-off-by: Tetiana Kravchenko <[email protected]>
Co-authored-by: kaiyan-sheng <[email protected]>
@DingGGu
Copy link

DingGGu commented Jul 26, 2023

This makes fetch IMDS Error on non EKS envs

{"log.level":"warn","@timestamp":"2023-07-26T02:20:11.324Z","log.logger":"add_cloud_metadata","log.origin":{"file.name":"add_cloud_metadata/provider_aws_ec2.go","file.line":102},"message":"error fetching cluster name metadata: error fetching EC2 Tags: operation error EC2: DescribeTags, failed to sign request: failed to retrieve credentials: failed to refresh cached credentials, failed to get nodes EC2 IMDS role credentials, operation error ec2imds: GetMetadata, request canceled, context deadline exceeded.","service.name":"filebeat","ecs.version":"1.6.0"}
{"log.level":"warn","@timestamp":"2023-07-26T02:20:16.470Z","log.logger":"add_cloud_metadata","log.origin":{"file.name":"add_cloud_metadata/provider_aws_ec2.go","file.line":102},"message":"error fetching cluster name metadata: error fetching EC2 Tags: operation error EC2: DescribeTags, exceeded maximum number of attempts, 3, failed to sign request: failed to retrieve credentials: failed to refresh cached credentials, failed to get nodes EC2 IMDS role credentials, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, http response error StatusCode: 500, request to EC2 IMDS failed.","service.name":"filebeat","ecs.version":"1.6.0"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants