kubernetes: yaml may fail to parse spuriously

What happened: JSON with comments in it (IE Yaml) will be “detected” as JSON because it matches ^\s+{.* in the first 100 bytes and be parsed as JSON even though it is valid YAML instead.

What you expected to happen: We should parse valid YAML as YAML. We should probably parse everything as YAML since it is a superset of JSON.

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

The code is here:

https://github.com/kubernetes/kubernetes/blob/14e322cc827ca546eb80b721a825a43311a99437/pkg/util/yaml/decoder.go#L202

https://github.com/kubernetes/kubernetes/blob/14e322cc827ca546eb80b721a825a43311a99437/pkg/util/yaml/decoder.go#L305-L327

Thread in slack: https://kubernetes.slack.com/archives/C13J86Z63/p1560351860082800

Environment: n/a

cc @smarterclayton @dims @liggitt @Katharine @chuckha /sig apimachinery

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (12 by maintainers)

Most upvoted comments

We should probably parse everything as YAML since it is a superset of JSON.

we need to be super sure that doesn’t regress performance or correctness (some yaml-related normalization issues at https://github.com/kubernetes/kubernetes/issues/73655 and https://github.com/kubernetes-sigs/yaml/pull/14)

For the sake of completeness, here is a valid yaml file that won’t work:

{
    # this is the deck service
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "deck",
        "namespace": "default",
    },
    "spec": {
        "selector": {
            "app": "deck",
        },
        "ports": [
            {
                "port": 80,
                "targetPort": 8080,
            },
        ],
        "type": "NodePort",
    },
}

This isn’t valid JSON because it has a comment and trailing commas, but it is valid yaml, and should be treated as such.