@@ -421,14 +421,15 @@ func apply(tvConfig, tvLive *typed.TypedValue, p *SMDParams) (*typed.TypedValue,
421421 if err != nil {
422422 return nil , fmt .Errorf ("error while running updater.Apply: %w" , err )
423423 }
424- return mergedLive , err
424+ return mergedLive , nil
425425}
426426
427427func buildManagerInfoForApply (manager string ) (string , error ) {
428428 managerInfo := metav1.ManagedFieldsEntry {
429429 Manager : manager ,
430430 Operation : metav1 .ManagedFieldsOperationApply ,
431431 }
432+ //nolint:wrapcheck // trivial function, wrapped nicely by the caller
432433 return fieldmanager .BuildManagerIdentifier (& managerInfo )
433434}
434435
@@ -489,13 +490,13 @@ func handleResourceCreateOrDeleteDiff(config, live *unstructured.Unstructured) (
489490 if live != nil {
490491 liveData , err := json .Marshal (live )
491492 if err != nil {
492- return nil , err
493+ return nil , fmt . Errorf ( "error marshaling live resource: %w" , err )
493494 }
494495 return & DiffResult {Modified : false , NormalizedLive : liveData , PredictedLive : []byte ("null" )}, nil
495496 } else if config != nil {
496497 predictedLiveData , err := json .Marshal (config .Object )
497498 if err != nil {
498- return nil , err
499+ return nil , fmt . Errorf ( "error marshaling config resource: %w" , err )
499500 }
500501 return & DiffResult {Modified : true , NormalizedLive : []byte ("null" ), PredictedLive : predictedLiveData }, nil
501502 }
@@ -539,7 +540,7 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
539540 // Apply the patchBytes patch against liveBytes, using predictedLive to indicate the k8s data type
540541 predictedLiveBytes , err := strategicpatch .StrategicMergePatch (liveBytes , patchBytes , predictedLive )
541542 if err != nil {
542- return nil , nil , err
543+ return nil , nil , fmt . Errorf ( "failed to construct strategic merge patch: %w" , err )
543544 }
544545
545546 // Unmarshal predictedLiveBytes into predictedLive; note that this will discard JSON fields in predictedLiveBytes
@@ -562,7 +563,7 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
562563 // to its k8s resource type (eg the JSON may contain those invalid fields that we do not wish to discard).
563564 predictedLiveBytes , err = strategicpatch .StrategicMergePatch (predictedLiveBytes , patch , predictedLive .DeepCopyObject ())
564565 if err != nil {
565- return nil , nil , err
566+ return nil , nil , fmt . Errorf ( "failed to construct strategic merge patch for predicted state: %w" , err )
566567 }
567568
568569 // 3) Unmarshall into a map[string]any, then back into byte[], to ensure the fields
@@ -571,11 +572,11 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
571572 var result map [string ]any
572573 err = json .Unmarshal ([]byte (predictedLiveBytes ), & result )
573574 if err != nil {
574- return nil , nil , err
575+ return nil , nil , fmt . Errorf ( "failed to unmarshal strategic merge patch for predicted state: %w" , err )
575576 }
576577 predictedLiveBytes , err = json .Marshal (result )
577578 if err != nil {
578- return nil , nil , err
579+ return nil , nil , fmt . Errorf ( "failed to marshal strategic merge patch for predicted state: %w" , err )
579580 }
580581 }
581582
@@ -595,18 +596,18 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
595596 }
596597 liveBytes , err = strategicpatch .StrategicMergePatch (liveBytes , patch , live .DeepCopyObject ())
597598 if err != nil {
598- return nil , nil , err
599+ return nil , nil , fmt . Errorf ( "failed to construct strategic merge patch for live state: %w" , err )
599600 }
600601
601602 // Ensure the fields are sorted in a consistent order (as above)
602603 var result map [string ]any
603604 err = json .Unmarshal ([]byte (liveBytes ), & result )
604605 if err != nil {
605- return nil , nil , err
606+ return nil , nil , fmt . Errorf ( "failed to unmarshal strategic merge patch for live state: %w" , err )
606607 }
607608 liveBytes , err = json .Marshal (result )
608609 if err != nil {
609- return nil , nil , err
610+ return nil , nil , fmt . Errorf ( "failed to marshal strategic merge patch for live state: %w" , err )
610611 }
611612 }
612613
@@ -662,7 +663,7 @@ func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, e
662663 // 2. get expected live object by applying the patch against the live object
663664 liveBytes , err := json .Marshal (live )
664665 if err != nil {
665- return nil , err
666+ return nil , fmt . Errorf ( "failed to marshal live state: %w" , err )
666667 }
667668
668669 var predictedLiveBytes []byte
@@ -677,7 +678,7 @@ func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, e
677678 // Otherwise, merge patch directly as JSON
678679 predictedLiveBytes , err = jsonpatch .MergePatch (liveBytes , patchBytes )
679680 if err != nil {
680- return nil , err
681+ return nil , fmt . Errorf ( "failed to construct merge patch for predicted state: %w" , err )
681682 }
682683 }
683684
@@ -733,11 +734,11 @@ func statefulSetWorkaround(orig, live *unstructured.Unstructured) *unstructured.
733734func threeWayMergePatch (orig , config , live * unstructured.Unstructured ) ([]byte , func () (runtime.Object , error ), error ) {
734735 origBytes , err := json .Marshal (orig .Object )
735736 if err != nil {
736- return nil , nil , err
737+ return nil , nil , fmt . Errorf ( "failed to marshal original object: %w" , err )
737738 }
738739 configBytes , err := json .Marshal (config .Object )
739740 if err != nil {
740- return nil , nil , err
741+ return nil , nil , fmt . Errorf ( "failed to marshal config object: %w" , err )
741742 }
742743
743744 if versionedObject , err := scheme .Scheme .New (orig .GroupVersionKind ()); err == nil {
@@ -748,16 +749,16 @@ func threeWayMergePatch(orig, config, live *unstructured.Unstructured) ([]byte,
748749
749750 liveBytes , err := json .Marshal (live .Object )
750751 if err != nil {
751- return nil , nil , err
752+ return nil , nil , fmt . Errorf ( "failed to marshal live object: %w" , err )
752753 }
753754
754755 lookupPatchMeta , err := strategicpatch .NewPatchMetaFromStruct (versionedObject )
755756 if err != nil {
756- return nil , nil , err
757+ return nil , nil , fmt . Errorf ( "failed to construct lookup patch: %w" , err )
757758 }
758759 patch , err := strategicpatch .CreateThreeWayMergePatch (origBytes , configBytes , liveBytes , lookupPatchMeta , true )
759760 if err != nil {
760- return nil , nil , err
761+ return nil , nil , fmt . Errorf ( "failed to construct thre way merge patch: %w" , err )
761762 }
762763 newVersionedObject := func () (runtime.Object , error ) {
763764 return scheme .Scheme .New (orig .GroupVersionKind ())
@@ -770,12 +771,12 @@ func threeWayMergePatch(orig, config, live *unstructured.Unstructured) ([]byte,
770771
771772 liveBytes , err := json .Marshal (live .Object )
772773 if err != nil {
773- return nil , nil , err
774+ return nil , nil , fmt . Errorf ( "failed to marshal live object: %w" , err )
774775 }
775776
776777 patch , err := jsonmergepatch .CreateThreeWayJSONMergePatch (origBytes , configBytes , liveBytes )
777778 if err != nil {
778- return nil , nil , err
779+ return nil , nil , fmt . Errorf ( "failed to construct thre way merge patch: %w" , err )
779780 }
780781 return patch , nil , nil
781782}
@@ -989,15 +990,15 @@ func normalizeRole(un *unstructured.Unstructured, o options) {
989990func CreateTwoWayMergePatch (orig , new , dataStruct any ) ([]byte , bool , error ) {
990991 origBytes , err := json .Marshal (orig )
991992 if err != nil {
992- return nil , false , err
993+ return nil , false , fmt . Errorf ( "failed to marshal orig object: %w" , err )
993994 }
994995 newBytes , err := json .Marshal (new )
995996 if err != nil {
996- return nil , false , err
997+ return nil , false , fmt . Errorf ( "failed to marshal new object: %w" , err )
997998 }
998999 patch , err := strategicpatch .CreateTwoWayMergePatch (origBytes , newBytes , dataStruct )
9991000 if err != nil {
1000- return nil , false , err
1001+ return nil , false , fmt . Errorf ( "failed to create two way merge patch: %w" , err )
10011002 }
10021003 return patch , string (patch ) != "{}" , nil
10031004}
0 commit comments