Skip to content

Commit

Permalink
WIP: add support for removing deleted vms from the inventory
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed Nov 20, 2023
1 parent 07f314c commit 7e82e60
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/controller/provider/container/ova/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// Retry interval.
RetryInterval = 5 * time.Second
// Refresh interval.
RefreshInterval = 5 * time.Minute
RefreshInterval = 10 * time.Second
)

// Phases
Expand Down Expand Up @@ -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
Expand Down
139 changes: 139 additions & 0 deletions pkg/controller/provider/container/ova/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 7e82e60

Please sign in to comment.