Skip to content

Commit

Permalink
cni: fix assertion ordering in tests with multiple node networks
Browse files Browse the repository at this point in the history
ignore comparing networking for now in test

Apply suggestions from code review

Co-authored-by: Seth Hoenig <shoenig@hashicorp.com>
  • Loading branch information
nickethier and shoenig committed May 15, 2020
1 parent d858411 commit 7611b30
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 23 deletions.
8 changes: 4 additions & 4 deletions client/allocrunner/networking_cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type cniNetworkConfigurator struct {
func newCNINetworkConfigurator(logger log.Logger, cniPath, cniInterfacePrefix, cniConfDir, networkName string) (*cniNetworkConfigurator, error) {
cniConf, err := loadCNIConf(cniConfDir, networkName)
if err != nil {
return nil, fmt.Errorf("failed to load cni config: %v", err)
return nil, fmt.Errorf("failed to load CNI config: %v", err)
}

return newCNINetworkConfiguratorWithConf(logger, cniPath, cniInterfacePrefix, cniConf)
Expand Down Expand Up @@ -83,7 +83,7 @@ func (c *cniNetworkConfigurator) Setup(ctx context.Context, alloc *structs.Alloc

// Depending on the version of bridge cni plugin used, a known race could occure
// where two alloc attempt to create the nomad bridge at the same time, resulting
// in one of them to fail. This rety attempts to overcome any
// in one of them to fail. This rety attempts to overcome those erroneous failures.
const retry = 3
var firstError error
for attempt := 1; ; attempt++ {
Expand Down Expand Up @@ -112,9 +112,9 @@ func loadCNIConf(confDir, name string) ([]byte, error) {
files, err := cnilibrary.ConfFiles(confDir, []string{".conf", ".conflist", ".json"})
switch {
case err != nil:
return nil, fmt.Errorf("failed to read cni config file: %v", err)
return nil, fmt.Errorf("failed to detect CNI config file: %v", err)
case len(files) == 0:
return nil, fmt.Errorf("no cni network config found in %s", confDir)
return nil, fmt.Errorf("no CNI network config found in %s", confDir)
}

// files contains the network config files associated with cni network.
Expand Down
4 changes: 3 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,8 @@ func (c *Client) updateNodeFromFingerprint(response *fingerprint.FingerprintResp
return c.configCopy.Node
}

// updateNetworks filters and overrides network speed of host networks based
// on configured settings
func updateNetworks(up structs.Networks, c *config.Config) structs.Networks {
if up == nil {
return nil
Expand All @@ -1463,7 +1465,7 @@ func updateNetworks(up structs.Networks, c *config.Config) structs.Networks {
up = upd
}

// ns is set, apply the config NetworkSpeed to all
// if set, apply the config NetworkSpeed to networks in host mode
if c.NetworkSpeed != 0 {
for _, n := range up {
if n.Mode == "host" {
Expand Down
10 changes: 7 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ func Test_UpdateNodeFromFingerprintMultiIP(t *testing.T) {
// Client without network configured updates to match fingerprint
client, cleanup := TestClient(t, func(c *config.Config) {
c.NetworkInterface = dev
c.Node.NodeResources.Networks[0].Device = dev
c.Options["fingerprint.blacklist"] = "network,cni,bridge"
c.Node.Resources.Networks = c.Node.NodeResources.Networks
})
defer cleanup()
Expand All @@ -1278,12 +1278,13 @@ func Test_UpdateNodeFromFingerprintMultiIP(t *testing.T) {
},
})

two := structs.Networks{
nets := structs.Networks{
mock.Node().NodeResources.Networks[0],
{Device: dev, IP: "127.0.0.1"},
{Device: dev, IP: "::1"},
}

require.Equal(t, two, client.config.Node.NodeResources.Networks)
require.Equal(t, nets, client.config.Node.NodeResources.Networks)
}

func TestClient_computeAllocatedDeviceStats(t *testing.T) {
Expand Down Expand Up @@ -1474,6 +1475,9 @@ func TestClient_getAllocatedResources(t *testing.T) {

result := client.getAllocatedResources(client.config.Node)

// Ignore comparing networks for now
result.Flattened.Networks = nil

expected := structs.ComparableResources{
Flattened: structs.AllocatedTaskResources{
Cpu: structs.AllocatedCpuResources{
Expand Down
7 changes: 5 additions & 2 deletions client/fingerprint/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"fmt"
"os"
"strings"
"regexp"

"github.com/hashicorp/nomad/nomad/structs"
)
Expand Down Expand Up @@ -36,9 +36,12 @@ func (f *BridgeFingerprint) checkKMod(mod string) error {
defer file.Close()

scanner := bufio.NewScanner(file)
pattern := fmt.Sprintf("%s\\s+.*$", mod)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), mod+" ") {
if matched, err := regexp.MatchString(pattern, scanner.Text()); matched {
return nil
} else if err != nil {
return fmt.Errorf("could not parse /proc/modules: %v", err)
}
}

Expand Down
8 changes: 5 additions & 3 deletions client/fingerprint/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (f *CNIFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintR

files, err := libcni.ConfFiles(confDir, []string{".conf", ".conflist", ".json"})
if err != nil {
return fmt.Errorf("failed to read cni conf files: %v", err)
return fmt.Errorf("failed to detect CNI conf files: %v", err)
}

for _, confFile := range files {
Expand All @@ -40,7 +40,7 @@ func (f *CNIFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintR
return fmt.Errorf("failed to load CNI config list file %s: %v", confFile, err)
}
if _, ok := networks[confList.Name]; ok {
f.logger.Warn("multiple CNI config names found, ignoring file", "name", confList.Name, "file", confFile)
f.logger.Warn("duplicate CNI config names found, ignoring file", "name", confList.Name, "file", confFile)
continue
}
networks[confList.Name] = struct{}{}
Expand All @@ -50,7 +50,7 @@ func (f *CNIFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintR
return fmt.Errorf("failed to load CNI config file %s: %v", confFile, err)
}
if _, ok := networks[conf.Network.Name]; ok {
f.logger.Warn("multiple CNI config names found, ignoring file", "name", conf.Network.Name, "file", confFile)
f.logger.Warn("duplicate CNI config names found, ignoring file", "name", conf.Network.Name, "file", confFile)
continue
}
networks[conf.Network.Name] = struct{}{}
Expand All @@ -73,3 +73,5 @@ func (f *CNIFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintR
resp.Detected = true
return nil
}

func (f *CNIFingerprint) Reload() {}
16 changes: 11 additions & 5 deletions client/fingerprint/cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/stretchr/testify/require"
)

// Test that CNI fingerprinter is reloadable
var _ ReloadableFingerprint = &CNIFingerprint{}

func TestCNIFingerprint(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -63,16 +66,19 @@ func TestCNIFingerprint(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
require := require.New(t)
r := require.New(t)
fp := NewCNIFingerprint(testlog.HCLogger(t))
resp := &FingerprintResponse{}
err := fp.Fingerprint(c.req, resp)
if c.err {
require.Error(err)
require.Contains(err.Error(), c.errMatch)
r.Error(err)
r.Contains(err.Error(), c.errMatch)
} else {
require.NoError(err)
require.Exactly(c.exp, resp)
r.NoError(err)
r.Equal(c.exp.Detected, resp.Detected)
if resp.NodeResources != nil || c.exp.NodeResources != nil {
r.ElementsMatch(c.exp.NodeResources.Networks, resp.NodeResources.Networks)
}
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/env_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (f *EnvAWSFingerprint) Fingerprint(request *FingerprintRequest, response *F
response.AddAttribute("unique.network.ip-address", val)

newNetwork := &structs.NetworkResource{
Mode: "host",
Mode: "host",
Device: "eth0",
IP: val,
CIDR: val + "/32",
Expand Down
3 changes: 1 addition & 2 deletions client/fingerprint/test_fixtures/cni/net2.conflist
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"type": "portmap",
"capabilities": {
"portMappings": true
},
"externalSetMarkChain": "KUBE-MARK-MASQ"
}
}
]
}
4 changes: 2 additions & 2 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2235,11 +2235,11 @@ func (nr *NetworkResource) Hash() uint32 {
data = append(data, []byte(fmt.Sprintf("%s%s%s%s%d", nr.Mode, nr.Device, nr.CIDR, nr.IP, nr.MBits))...)

for i, port := range nr.ReservedPorts {
data = append(data, []byte(fmt.Sprintf("%d%s%d%d", i, port.Label, port.Value, port.To))...)
data = append(data, []byte(fmt.Sprintf("r%d%s%d%d", i, port.Label, port.Value, port.To))...)
}

for i, port := range nr.DynamicPorts {
data = append(data, []byte(fmt.Sprintf("%d%s%d%d", i, port.Label, port.Value, port.To))...)
data = append(data, []byte(fmt.Sprintf("d%d%s%d%d", i, port.Label, port.Value, port.To))...)
}

return crc32.ChecksumIEEE(data)
Expand Down

0 comments on commit 7611b30

Please sign in to comment.