Fix for Rancher Service Discovery and adding custom tag support - #3366
Fix for Rancher Service Discovery and adding custom tag support#3366AlexTawse wants to merge 3 commits into
Conversation
61e31d5 to
47002ba
Compare
hkaj
left a comment
There was a problem hiding this comment.
Hi @alextbrandwatch
That's a great addition, thanks a lot! I left a few comments but it's already in great shape. I'm happy to see that Rancher is getting some interest as well. Having a RancherUtil in utils/orchestrator will definitely make it easier to improve our support of it.
I'll have a look at the related PRs as well. Thanks again!
| RANCHER_CONTAINER_NAME = 'io.rancher.container.name' | ||
| RANCHER_CONTAINER_IP = 'io.rancher.container.ip' | ||
| RANCHER_STACK_NAME = 'io.rancher.stack.name' | ||
| RANCHER_SVC_NAME = 'io.rancher.stack_service.name' |
There was a problem hiding this comment.
nice, you can get rid of https://github.com/alextbrandwatch/dd-agent/blob/47002bae23f99c7b38697d91036ec8be453d88c4/utils/dockerutil.py#L78 as well then
| # All rights reserved | ||
| # Licensed under Simplified BSD License (see LICENSE) | ||
|
|
||
| from .rancherutil import RancherUtil # noqa: F401 |
There was a problem hiding this comment.
could you move this to utils/orchestrator/__init__.py please?
| @@ -0,0 +1,69 @@ | |||
| import logging | |||
There was a problem hiding this comment.
and this file to utils/orchestrator/rancherutil.py
| def is_rancher(): | ||
| if RancherUtil._is_rancher is None: | ||
| try: | ||
| response = requests.get(url=RancherUtil.METADATA_URL) |
There was a problem hiding this comment.
Could you add a timeout here? In some network configs that might not resolve but not refuse the connection immediately either. 1 second seems reasonable to me, don't know if you've seen it take longer than that?
There was a problem hiding this comment.
1 second seems sensible, I'm fairly sure the request goes to another container on the same machine. I've never known it not respond immediately.
| tags.append('rancher_host_docker_version:%s' % v) | ||
|
|
||
| elif k == RancherUtil.HOST_LINUX_KERNEL_VERSION_LABEL: | ||
| tags.append('rancher_host_linux_kernel_version:%s' % v) |
There was a problem hiding this comment.
I don't think those 3 fit as tags in service discovery. They're good as host metadata but could you remove them from here?
There was a problem hiding this comment.
Yes I wondered about this - will remove. Thanks!
e8b551b to
9f3a837
Compare
|
Hi @hkaj, thank you for the speedy review. I've made the changes as requested: relocating I've also pushed up changes based on your comments on the other PRs. Thanks again - I appreciate your help! |
hkaj
left a comment
There was a problem hiding this comment.
LGTM, waiting on 5.14.x to be branched off and we'll merge 🎉
|
Thanks so much! 🎉 |
|
@alextbrandwatch so great! 👏 🎉 |
|
Hey @alextbrandwatch sorry we released a change that conflicts with your PR, I'll rebase your commits shortly. |
|
Thanks for the heads up @hkaj, looks like a bit of a change to how orchestration environments are detected and managed, a rebase would be appreciated though I will take a look myself when I'm next able to 🙇 |
9f3a837 to
8f29e6d
Compare
a256410 to
83c62b6
Compare
83c62b6 to
c2beea0
Compare
|
Hi @hkaj, I've rebased this PR against the changes that were made to the way that Orchestrator utility classes were handled. The result is that this PR is now a little simpler. I've also removed the I'll get this tested and deployed to our test environment as soon as possible, but in the meantime I would very much appreciate a re-review to see if I've missed anything. Thank you! |
hkaj
left a comment
There was a problem hiding this comment.
thanks for taking care of it @alextbrandwatch and apologies, I've bee pretty swamped with other stuff. Left a few comments but it's looking good overall!
| def is_detected(): | ||
| return RancherUtil.is_rancher() | ||
|
|
||
| def _get_cacheable_tags(self, cid=None, co=None): |
There was a problem hiding this comment.
also maybe make the default value for co {}? Otherwise co.get will error out
|
|
||
| def __init__(self): | ||
| BaseUtil.__init__(self) | ||
| self.needs_inspect_config = True |
There was a problem hiding this comment.
it doesn't look like you need config since you only use labels. Can you look at how ecsutil does it?
Here are the interesting parts:
- https://github.com/DataDog/dd-agent/blob/5.14.1/utils/orchestrator/ecsutil.py#L48-L70 for collecting the tags from a
docker ps(faster than one inspect per container) - https://github.com/DataDog/dd-agent/blob/5.14.1/utils/orchestrator/ecsutil.py#L74-L87 for returning the tags
| return tags | ||
|
|
||
| @staticmethod | ||
| def is_rancher(): |
There was a problem hiding this comment.
any reason not to rename it to is_detected and get rid of L29-31?
|
Hi @alextbrandwatch, thanks for your contribution. We've discussed this and while we agree the feature has merit, we decided to dedicate engineering resources to the new Agent thus limiting the work on this release line to bugfixes only. |
What does this PR do?
Motivation
In our experience running a modified version of the Datadog agent in production, we've seen that Rancher is not guaranteed to populate Docker container metadata correctly as seen with the
docker inspectcommand. We believe this is a bug with Rancher which we have separately raised with them. However, the result is that Service Discovery does not always work on the current release of Rancher, which is a major issue.To address this, we build on the work recently contributed by @zippolyte and in our own fork to introduce an integration with the Rancher Metadata API. When resolving host IP address and exposed ports for the purposes of Service Discovery, this PR adds the ability to read these from the Rancher Metadata API should all previous attempts to resolve these values fail.
In addition, we've taken this opportunity to integrate some of the work previously carried out by @janeczku with this customisation of the agent: https://github.com/janeczku/datadog-rancher-init. We've added a new configuration file flag:
collect_rancher_host_labels. With this flag set totrue, the agent will check for connectivity to the Metadata API of a local Rancher environment, and if successful will read the metadata labels of the host from the API and set each entry as a tag on the host in Datadog. See the below example of tags automatically inferred in this way:The net result is that Service Discovery is seen to be much more stable when running under Rancher, and Rancher users can deploy the "vanilla" agent directly from Datadog whilst retaining the functionality from datadog-rancher-init.
Additional Notes
Before this PR can be merged, the following PRs must be merged into their respective repos. These PRs constitute a single piece of work.
(adding the
rancher-metadatadependency, source: https://github.com/m4ce/rancher-metadata-python)(adding
rancher-metadatadependency)This PR has a co-dependency with this one:
(updating references to variables renamed and relocated in this PR)
Finally, this PR is dependent upon this one:
(adding environment variable
RANCHER_HOST_LABELSto update thedatadog.conffile)