Skip to content

Commit

Permalink
localnet, e2e: parse VLAN iface IP in the constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
  • Loading branch information
maiqueb committed Oct 29, 2024
1 parent 5c16420 commit 2eb0107
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
54 changes: 31 additions & 23 deletions test/e2e/localnet-underlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,35 @@ type Vlan struct {
deviceName string
id int
name string
ip *string
ip *net.IPNet
}

type option func(vlan *Vlan)
type option func(vlan *Vlan) error

func newVLANIface(deviceName string, vlanID int, opts ...option) *Vlan {
func newVLANIface(deviceName string, vlanID int, opts ...option) (*Vlan, error) {
vlan := &Vlan{
deviceName: deviceName,
id: vlanID,
name: vlanName(deviceName, fmt.Sprintf("%d", vlanID)),
}

for _, opt := range opts {
opt(vlan)
if err := opt(vlan); err != nil {
return nil, err
}
}
return vlan
return vlan, nil
}

func withIP(ipAddress string) option {
return func(vlan *Vlan) {
vlan.ip = &ipAddress
return func(vlan *Vlan) error {
ip, cidr, err := net.ParseCIDR(ipAddress)
if err != nil {
return fmt.Errorf("failed to parse IP address %s: %w", ipAddress, err)
}
cidr.IP = ip
vlan.ip = cidr
return nil
}
}

Expand Down Expand Up @@ -243,25 +251,11 @@ func (v *Vlan) ensureVLANHasIP() error {
return fmt.Errorf("failed to find VLAN interface %s: %v", v.name, err)
}

ip, cidr, err := net.ParseCIDR(*v.ip)
if err != nil {
return fmt.Errorf("failed to parse IP address %s: %v", *v.ip, err)
}
cidr.IP = ip

vlanAddrs, err := netlink.AddrList(vlanIface, netlink.FAMILY_ALL)
addr := netlink.Addr{IPNet: v.ip}
hasIP, err := isIPInLink(vlanIface, addr)
if err != nil {
return err
}
addr := netlink.Addr{IPNet: cidr}

hasIP := false
for _, vlanAddr := range vlanAddrs {
if vlanAddr.Equal(addr) {
hasIP = true
break
}
}

if !hasIP {
if err := netlink.AddrAdd(vlanIface, &addr); err != nil {
Expand All @@ -286,3 +280,17 @@ func vlanName(deviceName string, vlanID string) string {
}
return fmt.Sprintf("%s.%s", deviceName, vlanID)
}

func isIPInLink(vlanIface netlink.Link, addr netlink.Addr) (bool, error) {
vlanAddrs, err := netlink.AddrList(vlanIface, netlink.FAMILY_ALL)
if err != nil {
return false, err
}

for _, vlanAddr := range vlanAddrs {
if vlanAddr.Equal(addr) {
return true, nil
}
}
return false, nil
}
3 changes: 2 additions & 1 deletion test/e2e/multihoming.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ var _ = Describe("Multi Homing", func() {

underlayBridgeName, err = findInterfaceByIP(gatewayIP)
Expect(err).NotTo(HaveOccurred())
vlanIface = newVLANIface(underlayBridgeName, vlanID, withIP(underlayIP))
vlanIface, err = newVLANIface(underlayBridgeName, vlanID, withIP(underlayIP))
Expect(err).NotTo(HaveOccurred())
Expect(vlanIface.create()).To(
Succeed(),
"create a VLAN interface on the bridge interconnecting the cluster nodes",
Expand Down

0 comments on commit 2eb0107

Please sign in to comment.