Skip to content

Commit

Permalink
Merge pull request #41 from maiqueb/rename-master-api
Browse files Browse the repository at this point in the history
Rename master api
  • Loading branch information
kubevirt-bot authored May 24, 2021
2 parents 9c53132 + acb5646 commit f6ec806
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The value is a json array and each element of the array is a separate resource
to be made available:

* `name` (string, required) the name of the resource
* `master` (string, required) the name of the macvtap lower link
* `lowerDevice` (string, required) the name of the macvtap lower link
* `mode` (string, optional, default=bridge) the macvtap operating mode
* `capacity` (uint, optional, default=100) the capacity of the resource

Expand All @@ -36,7 +36,7 @@ data:
DP_MACVTAP_CONF: |
[ {
"name" : "dataplane",
"master" : "eth0",
"lowerDevice" : "eth0",
"mode": "bridge",
"capacity" : 50
} ]
Expand Down
2 changes: 1 addition & 1 deletion examples/macvtap-deviceplugin-config-explicit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data:
DP_MACVTAP_CONF: >-
[ {
"name" : "eth0",
"master" : "eth0",
"lowerDevice" : "eth0",
"mode": "bridge",
"capacity" : 50
} ]
18 changes: 9 additions & 9 deletions pkg/deviceplugin/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const (
)

type macvtapConfig struct {
Name string `json:"name"`
Master string `json:"master"`
Mode string `json:"mode"`
Capacity int `json:"capacity"`
Name string `json:"name"`
LowerDevice string `json:"lowerDevice"`
Mode string `json:"mode"`
Capacity int `json:"capacity"`
}

