Skip to content

Commit

Permalink
Set gateway only once
Browse files Browse the repository at this point in the history
Signed-off-by: Liran Rotenberg <lrotenbe@redhat.com>
  • Loading branch information
liranr23 committed Aug 13, 2024
1 parent bc99146 commit 41efdd8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 60 deletions.
62 changes: 33 additions & 29 deletions pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"errors"
"fmt"
"net"
"net/netip"
liburl "net/url"
"path"
"regexp"
"sort"
"strconv"
"strings"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
Expand Down Expand Up @@ -195,7 +195,10 @@ func (r *Builder) PodEnvironment(vmRef ref.Ref, sourceSecret *core.Secret) (env

macsToIps := ""
if r.Plan.Spec.PreserveStaticIPs {
macsToIps = r.mapMacStaticIps(vm)
macsToIps, err = r.mapMacStaticIps(vm)
if err != nil {
return
}
}

libvirtURL, fingerprint, err := r.getSourceDetails(vm, sourceSecret)
Expand Down Expand Up @@ -233,42 +236,43 @@ func (r *Builder) PodEnvironment(vmRef ref.Ref, sourceSecret *core.Secret) (env
return
}

func (r *Builder) mapMacStaticIps(vm *model.VM) string {
func (r *Builder) mapMacStaticIps(vm *model.VM) (ipMap string, err error) {
if !isWindows(vm) {
return ""
}
if vm.GuestNetworks == nil || len(vm.GuestNetworks) < 1 {
return ""
return "", nil
}
configurations := []string{}
sort.Slice(vm.GuestNetworks, func(i, j int) bool { return vm.GuestNetworks[i].DeviceId < vm.GuestNetworks[j].DeviceId })
firstId := vm.GuestNetworks[0].DeviceId
gatewaySet := false
for _, guestNetwork := range vm.GuestNetworks {
if guestNetwork.Origin == string(types.NetIpConfigInfoIpAddressOriginManual) {
isIpv4 := false
if net.IP.To4(net.ParseIP(guestNetwork.IP)) != nil {
isIpv4 = true
}
devCounter := guestNetwork.DeviceId - firstId
for _, ipStack := range vm.GuestIpStacks {
devId, err := strconv.Atoi(ipStack.DeviceId)
if err != nil {
return ""
}
if devCounter != int32(devId) {
// not the same device
continue
}
if net.IP.To4(net.ParseIP(ipStack.Gateway)) != nil && !isIpv4 || net.IP.To4(net.ParseIP(ipStack.Gateway)) == nil && isIpv4 {
// not the right IPv4 / IPv6 correlation
continue
gateway := ""
if !gatewaySet {
isIpv4 := net.IP.To4(net.ParseIP(guestNetwork.IP)) != nil
for _, ipStack := range vm.GuestIpStacks {
gwIpv4 := net.IP.To4(net.ParseIP(ipStack.Gateway)) != nil
if gwIpv4 && !isIpv4 || !gwIpv4 && isIpv4 {
// not the right IPv4 / IPv6 correlation
continue
}
network, err := netip.ParsePrefix(fmt.Sprintf("%s/%d", guestNetwork.IP, guestNetwork.PrefixLength))
if err != nil {
return "", err
}
gw, err := netip.ParseAddr(ipStack.Gateway)
if err != nil {
return "", err
}
if network.Contains(gw) {
// checks if the gateway in the right network subnet. we set gateway only once as it is suppose to be system wide.
gateway = ipStack.Gateway
gatewaySet = true
}
}
dnsString := strings.Join(guestNetwork.DNS, ",")
configurations = append(configurations, fmt.Sprintf("%s:ip:%s,%s,%d,%s", guestNetwork.MAC, guestNetwork.IP, ipStack.Gateway, guestNetwork.PrefixLength, dnsString))
}
dnsString := strings.Join(guestNetwork.DNS, ",")
configurations = append(configurations, fmt.Sprintf("%s:ip:%s,%s,%d,%s", guestNetwork.MAC, guestNetwork.IP, gateway, guestNetwork.PrefixLength, dnsString))
}
}
return strings.Join(configurations, "_")
return strings.Join(configurations, "_"), nil
}

func isWindows(vm *model.VM) bool {
Expand Down
32 changes: 9 additions & 23 deletions pkg/controller/plan/adapter/vsphere/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ var _ = Describe("vSphere builder", func() {
Origin: ManualOrigin,
PrefixLength: 16,
DNS: []string{"8.8.8.8"},
DeviceId: 4000,
}},
GuestIpStacks: []vsphere.GuestIpStack{
{
DeviceId: "0",
Gateway: "172.29.3.1",
Gateway: "172.29.3.1",
}},
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8"),
Entry("multiple static ips", &model.VM{
Expand All @@ -50,28 +48,24 @@ var _ = Describe("vSphere builder", func() {
Origin: ManualOrigin,
PrefixLength: 16,
DNS: []string{"8.8.8.8"},
DeviceId: 4001,
},
{
MAC: "00:50:56:83:25:47",
IP: "fe80::5da:b7a5:e0a2:a097",
Origin: ManualOrigin,
PrefixLength: 64,
DNS: []string{"fec0:0:0:ffff::1", "fec0:0:0:ffff::2", "fec0:0:0:ffff::3"},
DeviceId: 4001,
},
},
GuestIpStacks: []vsphere.GuestIpStack{
{
DeviceId: "0",
Gateway: "172.29.3.1",
Gateway: "172.29.3.1",
},
{
DeviceId: "0",
Gateway: "fe80::5da:b7a5:e0a2:a095",
Gateway: "fe80::5da:b7a5:e0a2:a095",
},
},
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097,fe80::5da:b7a5:e0a2:a095,64,fec0:0:0:ffff::1,fec0:0:0:ffff::2,fec0:0:0:ffff::3"),
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097,,64,fec0:0:0:ffff::1,fec0:0:0:ffff::2,fec0:0:0:ffff::3"),
Entry("non-static ip", &model.VM{GuestID: "windows9Guest", GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: string(types.NetIpConfigInfoIpAddressOriginDhcp)}}}, ""),
Entry("non windows vm", &model.VM{GuestID: "other", GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, ""),
Entry("no OS vm", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, ""),
Expand All @@ -84,52 +78,44 @@ var _ = Describe("vSphere builder", func() {
Origin: ManualOrigin,
PrefixLength: 16,
DNS: []string{"8.8.8.8"},
DeviceId: 4001,
},
{
MAC: "00:50:56:83:25:47",
IP: "fe80::5da:b7a5:e0a2:a097",
Origin: ManualOrigin,
PrefixLength: 64,
DNS: []string{"fec0:0:0:ffff::1", "fec0:0:0:ffff::2", "fec0:0:0:ffff::3"},
DeviceId: 4001,
},
{
MAC: "00:50:56:83:25:48",
IP: "172.29.3.192",
Origin: ManualOrigin,
PrefixLength: 24,
DNS: []string{"4.4.4.4"},
DeviceId: 4002,
},
{
MAC: "00:50:56:83:25:48",
IP: "fe80::5da:b7a5:e0a2:a090",
Origin: ManualOrigin,
PrefixLength: 32,
DNS: []string{"fec0:0:0:ffff::4", "fec0:0:0:ffff::5", "fec0:0:0:ffff::6"},
DeviceId: 4002,
},
},
GuestIpStacks: []vsphere.GuestIpStack{
{
DeviceId: "1",
Gateway: "172.29.3.2",
Gateway: "172.29.3.2",
},
{
DeviceId: "1",
Gateway: "fe80::5da:b7a5:e0a2:a098",
Gateway: "fe80::5da:b7a5:e0a2:a098",
},
{
DeviceId: "0",
Gateway: "172.29.3.1",
Gateway: "172.29.3.1",
},
{
DeviceId: "0",
Gateway: "fe80::5da:b7a5:e0a2:a095",
Gateway: "fe80::5da:b7a5:e0a2:a095",
},
},
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097,fe80::5da:b7a5:e0a2:a095,64,fec0:0:0:ffff::1,fec0:0:0:ffff::2,fec0:0:0:ffff::3_00:50:56:83:25:48:ip:172.29.3.192,172.29.3.2,24,4.4.4.4_00:50:56:83:25:48:ip:fe80::5da:b7a5:e0a2:a090,fe80::5da:b7a5:e0a2:a098,32,fec0:0:0:ffff::4,fec0:0:0:ffff::5,fec0:0:0:ffff::6"),
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097,,64,fec0:0:0:ffff::1,fec0:0:0:ffff::2,fec0:0:0:ffff::3_00:50:56:83:25:48:ip:172.29.3.192,,24,4.4.4.4_00:50:56:83:25:48:ip:fe80::5da:b7a5:e0a2:a090,,32,fec0:0:0:ffff::4,fec0:0:0:ffff::5,fec0:0:0:ffff::6"),
)
})

Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/provider/container/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) {
IP: ip.IpAddress,
Origin: ip.Origin,
PrefixLength: ip.PrefixLength,
DeviceId: info.DeviceConfigId,
DNS: dnsList,
})
}
Expand All @@ -648,9 +647,8 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) {
for _, route := range routes {
if len(route.Gateway.IpAddress) > 0 {
guestIpStackList = append(guestIpStackList, model.GuestIpStack{
DeviceId: route.Gateway.Device,
Gateway: route.Gateway.IpAddress,
DNS: ipa.DnsConfig.IpAddress,
Gateway: route.Gateway.IpAddress,
DNS: ipa.DnsConfig.IpAddress,
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/provider/model/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,10 @@ type GuestNetwork struct {
Origin string `json:"origin"`
PrefixLength int32 `json:"prefix"`
DNS []string `json:"dns"`
DeviceId int32 `json:"id"`
}

// Guest ipStack
type GuestIpStack struct {
DeviceId string `json:"id"`
Gateway string `json:"gateway"`
DNS []string `json:"dns"`
Gateway string `json:"gateway"`
DNS []string `json:"dns"`
}

0 comments on commit 41efdd8

Please sign in to comment.