From a6744706c5b0eac4746c27b78495a7caff84c4b0 Mon Sep 17 00:00:00 2001 From: Liran Rotenberg Date: Wed, 8 May 2024 18:17:15 +0300 Subject: [PATCH] don't get static ips for non-windows Signed-off-by: Liran Rotenberg --- pkg/controller/plan/adapter/vsphere/builder.go | 10 +++++++++- pkg/controller/plan/adapter/vsphere/builder_test.go | 10 ++++++---- pkg/controller/plan/adapter/vsphere/validator.go | 4 ++++ pkg/controller/plan/adapter/vsphere/validator_test.go | 7 ++++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/controller/plan/adapter/vsphere/builder.go b/pkg/controller/plan/adapter/vsphere/builder.go index 548591dcf..e71d5cc94 100644 --- a/pkg/controller/plan/adapter/vsphere/builder.go +++ b/pkg/controller/plan/adapter/vsphere/builder.go @@ -66,6 +66,7 @@ const ( DefaultWindows = "win10" DefaultLinux = "rhel8.1" Unknown = "unknown" + WindowsPrefix = "win" ) // Annotations @@ -224,6 +225,9 @@ func (r *Builder) PodEnvironment(vmRef ref.Ref, sourceSecret *core.Secret) (env } func (r *Builder) mapMacStaticIps(vm *model.VM) string { + if !isWindows(vm) { + return "" + } configurations := []string{} for _, guestNetwork := range vm.GuestNetworks { if guestNetwork.Origin == string(types.NetIpConfigInfoIpAddressOriginManual) { @@ -233,6 +237,10 @@ func (r *Builder) mapMacStaticIps(vm *model.VM) string { return strings.Join(configurations, "_") } +func isWindows(vm *model.VM) bool { + return strings.Contains(vm.GuestID, WindowsPrefix) || strings.Contains(vm.GuestName, WindowsPrefix) +} + func (r *Builder) getSourceDetails(vm *model.VM, sourceSecret *core.Secret) (libvirtURL liburl.URL, fingerprint string, err error) { host, err := r.host(vm.Host) if err != nil { @@ -721,7 +729,7 @@ func (r *Builder) TemplateLabels(vmRef ref.Ref) (labels map[string]string, err e os = osMap[vm.GuestID] } else if strings.Contains(vm.GuestName, "linux") || strings.Contains(vm.GuestName, "rhel") { os = DefaultLinux - } else if strings.Contains(vm.GuestName, "win") { + } else if strings.Contains(vm.GuestName, WindowsPrefix) { os = DefaultWindows } else { os = Unknown diff --git a/pkg/controller/plan/adapter/vsphere/builder_test.go b/pkg/controller/plan/adapter/vsphere/builder_test.go index 690ce10fd..571861b0a 100644 --- a/pkg/controller/plan/adapter/vsphere/builder_test.go +++ b/pkg/controller/plan/adapter/vsphere/builder_test.go @@ -23,9 +23,9 @@ var _ = Describe("vSphere builder", func() { DescribeTable("should", func(vm *model.VM, outputMap string) { Expect(builder.mapMacStaticIps(vm)).Should(Equal(outputMap)) }, - Entry("no static ips", &model.VM{}, ""), - Entry("single static ip", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, "00:50:56:83:25:47:ip:172.29.3.193"), - Entry("multiple static ips", &model.VM{GuestNetworks: []vsphere.GuestNetwork{ + Entry("no static ips", &model.VM{GuestID: "windows9Guest"}, ""), + Entry("single static ip", &model.VM{GuestID: "windows9Guest", GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, "00:50:56:83:25:47:ip:172.29.3.193"), + Entry("multiple static ips", &model.VM{GuestID: "windows9Guest", GuestNetworks: []vsphere.GuestNetwork{ { MAC: "00:50:56:83:25:47", IP: "172.29.3.193", @@ -38,7 +38,9 @@ var _ = Describe("vSphere builder", func() { }, }, }, "00:50:56:83:25:47:ip:172.29.3.193_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097"), - Entry("non-static ip", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: string(types.NetIpConfigInfoIpAddressOriginDhcp)}}}, ""), + 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}}}, ""), ) }) diff --git a/pkg/controller/plan/adapter/vsphere/validator.go b/pkg/controller/plan/adapter/vsphere/validator.go index fefd40202..f42911338 100644 --- a/pkg/controller/plan/adapter/vsphere/validator.go +++ b/pkg/controller/plan/adapter/vsphere/validator.go @@ -139,6 +139,10 @@ func (r *Validator) StaticIPs(vmRef ref.Ref) (ok bool, err error) { err = liberr.Wrap(err, "vm", vmRef) return } + if !isWindows(&vm.VM) { + return true, nil + } + for _, nic := range vm.NICs { found := false for _, guestNetwork := range vm.GuestNetworks { diff --git a/pkg/controller/plan/adapter/vsphere/validator_test.go b/pkg/controller/plan/adapter/vsphere/validator_test.go index 6f082cd43..f8d6989d1 100644 --- a/pkg/controller/plan/adapter/vsphere/validator_test.go +++ b/pkg/controller/plan/adapter/vsphere/validator_test.go @@ -27,11 +27,15 @@ func (m *mockInventory) Find(resource interface{}, ref ref.Ref) error { }, GuestNetworks: []vsphere.GuestNetwork{ {MAC: "mac1"}, - }}, + }, + GuestID: "windows7Guest"}, } if ref.Name == "full_guest_network" { res.VM.GuestNetworks = append(res.VM.GuestNetworks, vsphere.GuestNetwork{MAC: "mac2"}) } + if ref.Name == "not_windows_guest" { + res.VM.GuestID = "rhel8_64Guest" + } } return nil } @@ -97,6 +101,7 @@ var _ = Describe("vsphere validation tests", func() { Entry("when the vm doesn't have static ips, and the plan set without static ip", "test", false, false), Entry("when the vm have static ips, and the plan set with static ip", "full_guest_network", true, false), Entry("when the vm have static ips, and the plan set without static ip", "test", false, false), + Entry("when the vm doesn't have static ips, and the plan set without static ip, vm is non-windows", "not_windows_guest", true, false), ) }) })