Home > Mobile >  Creating a configMap from file is critical to file type and lines count in the file
Creating a configMap from file is critical to file type and lines count in the file

Time:05-27

I have a template that creates a configMap from the jmeter-test-data-file-configmap.yaml file in helm chart:

{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: jmeter-testdata
data:
  {{ .Values.env.datafile }}: |-
  {{ .Files.Get .Values.env.datafile | indent 4}}
{{- end }}

the corresponding yaml.configuration is:

env:
  testfile: sample
  datafile: example.csv

When I do the helm upgrade, everything is ok, if the example.csv is a single line text file like that:

1,1

but is it would have at least two or even more lines

1,1
2,2

the deployment fails with unrelated error message:

upgrade.go:123: [debug] preparing upgrade for jmeter
Error: UPGRADE FAILED: YAML parse error on jmeter/templates/jmeter-test-data-file-configmap.yaml: error converting YAML to JSON: yaml: line 7: did not find expected key
helm.go:81: [debug] error converting YAML to JSON: yaml: line 7: did not find expected key
YAML parse error on jmeter/templates/jmeter-test-data-file-configmap.yaml

and the debug details are:

helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort
        /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:146
helm.sh/helm/v3/pkg/releaseutil.SortManifests
        /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:106
helm.sh/helm/v3/pkg/action.(*Configuration).renderResources
        /home/circleci/helm.sh/helm/pkg/action/action.go:165
helm.sh/helm/v3/pkg/action.(*Upgrade).prepareUpgrade
        /home/circleci/helm.sh/helm/pkg/action/upgrade.go:215
helm.sh/helm/v3/pkg/action.(*Upgrade).Run
        /home/circleci/helm.sh/helm/pkg/action/upgrade.go:124
main.newUpgradeCmd.func2
        /home/circleci/helm.sh/helm/cmd/helm/upgrade.go:155
github.com/spf13/cobra.(*Command).execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:850
github.com/spf13/cobra.(*Command).ExecuteC
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:958
github.com/spf13/cobra.(*Command).Execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:895
main.main
        /home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
        /usr/local/go/src/runtime/proc.go:204
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1374
UPGRADE FAILED
main.newUpgradeCmd.func2
        /home/circleci/helm.sh/helm/cmd/helm/upgrade.go:157
github.com/spf13/cobra.(*Command).execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:850
github.com/spf13/cobra.(*Command).ExecuteC
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:958
github.com/spf13/cobra.(*Command).Execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:895
main.main
        /home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
        /usr/local/go/src/runtime/proc.go:204
runtime.goexit

So, the error points to this line:

  {{ .Values.env.datafile }}: |-

What could be wrong here and how can the file itself hurt the template processing because of its new lines? We tired /r/n, /n, /r with no success, all the same

We also tried to hadcode the line:7 like that:

{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: jmeter-testdata
data:
  example.csv: |-
  {{ .Files.Get .Values.env.datafile | indent 4}}
{{- end }}

and still the same error. And even to replace the template value with the hardcode:

{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: jmeter-testdata
data:
  {{ (printf "example.csv" ) }}: |-
  {{ .Files.Get (printf "example.csv" ) | indent 4}}
{{- end }}

But still the same error.

And that doesn't happens with almost the same config, that bring the multiline xml file to the configMap exactly the same way:

apiVersion: v1
kind: ConfigMap
metadata:
  name: jmeter-test
data:
  test.jmx: |-
{{ .Files.Get (printf "%s.jmx" .Values.env.testfile ) | indent 4}}

I suppose, that for some reason, probaby the intend is't applied to all lines for csv file, when it is applied to the jmx file. But how to figure that out ad how to work it around?

UPD:

1, 1
    2, 2
    3, 3

"intending" files manually like above made the trick, it was deployed, however this file isn't ok to use for the needs then. How to avoid that?

CodePudding user response:

If you have a line containing indent it probably needs to begin at the start of the line, even if it's in otherwise indented context.

{{- if .Values.env.datafile }}
...
data:
  {{ .Values.env.datafile }}: |-
{{ .Files.Get .Values.env.datafile | indent 4}}
{{/*- previous line is not indented */}}
{{- end }}

In your original example, let's focus on these two lines:

  {{ .Values.env.datafile }}: |-
  {{ .Files.Get .Values.env.datafile | indent 4}}
## (these two spaces are important)

Now, if the input line is your second example

1,1
2,2

Now: the line containing indent 4 is itself indented by 2 spaces. So in that line you have the two spaces at the start of the line, plus the four spaces from indent 4, so 6 spaces; then a newline and four spaces from indent 4, but no start-of-line spaces (you're still in the output from indent), so only 4 spaces.

  test.jmx: |-
      1,1
    2,2

If you run helm template --debug on your original chart, you will still get the YAML parsing error, but it should also print out this output.

In some contexts you may find it slightly more aesthetic to use nindent, which includes a newline before the first line's indentation, in combination with a - just inside the curly braces to consume the preceding whitespace (including both spaces and newlines). This should also work:

data:
  {{ .Values.env.datafile }}: |-
  {{- .Files.Get .Values.env.datafile | nindent 4}}
  {{/*- indented, starts with -, uses nindent */}}

But also for example:

metadata:
  labels: {{- include "common.labels" . | nindent 4 }}
  annotations: {{- include "common.annotations" . | nindent 4 }}

CodePudding user response:

{{ .Files.Get (printf "%s" .Values.env.datafile ) | nindent 4}}

instead of

{{ .Files.Get (printf "%s" .Values.env.datafile ) | indent 4}}

has fixed the problem.

But for me it is still unclear why the problem has been arisen for the csv file, while wasn't there for the XML files. Is it all due to the spaces?

  • Related