-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix duplicate check with different mac format (#397)
* Use removeMacEntry function instead of directly delete macPoolMap provides a save remove entry function that could always be preferred respect the directly `delete` function. Signed-off-by: fossedihelm <[email protected]> * Fix duplicate check with different mac format As per https://pkg.go.dev/net#ParseMAC, a macAddress can be specified as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP using the following formats: - 00:00:5e:00:53:01 - 02:00:5e:10:00:00:00:01 - 00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01 - 00-00-5e-00-53-01 - 02-00-5e-10-00-00-00-01 - 00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01 - 0000.5e00.5301 - 0200.5e10.0000.0001 - 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001 Currently, the duplicate mac check works only if the two compared mac address use the same separator. The duplicate check is made looking at the existence of key of the macMap. The keys of the mapMap are the macAddress string. With this commit, a `macKey` struct is introduced. The latter stores the normalized string of the macAddress and can be used as unique key and identifier of a macAddress. NB: Currently KubeVirt does not allow EUI-64 and 20-octet IP mac addresses. Signed-off-by: fossedihelm <[email protected]> --------- Signed-off-by: fossedihelm <[email protected]>
- Loading branch information
1 parent
94abfea
commit 69258d0
Showing
13 changed files
with
144 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pool_manager | ||
|
||
import "net" | ||
|
||
type macKey struct { | ||
normalizedMacAddress string | ||
} | ||
|
||
// NewMacKey creates a macKey struct containing a mac string | ||
// that can be used for different mac string format comparison. | ||
// It uses net.ParseMAC function to parse the given macAddress | ||
// and then stores its String() value. | ||
// The given macAddress MUST have already been validated. | ||
func NewMacKey(macAddress string) macKey { | ||
// we can ignore the error here since the string value has already been validated | ||
hwMacAddress, _ := net.ParseMAC(macAddress) | ||
return macKey{normalizedMacAddress: hwMacAddress.String()} | ||
} | ||
|
||
func (m macKey) String() string { | ||
return m.normalizedMacAddress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package pool_manager | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("MacKey", func() { | ||
DescribeTable("String should return the normalized MAC address", | ||
func(input string, expected string) { | ||
macKey := NewMacKey(input) | ||
Expect(macKey.String()).To(Equal(expected)) | ||
}, | ||
Entry("colon-separated EUI-48 input", "00:00:5e:00:53:01", "00:00:5e:00:53:01"), | ||
Entry("colon-separated EUI-64 input", "02:00:5e:10:00:00:00:01", "02:00:5e:10:00:00:00:01"), | ||
Entry("colon-separated 20-octet IP input", "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01", "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01"), | ||
Entry("hyphen-separated EUI-48 input", "00-00-5e-00-53-01", "00:00:5e:00:53:01"), | ||
Entry("hyphen-separated EUI-64 input", "02-00-5e-10-00-00-00-01", "02:00:5e:10:00:00:00:01"), | ||
Entry("hyphen-separated 20-octet IP input", "00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01", "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01"), | ||
Entry("period-separated EUI-48 input", "0000.5e00.5301", "00:00:5e:00:53:01"), | ||
Entry("period-separated EUI-64 input", "0200.5e10.0000.0001", "02:00:5e:10:00:00:00:01"), | ||
Entry("period-separated 20-octet IP input", "0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001", "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01"), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.