type macvtapLister struct {
Expand Down Expand Up @@ -157,13 +157,13 @@ func (ml *macvtapLister) NewPlugin(name string) dpm.PluginInterface {
c, ok := ml.Config[name]
if !ok {
c = macvtapConfig{
Name: name,
Master: name,
Mode: DefaultMode,
Capacity: DefaultCapacity,
Name: name,
LowerDevice: name,
Mode: DefaultMode,
Capacity: DefaultCapacity,
}
}

glog.V(3).Infof("Creating device plugin with config %+v", c)
return NewMacvtapDevicePlugin(c.Name, c.Master, c.Mode, c.Capacity, ml.NetNsPath)
return NewMacvtapDevicePlugin(c.Name, c.LowerDevice, c.Mode, c.Capacity, ml.NetNsPath)
}
50 changes: 25 additions & 25 deletions pkg/deviceplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const (
)

type macvtapDevicePlugin struct {
Name string
Master string
Mode string
Capacity int
Name string
LowerDevice string
Mode string
Capacity int
// NetNsPath is the path to the network namespace the plugin operates in.
NetNsPath string
stopWatcher chan struct{}
}

func NewMacvtapDevicePlugin(name string, master string, mode string, capacity int, netNsPath string) *macvtapDevicePlugin {
func NewMacvtapDevicePlugin(name string, lowerDevice string, mode string, capacity int, netNsPath string) *macvtapDevicePlugin {
return &macvtapDevicePlugin{
Name: name,
Master: master,
LowerDevice: lowerDevice,
Mode: mode,
Capacity: capacity,
NetNsPath: netNsPath,
Expand Down Expand Up @@ -62,47 +62,47 @@ func (mdp *macvtapDevicePlugin) generateMacvtapDevices() []*pluginapi.Device {
}

func (mdp *macvtapDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
// Initialize two arrays, one for devices offered when master exists,
// and no devices if master does not exist.
// Initialize two arrays, one for devices offered when lower device exists,
// and no devices if lower device does not exist.
allocatableDevs := mdp.generateMacvtapDevices()
emptyDevs := make([]*pluginapi.Device, 0)

emitResponse := func(masterExists bool) {
if masterExists {
glog.V(3).Info("Master exists, sending ListAndWatch response with available devices")
emitResponse := func(lowerDeviceExists bool) {
if lowerDeviceExists {
glog.V(3).Info("LowerDevice exists, sending ListAndWatch response with available devices")
s.Send(&pluginapi.ListAndWatchResponse{Devices: allocatableDevs})
} else {
glog.V(3).Info("Master does not exist, sending ListAndWatch response with no devices")
glog.V(3).Info("LowerDevice does not exist, sending ListAndWatch response with no devices")
s.Send(&pluginapi.ListAndWatchResponse{Devices: emptyDevs})
}
}

didMasterExist := false
onMasterEvent := func() {
var doesMasterExist bool
didLowerDeviceExist := false
onLowerDeviceEvent := func() {
var doesLowerDeviceExist bool
err := ns.WithNetNSPath(mdp.NetNsPath, func(_ ns.NetNS) error {
var err error
doesMasterExist, err = util.LinkExists(mdp.Master)
doesLowerDeviceExist, err = util.LinkExists(mdp.LowerDevice)
return err
})
if err != nil {
glog.Warningf("Error while checking on master %s: %v", mdp.Master, err)
glog.Warningf("Error while checking on lower device %s: %v", mdp.LowerDevice, err)
return
}

if didMasterExist != doesMasterExist {
emitResponse(doesMasterExist)
didMasterExist = doesMasterExist
if didLowerDeviceExist != doesLowerDeviceExist {
emitResponse(doesLowerDeviceExist)
didLowerDeviceExist = doesLowerDeviceExist
}
}

// Listen for events of master interface. On any, check if master a
// interface exists. If it does, offer up to capacity macvtap devices. Do
// Listen for events of lower device interface. On any, check if lower
// device exists. If it does, offer up to capacity macvtap devices. Do
// not offer any otherwise.
util.OnLinkEvent(
mdp.Master,
mdp.LowerDevice,
mdp.NetNsPath,
onMasterEvent,
onLowerDeviceEvent,
mdp.stopWatcher,
func(err error) {
glog.Error(err)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (mdp *macvtapDevicePlugin) Allocate(ctx context.Context, r *pluginapi.Alloc
var index int
err := ns.WithNetNSPath(mdp.NetNsPath, func(_ ns.NetNS) error {
var err error
index, err = util.RecreateMacvtap(name, mdp.Master, mdp.Mode)
index, err = util.RecreateMacvtap(name, mdp.LowerDevice, mdp.Mode)
return err
})
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions pkg/deviceplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,30 @@ func (s *ListAndWatchServerSendSpy) SetTrailer(m metadata.MD) {
}

var _ = Describe("Macvtap", func() {
var masterIfaceName string
var masterIface netlink.Link
var lowerDeviceIfaceName string
var lowerDeviceIface netlink.Link
var testNs ns.NetNS

BeforeEach(func() {
var err error
testNs, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())

masterIfaceName = fmt.Sprintf("master%d", rand.Intn(100))
masterIface = &netlink.Dummy{
lowerDeviceIfaceName = fmt.Sprintf("lowerdev%d", rand.Intn(100))
lowerDeviceIface = &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: masterIfaceName,
Name: lowerDeviceIfaceName,
Namespace: netlink.NsFd(int(testNs.Fd())),
},
}

err = netlink.LinkAdd(masterIface)
err = netlink.LinkAdd(lowerDeviceIface)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
testNs.Do(func(ns ns.NetNS) error {
netlink.LinkDel(masterIface)
netlink.LinkDel(lowerDeviceIface)
return nil
})
})
Expand All @@ -90,7 +90,7 @@ var _ = Describe("Macvtap", func() {
var sendSpy *ListAndWatchServerSendSpy

BeforeEach(func() {
mvdp = NewMacvtapDevicePlugin(masterIfaceName, masterIfaceName, "bridge", 0, testNs.Path())
mvdp = NewMacvtapDevicePlugin(lowerDeviceIfaceName, lowerDeviceIfaceName, "bridge", 0, testNs.Path())
sendSpy = &ListAndWatchServerSendSpy{}
go func() {
err := mvdp.ListAndWatch(nil, sendSpy)
Expand All @@ -103,7 +103,7 @@ var _ = Describe("Macvtap", func() {
})

It("should allocate a new device upon request", func() {
ifaceName := masterIfaceName + "Mvp99"
ifaceName := lowerDeviceIfaceName + "Mvp99"
req := &pluginapi.AllocateRequest{
ContainerRequests: []*pluginapi.ContainerAllocateRequest{
{
Expand Down Expand Up @@ -132,7 +132,7 @@ var _ = Describe("Macvtap", func() {
Expect(dev.HostPath).To(Equal(dev.ContainerPath))
})

Context("when master device does not exist", func() {
Context("when lower device does not exist", func() {
It("should not advertise devices", func() {
By("first advertising healthy devices", func() {
Eventually(func() int {
Expand All @@ -142,9 +142,9 @@ var _ = Describe("Macvtap", func() {
Expect(sendSpy.last.Devices).To(HaveLen(100))
})

By("then deleting the master device", func() {
By("then deleting the lower device", func() {
err := testNs.Do(func(ns ns.NetNS) error {
return util.LinkDelete(masterIfaceName)
return util.LinkDelete(lowerDeviceIfaceName)
})
Expect(err).NotTo(HaveOccurred())
})
Expand Down Expand Up @@ -183,10 +183,10 @@ var _ = Describe("Macvtap", func() {
resourceName := "dataplane"
mode := "vepa"
capacity := 30
config := `[{"name":"%s","master":"%s","mode":"%s","capacity":%d}]`
config := `[{"name":"%s","lowerDevice":"%s","mode":"%s","capacity":%d}]`

BeforeEach(func() {
config = fmt.Sprintf(config, resourceName, masterIfaceName, mode, capacity)
config = fmt.Sprintf(config, resourceName, lowerDeviceIfaceName, mode, capacity)
os.Setenv(ConfigEnvironmentVariable, config)
})

Expand All @@ -200,7 +200,7 @@ var _ = Describe("Macvtap", func() {

plugin := lister.NewPlugin(resourceName)
Expect(plugin.(*macvtapDevicePlugin).Name).To(Equal(resourceName))
Expect(plugin.(*macvtapDevicePlugin).Master).To(Equal(masterIfaceName))
Expect(plugin.(*macvtapDevicePlugin).LowerDevice).To(Equal(lowerDeviceIfaceName))
Expect(plugin.(*macvtapDevicePlugin).Mode).To(Equal(mode))
Expect(plugin.(*macvtapDevicePlugin).Capacity).To(Equal(capacity))
})
Expand Down Expand Up @@ -238,7 +238,7 @@ var _ = Describe("Macvtap", func() {

plugin := lister.NewPlugin(parentName)
Expect(plugin.(*macvtapDevicePlugin).Name).To(Equal(parentName))
Expect(plugin.(*macvtapDevicePlugin).Master).To(Equal(parentName))
Expect(plugin.(*macvtapDevicePlugin).LowerDevice).To(Equal(parentName))
Expect(plugin.(*macvtapDevicePlugin).Mode).To(Equal(DefaultMode))
Expect(plugin.(*macvtapDevicePlugin).Capacity).To(Equal(DefaultCapacity))
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func ModeFromString(s string) (netlink.MacvlanMode, error) {
}
}

func CreateMacvtap(name string, master string, mode string) (int, error) {
func CreateMacvtap(name string, lowerDevice string, mode string) (int, error) {
ifindex := 0

m, err := netlink.LinkByName(master)
m, err := netlink.LinkByName(lowerDevice)
if err != nil {
return ifindex, fmt.Errorf("failed to lookup master %q: %v", master, err)
return ifindex, fmt.Errorf("failed to lookup lowerDevice %q: %v", lowerDevice, err)
}

nlmode, err := ModeFromString(mode)
Expand Down Expand Up @@ -72,12 +72,12 @@ func CreateMacvtap(name string, master string, mode string) (int, error) {
return ifindex, nil
}

func RecreateMacvtap(name string, master string, mode string) (int, error) {
func RecreateMacvtap(name string, lowerDevice string, mode string) (int, error) {
err := LinkDelete(name)
if err != nil {
return 0, err
}
return CreateMacvtap(name, master, mode)
return CreateMacvtap(name, lowerDevice, mode)
}

func LinkExists(link string) (bool, error) {
Expand Down

0 comments on commit f6ec806

Please sign in to comment.