From 7e82e606980b110b9011866cd8146ca891193c90 Mon Sep 17 00:00:00 2001 From: Bella Khizgiyaev Date: Mon, 20 Nov 2023 19:24:47 +0200 Subject: [PATCH] WIP: add support for removing deleted vms from the inventory Signed-off-by: Bella Khizgiyaev --- .../provider/container/ova/collector.go | 12 +- .../provider/container/ova/model.go | 139 ++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/pkg/controller/provider/container/ova/collector.go b/pkg/controller/provider/container/ova/collector.go index 6e5e64fe2..b38d45af3 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 = 10 * time.Second ) // 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 deletes, updates []Updater mark := time.Now() for _, adapter := range adapterList { if ctx.canceled() { return } + deletes, err = adapter.DeleteUnexisting(ctx) + if err != nil { + return + } + err = r.apply(deletes) + 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..2298a8f05 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) (updates []Updater, err error) } // Base adapter. @@ -123,6 +125,50 @@ func (r *NetworkAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) return } +func (r *NetworkAdapter) DeleteUnexisting(ctx *Context) (updates []Updater, err error) { + // networkList := []model.Network{} + // err = ctx.db.List(&networkList, libmodel.FilterOptions{}) + // if err != nil { + // fmt.Println("error getting inventory net", err) + // if errors.Is(err, libmodel.NotFound) { + // err = nil + // } + // return + // } + // networkListServer := []Network{} + // err = ctx.client.list("networks", &networkListServer) + // if err != nil { + // fmt.Println("error getting server net", err) + // return + // } + + // elementMap := make(map[string]bool) + // for _, network := range networkListServer { + // elementMap[network.ID] = true + // } + + // networkToDelete := []string{} + // for _, network := range networkList { + // if _, found := elementMap[network.ID]; !found { + // fmt.Println("we founf net to delete", network.ID) + // networkToDelete = append(networkToDelete, network.ID) + // } + // } + // fmt.Println("this is net to delete", networkToDelete) + // for _, networkId := range networkToDelete { + // updater := func(tx *libmodel.Tx) (err error) { + // m := &model.Network{ + // Base: model.Base{ID: networkId}, + // } + // return tx.Delete(m) + // } + // updates = append(updates, updater) + // err = nil + // } + // return + return +} + // VM adapter. type VMAdapter struct { BaseAdapter @@ -177,6 +223,51 @@ func (r *VMAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) { return } +func (r *VMAdapter) DeleteUnexisting(ctx *Context) (updates []Updater, err error) { + vmList := []model.VM{} + err = ctx.db.List(&vmList, libmodel.FilterOptions{}) + if err != nil { + fmt.Println("error getting inventory vms", err) + if errors.Is(err, libmodel.NotFound) { + err = nil + } + return + } + vmListServer := []VM{} + err = ctx.client.list("vms", &vmListServer) + if err != nil { + fmt.Println("error getting server vms", err) + return + } + fmt.Println("this is vm lis server", vmListServer) + + elementMap := make(map[string]bool) + for _, vm := range vmListServer { + elementMap[vm.UUID] = true + } + fmt.Println("this is elementMap", elementMap) + + vmToDelete := []string{} + for _, vm := range vmList { + fmt.Println("this is vm id", vm.ID) + if _, found := elementMap[vm.ID]; !found { + vmToDelete = append(vmToDelete, vm.ID) + } + } + fmt.Println("this is vm to delete", vmToDelete) + for _, vmId := range vmToDelete { + updater := func(tx *libmodel.Tx) (err error) { + m := &model.VM{ + Base: model.Base{ID: vmId}, + } + return tx.Delete(m) + } + updates = append(updates, updater) + err = nil + } + return +} + // Disk adapter. type DiskAdapter struct { BaseAdapter @@ -232,6 +323,48 @@ func (r *DiskAdapter) GetUpdates(ctx *Context) (updates []Updater, err error) { return } +func (r *DiskAdapter) DeleteUnexisting(ctx *Context) (updates []Updater, err error) { + diskList := []model.Disk{} + err = ctx.db.List(&diskList, libmodel.FilterOptions{}) + if err != nil { + fmt.Println("error getting inventory disk", err) + if errors.Is(err, libmodel.NotFound) { + err = nil + } + return + } + diskListServer := []Disk{} + err = ctx.client.list("disks", &diskListServer) + if err != nil { + fmt.Println("error getting server disk", err) + return + } + + elementMap := make(map[string]bool) + for _, disk := range diskListServer { + elementMap[disk.ID] = true + } + + diskToDelete := []string{} + for _, disk := range diskList { + if _, found := elementMap[disk.ID]; !found { + diskToDelete = append(diskToDelete, disk.ID) + } + } + + for _, diskId := range diskToDelete { + updater := func(tx *libmodel.Tx) (err error) { + m := &model.Disk{ + Base: model.Base{ID: diskId}, + } + return tx.Delete(m) + } + updates = append(updates, updater) + err = nil + } + return +} + type StorageAdapter struct { BaseAdapter } @@ -261,3 +394,9 @@ func (r *StorageAdapter) List(ctx *Context, provider *api.Provider) (itr fb.Iter return } + +func (r *StorageAdapter) DeleteUnexisting(ctx *Context) (updates []Updater, err error) { + // Each provider have only one storage hence it can't be changed, + // Will be removed only if the provider deleted. + return +}