Stop reconcile loop when error has been repeated more than 15 times

This commit is contained in:
Tomasz Sęk 2019-06-17 22:46:48 +02:00
parent 2264e11a6d
commit a408112875
No known key found for this signature in database
GPG Key ID: DC356D23F6A644D0
2 changed files with 31 additions and 4 deletions

View File

@ -135,8 +135,6 @@ func (bar *BackupAndRestore) Restore(jenkinsClient jenkinsclient.Jenkins) error
return bar.k8sClient.Update(context.TODO(), jenkins)
}
//TODO after 3 fails stop
return err
}
@ -163,8 +161,6 @@ func (bar *BackupAndRestore) Backup() error {
return bar.k8sClient.Update(context.TODO(), jenkins)
}
//TODO after 3 fails stop
return err
}

View File

@ -42,6 +42,13 @@ const (
reasonCRValidationFailure event.Reason = "CRValidationFailure"
)
type reconcileError struct {
err error
counter uint64
}
var reconcileErrors = map[string]reconcileError{}
// Add creates a new Jenkins Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, local, minikube bool, events event.Recorder, clientSet kubernetes.Clientset, config rest.Config) error {
@ -120,6 +127,30 @@ func (r *ReconcileJenkins) Reconcile(request reconcile.Request) (reconcile.Resul
if err != nil && apierrors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
} else if err != nil {
lastErrors, found := reconcileErrors[request.Name]
if found {
if err.Error() == lastErrors.err.Error() {
lastErrors.counter++
} else {
lastErrors.counter = 1
lastErrors.err = err
}
} else {
lastErrors = reconcileError{
err: err,
counter: 1,
}
}
reconcileErrors[request.Name] = lastErrors
if lastErrors.counter >= 15 {
if log.Debug {
logger.V(log.VWarn).Info(fmt.Sprintf("Reconcile loop failed ten times with the same error, giving up: %+v", err))
} else {
logger.V(log.VWarn).Info(fmt.Sprintf("Reconcile loop failed ten times with the same error, giving up: %s", err))
}
return reconcile.Result{Requeue: false}, nil
}
if log.Debug {
logger.V(log.VWarn).Info(fmt.Sprintf("Reconcile loop failed: %+v", err))
} else {