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

Inspect output on service without labels is an empty map instead of null, fixes #24631 #30967

Merged

Conversation

adshmh
Copy link
Contributor

@adshmh adshmh commented Feb 13, 2017

Added code to output an empty map, instead of null, by the inspect command for labels of a service that has no labels. Fixes #24631 (also changes 'null' output to an empty object for other types with labels: secret, node, task, network, swarm)

Signed-off-by: Arash Deshmeh adeshmeh@ca.ibm.com

- What I did
The output of inspect command for labels of a service that has no labels is currently 'null', which is inconsistent with other objects, e.g. inspect output for labels of containers without labels is an empty object: {}. This PR fixes this inconsistency by changing the output to '{}' when the service has no labels.

- How I did it
Added code which replaces nil labels maps on service objects with an empty map, when retrieved by the daemon.

- How to verify it
Create a service:
$ docker service create --name nolabels nginx:alpine

Run inspect on the service, with Labels specified in format string.

  • Without this PR:
    $ docker service inspect --format '{{ json .Spec.Labels }}' nolabels
    null

  • With this PR included:
    $ docker service inspect --format '{{ json .Spec.Labels }}' nolabels
    {}

- Description for the changelog
The inspect command outputs an empty json object, i.e. {}, instead of null, for labels of a service that has no labels

- A picture of a cute animal (not mandatory but encouraged)

@adshmh adshmh changed the title Fixing #24631, inspect output on service without labels is empty obje… Inspect output on service without labels is an empty map instead of null, fixes #24631 Feb 13, 2017
Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

LGTM 🐯

@@ -17,7 +17,7 @@ type Meta struct {
// Annotations represents how to describe an object.
type Annotations struct {
Name string `json:",omitempty"`
Labels map[string]string `json:",omitempty"`
Labels map[string]string `json:"Labels"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the omitempty here is related to #24631. I don't think there's any harm in continuing to omit the Labels field when it's empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review. I removed the omitempty because my tests show 'null' as output with omitempty left as is, while including all other changes of the PR. (I think the empty labels map is lost in marshalling/unmarshaling due to omitempty. The test steps are listed in the PR description). I did not find any client-side code that makes changes to the retrieved object, so avoided adding such code (e.g. in client/service_inspect.go). Is fixing the nil labels map on the client-side the preferred approach? (in that case, I can update the PR accordingly)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for confirming that this makes a difference.

I honestly don't know the right solution. It doesn't feel intuitively right to me to force the field to be marshalled in all cases so that the client avoids outputting null for a specific query. My sense is that it would be better for the client to make sure the map is initialized after unmarshalling, but I'm not sure how we handle these cases in general. I'll step back and let other maintainers offer opinions.

@aaronlehmann
Copy link
Contributor

This should not just be done for Service. There are other other objects that have Annotations and they need to be consistent.

…s is empty object {}

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
@adshmh adshmh force-pushed the 24631-service-without-labels-returns-empty-map branch from 90f27a1 to 1b347cf Compare February 15, 2017 06:43
@adshmh
Copy link
Contributor Author

adshmh commented Feb 15, 2017

@aaronlehmann Thank you for the review. I have updated the PR to include all object types using Annotations.

@LK4D4
Copy link
Contributor

LK4D4 commented Feb 17, 2017

I think we had not-nil slices and maps all around the place already.
LGTM

@LK4D4 LK4D4 merged commit 0de867b into moby:master Feb 17, 2017
@GordonTheTurtle GordonTheTurtle added this to the 1.14.0 milestone Feb 17, 2017
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.

Service without labels returns null, not {}
5 participants