@@ -21,7 +21,7 @@ import (
21
21
22
22
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
23
23
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
24
- "gopkg.in/yaml.v2 "
24
+ "gopkg.in/yaml.v3 "
25
25
)
26
26
27
27
// Stores some statistics about the results of a run
@@ -459,7 +459,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
459
459
if strings .HasPrefix (app .Annotations [common .WriteBackTargetAnnotation ], common .HelmPrefix ) {
460
460
images := GetImagesAndAliasesFromApplication (app )
461
461
462
- helmNewValues := yaml.MapSlice {}
462
+ helmNewValues := yaml.Node {}
463
463
err = yaml .Unmarshal (originalData , & helmNewValues )
464
464
if err != nil {
465
465
return nil , err
@@ -505,7 +505,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
505
505
}
506
506
}
507
507
508
- override , err = yaml .Marshal (helmNewValues )
508
+ override , err = yaml .Marshal (& helmNewValues )
509
509
} else {
510
510
var params helmOverride
511
511
newParams := helmOverride {
@@ -572,72 +572,94 @@ func mergeKustomizeOverride(t *kustomizeOverride, o *kustomizeOverride) {
572
572
}
573
573
}
574
574
575
- // Check if a key exists in a MapSlice and return its index and value
576
- func findHelmValuesKey (m yaml.MapSlice , key string ) (int , bool ) {
577
- for i , item := range m {
578
- if item .Key == key {
579
- return i , true
575
+ // Check if a key exists in a MappingNode and return the index of its value
576
+ func findHelmValuesKey (m * yaml.Node , key string ) (int , bool ) {
577
+ for i , item := range m . Content {
578
+ if i % 2 == 0 && item .Value == key {
579
+ return i + 1 , true
580
580
}
581
581
}
582
582
return - 1 , false
583
583
}
584
584
585
+ func nodeKindString (k yaml.Kind ) string {
586
+ return map [yaml.Kind ]string {
587
+ yaml .DocumentNode : "DocumentNode" ,
588
+ yaml .SequenceNode : "SequenceNode" ,
589
+ yaml .MappingNode : "MappingNode" ,
590
+ yaml .ScalarNode : "ScalarNode" ,
591
+ yaml .AliasNode : "AliasNode" ,
592
+ }[k ]
593
+ }
594
+
585
595
// set value of the parameter passed from the annotations.
586
- func setHelmValue (currentValues * yaml.MapSlice , key string , value interface {}) error {
596
+ func setHelmValue (currentValues * yaml.Node , key string , value interface {}) error {
597
+ current := currentValues
598
+
599
+ // an unmarshalled document has a DocumentNode at the root, but
600
+ // we navigate from a MappingNode.
601
+ if current .Kind == yaml .DocumentNode {
602
+ current = current .Content [0 ]
603
+ }
604
+
605
+ if current .Kind != yaml .MappingNode {
606
+ return fmt .Errorf ("unexpected type %s for root" , nodeKindString (current .Kind ))
607
+ }
608
+
587
609
// Check if the full key exists
588
- if idx , found := findHelmValuesKey (* currentValues , key ); found {
589
- (* currentValues ) [idx ].Value = value
610
+ if idx , found := findHelmValuesKey (current , key ); found {
611
+ (* current ). Content [idx ].Value = value .( string )
590
612
return nil
591
613
}
592
614
593
615
var err error
594
616
keys := strings .Split (key , "." )
595
- current := currentValues
596
- var parent * yaml.MapSlice
597
- parentIdx := - 1
598
617
599
618
for i , k := range keys {
600
- if idx , found := findHelmValuesKey (* current , k ); found {
619
+ if idx , found := findHelmValuesKey (current , k ); found {
620
+ // Navigate deeper into the map
621
+ current = (* current ).Content [idx ]
622
+ // unpack one level of alias; an alias of an alias is not supported
623
+ if current .Kind == yaml .AliasNode {
624
+ current = current .Alias
625
+ }
601
626
if i == len (keys )- 1 {
602
627
// If we're at the final key, set the value and return
603
- (* current )[idx ].Value = value
604
- return nil
605
- } else {
606
- // Navigate deeper into the map
607
- if nestedMap , ok := (* current )[idx ].Value .(yaml.MapSlice ); ok {
608
- parent = current
609
- parentIdx = idx
610
- current = & nestedMap
628
+ if current .Kind == yaml .ScalarNode {
629
+ current .Value = value .(string )
611
630
} else {
612
- return fmt .Errorf ("unexpected type %T for key %s" , ( * current )[ idx ]. Value , k )
631
+ return fmt .Errorf ("unexpected type %s for key %s" , nodeKindString ( current . Kind ) , k )
613
632
}
633
+ return nil
634
+ } else if current .Kind != yaml .MappingNode {
635
+ return fmt .Errorf ("unexpected type %s for key %s" , nodeKindString (current .Kind ), k )
614
636
}
615
637
} else {
616
- newCurrent := yaml.MapSlice {}
617
- var newParent yaml.MapSlice
618
-
619
638
if i == len (keys )- 1 {
620
- newParent = append (* current , yaml.MapItem {Key : k , Value : value })
621
- } else {
622
- newParent = append (* current , yaml.MapItem {Key : k , Value : newCurrent })
623
- }
624
-
625
- if parent == nil {
626
- * currentValues = newParent
639
+ current .Content = append (current .Content ,
640
+ & yaml.Node {
641
+ Kind : yaml .ScalarNode ,
642
+ Value : k ,
643
+ },
644
+ & yaml.Node {
645
+ Kind : yaml .ScalarNode ,
646
+ Value : value .(string ),
647
+ },
648
+ )
649
+ return nil
627
650
} else {
628
- // if parentIdx has not been set (parent element is also new), set it to the last element
629
- if parentIdx == - 1 {
630
- parentIdx = len (* parent ) - 1
631
- if parentIdx < 0 {
632
- parentIdx = 0
633
- }
634
- }
635
- (* parent )[parentIdx ].Value = newParent
651
+ current .Content = append (current .Content ,
652
+ & yaml.Node {
653
+ Kind : yaml .ScalarNode ,
654
+ Value : k ,
655
+ },
656
+ & yaml.Node {
657
+ Kind : yaml .MappingNode ,
658
+ Content : []* yaml.Node {},
659
+ },
660
+ )
661
+ current = current .Content [len (current .Content )- 1 ]
636
662
}
637
-
638
- parent = & newParent
639
- current = & newCurrent
640
- parentIdx = - 1
641
663
}
642
664
}
643
665
0 commit comments