Search code examples
kubernetesjqmetrics

Making request jq to output memory and container


how to make request to output coantainer name and how much memory and cpu usage.

\"container\":{\"memory\":{\"request\":{\"bytes\":6.142515354e+08}},\"name\":\"testcontainer\",\"cpu\":{\"request\":{\"cores\":0.3}}

Solution

  • Your sample input is a bit of a mess. If you really have to deal with something like that, the best would be to modify the beginning and end of the string, to make it parseable as a JSON string:

    "{ \"container\":{\"memory\":{\"request\":{\"bytes\":6.142515354e+08}},\"name\":\"testcontainer\",\"cpu\":{\"request\":{\"cores\":0.3}}}}"
    

    Then you can run jq on it, e.g.:

    < kubernetes.txt jq fromjson
    

    yields:

    {
      "container": {
        "memory": {
          "request": {
            "bytes": 614251535.4
          }
        },
        "name": "testcontainer",
        "cpu": {
          "request": {
            "cores": 0.3
          }
        }
      }
    }
    

    Now it's easy to write the queries to get the details you want, e.g.

    < kubernetes.txt jq fromjson.container.memory.request