Skip to content

Fix for Rancher Service Discovery and adding custom tag support - #3366

Closed
AlexTawse wants to merge 3 commits into
DataDog:masterfrom
AlexTawse:supporting-rancher-for-service-discovery-using-api
Closed

Fix for Rancher Service Discovery and adding custom tag support#3366
AlexTawse wants to merge 3 commits into
DataDog:masterfrom
AlexTawse:supporting-rancher-for-service-discovery-using-api

Conversation

@AlexTawse

@AlexTawse AlexTawse commented Jun 1, 2017

Copy link
Copy Markdown

What does this PR do?

  • Restores Service Discovery functionality in Rancher in the event that Docker container metadata isn't populated as expected.
  • Incorporates functionality previously created by @janeczku at Rancher to automatically tag hosts based on their labels in Rancher.

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 inspect command. 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 to true, 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:

screen shot 2017-06-01 at 21 10 29

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.

This PR has a co-dependency with this one:

Finally, this PR is dependent upon this one:

@hkaj hkaj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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!

Comment thread utils/dockerutil.py
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'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread utils/rancher/__init__.py Outdated
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

from .rancherutil import RancherUtil # noqa: F401

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

could you move this to utils/orchestrator/__init__.py please?

Comment thread utils/rancher/rancherutil.py Outdated
@@ -0,0 +1,69 @@
import logging

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and this file to utils/orchestrator/rancherutil.py

Comment thread utils/rancher/rancherutil.py Outdated
def is_rancher():
if RancherUtil._is_rancher is None:
try:
response = requests.get(url=RancherUtil.METADATA_URL)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes I wondered about this - will remove. Thanks!

@AlexTawse
AlexTawse force-pushed the supporting-rancher-for-service-discovery-using-api branch from e8b551b to 9f3a837 Compare June 2, 2017 14:36
@AlexTawse

Copy link
Copy Markdown
Author

Hi @hkaj, thank you for the speedy review. I've made the changes as requested: relocating rancherutil.py into the orchestrator folder and removing the unnecessary container tags. I've built and re-deployed the agent internally and everything looks good from what I can see.

I've also pushed up changes based on your comments on the other PRs.

Thanks again - I appreciate your help!

@hkaj hkaj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, waiting on 5.14.x to be branched off and we'll merge 🎉

@AlexTawse

Copy link
Copy Markdown
Author

Thanks so much! 🎉

@janeczku

Copy link
Copy Markdown

@alextbrandwatch so great! 👏 🎉

@hkaj

hkaj commented Jun 12, 2017

Copy link
Copy Markdown
Member

Hey @alextbrandwatch sorry we released a change that conflicts with your PR, I'll rebase your commits shortly.

@AlexTawse

Copy link
Copy Markdown
Author

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 🙇

@AlexTawse
AlexTawse force-pushed the supporting-rancher-for-service-discovery-using-api branch from 9f3a837 to 8f29e6d Compare July 4, 2017 10:56
@AlexTawse
AlexTawse force-pushed the supporting-rancher-for-service-discovery-using-api branch from a256410 to 83c62b6 Compare July 4, 2017 15:37
@AlexTawse
AlexTawse force-pushed the supporting-rancher-for-service-discovery-using-api branch from 83c62b6 to c2beea0 Compare July 4, 2017 16:32
@AlexTawse

AlexTawse commented Jul 4, 2017

Copy link
Copy Markdown
Author

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 check_rancher_host_labels configuration file option that I proposed, given that a collector_orchestrator_tags option has now been added.

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 hkaj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cid is not optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

return tags

@staticmethod
def is_rancher():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any reason not to rename it to is_detected and get rid of L29-31?

@hush-hush hush-hush added this to the 5.16 milestone Jul 10, 2017
@truthbk truthbk modified the milestones: 5.16, 5.17 Jul 25, 2017
@olivielpeau olivielpeau added this to the 5.18.0 milestone Aug 18, 2017
@olivielpeau olivielpeau removed this from the 5.17 milestone Aug 18, 2017
@truthbk truthbk modified the milestones: 5.18.0, 5.19.0 Oct 2, 2017
@olivielpeau olivielpeau modified the milestones: 5.19.0, 5.20.0 Oct 31, 2017
@hush-hush hush-hush modified the milestones: 5.20.0, 5.21.0 Nov 13, 2017
@xvello xvello modified the milestones: 5.21.0, Future Dec 27, 2017
@masci

masci commented Apr 12, 2018

Copy link
Copy Markdown
Contributor

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.

@masci masci closed this Apr 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants