Skip to content

fix ansible reconcile when cr was not found #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions pkg/ansible/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ type AnsibleOperatorReconciler struct {
func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(r.GVK)
err := r.Client.Get(context.TODO(), request.NamespacedName, u)
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
if err != nil {
return reconcile.Result{}, err
}

ident := strconv.Itoa(rand.Int())
logger := logf.Log.WithName("reconciler").WithValues(
Expand All @@ -79,6 +72,20 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
"namespace", u.GetNamespace(),
)

err := r.Client.Get(context.TODO(), request.NamespacedName, u)
if err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
logger.Info("Resource not found, assuming it was deleted", "namespace", request.NamespacedName, "resource", u)
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
logger.Error(err, "Failed to get Resource.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary to log this error, since controller-runtime will also log it when we return from Reconcile().

Ditto for the logged error in line 209.

return reconcile.Result{}, err
}

reconcileResult := reconcile.Result{RequeueAfter: r.ReconcilePeriod}
if ds, ok := u.GetAnnotations()[ReconcilePeriodAnnotation]; ok {
duration, err := time.ParseDuration(ds)
Expand Down Expand Up @@ -190,10 +197,16 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
// Need to get the unstructured object after ansible
// this needs to hit the API
err = r.Client.Get(context.TODO(), request.NamespacedName, u)
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
if err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
logger.Info("Resource not found, assuming it was deleted", "namespace", request.NamespacedName, "resource", u)
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
logger.Error(err, "Failed to get Resource.")
return reconcile.Result{}, err
}

Expand Down Expand Up @@ -233,6 +246,7 @@ func (r *AnsibleOperatorReconciler) markRunning(u *unstructured.Unstructured, na
if err != nil {
return err
}

statusInterface := u.Object["status"]
statusMap, _ := statusInterface.(map[string]interface{})
crStatus := ansiblestatus.CreateFromMap(statusMap)
Expand Down Expand Up @@ -270,13 +284,11 @@ func (r *AnsibleOperatorReconciler) markError(u *unstructured.Unstructured, name
metrics.ReconcileFailed(r.GVK.String())
// Get the latest resource to prevent updating a stale status
err := r.Client.Get(context.TODO(), namespacedName, u)
if apierrors.IsNotFound(err) {
logger.Info("Resource not found, assuming it was deleted", err)
return nil
}
if err != nil {
logger.Error(err,"Resource not found")
return err
}

statusInterface := u.Object["status"]
statusMap, ok := statusInterface.(map[string]interface{})
// If the map is not available create one.
Expand Down Expand Up @@ -306,16 +318,12 @@ func (r *AnsibleOperatorReconciler) markError(u *unstructured.Unstructured, name
}

func (r *AnsibleOperatorReconciler) markDone(u *unstructured.Unstructured, namespacedName types.NamespacedName, statusEvent eventapi.StatusJobEvent, failureMessages eventapi.FailureMessages) error {
logger := logf.Log.WithName("markDone")
// Get the latest resource to prevent updating a stale status
err := r.Client.Get(context.TODO(), namespacedName, u)
if apierrors.IsNotFound(err) {
logger.Info("Resource not found, assuming it was deleted", err)
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change would result in returning an error from this function when the CR is not found, which would result in returning an error from the main Reconcile function (see here).

By returning the not found error, we would trigger another Reconcile unnecessarily. By returning nil, the Reconcile function will also return nil, which will result in no further reconciliations occurring.

Ditto for the changes in markError, except that we never actually check the error returned from markError. In the markError cases, we always return the original error no matter what happens with markError.

if err != nil {
return err
}

statusInterface := u.Object["status"]
statusMap, _ := statusInterface.(map[string]interface{})
crStatus := ansiblestatus.CreateFromMap(statusMap)
Expand Down