@@ -421,14 +421,15 @@ func apply(tvConfig, tvLive *typed.TypedValue, p *SMDParams) (*typed.TypedValue,
421
421
if err != nil {
422
422
return nil , fmt .Errorf ("error while running updater.Apply: %w" , err )
423
423
}
424
- return mergedLive , err
424
+ return mergedLive , nil
425
425
}
426
426
427
427
func buildManagerInfoForApply (manager string ) (string , error ) {
428
428
managerInfo := metav1.ManagedFieldsEntry {
429
429
Manager : manager ,
430
430
Operation : metav1 .ManagedFieldsOperationApply ,
431
431
}
432
+ //nolint:wrapcheck // trivial function, wrapped nicely by the caller
432
433
return fieldmanager .BuildManagerIdentifier (& managerInfo )
433
434
}
434
435
@@ -489,13 +490,13 @@ func handleResourceCreateOrDeleteDiff(config, live *unstructured.Unstructured) (
489
490
if live != nil {
490
491
liveData , err := json .Marshal (live )
491
492
if err != nil {
492
- return nil , err
493
+ return nil , fmt . Errorf ( "error marshaling live resource: %w" , err )
493
494
}
494
495
return & DiffResult {Modified : false , NormalizedLive : liveData , PredictedLive : []byte ("null" )}, nil
495
496
} else if config != nil {
496
497
predictedLiveData , err := json .Marshal (config .Object )
497
498
if err != nil {
498
- return nil , err
499
+ return nil , fmt . Errorf ( "error marshaling config resource: %w" , err )
499
500
}
500
501
return & DiffResult {Modified : true , NormalizedLive : []byte ("null" ), PredictedLive : predictedLiveData }, nil
501
502
}
@@ -539,7 +540,7 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
539
540
// Apply the patchBytes patch against liveBytes, using predictedLive to indicate the k8s data type
540
541
predictedLiveBytes , err := strategicpatch .StrategicMergePatch (liveBytes , patchBytes , predictedLive )
541
542
if err != nil {
542
- return nil , nil , err
543
+ return nil , nil , fmt . Errorf ( "failed to construct strategic merge patch: %w" , err )
543
544
}
544
545
545
546
// 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() (
562
563
// to its k8s resource type (eg the JSON may contain those invalid fields that we do not wish to discard).
563
564
predictedLiveBytes , err = strategicpatch .StrategicMergePatch (predictedLiveBytes , patch , predictedLive .DeepCopyObject ())
564
565
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 )
566
567
}
567
568
568
569
// 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() (
571
572
var result map [string ]any
572
573
err = json .Unmarshal ([]byte (predictedLiveBytes ), & result )
573
574
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 )
575
576
}
576
577
predictedLiveBytes , err = json .Marshal (result )
577
578
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 )
579
580
}
580
581
}
581
582
@@ -595,18 +596,18 @@ func applyPatch(liveBytes []byte, patchBytes []byte, newVersionedObject func() (
595
596
}
596
597
liveBytes , err = strategicpatch .StrategicMergePatch (liveBytes , patch , live .DeepCopyObject ())
597
598
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 )
599
600
}
600
601
601
602
// Ensure the fields are sorted in a consistent order (as above)
602
603
var result map [string ]any
603
604
err = json .Unmarshal ([]byte (liveBytes ), & result )
604
605
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 )
606
607
}
607
608
liveBytes , err = json .Marshal (result )
608
609
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 )
610
611
}
611
612
}
612
613
@@ -662,7 +663,7 @@ func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, e
662
663
// 2. get expected live object by applying the patch against the live object
663
664
liveBytes , err := json .Marshal (live )
664
665
if err != nil {
665
- return nil , err
666
+ return nil , fmt . Errorf ( "failed to marshal live state: %w" , err )
666
667
}
667
668
668
669
var predictedLiveBytes []byte
@@ -677,7 +678,7 @@ func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, e
677
678
// Otherwise, merge patch directly as JSON
678
679
predictedLiveBytes , err = jsonpatch .MergePatch (liveBytes , patchBytes )
679
680
if err != nil {
680
- return nil , err
681
+ return nil , fmt . Errorf ( "failed to construct merge patch for predicted state: %w" , err )
681
682
}
682
683
}
683
684
@@ -733,11 +734,11 @@ func statefulSetWorkaround(orig, live *unstructured.Unstructured) *unstructured.
733
734
func threeWayMergePatch (orig , config , live * unstructured.Unstructured ) ([]byte , func () (runtime.Object , error ), error ) {
734
735
origBytes , err := json .Marshal (orig .Object )
735
736
if err != nil {
736
- return nil , nil , err
737
+ return nil , nil , fmt . Errorf ( "failed to marshal original object: %w" , err )
737
738
}
738
739
configBytes , err := json .Marshal (config .Object )
739
740
if err != nil {
740
- return nil , nil , err
741
+ return nil , nil , fmt . Errorf ( "failed to marshal config object: %w" , err )
741
742
}
742
743
743
744
if versionedObject , err := scheme .Scheme .New (orig .GroupVersionKind ()); err == nil {
@@ -748,16 +749,16 @@ func threeWayMergePatch(orig, config, live *unstructured.Unstructured) ([]byte,
748
749
749
750
liveBytes , err := json .Marshal (live .Object )
750
751
if err != nil {
751
- return nil , nil , err
752
+ return nil , nil , fmt . Errorf ( "failed to marshal live object: %w" , err )
752
753
}
753
754
754
755
lookupPatchMeta , err := strategicpatch .NewPatchMetaFromStruct (versionedObject )
755
756
if err != nil {
756
- return nil , nil , err
757
+ return nil , nil , fmt . Errorf ( "failed to construct lookup patch: %w" , err )
757
758
}
758
759
patch , err := strategicpatch .CreateThreeWayMergePatch (origBytes , configBytes , liveBytes , lookupPatchMeta , true )
759
760
if err != nil {
760
- return nil , nil , err
761
+ return nil , nil , fmt . Errorf ( "failed to construct thre way merge patch: %w" , err )
761
762
}
762
763
newVersionedObject := func () (runtime.Object , error ) {
763
764
return scheme .Scheme .New (orig .GroupVersionKind ())
@@ -770,12 +771,12 @@ func threeWayMergePatch(orig, config, live *unstructured.Unstructured) ([]byte,
770
771
771
772
liveBytes , err := json .Marshal (live .Object )
772
773
if err != nil {
773
- return nil , nil , err
774
+ return nil , nil , fmt . Errorf ( "failed to marshal live object: %w" , err )
774
775
}
775
776
776
777
patch , err := jsonmergepatch .CreateThreeWayJSONMergePatch (origBytes , configBytes , liveBytes )
777
778
if err != nil {
778
- return nil , nil , err
779
+ return nil , nil , fmt . Errorf ( "failed to construct thre way merge patch: %w" , err )
779
780
}
780
781
return patch , nil , nil
781
782
}
@@ -989,15 +990,15 @@ func normalizeRole(un *unstructured.Unstructured, o options) {
989
990
func CreateTwoWayMergePatch (orig , new , dataStruct any ) ([]byte , bool , error ) {
990
991
origBytes , err := json .Marshal (orig )
991
992
if err != nil {
992
- return nil , false , err
993
+ return nil , false , fmt . Errorf ( "failed to marshal orig object: %w" , err )
993
994
}
994
995
newBytes , err := json .Marshal (new )
995
996
if err != nil {
996
- return nil , false , err
997
+ return nil , false , fmt . Errorf ( "failed to marshal new object: %w" , err )
997
998
}
998
999
patch , err := strategicpatch .CreateTwoWayMergePatch (origBytes , newBytes , dataStruct )
999
1000
if err != nil {
1000
- return nil , false , err
1001
+ return nil , false , fmt . Errorf ( "failed to create two way merge patch: %w" , err )
1001
1002
}
1002
1003
return patch , string (patch ) != "{}" , nil
1003
1004
}
0 commit comments