Skip to content

Commit 6fdd5f2

Browse files
author
Wei-Chen Chen
committed
Add imagePullSecrets parsing in helm v2-alpha plugin
1 parent 4d76fe8 commit 6fdd5f2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func (t *HelmTemplater) templateDeploymentFields(yamlContent string) string {
183183
// Template configuration fields
184184
yamlContent = t.templateImageReference(yamlContent)
185185
yamlContent = t.templateEnvironmentVariables(yamlContent)
186+
yamlContent = t.templateImagePullSecrets(yamlContent)
186187
yamlContent = t.templatePodSecurityContext(yamlContent)
187188
yamlContent = t.templateContainerSecurityContext(yamlContent)
188189
yamlContent = t.templateResources(yamlContent)
@@ -325,6 +326,57 @@ func (t *HelmTemplater) templateVolumes(yamlContent string) string {
325326
return yamlContent
326327
}
327328

329+
// templateImagePullSecrets exposes imagePullSecrets via values.yaml
330+
func (t *HelmTemplater) templateImagePullSecrets(yamlContent string) string {
331+
if !strings.Contains(yamlContent, "imagePullSecrets:") {
332+
return yamlContent
333+
}
334+
335+
lines := strings.Split(yamlContent, "\n")
336+
for i := 0; i < len(lines); i++ {
337+
if strings.TrimSpace(lines[i]) != "imagePullSecrets:" {
338+
continue
339+
}
340+
indentStr, indentLen := leadingWhitespace(lines[i])
341+
end := i + 1
342+
for ; end < len(lines); end++ {
343+
trimmed := strings.TrimSpace(lines[end])
344+
if trimmed == "" {
345+
break
346+
}
347+
lineIndent := len(lines[end]) - len(strings.TrimLeft(lines[end], " \t"))
348+
if lineIndent <= indentLen {
349+
break
350+
}
351+
}
352+
353+
if end >= len(lines) {
354+
break
355+
}
356+
357+
if i+1 < len(lines) && strings.Contains(lines[i+1], ".Values.manager.imagePullSecrets") {
358+
return yamlContent
359+
}
360+
361+
childIndent := indentStr + " "
362+
childIndentWidth := strconv.Itoa(len(childIndent))
363+
364+
block := []string{
365+
indentStr + "{{- if .Values.manager.imagePullSecrets }}",
366+
indentStr + "imagePullSecrets:",
367+
childIndent + "{{- toYaml .Values.manager.imagePullSecrets | nindent " + childIndentWidth + " }}",
368+
indentStr + "{{- end }}",
369+
}
370+
371+
newLines := append([]string{}, lines[:i]...)
372+
newLines = append(newLines, block...)
373+
newLines = append(newLines, lines[end:]...)
374+
return strings.Join(newLines, "\n")
375+
}
376+
377+
return yamlContent
378+
}
379+
328380
// templatePodSecurityContext exposes podSecurityContext via values.yaml
329381
func (t *HelmTemplater) templatePodSecurityContext(yamlContent string) string {
330382
if !strings.Contains(yamlContent, "securityContext:") {

0 commit comments

Comments
 (0)