Skip to content

Commit 760ce58

Browse files
committed
Set gateway only once
Signed-off-by: Liran Rotenberg <lrotenbe@redhat.com>
1 parent 061f5b5 commit 760ce58

File tree

4 files changed

+46
-60
lines changed

4 files changed

+46
-60
lines changed

pkg/controller/plan/adapter/vsphere/builder.go

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"errors"
66
"fmt"
77
"net"
8+
"net/netip"
89
liburl "net/url"
910
"path"
1011
"regexp"
1112
"sort"
12-
"strconv"
1313
"strings"
1414

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

196196
macsToIps := ""
197197
if r.Plan.Spec.PreserveStaticIPs {
198-
macsToIps = r.mapMacStaticIps(vm)
198+
macsToIps, err = r.mapMacStaticIps(vm)
199+
if err != nil {
200+
return
201+
}
199202
}
200203

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

236-
func (r *Builder) mapMacStaticIps(vm *model.VM) string {
239+
func (r *Builder) mapMacStaticIps(vm *model.VM) (ipMap string, err error) {
237240
if !isWindows(vm) {
238-
return ""
239-
}
240-
if vm.GuestNetworks == nil || len(vm.GuestNetworks) < 1 {
241-
return ""
241+
return "", nil
242242
}
243243
configurations := []string{}
244-
sort.Slice(vm.GuestNetworks, func(i, j int) bool { return vm.GuestNetworks[i].DeviceId < vm.GuestNetworks[j].DeviceId })
245-
firstId := vm.GuestNetworks[0].DeviceId
244+
gatewaySet := false
246245
for _, guestNetwork := range vm.GuestNetworks {
247246
if guestNetwork.Origin == string(types.NetIpConfigInfoIpAddressOriginManual) {
248-
isIpv4 := false
249-
if net.IP.To4(net.ParseIP(guestNetwork.IP)) != nil {
250-
isIpv4 = true
251-
}
252-
devCounter := guestNetwork.DeviceId - firstId
253-
for _, ipStack := range vm.GuestIpStacks {
254-
devId, err := strconv.Atoi(ipStack.DeviceId)
255-
if err != nil {
256-
return ""
257-
}
258-
if devCounter != int32(devId) {
259-
// not the same device
260-
continue
261-
}
262-
if net.IP.To4(net.ParseIP(ipStack.Gateway)) != nil && !isIpv4 || net.IP.To4(net.ParseIP(ipStack.Gateway)) == nil && isIpv4 {
263-
// not the right IPv4 / IPv6 correlation
264-
continue
247+
gateway := ""
248+
if !gatewaySet {
249+
isIpv4 := net.IP.To4(net.ParseIP(guestNetwork.IP)) != nil
250+
for _, ipStack := range vm.GuestIpStacks {
251+
gwIpv4 := net.IP.To4(net.ParseIP(ipStack.Gateway)) != nil
252+
if gwIpv4 && !isIpv4 || !gwIpv4 && isIpv4 {
253+
// not the right IPv4 / IPv6 correlation
254+
continue
255+
}
256+
network, err := netip.ParsePrefix(fmt.Sprintf("%s/%d", guestNetwork.IP, guestNetwork.PrefixLength))
257+
if err != nil {
258+
return "", err
259+
}
260+
gw, err := netip.ParseAddr(ipStack.Gateway)
261+
if err != nil {
262+
return "", err
263+
}
264+
if network.Contains(gw) {
265+
// checks if the gateway in the right network subnet. we set gateway only once as it is suppose to be system wide.
266+
gateway = ipStack.Gateway
267+
gatewaySet = true
268+
}
265269
}
266-
dnsString := strings.Join(guestNetwork.DNS, ",")
267-
configurations = append(configurations, fmt.Sprintf("%s:ip:%s,%s,%d,%s", guestNetwork.MAC, guestNetwork.IP, ipStack.Gateway, guestNetwork.PrefixLength, dnsString))
268270
}
271+
dnsString := strings.Join(guestNetwork.DNS, ",")
272+
configurations = append(configurations, fmt.Sprintf("%s:ip:%s,%s,%d,%s", guestNetwork.MAC, guestNetwork.IP, gateway, guestNetwork.PrefixLength, dnsString))
269273
}
270274
}
271-
return strings.Join(configurations, "_")
275+
return strings.Join(configurations, "_"), nil
272276
}
273277

274278
func isWindows(vm *model.VM) bool {

pkg/controller/plan/adapter/vsphere/builder_test.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ var _ = Describe("vSphere builder", func() {
3333
Origin: ManualOrigin,
3434
PrefixLength: 16,
3535
DNS: []string{"8.8.8.8"},
36-
DeviceId: 4000,
3736
}},
3837
GuestIpStacks: []vsphere.GuestIpStack{
3938
{
40-
DeviceId: "0",
41-
Gateway: "172.29.3.1",
39+
Gateway: "172.29.3.1",
4240
}},
4341
}, "00:50:56:83:25:47:ip:172.29.3.193,172.29.3.1,16,8.8.8.8"),
4442
Entry("multiple static ips", &model.VM{
@@ -50,28 +48,24 @@ var _ = Describe("vSphere builder", func() {
5048
Origin: ManualOrigin,
5149
PrefixLength: 16,
5250
DNS: []string{"8.8.8.8"},
53-
DeviceId: 4001,
5451
},
5552
{
5653
MAC: "00:50:56:83:25:47",
5754
IP: "fe80::5da:b7a5:e0a2:a097",
5855
Origin: ManualOrigin,
5956
PrefixLength: 64,
6057
DNS: []string{"fec0:0:0:ffff::1", "fec0:0:0:ffff::2", "fec0:0:0:ffff::3"},
61-
DeviceId: 4001,
6258
},
6359
},
6460
GuestIpStacks: []vsphere.GuestIpStack{
6561
{
66-
DeviceId: "0",
67-
Gateway: "172.29.3.1",
62+
Gateway: "172.29.3.1",
6863
},
6964
{
70-
DeviceId: "0",
71-
Gateway: "fe80::5da:b7a5:e0a2:a095",
65+
Gateway: "fe80::5da:b7a5:e0a2:a095",
7266
},
7367
},
74-
}, "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"),
68+
}, "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"),
7569
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)}}}, ""),
7670
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}}}, ""),
7771
Entry("no OS vm", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, ""),
@@ -84,52 +78,44 @@ var _ = Describe("vSphere builder", func() {
8478
Origin: ManualOrigin,
8579
PrefixLength: 16,
8680
DNS: []string{"8.8.8.8"},
87-
DeviceId: 4001,
8881
},
8982
{
9083
MAC: "00:50:56:83:25:47",
9184
IP: "fe80::5da:b7a5:e0a2:a097",
9285
Origin: ManualOrigin,
9386
PrefixLength: 64,
9487
DNS: []string{"fec0:0:0:ffff::1", "fec0:0:0:ffff::2", "fec0:0:0:ffff::3"},
95-
DeviceId: 4001,
9688
},
9789
{
9890
MAC: "00:50:56:83:25:48",
9991
IP: "172.29.3.192",
10092
Origin: ManualOrigin,
10193
PrefixLength: 24,
10294
DNS: []string{"4.4.4.4"},
103-
DeviceId: 4002,
10495
},
10596
{
10697
MAC: "00:50:56:83:25:48",
10798
IP: "fe80::5da:b7a5:e0a2:a090",
10899
Origin: ManualOrigin,
109100
PrefixLength: 32,
110101
DNS: []string{"fec0:0:0:ffff::4", "fec0:0:0:ffff::5", "fec0:0:0:ffff::6"},
111-
DeviceId: 4002,
112102
},
113103
},
114104
GuestIpStacks: []vsphere.GuestIpStack{
115105
{
116-
DeviceId: "1",
117-
Gateway: "172.29.3.2",
106+
Gateway: "172.29.3.2",
118107
},
119108
{
120-
DeviceId: "1",
121-
Gateway: "fe80::5da:b7a5:e0a2:a098",
109+
Gateway: "fe80::5da:b7a5:e0a2:a098",
122110
},
123111
{
124-
DeviceId: "0",
125-
Gateway: "172.29.3.1",
112+
Gateway: "172.29.3.1",
126113
},
127114
{
128-
DeviceId: "0",
129-
Gateway: "fe80::5da:b7a5:e0a2:a095",
115+
Gateway: "fe80::5da:b7a5:e0a2:a095",
130116
},
131117
},
132-
}, "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"),
118+
}, "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"),
133119
)
134120
})
135121

