From eaecc5391a0c95bde0ce763f56b013c59c229597 Mon Sep 17 00:00:00 2001 From: Arik Hadas Date: Mon, 10 Jun 2024 11:45:09 +0300 Subject: [PATCH] vsphere: load hosts per source provider When looking for the configuration of an ESXi host that a certain VM resides on, we query all the hosts in the namespace and then looking for the host by its MOR (Managed Object Reference). This almost always works but it could be wrong when there are several providers within the same namespace that refer to the same host. This is more likely to happen during tests where we define two providers with different propreties for the same vSphere environment. Therefore, adding another check that ensures the host refers to the provider which is the source provider on the plan of the migrated VM, otherwise skip that host. Signed-off-by: Arik Hadas --- pkg/controller/plan/adapter/vsphere/BUILD.bazel | 1 + pkg/controller/plan/adapter/vsphere/builder.go | 5 +++++ pkg/lib/ref/ref.go | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/controller/plan/adapter/vsphere/BUILD.bazel b/pkg/controller/plan/adapter/vsphere/BUILD.bazel index 74a5ba7e7..66ecd8c86 100644 --- a/pkg/controller/plan/adapter/vsphere/BUILD.bazel +++ b/pkg/controller/plan/adapter/vsphere/BUILD.bazel @@ -27,6 +27,7 @@ go_library( "//pkg/lib/condition", "//pkg/lib/error", "//pkg/lib/itinerary", + "//pkg/lib/ref", "//pkg/settings", "//vendor/github.com/vmware/govmomi", "//vendor/github.com/vmware/govmomi/find", diff --git a/pkg/controller/plan/adapter/vsphere/builder.go b/pkg/controller/plan/adapter/vsphere/builder.go index cad607afe..3e58bcac2 100644 --- a/pkg/controller/plan/adapter/vsphere/builder.go +++ b/pkg/controller/plan/adapter/vsphere/builder.go @@ -24,6 +24,7 @@ import ( libcnd "github.com/konveyor/forklift-controller/pkg/lib/condition" liberr "github.com/konveyor/forklift-controller/pkg/lib/error" libitr "github.com/konveyor/forklift-controller/pkg/lib/itinerary" + libref "github.com/konveyor/forklift-controller/pkg/lib/ref" "github.com/vmware/govmomi/vim25" "github.com/vmware/govmomi/vim25/types" core "k8s.io/api/core/v1" @@ -787,6 +788,10 @@ func (r *Builder) loadHosts() (err error) { for i := range list.Items { host := &list.Items[i] ref := host.Spec.Ref + if !libref.Equals(&host.Spec.Provider, &r.Plan.Spec.Provider.Source) { + continue + } + if !host.Status.HasCondition(libcnd.Ready) { continue } diff --git a/pkg/lib/ref/ref.go b/pkg/lib/ref/ref.go index b3f4185ba..395afe025 100644 --- a/pkg/lib/ref/ref.go +++ b/pkg/lib/ref/ref.go @@ -33,10 +33,19 @@ func RefSet(ref *v1.ObjectReference) bool { // Equals comparison. // May be used with `nil` pointers. -func Equals(refA, refB *v1.ObjectReference) bool { +func DeepEquals(refA, refB *v1.ObjectReference) bool { if refA == nil || refB == nil { return false } return reflect.DeepEqual(refA, refB) } + +// Determind if both refs have the same name and namespace +func Equals(refA, refB *v1.ObjectReference) bool { + if refA == nil || refB == nil { + return refA == refB + } + + return refA.Name == refB.Name && refA.Namespace == refB.Namespace +}