-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9895 from hashicorp/b-cni-ipaddr
CNI: add fallback logic if no ip address references sandboxed interface
- Loading branch information
1 parent
f279626
commit 98ef16e
Showing
2 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package allocrunner | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
cni "github.com/containerd/go-cni" | ||
"github.com/hashicorp/nomad/helper/testlog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestCNI_cniToAllocNet_Fallback asserts if a CNI plugin result lacks an IP on | ||
// its sandbox interface, the first IP found is used. | ||
func TestCNI_cniToAllocNet_Fallback(t *testing.T) { | ||
// Calico's CNI plugin v3.12.3 has been observed to return the | ||
// following: | ||
cniResult := &cni.CNIResult{ | ||
Interfaces: map[string]*cni.Config{ | ||
"cali39179aa3-74": &cni.Config{}, | ||
"eth0": &cni.Config{ | ||
IPConfigs: []*cni.IPConfig{ | ||
&cni.IPConfig{ | ||
IP: net.IPv4(192, 168, 135, 232), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Only need a logger | ||
c := &cniNetworkConfigurator{ | ||
logger: testlog.HCLogger(t), | ||
} | ||
allocNet, err := c.cniToAllocNet(cniResult) | ||
require.NoError(t, err) | ||
require.NotNil(t, allocNet) | ||
assert.Equal(t, "192.168.135.232", allocNet.Address) | ||
assert.Equal(t, "eth0", allocNet.InterfaceName) | ||
assert.Nil(t, allocNet.DNS) | ||
} | ||
|
||
// TestCNI_cniToAllocNet_Invalid asserts an error is returned if a CNI plugin | ||
// result lacks any IP addresses. This has not been observed, but Nomad still | ||
// must guard against invalid results from external plugins. | ||
func TestCNI_cniToAllocNet_Invalid(t *testing.T) { | ||
cniResult := &cni.CNIResult{ | ||
Interfaces: map[string]*cni.Config{ | ||
"eth0": &cni.Config{}, | ||
"veth1": &cni.Config{ | ||
IPConfigs: []*cni.IPConfig{}, | ||
}, | ||
}, | ||
} | ||
|
||
// Only need a logger | ||
c := &cniNetworkConfigurator{ | ||
logger: testlog.HCLogger(t), | ||
} | ||
allocNet, err := c.cniToAllocNet(cniResult) | ||
require.Error(t, err) | ||
require.Nil(t, allocNet) | ||
} |