Skip to content

Commit

Permalink
Cleanups in createOrUpdateManifestWork()
Browse files Browse the repository at this point in the history
- More consistent logging - "Creating ManifestWork" or "Updating
  ManifestWork".
- Extract key variable to clean up the Client.Get() calls
- Eliminate unneeded temporary err and retryErr variables
- Remove commented code

Signed-off-by: Nir Soffer <[email protected]>
  • Loading branch information
nirs authored and ShyamsundarR committed Dec 13, 2023
1 parent 948a47c commit c75ee6b
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions controllers/util/mw_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,50 +481,32 @@ func (mwu *MWUtil) createOrUpdateManifestWork(
mw *ocmworkv1.ManifestWork,
managedClusternamespace string,
) error {
key := types.NamespacedName{Name: mw.Name, Namespace: managedClusternamespace}
foundMW := &ocmworkv1.ManifestWork{}

err := mwu.Client.Get(mwu.Ctx,
types.NamespacedName{Name: mw.Name, Namespace: managedClusternamespace},
foundMW)
err := mwu.Client.Get(mwu.Ctx, key, foundMW)
if err != nil {
if !errors.IsNotFound(err) {
return errorswrapper.Wrap(err, fmt.Sprintf("failed to fetch ManifestWork %s", mw.Name))
return errorswrapper.Wrap(err, fmt.Sprintf("failed to fetch ManifestWork %s", key))
}

// Let DRPC receive notification for any changes to ManifestWork CR created by it.
// if err := ctrl.SetControllerReference(d.instance, mw, d.reconciler.Scheme); err != nil {
// return fmt.Errorf("failed to set owner reference to ManifestWork resource (%s/%s) (%v)",
// mw.Name, mw.Namespace, err)
// }

mwu.Log.Info("Creating ManifestWork", "cluster", managedClusternamespace, "MW", mw)

return mwu.Client.Create(mwu.Ctx, mw)
}

if !reflect.DeepEqual(foundMW.Spec, mw.Spec) {
mwu.Log.Info("ManifestWork exists.", "name", mw.Name, "namespace", foundMW.Namespace)

retryErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
var err error
mwu.Log.Info("Updating ManifestWork", "name", mw.Name, "namespace", foundMW.Namespace)

err = mwu.Client.Get(mwu.Ctx,
types.NamespacedName{Name: mw.Name, Namespace: managedClusternamespace},
foundMW)
if err != nil {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := mwu.Client.Get(mwu.Ctx, key, foundMW); err != nil {
return err
}

mw.Spec.DeepCopyInto(&foundMW.Spec)

err = mwu.Client.Update(mwu.Ctx, foundMW)

return err
return mwu.Client.Update(mwu.Ctx, foundMW)
})

if retryErr != nil {
return retryErr
}
}

return nil
Expand Down

0 comments on commit c75ee6b

Please sign in to comment.