Skip to content

Commit

Permalink
don't get static ips for non-windows
Browse files Browse the repository at this point in the history
Signed-off-by: Liran Rotenberg <[email protected]>
  • Loading branch information
liranr23 authored and ahadas committed May 16, 2024
1 parent 7302047 commit a674470
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
10 changes: 9 additions & 1 deletion pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
DefaultWindows = "win10"
DefaultLinux = "rhel8.1"
Unknown = "unknown"
WindowsPrefix = "win"
)

// Annotations
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/plan/adapter/vsphere/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}}}, ""),
)
})

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/plan/adapter/vsphere/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/plan/adapter/vsphere/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
)
})
})
Expand Down

0 comments on commit a674470

Please sign in to comment.