From 986f18430c34f52f5d25f44a7a1117ad06ec18e9 Mon Sep 17 00:00:00 2001 From: Eli Wrenn Date: Thu, 12 Aug 2021 13:11:42 -0700 Subject: [PATCH] Retry status updates in case resource changes during reconciliation --- pkg/packageinstall/packageinstall.go | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/packageinstall/packageinstall.go b/pkg/packageinstall/packageinstall.go index 7269ec3af..98ceb5ed8 100644 --- a/pkg/packageinstall/packageinstall.go +++ b/pkg/packageinstall/packageinstall.go @@ -241,17 +241,28 @@ func (pi *PackageInstallCR) update(updateFunc func(*pkgingv1alpha1.PackageInstal pi.log.Info("Updating installed package") modelForUpdate := pi.model.DeepCopy() - updateFunc(modelForUpdate) - var err error + var lastErr error + for i := 0; i < 5; i++ { + updateFunc(modelForUpdate) - pi.model, err = pi.kcclient.PackagingV1alpha1().PackageInstalls(modelForUpdate.Namespace).Update( - context.Background(), modelForUpdate, metav1.UpdateOptions{}) - if err != nil { - return fmt.Errorf("Updating installed package: %s", err) - } + updatedModel, err := pi.kcclient.PackagingV1alpha1().PackageInstalls(modelForUpdate.Namespace).Update( + context.Background(), modelForUpdate, metav1.UpdateOptions{}) + if err == nil { + pi.model = updatedModel + pi.unmodifiedModel = updatedModel.DeepCopy() + return nil + } - pi.unmodifiedModel = pi.model.DeepCopy() + lastErr = err - return nil + // if we errored, refresh the model we have + modelForUpdate, err = pi.kcclient.PackagingV1alpha1().PackageInstalls(modelForUpdate.Namespace).Get( + context.Background(), modelForUpdate.Name, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("Getting package install model: %s", err) + } + } + + return fmt.Errorf("Updating package install: %s", lastErr) }