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

types: fix marshalling of omitted "interfaces" key in IPConfig JSON #477

Merged

Conversation

dcbw
Copy link
Member

@dcbw dcbw commented Jun 15, 2017

Plugins that don't have knowledge of interfaces, like host-local or
other IPAM plugins, should not set the 'interfaces' key of their
returned "Result" JSON. This should then not be translated into
an interface index of 0, which it was due to the int marshaling and
omitempty.

Instead, ensure that an omitted 'interface' in JSON ends up being
-1 in the IPConfig structure, and that a -1 ensures that no 'interfaces'
key is present in the JSON.

Yes, this means that you must set Interface:-1 in IPConfig in your
plugin, but if you weren't already doing that and your plugin didn't
deal with interface indexes, you already had a problem.

@containernetworking/cni-maintainers

A bit ugly, but we can't also change the IPConfig.Interface field to a *int because then it's really, really icky to actually set that member, since you can't &0 and such. Thoughts?

Will require a plugins revendor and update as well.

Supercedes: #434
Fixes: #404

@rosenhouse
Copy link
Contributor

but we can't also change the IPConfig.Interface field to a *int because then it's really, really icky to actually set that member, since you can't &0 and such.

Some libraries address this by providing a helper function, e.g.

// Int returns a pointer to the int value passed in.
func Int(v int) *int {
	return &v
}

[ref aws-sdk-go]

I don't love it. But it's worth considering.

@squeed
Copy link
Member

squeed commented Jun 16, 2017

We really have no other choice. It's an optional field.

(something something type unions something rust something)

@dcbw dcbw force-pushed the fix-interface-index-marshaling branch from 489e9e5 to edbbde7 Compare June 21, 2017 04:20
@dcbw
Copy link
Member Author

dcbw commented Jun 21, 2017

@rosenhouse yeah, that's the least bad option. I disliked having to ensure Interface: was -1, I feel like that would often be forgotten. Your way is better. PTAL thanks!

// IPConfig contains values necessary to configure an IP address on an interface
type IPConfig struct {
// IP version, either "4" or "6"
Version string
// Index into Result structs Interfaces list
Interface int
// Index into Result structs Interfaces list; -1 if no interface is given
Copy link
Member

Choose a reason for hiding this comment

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

remove this doc comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

@squeed done

Plugins that don't have knowledge of interfaces, like host-local or
other IPAM plugins, should not set the 'interfaces' key of their
returned "Result" JSON.  This should then not be translated into
an interface index of 0, which it was due to the int marshaling and
omitempty.

Instead, ensure that an omitted 'interface' in JSON ends up being
nil in the IPConfig structure, and that a nil ensures that no 'interfaces'
key is present in the JSON.

Yes, this means that plugins must call the 'current.Int(x)' method
when setting the Interfaces member.  Oh well.

Fixes: containernetworking#404
@dcbw dcbw force-pushed the fix-interface-index-marshaling branch from edbbde7 to 30ab805 Compare June 21, 2017 14:56
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