Skip to content

Commit 20bf33c

Browse files
committed
add tests and make generate-charts
1 parent 1641876 commit 20bf33c

File tree

10 files changed

+76
-9
lines changed

10 files changed

+76
-9
lines changed

docs/book/src/cronjob-tutorial/testdata/project/dist/chart/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ manager:
1414
# Environment variables
1515
env: []
1616

17+
# Image pull secrets
18+
imagePullSecrets: []
19+
1720
# Pod-level security settings
1821
podSecurityContext:
1922
runAsNonRoot: true

docs/book/src/getting-started/testdata/project/dist/chart/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ manager:
1414
# Environment variables
1515
env: []
1616

17+
# Image pull secrets
18+
imagePullSecrets: []
19+
1720
# Pod-level security settings
1821
podSecurityContext:
1922
runAsNonRoot: true

docs/book/src/multiversion-tutorial/testdata/project/dist/chart/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ manager:
1414
# Environment variables
1515
env: []
1616

17+
# Image pull secrets
18+
imagePullSecrets: []
19+
1720
# Pod-level security settings
1821
podSecurityContext:
1922
runAsNonRoot: true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (c *ChartConverter) ExtractDeploymentConfig() map[string]interface{} {
105105
}
106106

107107
extractPodSecurityContext(specMap, config)
108-
extractPodImagePullSecrets(specMap, config)
108+
extractImagePullSecrets(specMap, config)
109109
container := firstManagerContainer(specMap)
110110
if container == nil {
111111
return config
@@ -135,7 +135,7 @@ func extractDeploymentSpec(deployment *unstructured.Unstructured) map[string]int
135135
return specMap
136136
}
137137

138-
func extractPodImagePullSecrets(specMap map[string]interface{}, config map[string]interface{}) {
138+
func extractImagePullSecrets(specMap map[string]interface{}, config map[string]interface{}) {
139139
imagePullSecrets, found, err := unstructured.NestedFieldNoCopy(specMap, "imagePullSecrets")
140140
if !found || err != nil {
141141
return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ var _ = Describe("ChartConverter", func() {
274274
Expect(config["webhookPort"]).To(Equal(9444))
275275
})
276276

277-
It("should extract image pull secrets", func() {
277+
It("should extract imagePullSecrets", func() {
278278
// Set up deployment with image pull secrets
279279
containers := []interface{}{
280280
map[string]interface{}{

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ func (t *HelmTemplater) templateImagePullSecrets(yamlContent string) string {
334334

335335
lines := strings.Split(yamlContent, "\n")
336336
for i := 0; i < len(lines); i++ {
337-
if strings.TrimSpace(lines[i]) != "imagePullSecrets:" {
337+
// Use prefix to allow `imagePullSecrets: []` to be preserved
338+
if !strings.HasPrefix(strings.TrimSpace(lines[i]), "imagePullSecrets:") {
338339
continue
339340
}
340341
indentStr, indentLen := leadingWhitespace(lines[i])
@@ -345,13 +346,12 @@ func (t *HelmTemplater) templateImagePullSecrets(yamlContent string) string {
345346
break
346347
}
347348
lineIndent := len(lines[end]) - len(strings.TrimLeft(lines[end], " \t"))
348-
if lineIndent <= indentLen {
349+
if lineIndent < indentLen {
350+
break
351+
}
352+
if lineIndent == indentLen && !strings.HasPrefix(trimmed, "-") {
349353
break
350354
}
351-
}
352-
353-
if end >= len(lines) {
354-
break
355355
}
356356

357357
if i+1 < len(lines) && strings.Contains(lines[i+1], ".Values.manager.imagePullSecrets") {

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,58 @@ metadata:
230230
Expect(result).NotTo(ContainSubstring(`{{- include "chart.labels"`))
231231
Expect(result).NotTo(ContainSubstring(`{{- include "chart.annotations"`))
232232
})
233+
234+
It("should template imagePullSecrets", func() {
235+
deploymentResource := &unstructured.Unstructured{}
236+
deploymentResource.SetAPIVersion("apps/v1")
237+
deploymentResource.SetKind("Deployment")
238+
deploymentResource.SetName("test-project-controller-manager")
239+
240+
content := `apiVersion: apps/v1
241+
kind: Deployment
242+
spec:
243+
template:
244+
spec:
245+
imagePullSecrets:
246+
- name: test-secret
247+
containers:
248+
- args:
249+
- --metrics-bind-address=:8443
250+
- --health-probe-bind-address=:8081
251+
- --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs/tls.crt
252+
- --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs/tls.crt
253+
- --leader-elect`
254+
255+
result := templater.ApplyHelmSubstitutions(content, deploymentResource)
256+
257+
Expect(result).To(ContainSubstring("imagePullSecrets:"))
258+
Expect(result).NotTo(ContainSubstring("test-secret"))
259+
})
260+
261+
It("should template empty imagePullSecrets", func() {
262+
deploymentResource := &unstructured.Unstructured{}
263+
deploymentResource.SetAPIVersion("apps/v1")
264+
deploymentResource.SetKind("Deployment")
265+
deploymentResource.SetName("test-project-controller-manager")
266+
267+
content := `apiVersion: apps/v1
268+
kind: Deployment
269+
spec:
270+
template:
271+
spec:
272+
imagePullSecrets: []
273+
containers:
274+
- args:
275+
- --metrics-bind-address=:8443
276+
- --health-probe-bind-address=:8081
277+
- --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs/tls.crt
278+
- --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs/tls.crt
279+
- --leader-elect`
280+
281+
result := templater.ApplyHelmSubstitutions(content, deploymentResource)
282+
283+
Expect(result).To(ContainSubstring("imagePullSecrets:"))
284+
})
233285
})
234286

235287
Context("conditional wrapping", func() {

testdata/project-v4-with-plugins/config/manager/manager.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ spec:
5757
runAsNonRoot: true
5858
seccompProfile:
5959
type: RuntimeDefault
60+
imagePullSecrets: []
6061
containers:
6162
- command:
6263
- /manager

testdata/project-v4-with-plugins/dist/chart/templates/manager/manager.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ spec:
8181
name: webhook-certs
8282
readOnly: true
8383
{{- end }}
84+
{{- if .Values.manager.imagePullSecrets }}
85+
imagePullSecrets:
86+
{{- toYaml .Values.manager.imagePullSecrets | nindent 14 }}
87+
{{- end }}
8488
securityContext:
8589
{{- if .Values.manager.podSecurityContext }}
8690
{{- toYaml .Values.manager.podSecurityContext | nindent 14 }}

testdata/project-v4-with-plugins/dist/install.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ spec:
10261026
- mountPath: /tmp/k8s-webhook-server/serving-certs
10271027
name: webhook-certs
10281028
readOnly: true
1029+
imagePullSecrets: []
10291030
securityContext:
10301031
runAsNonRoot: true
10311032
seccompProfile:

0 commit comments

Comments
 (0)