Skip to content

Commit

Permalink
Fix the cache save
Browse files Browse the repository at this point in the history
before we send a pointer to the netConf object so the marshal
function didn't parse all the information needed

Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed May 17, 2024
1 parent 20c67e0 commit d762c31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/sriov/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func cmdAdd(args *skel.CmdArgs) error {
"func", "cmdAdd",
"config.DefaultCNIDir", config.DefaultCNIDir,
"netConf", netConf)
if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, netConf); err != nil {
if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, *netConf); err != nil {
return fmt.Errorf("error saving NetConf %q", err)
}

Expand Down
6 changes: 4 additions & 2 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,8 +240,8 @@ 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)
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package utils

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

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

"github.com/vishvananda/netlink"

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

Expand Down Expand Up @@ -184,4 +188,28 @@ 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", func() {
netconf := &sriovtypes.NetConf{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())

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

Expect(netconf.DeviceID).To(Equal(newNetConf.DeviceID))
})
})
})

0 comments on commit d762c31

Please sign in to comment.