Skip to content

Commit

Permalink
applying suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jan 14, 2020
1 parent b3a8b27 commit 1ff21bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
22 changes: 18 additions & 4 deletions pkg/ansible/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc

// iterate events from ansible, looking for the final one
statusEvent := eventapi.StatusJobEvent{}
failureMessages := eventapi.FailureMessages{}
for event := range result.Events() {
for _, eHandler := range r.EventHandlers {
go eHandler.Handle(ident, u, event)
Expand All @@ -182,8 +183,12 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
}
}
if event.Event == eventapi.EventRunnerOnFailed && !event.IgnoreError() {
eventErr := errors.New("event runner on failed")
return reconcile.Result{}, eventErr
if len(event.GetFailedPlaybookMessage()) < 1 {
eventErr := fmt.Errorf("Event runner on failed (%v)", event.EventData["Task"])
failureMessages = append(failureMessages, eventErr.Error())
} else {
failureMessages = append(failureMessages, event.GetFailedPlaybookMessage())
}
}
}
if statusEvent.Event == "" {
Expand All @@ -210,8 +215,12 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
// try to get the updated finalizers
pendingFinalizers = u.GetFinalizers()

// We only want to update the CustomResource once, so we'll track changes
// and do it at the end
runSuccessful := len(failureMessages) == 0

// The finalizer has run successfully, time to remove it
if deleted && finalizerExists {
if deleted && finalizerExists && runSuccessful {
finalizers := []string{}
for _, pendingFinalizer := range pendingFinalizers {
if pendingFinalizer != finalizer {
Expand All @@ -226,12 +235,17 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
}
}
if r.ManageStatus {
errmark := r.markDone(u, request.NamespacedName, statusEvent, nil)
errmark := r.markDone(u, request.NamespacedName, statusEvent, failureMessages)
if errmark != nil {
logger.Error(errmark, "Failed to mark status done")
}
return reconcileResult, errmark
}

// re-trigger reconcile because of failures
if !runSuccessful {
return reconcileResult, errors.New("Event runner on failed")
}
return reconcileResult, nil
}

Expand Down
16 changes: 14 additions & 2 deletions pkg/ansible/controller/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func TestReconcile(t *testing.T) {
Namespace: "default",
},
},
// Since face an event error needs re-trigger the reconcile
ExpectedObject: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
Expand All @@ -183,11 +182,24 @@ func TestReconcile(t *testing.T) {
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"status": "True",
"status": "False",
"type": "Running",
"message": "Running reconciliation",
"reason": "Running",
},
map[string]interface{}{
"status": "True",
"type": "Failure",
"ansibleResult": map[string]interface{}{
"changed": int64(0),
"failures": int64(0),
"ok": int64(0),
"skipped": int64(0),
"completion": eventTime.Format("2006-01-02T15:04:05.99999999"),
},
"message": "new failure message",
"reason": "Failed",
},
},
},
},
Expand Down

0 comments on commit 1ff21bf

Please sign in to comment.