Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OVA: Add support for removing deleted vms from the inventory #659

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 1 * time.Minute
)

// 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 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
Expand Down
125 changes: 125 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) (deletions []Updater, err error)
}

// Base adapter.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Loading