pkg/controller/provider/container/vsphere/model.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) {
630630
IP: ip.IpAddress,
631631
Origin: ip.Origin,
632632
PrefixLength: ip.PrefixLength,
633-
DeviceId: info.DeviceConfigId,
634633
DNS: dnsList,
635634
})
636635
}
@@ -648,9 +647,8 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) {
648647
for _, route := range routes {
649648
if len(route.Gateway.IpAddress) > 0 {
650649
guestIpStackList = append(guestIpStackList, model.GuestIpStack{
651-
DeviceId: route.Gateway.Device,
652-
Gateway: route.Gateway.IpAddress,
653-
DNS: ipa.DnsConfig.IpAddress,
650+
Gateway: route.Gateway.IpAddress,
651+
DNS: ipa.DnsConfig.IpAddress,
654652
})
655653
}
656654
}

pkg/controller/provider/model/vsphere/model.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,10 @@ type GuestNetwork struct {
300300
Origin string `json:"origin"`
301301
PrefixLength int32 `json:"prefix"`
302302
DNS []string `json:"dns"`
303-
DeviceId int32 `json:"id"`
304303
}
305304

306305
// Guest ipStack
307306
type GuestIpStack struct {
308-
DeviceId string `json:"id"`
309-
Gateway string `json:"gateway"`
310-
DNS []string `json:"dns"`
307+
Gateway string `json:"gateway"`
308+
DNS []string `json:"dns"`
311309
}

0 commit comments

Comments
 (0)