kubernetes: wrong line number in yaml syntax error message
What happened:
I tried to apply a Kubefile that contained a yaml syntax error.
The following error was returned
failed to decode Kubernetes YAML from /tmp/myfile.yml: yaml: line 13: mapping values are not allowed in this context
The error was not on line 13 of the actual file. The file was a multi resource file, and the error was on line 13 of the 3rd resource.
What you expected to happen:
I expect kubectl to return an actionable error message that points to the place where the syntax error is. Ideally, it would give the actual line where the error is in the file, the resource, and would print the incriminated lines.
How to reproduce it (as minimally and precisely as possible):
Apply a multi resource yaml file with a syntax error that is not on the first resource.
Environment:
- Kubernetes version (use
kubectl version):Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.5", GitCommit:"6b1d87acf3c8253c123756b9e61dac642678305f", GitTreeState:"archive", BuildDate:"2021-03-30T00:00:00Z", GoVersion:"go1.16", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"21+", GitVersion:"v1.21.2-eks-0389ca3", GitCommit:"8a4e27b9d88142bbdd21b997b532eb6d493df6d2", GitTreeState:"clean", BuildDate:"2021-07-31T01:34:46Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
- OS (e.g:
cat /etc/os-release): Fedora 34
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 24 (11 by maintainers)
actually, reading the OP again,
this seems like a problem with the code in apimachinery to split documents: https://github.com/kubernetes/apimachinery/blob/a644435e2c133d990b624858d9c01985d7f59adf/pkg/util/yaml/decoder.go#L128
the code works, but you have a source YAML with N yaml documents separated by
---. if the error is in one of them it reports line number X in that particular document. the user source YAML however, has multiple documents and X may not map.so somehow, the error must include document number N to point the user at the right place.
yaml.go seems the right place. to test the change you should feed erroneous input and see if the error output can be modified. e.g. https://stackoverflow.com/questions/10971621/error-parsing-yaml-file-mapping-values-are-not-allowed-here
but not sure if API machinery would like to have this changed. have you tried asking in k8s slack #sig-api-machinery ?
this is technically an issue that can be logged in the kubernetes-sigs/yaml repository. the cause for the line mismatch is that the library in question converts YAML to JSON for internal usage. on parsing errors (post conversion) the line would map to JSON.
i recall discussions about doing some post processing on the errors to provide a better explanation to the user, but that seemed brittle.
/sig api-machinery