Skip to content

Commit d748811

Browse files
committed
BUG/MINOR: keep service discovery running despite errors
Signed-off-by: Dario Tranchitella <dtranchitella@haproxy.com>
1 parent 1d084e8 commit d748811

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

discovery/aws_service_discovery_instance.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,37 @@ func (a *awsInstance) updateTimeout(timeoutSeconds int64) error {
155155
return nil
156156
}
157157

158+
func (a *awsInstance) updateServicesFn() {
159+
a.logDebug("discovery job update triggered")
160+
161+
var api *ec2.Client
162+
var err error
163+
164+
if api, err = a.setAPIClient(); err != nil {
165+
a.logErrorf("error while setting up the API client: %s", err.Error())
166+
return
167+
}
168+
169+
if err = a.updateServices(api); err != nil {
170+
switch t := err.(type) {
171+
case *configuration.ConfError:
172+
switch t.Err() {
173+
case configuration.ErrObjectAlreadyExists:
174+
a.logDebug("object already exists, ignoring error")
175+
return
176+
default:
177+
a.logErrorf("a configuration error occurred while updating service: %s", err.Error())
178+
return
179+
}
180+
default:
181+
a.logErrorf("an error occurred while updating service: %s", err.Error())
182+
return
183+
}
184+
}
185+
186+
a.logDebug("discovery job reconciliation completed")
187+
}
188+
158189
func (a *awsInstance) start() {
159190
a.update = make(chan struct{})
160191

@@ -170,7 +201,9 @@ func (a *awsInstance) start() {
170201
if !ok {
171202
return
172203
}
204+
173205
a.logDebug("discovery job update triggered")
206+
174207
err := a.discoveryConfig.UpdateParams(discoveryInstanceParams{
175208
Allowlist: []string{},
176209
Denylist: []string{},
@@ -180,34 +213,13 @@ func (a *awsInstance) start() {
180213
SlotsIncrement: int(a.params.ServerSlotsGrowthIncrement),
181214
})
182215
if err != nil {
183-
a.stop()
216+
a.logErrorf("error while updating discovery settings: %s", err.Error())
217+
break
184218
}
185-
case <-discoveryTimer.C:
186-
a.logDebug("discovery job update triggered")
187-
188-
var api *ec2.Client
189-
var err error
190219

191-
if api, err = a.setAPIClient(); err != nil {
192-
a.logErrorf("error while setting up the API client: %s", err.Error())
193-
a.stop()
194-
}
195-
if err = a.updateServices(api); err != nil {
196-
switch t := err.(type) {
197-
case *configuration.ConfError:
198-
switch t.Err() {
199-
case configuration.ErrObjectAlreadyExists:
200-
continue
201-
default:
202-
a.stop()
203-
a.logErrorf("error while updating service: %s", err.Error())
204-
}
205-
default:
206-
a.stop()
207-
}
208-
}
209-
210-
a.logDebug("discovery job reconciliation completed")
220+
a.logDebug("discovery job update completed")
221+
case <-discoveryTimer.C:
222+
a.updateServicesFn()
211223
discoveryTimer.Reset(a.timeout)
212224
case <-a.ctx.Done():
213225
a.stop()
@@ -217,6 +229,9 @@ func (a *awsInstance) start() {
217229
}
218230

219231
func (a *awsInstance) setAPIClient() (*ec2.Client, error) {
232+
ctx, cancelFn := context.WithTimeout(a.ctx, a.timeout)
233+
defer cancelFn()
234+
220235
opts := []func(options *config.LoadOptions) error{
221236
config.WithRegion(*a.params.Region),
222237
}
@@ -228,7 +243,7 @@ func (a *awsInstance) setAPIClient() (*ec2.Client, error) {
228243
},
229244
}))
230245
}
231-
cfg, err := config.LoadDefaultConfig(context.Background(), opts...)
246+
cfg, err := config.LoadDefaultConfig(ctx, opts...)
232247
if err != nil {
233248
return nil, fmt.Errorf("cannot generate the AWS instance due to a configuration setup error: %w", err)
234249
}

0 commit comments

Comments
 (0)