From 6e306b0e8640d28024178b711eaa17d9ed7e40d0 Mon Sep 17 00:00:00 2001 From: Arik Hadas Date: Wed, 3 Apr 2024 13:25:36 +0300 Subject: [PATCH] Sanitize datastore identifiers from ESXi providers when we get datastores from the ESXi SDK, their identifier may be in the form of '10.11.12.13:/vol/virtv2v/function' which leads to invalid endpoints in the inventory so we sanitize such identifiers Signed-off-by: Arik Hadas --- .../provider/container/vsphere/BUILD.bazel | 1 + .../provider/container/vsphere/collector.go | 12 ++++++++++-- .../provider/container/vsphere/model.go | 9 ++++++--- .../provider/container/vsphere/utils.go | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 pkg/controller/provider/container/vsphere/utils.go diff --git a/pkg/controller/provider/container/vsphere/BUILD.bazel b/pkg/controller/provider/container/vsphere/BUILD.bazel index 98552dea5..e6de53c8b 100644 --- a/pkg/controller/provider/container/vsphere/BUILD.bazel +++ b/pkg/controller/provider/container/vsphere/BUILD.bazel @@ -6,6 +6,7 @@ go_library( "collector.go", "doc.go", "model.go", + "utils.go", "watch.go", ], importpath = "github.com/konveyor/forklift-controller/pkg/controller/provider/container/vsphere", diff --git a/pkg/controller/provider/container/vsphere/collector.go b/pkg/controller/provider/container/vsphere/collector.go index f7077d7c7..899e559ea 100644 --- a/pkg/controller/provider/container/vsphere/collector.go +++ b/pkg/controller/provider/container/vsphere/collector.go @@ -838,10 +838,17 @@ func (r *Collector) selectAdapter(u types.ObjectUpdate) (Adapter, bool) { }, } case Datastore: + // when we get datastores from the ESXi SDK, their identifier may be in the + // form of '10.11.12.13:/vol/virtv2v/function' which leads to invalid + // endpoints in the inventory so we sanitize such identifiers + datastoreId, changed := sanitize(u.Obj.Value) + if changed { + r.log.Info("sanitized datastore ID", "reported", u.Obj.Value, "sanitized", datastoreId) + } adapter = &DatastoreAdapter{ model: model.Datastore{ Base: model.Base{ - ID: u.Obj.Value, + ID: datastoreId, }, }, } @@ -932,9 +939,10 @@ func (r Collector) applyLeave(tx *libmodel.Tx, u types.ObjectUpdate) error { }, } case Datastore: + datastoreId, _ := sanitize(u.Obj.Value) deleted = &model.Datastore{ Base: model.Base{ - ID: u.Obj.Value, + ID: datastoreId, }, } case VirtualMachine: diff --git a/pkg/controller/provider/container/vsphere/model.go b/pkg/controller/provider/container/vsphere/model.go index bcead2414..45ed63eac 100644 --- a/pkg/controller/provider/container/vsphere/model.go +++ b/pkg/controller/provider/container/vsphere/model.go @@ -688,18 +688,20 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { switch disk.Backing.(type) { case *types.VirtualDiskFlatVer1BackingInfo: backing := disk.Backing.(*types.VirtualDiskFlatVer1BackingInfo) + datastoreId, _ := sanitize(backing.Datastore.Value) md := model.Disk{ Key: disk.Key, File: backing.FileName, Capacity: disk.CapacityInBytes, Datastore: model.Ref{ Kind: model.DsKind, - ID: backing.Datastore.Value, + ID: datastoreId, }, } disks = append(disks, md) case *types.VirtualDiskFlatVer2BackingInfo: backing := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo) + datastoreId, _ := sanitize(backing.Datastore.Value) md := model.Disk{ Key: disk.Key, File: backing.FileName, @@ -707,12 +709,13 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { Shared: backing.Sharing != "sharingNone", Datastore: model.Ref{ Kind: model.DsKind, - ID: backing.Datastore.Value, + ID: datastoreId, }, } disks = append(disks, md) case *types.VirtualDiskRawDiskMappingVer1BackingInfo: backing := disk.Backing.(*types.VirtualDiskRawDiskMappingVer1BackingInfo) + datastoreId, _ := sanitize(backing.Datastore.Value) md := model.Disk{ Key: disk.Key, File: backing.FileName, @@ -720,7 +723,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { Shared: backing.Sharing != "sharingNone", Datastore: model.Ref{ Kind: model.DsKind, - ID: backing.Datastore.Value, + ID: datastoreId, }, RDM: true, } diff --git a/pkg/controller/provider/container/vsphere/utils.go b/pkg/controller/provider/container/vsphere/utils.go new file mode 100644 index 000000000..4e1bf53e6 --- /dev/null +++ b/pkg/controller/provider/container/vsphere/utils.go @@ -0,0 +1,17 @@ +package vsphere + +import ( + "crypto/md5" + "encoding/hex" + "net/url" +) + +func sanitize(datastoreId string) (sanitizedId string, changed bool) { + sanitizedId = url.PathEscape(datastoreId) + if sanitizedId != datastoreId { + sum := md5.Sum([]byte(datastoreId)) + sanitizedId = hex.EncodeToString(sum[:]) + changed = true + } + return +}