Skip to content

Commit

Permalink
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]>
bkhizgiy committed Nov 21, 2023
1 parent 07f314c commit 5f7d287
Showing 2 changed files with 135 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
@@ -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
125 changes: 125 additions & 0 deletions pkg/controller/provider/container/ova/model.go
Original file line number Diff line number Diff line change
@@ -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,45 @@ 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 {
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
}

networkToDelete := []string{}
for _, network := range networkList {
if _, found := elementMap[network.ID]; !found {
networkToDelete = append(networkToDelete, network.ID)
}
}
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
}

// 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) (updates []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
}

vmToDelete := []string{}
for _, vm := range vmList {
if _, found := elementMap[vm.ID]; !found {
vmToDelete = append(vmToDelete, vm.ID)
}
}
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 +312,45 @@ 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 {
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
}

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 +380,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 5f7d287

Please sign in to comment.