kubectl on on Powershell gives a string representation of a custom object created from JSON

202 views Asked by At

I ran into this weird issue and wonder if how to resolve it.

On my Windows machine running Powershell 5.1.19041.1682 (major.minor.build.revision), this command below gives me a proper JSON.

PS C:\workspace\rapidresponse\jenkinslib-ds\resources\scripts> kubectl get accountquota account-name-quota -o jsonpath="{.status.total}"

{"hard":{"limits.cpu":"256","limits.memory":"256G","pods":"200","requests.cpu":"64","requests.memory":"128G"},"used":{"limits.cpu":"28","limits.memory":"73Gi","pods":"13","requests.cpu":"28","requests.memory":"73Gi"}}

Edit: The kubectl version here is 1.24.1

However, on another machine (a Windows machine as well running an older version 5.1.14393.2636), I got a completely different result.

PS C:\Users\sa_a9_dst_validation> kubectl get accountquota account-name-quota -o jsonpath="{.status.total}"

map[hard:map[limits.cpu:256 limits.memory:256G pods:200 requests.cpu:64 requests.memory:128G] used:map[limits.cpu:27 lim
its.memory:72Gi pods:12 requests.cpu:27 requests.memory:72Gi]]

Edit: The version of kubectl of this machine is 1.18.2

The result looks like the JSON is internally converted into a PS Custom Object and ​ToString()​ is called on it before the result is returned.

My goal

I am trying to convert the JSON result into an object, i.e. using ConvertFrom-Json. Having a strange string like that forces me to write a custom parser for it and it is not what I want to do. Do you know of a reason for why it behaves that way? If so, is there a way to use existing Powershell functions to do so?

1

There are 1 answers

2
Phuong Hoang On

It turns out that kubectl version 1.18.2 is indeed the one that causes the issue. When I downgraded my kubectl version from 1.24.2 to 1.18.2, I could reproduce the issue.

Thanks @@mclayton, @MathiasR.Jessen, and @mklement0