-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
types: fix marshalling of omitted "interfaces" key in IPConfig JSON #477
Conversation
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 I don't love it. But it's worth considering. |
We really have no other choice. It's an optional field. (something something type unions something rust something) |
489e9e5
to
edbbde7
Compare
@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! |
pkg/types/current/types.go
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this doc comment.
There was a problem hiding this comment.
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
edbbde7
to
30ab805
Compare
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