Skip to content

Commit 62f14cf

Browse files
robinlioretRobin LIORET
authored andcommitted
feat: helm v2-alpha: refactorize helm templater for the basic with statement
Signed-off-by: Robin LIORET <robin.lioret@darylsocialsoftware.com>
1 parent c9d6836 commit 62f14cf

File tree

1 file changed

+12
-99
lines changed

1 file changed

+12
-99
lines changed

pkg/plugins/optional/helm/v2alpha/scaffolds/internal/kustomize/helm_templater.go

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -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.templateNodeSelector(yamlContent)
194-
yamlContent = t.templateAffinity(yamlContent)
195-
yamlContent = t.templateTolerations(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")
196196

197197
return yamlContent
198198
}
@@ -623,18 +623,19 @@ func (t *HelmTemplater) templateImageReference(yamlContent string) string {
623623
return yamlContent
624624
}
625625

626-
func (t *HelmTemplater) templateNodeSelector(yamlContent string) string {
627-
if !strings.Contains(yamlContent, "nodeSelector:") {
626+
func (t *HelmTemplater) templateBasicWithStatement(yamlContent string, key string, valuePath string) string {
627+
yamlKey := fmt.Sprintf("%s:", key)
628+
if !strings.Contains(yamlContent, yamlKey) {
628629
return yamlContent
629630
}
630631
lines := strings.Split(yamlContent, "\n")
631632
for i := 0; i < len(lines); i++ {
632-
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "nodeSelector") {
633+
if !strings.HasPrefix(strings.TrimSpace(lines[i]), key) {
633634
continue
634635
}
635636
end := i + 1
636637
trimmed := strings.TrimSpace(lines[i])
637-
if len(trimmed) == len("nodeSelector:") {
638+
if len(trimmed) == len(yamlKey) {
638639
_, indentLen := leadingWhitespace(lines[i])
639640
for j := end; j < len(lines); j++ {
640641
_, indentLenLine := leadingWhitespace(lines[j])
@@ -649,100 +650,12 @@ func (t *HelmTemplater) templateNodeSelector(yamlContent string) string {
649650

650651
var builder strings.Builder
651652
builder.WriteString(indentStr)
652-
builder.WriteString("{{- with .Values.manager.nodeSelector }}\n")
653-
builder.WriteString(indentStr)
654-
builder.WriteString("nodeSelector: ")
655-
builder.WriteString("{{ toYaml . | nindent ")
656-
builder.WriteString(strconv.Itoa(indentLen + 2))
653+
builder.WriteString("{{- with ")
654+
builder.WriteString(valuePath)
657655
builder.WriteString(" }}\n")
658656
builder.WriteString(indentStr)
659-
builder.WriteString("{{- end }}\n")
660-
661-
newBlock := strings.TrimRight(builder.String(), "\n")
662-
663-
newLines := append([]string{}, lines[:i]...)
664-
newLines = append(newLines, strings.Split(newBlock, "\n")...)
665-
newLines = append(newLines, lines[end:]...)
666-
return strings.Join(newLines, "\n")
667-
}
668-
return yamlContent
669-
}
670-
671-
func (t *HelmTemplater) templateAffinity(yamlContent string) string {
672-
if !strings.Contains(yamlContent, "affinity:") {
673-
return yamlContent
674-
}
675-
lines := strings.Split(yamlContent, "\n")
676-
for i := 0; i < len(lines); i++ {
677-
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "affinity") {
678-
continue
679-
}
680-
end := i + 1
681-
trimmed := strings.TrimSpace(lines[i])
682-
if len(trimmed) == len("affinity:") {
683-
_, indentLen := leadingWhitespace(lines[i])
684-
for j := end; j < len(lines); j++ {
685-
_, indentLenLine := leadingWhitespace(lines[j])
686-
if indentLenLine <= indentLen {
687-
end = j
688-
break
689-
}
690-
}
691-
}
692-
693-
indentStr, indentLen := leadingWhitespace(lines[i])
694-
695-
var builder strings.Builder
696-
builder.WriteString(indentStr)
697-
builder.WriteString("{{- with .Values.manager.affinity }}\n")
698-
builder.WriteString(indentStr)
699-
builder.WriteString("affinity: ")
700-
builder.WriteString("{{ toYaml . | nindent ")
701-
builder.WriteString(strconv.Itoa(indentLen + 2))
702-
builder.WriteString(" }}\n")
703-
builder.WriteString(indentStr)
704-
builder.WriteString("{{- end }}\n")
705-
706-
newBlock := strings.TrimRight(builder.String(), "\n")
707-
708-
newLines := append([]string{}, lines[:i]...)
709-
newLines = append(newLines, strings.Split(newBlock, "\n")...)
710-
newLines = append(newLines, lines[end:]...)
711-
return strings.Join(newLines, "\n")
712-
}
713-
return yamlContent
714-
}
715-
716-
func (t *HelmTemplater) templateTolerations(yamlContent string) string {
717-
if !strings.Contains(yamlContent, "tolerations:") {
718-
return yamlContent
719-
}
720-
lines := strings.Split(yamlContent, "\n")
721-
for i := 0; i < len(lines); i++ {
722-
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "tolerations") {
723-
continue
724-
}
725-
end := i + 1
726-
trimmed := strings.TrimSpace(lines[i])
727-
if len(trimmed) == len("tolerations:") {
728-
_, indentLen := leadingWhitespace(lines[i])
729-
for j := end; j < len(lines); j++ {
730-
_, indentLenLine := leadingWhitespace(lines[j])
731-
if indentLenLine <= indentLen {
732-
end = j
733-
break
734-
}
735-
}
736-
}
737-
738-
indentStr, indentLen := leadingWhitespace(lines[i])
739-
740-
var builder strings.Builder
741-
builder.WriteString(indentStr)
742-
builder.WriteString("{{- with .Values.manager.tolerations }}\n")
743-
builder.WriteString(indentStr)
744-
builder.WriteString("tolerations: ")
745-
builder.WriteString("{{ toYaml . | nindent ")
657+
builder.WriteString(yamlKey)
658+
builder.WriteString(" {{ toYaml . | nindent ")
746659
builder.WriteString(strconv.Itoa(indentLen + 2))
747660
builder.WriteString(" }}\n")
748661
builder.WriteString(indentStr)

0 commit comments

Comments
 (0)