Wait for addPendingInstances to finish

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2023-06-06 16:40:27 +03:00
parent e9f66c2035
commit bd2f103743
No known key found for this signature in database
GPG key ID: 7D073DCC2C074CB5

View file

@ -1068,7 +1068,7 @@ func (r *basePoolManager) addPendingInstances() {
log.Printf("failed to fetch instances from store: %s", err)
return
}
g, _ := errgroup.WithContext(r.ctx)
for _, instance := range instances {
if instance.Status != providerCommon.InstancePendingCreate {
// not in pending_create status. Skip.
@ -1082,7 +1082,8 @@ func (r *basePoolManager) addPendingInstances() {
// when the loop runs again and we end up with multiple instances.
continue
}
go func(instance params.Instance) {
instance := instance
g.Go(func() error {
log.Printf("creating instance %s in pool %s", instance.Name, instance.PoolID)
if err := r.addInstanceToProvider(instance); err != nil {
log.Printf("failed to add instance to provider: %s", err)
@ -1092,7 +1093,11 @@ func (r *basePoolManager) addPendingInstances() {
}
log.Printf("failed to create instance in provider: %s", err)
}
}(instance)
return nil
})
}
if err := g.Wait(); err != nil {
log.Printf("failed to add pending instances: %s", err)
}
}