@@ -190,9 +190,9 @@ func (t *HelmTemplater) templateDeploymentFields(yamlContent string) string {
190190 yamlContent = t .templateVolumeMounts (yamlContent )
191191 yamlContent = t .templateVolumes (yamlContent )
192192 yamlContent = t .templateControllerManagerArgs (yamlContent )
193- yamlContent = t .templateBasicWithStatement (yamlContent , "nodeSelector" , ".Values.manager.nodeSelector" )
194- yamlContent = t .templateBasicWithStatement (yamlContent , "affinity" , ".Values.manager.affinity" )
195- yamlContent = t .templateBasicWithStatement (yamlContent , "tolerations" , ".Values.manager.tolerations" )
193+ yamlContent = t .templateBasicWithStatement (yamlContent , "nodeSelector" , "spec.template.spec" , " .Values.manager.nodeSelector" )
194+ yamlContent = t .templateBasicWithStatement (yamlContent , "affinity" , "spec.template.spec" , " .Values.manager.affinity" )
195+ yamlContent = t .templateBasicWithStatement (yamlContent , "tolerations" , "spec.template.spec" , " .Values.manager.tolerations" )
196196
197197 return yamlContent
198198}
@@ -623,52 +623,89 @@ func (t *HelmTemplater) templateImageReference(yamlContent string) string {
623623 return yamlContent
624624}
625625
626- func (t * HelmTemplater ) templateBasicWithStatement (yamlContent string , key string , valuePath string ) string {
626+ func (t * HelmTemplater ) templateBasicWithStatement (yamlContent string , key string , parentKey string , valuePath string ) string {
627+ fmt .Printf ("========================\n " )
628+
629+ lines := strings .Split (yamlContent , "\n " )
627630 yamlKey := fmt .Sprintf ("%s:" , key )
631+
632+ var start , end int
633+ var indentLen int
628634 if ! strings .Contains (yamlContent , yamlKey ) {
629- return yamlContent
630- }
631- lines := strings .Split (yamlContent , "\n " )
632- for i := 0 ; i < len (lines ); i ++ {
633- if ! strings .HasPrefix (strings .TrimSpace (lines [i ]), key ) {
634- continue
635+ // Find parent block start if the key is missing
636+ fmt .Printf ("Looking for the parent key\n " )
637+ pKeyParts := strings .Split (parentKey , "." )
638+ pKeyIdx := 0
639+ pKeyInit := false
640+ currIndent := 0
641+ for i := 0 ; i < len (lines ); i ++ {
642+ _ , lineIndent := leadingWhitespace (lines [i ])
643+ if pKeyInit && lineIndent <= currIndent {
644+ fmt .Printf ("Parent key not found\n " , start )
645+ return yamlContent
646+ }
647+ if ! strings .HasPrefix (strings .TrimSpace (lines [i ]), pKeyParts [pKeyIdx ]) {
648+ continue
649+ }
650+
651+ // Parent key part found
652+ pKeyIdx ++
653+ pKeyInit = true
654+ if pKeyIdx >= len (pKeyParts ) {
655+ start = i + 1
656+ end = start
657+ break
658+ }
635659 }
636- end := i + 1
637- trimmed := strings .TrimSpace (lines [i ])
638- if len (trimmed ) == len (yamlKey ) {
639- _ , indentLen := leadingWhitespace (lines [i ])
640- for j := end ; j < len (lines ); j ++ {
641- _ , indentLenLine := leadingWhitespace (lines [j ])
642- if indentLenLine <= indentLen {
643- end = j
644- break
660+ _ , indentLen = leadingWhitespace (lines [start ])
661+ } else {
662+ // Find the existing block
663+ fmt .Printf ("Looking for the existing block\n " )
664+ for i := 0 ; i < len (lines ); i ++ {
665+ if ! strings .HasPrefix (strings .TrimSpace (lines [i ]), key ) {
666+ continue
667+ }
668+ start = i
669+ end = i + 1
670+ trimmed := strings .TrimSpace (lines [i ])
671+ if len (trimmed ) == len (yamlKey ) {
672+ _ , indentLen := leadingWhitespace (lines [i ])
673+ for j := end ; j < len (lines ); j ++ {
674+ _ , indentLenLine := leadingWhitespace (lines [j ])
675+ if indentLenLine <= indentLen {
676+ end = j
677+ break
678+ }
645679 }
646680 }
647681 }
682+ _ , indentLen = leadingWhitespace (lines [start ])
683+ }
648684
649- indentStr , indentLen := leadingWhitespace (lines [i ])
685+ fmt .Printf ("Start: %d\n " , start )
686+ fmt .Printf ("End: %d\n " , end )
650687
651- var builder strings.Builder
652- builder .WriteString (indentStr )
653- builder .WriteString ("{{- with " )
654- builder .WriteString (valuePath )
655- builder .WriteString (" }}\n " )
656- builder .WriteString (indentStr )
657- builder .WriteString (yamlKey )
658- builder .WriteString (" {{ toYaml . | nindent " )
659- builder .WriteString (strconv .Itoa (indentLen + 2 ))
660- builder .WriteString (" }}\n " )
661- builder .WriteString (indentStr )
662- builder .WriteString ("{{- end }}\n " )
688+ indentStr := strings .Repeat (" " , indentLen )
663689
664- newBlock := strings .TrimRight (builder .String (), "\n " )
690+ var builder strings.Builder
691+ builder .WriteString (indentStr )
692+ builder .WriteString ("{{- with " )
693+ builder .WriteString (valuePath )
694+ builder .WriteString (" }}\n " )
695+ builder .WriteString (indentStr )
696+ builder .WriteString (yamlKey )
697+ builder .WriteString (" {{ toYaml . | nindent " )
698+ builder .WriteString (strconv .Itoa (indentLen + 4 ))
699+ builder .WriteString (" }}\n " )
700+ builder .WriteString (indentStr )
701+ builder .WriteString ("{{- end }}\n " )
665702
666- newLines := append ([] string {}, lines [: i ] ... )
667- newLines = append ( newLines , strings . Split ( newBlock , " \n " ) ... )
668- newLines = append (newLines , lines [end : ]... )
669- return strings .Join ( newLines , "\n " )
670- }
671- return yamlContent
703+ newBlock := strings . TrimRight ( builder . String (), " \n " )
704+
705+ newLines : = append ([] string {} , lines [: start ]... )
706+ newLines = append ( newLines , strings .Split ( newBlock , "\n " ) ... )
707+ newLines = append ( newLines , lines [ end :] ... )
708+ return strings . Join ( newLines , " \n " )
672709}
673710
674711// makeWebhookAnnotationsConditional makes only cert-manager annotations conditional, not the entire webhook
0 commit comments