Skip to content

Commit 57ce1af

Browse files
author
Robin LIORET
committed
feat: helm v2-alpha: add nodeSelector, tolerations and affinity to the helm templater
1 parent c2618cf commit 57ce1af

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

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

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +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)
193196

194197
return yamlContent
195198
}
@@ -620,6 +623,141 @@ func (t *HelmTemplater) templateImageReference(yamlContent string) string {
620623
return yamlContent
621624
}
622625

626+
func (t *HelmTemplater) templateNodeSelector(yamlContent string) string {
627+
if !strings.Contains(yamlContent, "nodeSelector:") {
628+
return yamlContent
629+
}
630+
lines := strings.Split(yamlContent, "\n")
631+
for i := 0; i < len(lines); i++ {
632+
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "nodeSelector") {
633+
continue
634+
}
635+
end := i + 1
636+
trimmed := strings.TrimSpace(lines[i])
637+
if len(trimmed) == len("nodeSelector:") {
638+
_, indentLen := leadingWhitespace(lines[i])
639+
for j := end; j < len(lines); j++ {
640+
_, indentLenLine := leadingWhitespace(lines[j])
641+
if indentLenLine <= indentLen {
642+
end = j
643+
break
644+
}
645+
}
646+
}
647+
648+
indentStr, indentLen := leadingWhitespace(lines[i])
649+
650+
var builder strings.Builder
651+
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))
657+
builder.WriteString(" }}\n")
658+
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 ")
746+
builder.WriteString(strconv.Itoa(indentLen + 2))
747+
builder.WriteString(" }}\n")
748+
builder.WriteString(indentStr)
749+
builder.WriteString("{{- end }}\n")
750+
751+
newBlock := strings.TrimRight(builder.String(), "\n")
752+
753+
newLines := append([]string{}, lines[:i]...)
754+
newLines = append(newLines, strings.Split(newBlock, "\n")...)
755+
newLines = append(newLines, lines[end:]...)
756+
return strings.Join(newLines, "\n")
757+
}
758+
return yamlContent
759+
}
760+
623761
// makeWebhookAnnotationsConditional makes only cert-manager annotations conditional, not the entire webhook
624762
func (t *HelmTemplater) makeWebhookAnnotationsConditional(yamlContent string) string {
625763
// Find cert-manager.io/inject-ca-from annotation and make it conditional

0 commit comments

Comments
 (0)