@@ -2,7 +2,6 @@ package controller
22
33import (
44 "context"
5- "encoding/json"
65 stderrors "errors"
76 "fmt"
87 "os"
@@ -263,7 +262,7 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, project *v1alp
263262 // resources which in this case applies the live values in the configured
264263 // ignore differences fields.
265264 if syncOp .SyncOptions .HasOption ("RespectIgnoreDifferences=true" ) {
266- patchedTargets , err := normalizeTargetResources (openAPISchema , compareResult )
265+ patchedTargets , err := normalizeTargetResources (compareResult )
267266 if err != nil {
268267 state .Phase = common .OperationError
269268 state .Message = fmt .Sprintf ("Failed to normalize target resources: %s" , err )
@@ -435,65 +434,53 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, project *v1alp
435434// - applies normalization to the target resources based on the live resources
436435// - copies ignored fields from the matching live resources: apply normalizer to the live resource,
437436// calculates the patch performed by normalizer and applies the patch to the target resource
438- func normalizeTargetResources (openAPISchema openapi. Resources , cr * comparisonResult ) ([]* unstructured.Unstructured , error ) {
439- // Normalize live and target resources (cleaning or aligning them)
437+ func normalizeTargetResources (cr * comparisonResult ) ([]* unstructured.Unstructured , error ) {
438+ // normalize live and target resources
440439 normalized , err := diff .Normalize (cr .reconciliationResult .Live , cr .reconciliationResult .Target , cr .diffConfig )
441440 if err != nil {
442441 return nil , err
443442 }
444-
445443 patchedTargets := []* unstructured.Unstructured {}
446-
447444 for idx , live := range cr .reconciliationResult .Live {
448445 normalizedTarget := normalized .Targets [idx ]
449446 if normalizedTarget == nil {
450447 patchedTargets = append (patchedTargets , nil )
451448 continue
452449 }
453- gvk := normalizedTarget .GroupVersionKind ()
454-
455450 originalTarget := cr .reconciliationResult .Target [idx ]
456451 if live == nil {
457- // No live resource, just use target
458452 patchedTargets = append (patchedTargets , originalTarget )
459453 continue
460454 }
461455
462- var (
463- lookupPatchMeta strategicpatch.LookupPatchMeta
464- versionedObject any
465- )
466-
467- // Load patch meta struct or OpenAPI schema for CRDs
468- if versionedObject , err = scheme .Scheme .New (gvk ); err == nil {
469- if lookupPatchMeta , err = strategicpatch .NewPatchMetaFromStruct (versionedObject ); err != nil {
456+ var lookupPatchMeta * strategicpatch.PatchMetaFromStruct
457+ versionedObject , err := scheme .Scheme .New (normalizedTarget .GroupVersionKind ())
458+ if err == nil {
459+ meta , err := strategicpatch .NewPatchMetaFromStruct (versionedObject )
460+ if err != nil {
470461 return nil , err
471462 }
472- } else if crdSchema := openAPISchema .LookupResource (gvk ); crdSchema != nil {
473- lookupPatchMeta = strategicpatch .NewPatchMetaFromOpenAPI (crdSchema )
463+ lookupPatchMeta = & meta
474464 }
475465
476- // Calculate live patch
477466 livePatch , err := getMergePatch (normalized .Lives [idx ], live , lookupPatchMeta )
478467 if err != nil {
479468 return nil , err
480469 }
481470
482- // Apply the patch to the normalized target
483- // This ensures ignored fields in live are restored into the target before syncing
484- normalizedTarget , err = applyMergePatch (normalizedTarget , livePatch , versionedObject , lookupPatchMeta )
471+ normalizedTarget , err = applyMergePatch (normalizedTarget , livePatch , versionedObject )
485472 if err != nil {
486473 return nil , err
487474 }
475+
488476 patchedTargets = append (patchedTargets , normalizedTarget )
489477 }
490-
491478 return patchedTargets , nil
492479}
493480
494481// getMergePatch calculates and returns the patch between the original and the
495482// modified unstructures.
496- func getMergePatch (original , modified * unstructured.Unstructured , lookupPatchMeta strategicpatch.LookupPatchMeta ) ([]byte , error ) {
483+ func getMergePatch (original , modified * unstructured.Unstructured , lookupPatchMeta * strategicpatch.PatchMetaFromStruct ) ([]byte , error ) {
497484 originalJSON , err := original .MarshalJSON ()
498485 if err != nil {
499486 return nil , err
@@ -509,35 +496,18 @@ func getMergePatch(original, modified *unstructured.Unstructured, lookupPatchMet
509496 return jsonpatch .CreateMergePatch (originalJSON , modifiedJSON )
510497}
511498
512- // applyMergePatch will apply the given patch in the obj and return the patched unstructure.
513- func applyMergePatch (obj * unstructured.Unstructured , patch []byte , versionedObject any , meta strategicpatch.LookupPatchMeta ) (* unstructured.Unstructured , error ) {
499+ // applyMergePatch will apply the given patch in the obj and return the patched
500+ // unstructure.
501+ func applyMergePatch (obj * unstructured.Unstructured , patch []byte , versionedObject any ) (* unstructured.Unstructured , error ) {
514502 originalJSON , err := obj .MarshalJSON ()
515503 if err != nil {
516504 return nil , err
517505 }
518506 var patchedJSON []byte
519- switch {
520- case versionedObject != nil :
521- patchedJSON , err = strategicpatch .StrategicMergePatch (originalJSON , patch , versionedObject )
522- case meta != nil :
523- var originalMap , patchMap map [string ]any
524- if err := json .Unmarshal (originalJSON , & originalMap ); err != nil {
525- return nil , err
526- }
527- if err := json .Unmarshal (patch , & patchMap ); err != nil {
528- return nil , err
529- }
530-
531- patchedMap , err := strategicpatch .StrategicMergeMapPatchUsingLookupPatchMeta (originalMap , patchMap , meta )
532- if err != nil {
533- return nil , err
534- }
535- patchedJSON , err = json .Marshal (patchedMap )
536- if err != nil {
537- return nil , err
538- }
539- default :
507+ if versionedObject == nil {
540508 patchedJSON , err = jsonpatch .MergePatch (originalJSON , patch )
509+ } else {
510+ patchedJSON , err = strategicpatch .StrategicMergePatch (originalJSON , patch , versionedObject )
541511 }
542512 if err != nil {
543513 return nil , err
0 commit comments