Skip to content

Commit 8963552

Browse files
prometherionmjuraga
authored andcommitted
BUG/MINOR: keep service discovery running despite errors
Signed-off-by: Dario Tranchitella <dtranchitella@haproxy.com>
1 parent 511356d commit 8963552

1 file changed

Lines changed: 42 additions & 27 deletions

File tree

discovery/aws_service_discovery_instance.go

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

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

@@ -163,7 +194,9 @@ func (a *awsInstance) start() {
163194
if !ok {
164195
return
165196
}
197+
166198
a.logDebug("discovery job update triggered")
199+
167200
err := a.discoveryConfig.UpdateParams(discoveryInstanceParams{
168201
Allowlist: []string{},
169202
Denylist: []string{},
@@ -173,34 +206,13 @@ func (a *awsInstance) start() {
173206
SlotsIncrement: int(a.params.ServerSlotsGrowthIncrement),
174207
})
175208
if err != nil {
176-
a.stop()
209+
a.logErrorf("error while updating discovery settings: %s", err.Error())
210+
break
177211
}
178-
case <-discoveryTimer.C:
179-
a.logDebug("discovery job update triggered")
180-
181-
var api *ec2.Client
182-
var err error
183212

184-
if api, err = a.setAPIClient(); err != nil {
185-
a.logErrorf("error while setting up the API client: %s", err.Error())
186-
a.stop()
187-
}
188-
if err = a.updateServices(api); err != nil {
189-
switch t := err.(type) {
190-
case *configuration.ConfError:
191-
switch t.Err() {
192-
case configuration.ErrObjectAlreadyExists:
193-
continue
194-
default:
195-
a.stop()
196-
a.logErrorf("error while updating service: %s", err.Error())
197-
}
198-
default:
199-
a.stop()
200-
}
201-
}
202-
203-
a.logDebug("discovery job reconciliation completed")
213+
a.logDebug("discovery job update completed")
214+
case <-discoveryTimer.C:
215+
a.updateServicesFn()
204216
discoveryTimer.Reset(a.timeout)
205217
case <-a.ctx.Done():
206218
a.stop()
@@ -210,6 +222,9 @@ func (a *awsInstance) start() {
210222
}
211223

212224
func (a *awsInstance) setAPIClient() (*ec2.Client, error) {
225+
ctx, cancelFn := context.WithTimeout(a.ctx, a.timeout)
226+
defer cancelFn()
227+
213228
opts := []func(options *config.LoadOptions) error{
214229
config.WithRegion(*a.params.Region),
215230
}
@@ -221,7 +236,7 @@ func (a *awsInstance) setAPIClient() (*ec2.Client, error) {
221236
},
222237
}))
223238
}
224-
cfg, err := config.LoadDefaultConfig(context.Background(), opts...)
239+
cfg, err := config.LoadDefaultConfig(ctx, opts...)
225240
if err != nil {
226241
return nil, fmt.Errorf("cannot generate the AWS instance due to a configuration setup error: %w", err)
227242
}

0 commit comments

Comments
 (0)