Skip to content

Commit

Permalink
Merge pull request #298 from SchSeba/fix_cache
Browse files Browse the repository at this point in the history
Fix the cache save
  • Loading branch information
adrianchiris authored May 26, 2024
2 parents 20c67e0 + a4fbc52 commit 36570f5
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 27 deletions.
16 changes: 8 additions & 8 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,44 +175,44 @@ var _ = Describe("Config", func() {
})
Context("Checking GetMacAddressForResult function", func() {
It("Should return the mac address requested by the user", func() {
netconf := &types.NetConf{
netconf := &types.NetConf{SriovNetConf: types.SriovNetConf{
MAC: "MAC",
OrigVfState: types.VfState{
EffectiveMAC: "EffectiveMAC",
AdminMAC: "AdminMAC",
},
}},
}

Expect(GetMacAddressForResult(netconf)).To(Equal("MAC"))
})
It("Should return the EffectiveMAC mac address if the user didn't request and the the driver is not DPDK", func() {
netconf := &types.NetConf{
netconf := &types.NetConf{SriovNetConf: types.SriovNetConf{
DPDKMode: false,
OrigVfState: types.VfState{
EffectiveMAC: "EffectiveMAC",
AdminMAC: "AdminMAC",
},
}},
}

Expect(GetMacAddressForResult(netconf)).To(Equal("EffectiveMAC"))
})
It("Should return the AdminMAC mac address if the user didn't request and the the driver is DPDK", func() {
netconf := &types.NetConf{
netconf := &types.NetConf{SriovNetConf: types.SriovNetConf{
DPDKMode: true,
OrigVfState: types.VfState{
EffectiveMAC: "EffectiveMAC",
AdminMAC: "AdminMAC",
},
}},
}

Expect(GetMacAddressForResult(netconf)).To(Equal("AdminMAC"))
})
It("Should return empty string if the user didn't request the the driver is DPDK and adminMac is 0", func() {
netconf := &types.NetConf{
netconf := &types.NetConf{SriovNetConf: types.SriovNetConf{
DPDKMode: true,
OrigVfState: types.VfState{
AdminMAC: "00:00:00:00:00:00",
},
}},
}

Expect(GetMacAddressForResult(netconf)).To(Equal(""))
Expand Down
34 changes: 19 additions & 15 deletions pkg/sriov/sriov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ var _ = Describe("Sriov", func() {

BeforeEach(func() {
podifName = "net1"
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
OrigVfState: sriovtypes.VfState{
HostIFName: "enp175s6",
},
}},
}
t = GinkgoT()
})
Expand Down Expand Up @@ -243,10 +243,10 @@ var _ = Describe("Sriov", func() {
)

BeforeEach(func() {
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
VFID: 0,
}
}}
mocked = &mocks_utils.NetlinkManager{}
fakeLink = &utils.FakeLink{}
})
Expand Down Expand Up @@ -299,14 +299,14 @@ var _ = Describe("Sriov", func() {

BeforeEach(func() {
podifName = "net1"
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
OrigVfState: sriovtypes.VfState{
HostIFName: "enp175s6",
EffectiveMAC: "6e:16:06:0e:b7:e9",
},
}},
}
})
It("Assuming existing interface", func() {
Expand Down Expand Up @@ -342,14 +342,14 @@ var _ = Describe("Sriov", func() {

BeforeEach(func() {
podifName = "net1"
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
OrigVfState: sriovtypes.VfState{
HostIFName: "enp175s6",
EffectiveMAC: "c6:c8:7f:1f:21:90",
},
}},
}
})
It("Should not restores Effective MAC address when it is not provided in netconf", func() {
Expand Down Expand Up @@ -413,13 +413,13 @@ var _ = Describe("Sriov", func() {
)

BeforeEach(func() {
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
OrigVfState: sriovtypes.VfState{
HostIFName: "enp175s6",
},
}},
}
})
It("Saves the current VF state", func() {
Expand Down Expand Up @@ -452,13 +452,13 @@ var _ = Describe("Sriov", func() {
)

BeforeEach(func() {
netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
OrigVfState: sriovtypes.VfState{
HostIFName: "enp175s6",
},
}},
}
})
It("Does not change VF config if it wasnt requested to be changed in netconf", func() {
Expand All @@ -483,7 +483,7 @@ var _ = Describe("Sriov", func() {
maxTxRate := 4000
minTxRate := 1000

netconf = &sriovtypes.NetConf{
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "enp175s0f1",
DeviceID: "0000:af:06.0",
VFID: 0,
Expand All @@ -505,7 +505,7 @@ var _ = Describe("Sriov", func() {
MinTxRate: 0,
MaxTxRate: 0,
LinkState: 2, // disable
},
}},
}
})
It("Restores original VF configurations", func() {
Expand Down Expand Up @@ -540,7 +540,11 @@ var _ = Describe("Sriov", func() {
mockedPciUtils := &mocks.PciUtils{}
vlan := 0
vlanProto := sriovtypes.Proto8021q
netconf = &sriovtypes.NetConf{Master: "ens1s0", Vlan: &vlan, VlanQoS: &vlan, VlanProto: &vlanProto}
netconf = &sriovtypes.NetConf{SriovNetConf: sriovtypes.SriovNetConf{
Master: "ens1s0",
Vlan: &vlan,
VlanQoS: &vlan,
VlanProto: &vlanProto}}
fakeLink := &utils.FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "ens1s0",
Expand Down
41 changes: 40 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"encoding/json"
"fmt"
"github.com/containernetworking/cni/pkg/types"
"github.com/vishvananda/netlink"
)
Expand Down Expand Up @@ -40,9 +42,13 @@ func (vs *VfState) FillFromVfInfo(info *netlink.VfInfo) {
vs.Trust = info.Trust != 0
}

// NetConf extends types.NetConf for sriov-cni
type NetConf struct {
types.NetConf
SriovNetConf
}

// NetConf extends types.NetConf for sriov-cni
type SriovNetConf struct {
OrigVfState VfState // Stores the original VF state as it was prior to any operations done during cmdAdd flow
DPDKMode bool `json:"-"`
Master string
Expand All @@ -64,3 +70,36 @@ type NetConf struct {
LogLevel string `json:"logLevel,omitempty"`
LogFile string `json:"logFile,omitempty"`
}

func (n *NetConf) MarshalJSON() ([]byte, error) {
netConfBytes, err := json.Marshal(&n.NetConf)
if err != nil {
return nil, fmt.Errorf("error serializing delegate netConf: %v", err)
}

sriovNetConfBytes, err := json.Marshal(&n.SriovNetConf)
if err != nil {
return nil, fmt.Errorf("error serializing delegate sriovNetConf: %v", err)
}

netConfMap := make(map[string]interface{})
if err := json.Unmarshal(netConfBytes, &netConfMap); err != nil {
return nil, err
}

sriovNetConfMap := make(map[string]interface{})
if err := json.Unmarshal(sriovNetConfBytes, &sriovNetConfMap); err != nil {
return nil, err
}

for k, v := range netConfMap {
sriovNetConfMap[k] = v
}

sriovNetConfBytes, err = json.Marshal(sriovNetConfMap)
if err != nil {
return nil, err
}

return sriovNetConfBytes, nil
}
8 changes: 5 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"time"

sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types"
)

var (
Expand Down Expand Up @@ -238,10 +240,10 @@ func HasDpdkDriver(pciAddr string) (bool, error) {

// SaveNetConf takes in container ID, data dir and Pod interface name as string and a json encoded struct Conf
// and save this Conf in data dir
func SaveNetConf(cid, dataDir, podIfName string, conf interface{}) error {
netConfBytes, err := json.Marshal(conf)
func SaveNetConf(cid, dataDir, podIfName string, netConf *sriovtypes.NetConf) error {
netConfBytes, err := json.Marshal(netConf)
if err != nil {
return fmt.Errorf("error serializing delegate netconf: %v", err)
return fmt.Errorf("error serializing delegate netConf: %v", err)
}

s := []string{cid, podIfName}
Expand Down
49 changes: 49 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package utils

import (
"encoding/json"
"errors"
"net"
"os"
"path/filepath"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/vishvananda/netlink"

cnitypes "github.com/containernetworking/cni/pkg/types"

sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types"
mocks_utils "github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils/mocks"
)

Expand Down Expand Up @@ -184,4 +190,47 @@ var _ = Describe("Utils", func() {
Expect(err).ToNot(HaveOccurred())
})
})

Context("Checking SaveNetConf function", func() {
var tmpDir string

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "sriov")
Expect(err).ToNot(HaveOccurred())
})
It("should save all the netConf struct to the cache file without dns", func() {
netconf := &sriovtypes.NetConf{NetConf: cnitypes.NetConf{CNIVersion: "1.0.0"}, SriovNetConf: sriovtypes.SriovNetConf{DeviceID: "0000:af:06.0"}}
err := SaveNetConf("test", tmpDir, "net1", netconf)
Expect(err).ToNot(HaveOccurred())

data, err := os.ReadFile(filepath.Join(tmpDir, "test-net1"))
Expect(err).ToNot(HaveOccurred())
Expect(data).ToNot(ContainSubstring("dns"))

newNetConf := &sriovtypes.NetConf{}
err = json.Unmarshal(data, newNetConf)
Expect(err).ToNot(HaveOccurred())

Expect(netconf.DeviceID).To(Equal(newNetConf.DeviceID))
Expect(netconf.CNIVersion).To(Equal(newNetConf.CNIVersion))
})
It("should save all the netConf struct to the cache file with dns", func() {
netconf := sriovtypes.NetConf{NetConf: cnitypes.NetConf{CNIVersion: "1.0.0", DNS: cnitypes.DNS{Domain: "bla"}}, SriovNetConf: sriovtypes.SriovNetConf{DeviceID: "0000:af:06.0"}}
err := SaveNetConf("test", tmpDir, "net1", &netconf)
Expect(err).ToNot(HaveOccurred())

data, err := os.ReadFile(filepath.Join(tmpDir, "test-net1"))
Expect(err).ToNot(HaveOccurred())
Expect(data).To(ContainSubstring("dns"))

newNetConf := &sriovtypes.NetConf{}
err = json.Unmarshal(data, newNetConf)
Expect(err).ToNot(HaveOccurred())

Expect(netconf.DeviceID).To(Equal(newNetConf.DeviceID))
Expect(netconf.CNIVersion).To(Equal(newNetConf.CNIVersion))
Expect(netconf.DNS.Domain).To(Equal(newNetConf.DNS.Domain))
})
})
})

0 comments on commit 36570f5

Please sign in to comment.