Skip to content

Commit

Permalink
cni: prevent NPE if no interface has sandbox field set
Browse files Browse the repository at this point in the history
When we iterate over the interfaces returned from CNI setup, we filter for one
with the `Sandbox` field set. Ensure that if none of the interfaces has that
field set that we still return an available interface.
  • Loading branch information
tgross committed Dec 16, 2020
1 parent 9a112af commit 463c304
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IMPROVEMENTS:
BUG FIXES:
* core: Fixed a bug where ACLToken and ACLPolicy changes were ignored by the event stream [[GH-9595](https://github.com/hashicorp/nomad/issues/9595)]
* core: Fixed a bug to honor HCL2 variables set by environment variables or variable files [[GH-9592](https://github.com/hashicorp/nomad/issues/9592)] [[GH-9623](https://github.com/hashicorp/nomad/issues/9623)]
* cni: Fixed a bug where plugins that do not set the interface sandbox value could crash the Nomad client. [[GH-9648](https://github.com/hashicorp/nomad/issues/9648)]

## 1.0.0 (December 8, 2020)

Expand Down
20 changes: 13 additions & 7 deletions client/allocrunner/networking_cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@ func (c *cniNetworkConfigurator) Setup(ctx context.Context, alloc *structs.Alloc
netStatus := new(structs.AllocNetworkStatus)

if len(res.Interfaces) > 0 {
iface, name := func(r *cni.CNIResult) (*cni.Config, string) {
for i := range r.Interfaces {
if r.Interfaces[i].Sandbox != "" {
return r.Interfaces[i], i
}
// find an interface with Sandbox set, or any one of them if no
// interface has it set
var iface *cni.Config
var name string
for name, iface = range res.Interfaces {
if iface != nil && iface.Sandbox != "" {
break
}
return nil, ""
}(res)
}
if iface == nil {
// this should never happen but this value is coming from external
// plugins so we should guard against it
return nil, fmt.Errorf("failed to configure network: no valid interface")
}

netStatus.InterfaceName = name
if len(iface.IPConfigs) > 0 {
Expand Down

0 comments on commit 463c304

Please sign in to comment.