From 3720d88bafe92263bf0a58b1038d9dc503b549f1 Mon Sep 17 00:00:00 2001 From: Bella Khizgiyaev Date: Mon, 20 Nov 2023 19:24:47 +0200 Subject: [PATCH] OVA: Add support for removing deleted vms from the inventory Currently, when a VM is deleted from the NFS share it is not removed from the inventory. With this change it will be removed. Signed-off-by: Bella Khizgiyaev --- .../provider/container/ova/collector.go | 12 +- .../provider/container/ova/model.go | 125 ++++++++++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/pkg/controller/provider/container/ova/collector.go b/pkg/controller/provider/container/ova/collector.go index 6e5e64fe2..9e69787f6 100644 --- a/pkg/controller/provider/container/ova/collector.go +++ b/pkg/controller/provider/container/ova/collector.go @@ -20,7 +20,7 @@ const ( // Retry interval. RetryInterval = 5 * time.Second // Refresh interval. - RefreshInterval = 5 * time.Minute + RefreshInterval = 1 * time.Minute ) // Phases @@ -303,12 +303,20 @@ func (r *Collector) endWatch() { // DB transaction while using the provider API which // can block or be slow. func (r *Collector) refresh(ctx *Context) (err error) { - var updates []Updater + var deletions, updates []Updater mark := time.Now() for _, adapter := range adapterList { if ctx.canceled() { return } + deletions, err = adapter.DeleteUnexisting(ctx) + if err != nil { + return + } + err = r.apply(deletions) + if err != nil { + return + } updates, err = adapter.GetUpdates(ctx) if err != nil { return diff --git a/pkg/controller/provider/container/ova/model.go b/pkg/controller/provider/container/ova/model.go index 438b865a5..651733334 100644 --- a/pkg/controller/provider/container/ova/model.go +++ b/pkg/controller/provider/container/ova/model.go @@ -62,6 +62,8 @@ type Adapter interface { List(ctx *Context, provider *api.Provider) (itr fb.Iterator, err error) // Get object updates GetUpdates(ctx *Context) (updater []Updater, err error) + // Clean unexisting objects within the database + DeleteUnexisting(ctx *Context) (deletions []Updater, err error) } // Base adapter. @@ -123,6 +125,45 @@ func (r *NetworkAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) return } +func (r *NetworkAdapter) DeleteUnexisting(ctx *Context) (deletions []Updater, err error) { + networkList := []model.Network{} + err = ctx.db.List(&networkList, libmodel.FilterOptions{}) + if err != nil { + if errors.Is(err, libmodel.NotFound) { + err = nil + } + return + } + networkListServer := []Network{} + err = ctx.client.list("networks", &networkListServer) + if err != nil { + return + } + + elementMap := make(map[string]bool) + for _, network := range networkListServer { + elementMap[network.ID] = true + } + + networksToDelete := []string{} + for _, network := range networkList { + if _, found := elementMap[network.ID]; !found { + networksToDelete = append(networksToDelete, network.ID) + } + } + for _, networkId := range networksToDelete { + currentID := networkId + updater := func(tx *libmodel.Tx) (err error) { + m := &model.Network{ + Base: model.Base{ID: currentID}, + } + return tx.Delete(m) + } + deletions = append(deletions, updater) + } + return +} + // VM adapter. type VMAdapter struct { BaseAdapter @@ -177,6 +218,45 @@ func (r *VMAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) { return } +func (r *VMAdapter) DeleteUnexisting(ctx *Context) (deletions []Updater, err error) { + vmList := []model.VM{} + err = ctx.db.List(&vmList, libmodel.FilterOptions{}) + if err != nil { + if errors.Is(err, libmodel.NotFound) { + err = nil + } + return + } + vmListServer := []VM{} + err = ctx.client.list("vms", &vmListServer) + if err != nil { + return + } + + elementMap := make(map[string]bool) + for _, vm := range vmListServer { + elementMap[vm.UUID] = true + } + + vmsToDelete := []string{} + for _, vm := range vmList { + if _, found := elementMap[vm.ID]; !found { + vmsToDelete = append(vmsToDelete, vm.ID) + } + } + for _, vmId := range vmsToDelete { + currentID := vmId + updater := func(tx *libmodel.Tx) (err error) { + m := &model.VM{ + Base: model.Base{ID: currentID}, + } + return tx.Delete(m) + } + deletions = append(deletions, updater) + } + return +} + // Disk adapter. type DiskAdapter struct { BaseAdapter @@ -232,6 +312,45 @@ func (r *DiskAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) { return } +func (r *DiskAdapter) DeleteUnexisting(ctx *Context) (deletions []Updater, err error) { + diskList := []model.Disk{} + err = ctx.db.List(&diskList, libmodel.FilterOptions{}) + if err != nil { + if errors.Is(err, libmodel.NotFound) { + err = nil + } + return + } + diskListServer := []Disk{} + err = ctx.client.list("disks", &diskListServer) + if err != nil { + return + } + + elementMap := make(map[string]bool) + for _, disk := range diskListServer { + elementMap[disk.ID] = true + } + + disksToDelete := []string{} + for _, disk := range diskList { + if _, found := elementMap[disk.ID]; !found { + disksToDelete = append(disksToDelete, disk.ID) + } + } + for _, diskId := range disksToDelete { + currentID := diskId + updater := func(tx *libmodel.Tx) (err error) { + m := &model.Disk{ + Base: model.Base{ID: currentID}, + } + return tx.Delete(m) + } + deletions = append(deletions, updater) + } + return +} + type StorageAdapter struct { BaseAdapter } @@ -261,3 +380,9 @@ func (r *StorageAdapter) List(ctx *Context, provider *api.Provider) (itr fb.Iter return } + +func (r *StorageAdapter) DeleteUnexisting(ctx *Context) (deletions []Updater, err error) { + // Each provider have only one storage hence it can't be changed, + // Will be removed only if the provider deleted. + return +}