@@ -22,7 +22,7 @@ import (
2222
2323 "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
2424 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
25- "gopkg.in /yaml.v3"
25+ yaml "sigs.k8s.io /yaml/goyaml .v3"
2626)
2727
2828// Stores some statistics about the results of a run
@@ -61,6 +61,8 @@ const (
6161 WriteBackGit WriteBackMethod = 1
6262)
6363
64+ const defaultIndent = 2
65+
6466// WriteBackConfig holds information on how to write back the changes to an Application
6567type WriteBackConfig struct {
6668 Method WriteBackMethod
@@ -425,6 +427,7 @@ func marshalWithIndent(in interface{}, indent int) (out []byte, err error) {
425427 defer encoder .Close ()
426428 // note: yaml.v3 will only respect indents from 1 to 9 inclusive.
427429 encoder .SetIndent (indent )
430+ encoder .CompactSeqIndent ()
428431 if err = encoder .Encode (in ); err != nil {
429432 return nil , err
430433 }
@@ -434,30 +437,6 @@ func marshalWithIndent(in interface{}, indent int) (out []byte, err error) {
434437 return b .Bytes (), nil
435438}
436439
437- func guessIndent (root * yaml.Node ) int {
438- node := root
439- if root .Kind == yaml .DocumentNode {
440- if len (node .Content ) == 0 {
441- return 2
442- }
443- node = root .Content [0 ]
444- }
445- // anything other than a map at the root makes guessing difficult
446- if node .Kind != yaml .MappingNode || len (node .Content ) == 0 {
447- return 2
448- }
449- // first level map entries that are themselves mappings or sequences,
450- // in block style, and are indented, allow guessing the preferred indent.
451- for i , child := range node .Content {
452- if i % 2 == 1 && child .Column > 1 && child .Column < 10 && child .Style != yaml .FlowStyle {
453- if child .Kind == yaml .MappingNode || child .Kind == yaml .SequenceNode {
454- return child .Column - 1
455- }
456- }
457- }
458- return 2
459- }
460-
461440// marshalParamsOverride marshals the parameter overrides of a given application
462441// into YAML bytes
463442func marshalParamsOverride (app * v1alpha1.Application , originalData []byte ) ([]byte , error ) {
@@ -481,16 +460,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
481460 }
482461
483462 if len (originalData ) == 0 {
484- override , err = marshalWithIndent (newParams , 2 )
463+ override , err = marshalWithIndent (newParams , defaultIndent )
485464 break
486465 }
487466 err = yaml .Unmarshal (originalData , & params )
488467 if err != nil {
489- override , err = marshalWithIndent (newParams , 2 )
468+ override , err = marshalWithIndent (newParams , defaultIndent )
490469 break
491470 }
492471 mergeKustomizeOverride (& params , & newParams )
493- override , err = marshalWithIndent (params , 2 )
472+ override , err = marshalWithIndent (params , defaultIndent )
494473 case ApplicationTypeHelm :
495474 if appSource .Helm == nil {
496475 return []byte {}, nil
@@ -504,7 +483,6 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
504483 if err != nil {
505484 return nil , err
506485 }
507- indent := guessIndent (& helmNewValues )
508486
509487 for _ , c := range images {
510488 if c .ImageAlias == "" {
@@ -546,7 +524,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
546524 }
547525 }
548526
549- override , err = marshalWithIndent (& helmNewValues , indent )
527+ override , err = marshalWithIndent (& helmNewValues , defaultIndent )
550528 } else {
551529 var params helmOverride
552530 newParams := helmOverride {
@@ -559,16 +537,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
559537 log .WithContext ().AddField ("application" , app ).Debugf ("values: '%s'" , outputParams )
560538
561539 if len (originalData ) == 0 {
562- override , err = marshalWithIndent (newParams , 2 )
540+ override , err = marshalWithIndent (newParams , defaultIndent )
563541 break
564542 }
565543 err = yaml .Unmarshal (originalData , & params )
566544 if err != nil {
567- override , err = marshalWithIndent (newParams , 2 )
545+ override , err = marshalWithIndent (newParams , defaultIndent )
568546 break
569547 }
570548 mergeHelmOverride (& params , & newParams )
571- override , err = marshalWithIndent (params , 2 )
549+ override , err = marshalWithIndent (params , defaultIndent )
572550 }
573551 default :
574552 err = fmt .Errorf ("unsupported application type" )
0 commit comments