Search code examples
powershellsyntaxparameter-passingkubectlquoting

'kubectl patch' works on Linux Bash but not in Windows Powershell ISE


The following command works fine on Ubuntu bash:

kubectl patch deployment wapi-backend-d1 --patch '{"spec": {"template": {"metadata": {"labels": {"date": "test"}}}}}'

The same command does not work in Windows Powershell Console (ISE).

The error is:

kubectl : Error from server (BadRequest): invalid character 's' looking for beginning of object key string
At line:1 char:1
+ kubectl patch deployment wapi-backend-d1 --patch '{"spec": {"template ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error from serv...ject key string:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

The powershell console version is:

PS > $PSVersionTable

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      5.1.14409.1005                                                                                                                                                                                                                    
PSEdition                      Desktop                                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                           
BuildVersion                   10.0.14409.1005                                                                                                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1            

I have tried the command with a different patched value too as I saw somebody write that patch may fail if it is already applied.

The path /spec/template/metadata/labels/date indeed exists in the deployment's yaml, so that isn't a problem either.

I presume that it might have something to do with kubectl working differently in Powershell in relation to quotes, but could not find a way to make it work.

I have tried

kubectl patch deployment wapi-backend-d1 --patch "{\"spec\": {\"template\": {\"metadata\": {\"labels\": {\"date\": \"test123\"}}}}}"

But that results in

Error from server (NotFound): deployments.extensions "spec\\: {\\template\\: {\\metadata\\: {\\labels\\: {\\date\\: \\test123\\}}}}}" not found

What should be the command on Powershell?


Solution

  • For detailed and very useful background, see the answer by mklement0

    After much frustration, I have decided to list all variants of quote escaping that I've tried, and came up with one more, which suddenly worked! So, sharing it here:

    kubectl patch deployment wapi-backend-d1 --patch '{\"spec\": {\"template\": {\"metadata\": {\"labels\": {\"date\": \"test123\"}}}}}'
    

    This is how to use kubectl patch with Powershell

    Also, of note: I was actually trying to patch it with a timestamp to trigger a rolling update without changing tags of container images (so set image would not help me).

    When you try to put your JSON into a variable and then call kubectl patch with a variable, you get into trouble with escaping again. This is what I ended up with:

    $patchRequest = @{
        spec = @{
            template = @{
                metadata = @{
                    labels = @{
                        date = ((((Get-Date -Format o)).replace(':','-').replace('+','_')))
                    }
                }
            }
        }
    }
    $patchJson = ((ConvertTo-Json -InputObject $patchRequest -Compress -Depth 10))
    $patchJson = $patchJson.replace('"','\"')
    kubectl patch deployment wapi-backend-d1 --patch $patchJson