diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 74d7179f8..fe7b81425 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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("")) diff --git a/pkg/sriov/sriov_test.go b/pkg/sriov/sriov_test.go index dd0541c41..5f9512d2b 100644 --- a/pkg/sriov/sriov_test.go +++ b/pkg/sriov/sriov_test.go @@ -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() }) @@ -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{} }) @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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, @@ -505,7 +505,7 @@ var _ = Describe("Sriov", func() { MinTxRate: 0, MaxTxRate: 0, LinkState: 2, // disable - }, + }}, } }) It("Restores original VF configurations", func() { @@ -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", diff --git a/pkg/types/types.go b/pkg/types/types.go index b3dccc4c3..07b30f83f 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -1,6 +1,8 @@ package types import ( + "encoding/json" + "fmt" "github.com/containernetworking/cni/pkg/types" "github.com/vishvananda/netlink" ) @@ -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 @@ -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 +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 81bcf70f3..b5bd2b95f 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types" ) var ( @@ -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} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 273f02e91..bcd891aa0 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -1,8 +1,11 @@ package utils import ( + "encoding/json" "errors" "net" + "os" + "path/filepath" "time" . "github.com/onsi/ginkgo/v2" @@ -10,6 +13,9 @@ import ( "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" ) @@ -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)) + }) + }) })