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

Add Docker container environment variables as tags. Only whitelisted #2580 #2581

Merged
merged 1 commit into from
May 18, 2017

Conversation

rsingh2411
Copy link
Contributor

@rsingh2411 rsingh2411 commented Mar 28, 2017

Add Docker container environment variables as tags. Only whitelisted #2580
PR for the above issue.

Required for all PRs:

  • CHANGELOG.md updated (we recommend not updating this until the PR has been approved by a maintainer)
  • Sign CLA (if not already signed)

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

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

Looks good, just a few minor changes needed.

@@ -98,6 +111,8 @@ var sampleConfig = `
perdevice = true
## Whether to report for each container total blkio and network stats or not
total = false
## Which environment variables should we use as a tag
gather_environment = ["ENV", "DC"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment this line so its not on by default, change the names to something a little more standard so its extra clear these are environment variables.

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

if len(d.GatherEnvironment) > 0 {
info, err := inspectWrapper(d.client, ctx, container.ID)
if err != nil {
return fmt.Errorf("Error getting docker container inspect: %s", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

The wording here is confusing, I suggest "Error inspecting docker container: %s"

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


for _, envvar := range info.Config.Env {
kvs := strings.SplitN(envvar, "=", 2)
if !sliceContains(kvs[0], d.GatherEnvironment) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use this function since it is private to the libraries docker package, its just a coincidence that we are also in a docker package. I think this loop would be better if we ranged over d.GatherEnvironment and split only if the prefix matches.

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

if !sliceContains(kvs[0], d.GatherEnvironment) {
continue
}
tags[kvs[0]] = kvs[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets be extra careful and check the len of kvs, since SplitN could return a slice of length 1.

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

testing: true,
client: nil,
testing: true,
GatherEnvironment: []string{"ENVVAR1", "ENVVAR2"},
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add tests for envvars that are not found in the container, envvars with zero length values, multiple '=' signs, and any other edge cases you can think of.

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 have added mentioned negative edge cases.

@danielnelson
Copy link
Contributor

Please update the docker README.md too.

@rsingh2411
Copy link
Contributor Author

Review changes are incorporated. Please have a look

@rsingh2411
Copy link
Contributor Author

rsingh2411 commented Apr 3, 2017

There were some conflicts due to Docker: optionally add labels as tags (#2425) ,
apologies for redundant commits. Tests are passing successfully

@@ -30,12 +30,13 @@ for the stat structure can be found
perdevice = true
## Whether to report for each container total blkio and network stats or not
total = false
## Which environment variables should we use as a tag
gather_environment = ["JAVA_HOME", "HEAP_SIZE"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should rename this option to tag_env?

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

}
for _, envvar := range info.Config.Env {
for _, prefix := range d.GatherEnvironment {
if strings.Count(envvar, "=") > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should't skip environment variables like: FOO=BAR=BAZ, also add a testcase for this.

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, added the testcase

if strings.Count(envvar, "=") > 1 {
continue
}
if strings.HasPrefix(envvar, prefix) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think items with a common prefix will get picked up. Try adding a test case for if there are environment variables FOO and FOOBAR, and specify FOO in the config.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an testcase for this as well

@rsingh2411
Copy link
Contributor Author

rsingh2411 commented May 10, 2017

I have incorporated review changes. Testing failed due to Test killed with quit: ran too long
coudn't re run as didn't have privelages

testing: true,
client: nil,
testing: true,
TagEnvironment: []string{"ENVVAR1", "ENVVAR2", "ENVVAR3", "ENVVAR4", "ENVVAR5", "ENVVAR6", "ENVVAR7", "ENVVAR8", "ENVVAR9"},
Copy link
Contributor

Choose a reason for hiding this comment

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

Please line wrap this after each element, not a hard rule but try to stick to about 80 chars per line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be done.

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

"ENVVAR2": "dolorsitamet",
"ENVVAR3": "ubuntu:10.04",
"ENVVAR6": " ",
"ENVVAR7": "ENVVAR9",
Copy link
Contributor

Choose a reason for hiding this comment

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

Value should be ENVVAR8=ENVVAR9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apologies, didn't get this comment
say ENVVAR7=ENVVAR8=ENVVAR9
then tags would be
ENVVAR7=ENVVAR9
ENVVAR8=ENVVAR9
please correct me if my understanding is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

The tagkey should be ENVVAR7 and the tagvalue should be ENVVAR8=ENVVAR9

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

"ENVVAR3": "ubuntu:10.04",
"ENVVAR6": " ",
"ENVVAR7": "ENVVAR9",
"ENVVAR8": "ENVVAR9",
Copy link
Contributor

Choose a reason for hiding this comment

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

There shouldn't be an ENVVAR8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apologies, didn't get this comment
say ENVVAR7=ENVVAR8=ENVVAR9
then tags would be
ENVVAR7=ENVVAR9
ENVVAR8=ENVVAR9
please correct me if my understanding is wrong.

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

@danielnelson
Copy link
Contributor

@rsingh2411 Can you add an entry to the 1.4 feature section of the CHANGELOG?

@nhaugo Did you want to review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants