Skip to content

add groupByLabel function #165

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

Merged
merged 2 commits into from
Feb 24, 2016
Merged

Conversation

codekitchen
Copy link
Contributor

This is convenient, but also necessary since the $fieldPath parameter to groupBy can't contain dots in map key names, and labels are typically reverse-domain-name prefixed.

func groupByLabel(entries interface{}, label string) (map[string][]interface{}, error) {
getLabel := func(v interface{}) (interface{}, error) {
if container, ok := v.(RuntimeContainer); ok {
if container.Labels[label] == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will break if the label actually has a blank value. You may want to check the second return value from container.Labels[label] to see if it is false instead of checking for "". I did this in the whereLabel function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you are right, I misread groupBy as excluding the container if the value is blank, so I went this route for consistency, but it only excludes if the value is nil. I'll change it.

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

@tompson
Copy link

tompson commented Feb 21, 2016

I built something specific for grouping by the service name of the docker compose file:

troii@e9eded7

but this pull would make mine obsolete and make it more generic

my vote for this one! ;)

@tompson
Copy link

tompson commented Feb 22, 2016

I tried to use groubByLabel like

{{ range $host, $containers := groupByLabel $ "com.docker.compose.service"  }} 

but I always get

function "groupByLabel" not defined

@codekitchen
Copy link
Contributor Author

@tompson I've successfully used the new function with a binary built by running make with this PR applied, e.g. make && ./docker-gen -only-exposed /path/to/my/tempate.tmpl. It sounds like you're probably not using the right docker-gen binary?

@tompson
Copy link

tompson commented Feb 22, 2016

did exactly that, checked multiple times that I got the right binary

do you have an example template file?

@codekitchen
Copy link
Contributor Author

You probably don't have this PR applied locally then. A simple example template would be:

{{range $project, $containers := groupByLabel $ "com.docker.compose.project"}}{{printf "%s %s\n" $project (first $containers).ID}}{{end}}

@tompson
Copy link

tompson commented Feb 22, 2016

wow, that was hard to track down, problem was that in cmd/docker-gen/main.go the import

"github.com/jwilder/docker-gen"

caused that make did not build codekitchen/docker-gen or troii/docker-gen even when I ran make in my repository

@codekitchen
Copy link
Contributor Author

Ah dang, that's a pain yeah. Glad you got it sorted out.

@md5
Copy link
Contributor

md5 commented Feb 22, 2016

@tompson When I work on this project I add an additional Git remote to my checkout of github.com/jwilder/docker-gen to avoid this problem.

jwilder added a commit that referenced this pull request Feb 24, 2016
@jwilder jwilder merged commit 0de7d92 into nginx-proxy:master Feb 24, 2016
@jwilder
Copy link
Collaborator

jwilder commented Feb 24, 2016

Thanks @codekitchen!

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.

4 participants