"kubectl修补程序"在Linux Bash上有效,但在Windows Powershell ISE中不可用 [英] 'kubectl patch' works on Linux Bash but not in Windows Powershell ISE

查看:342
本文介绍了"kubectl修补程序"在Linux Bash上有效,但在Windows Powershell ISE中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下命令在Ubuntu bash上运行良好:

The following command works fine on Ubuntu bash:

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

同一命令在Windows Powershell控制台(ISE)中不起作用.

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

错误是:

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

powershell控制台版本为:

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.

/spec/template/metadata/labels/date路径确实存在于部署的Yaml中,因此也不是问题.

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

我认为这可能与kubectl在Powershell中相对于引号的工作方式有所不同,但找不到找到使之工作的方法.

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.

我尝试过

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

但这会导致

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

Powershell上的命令应该是什么?

What should be the command on Powershell?

推荐答案

有关详细且非常有用的背景,请参阅 answer by mklement0

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\"}}}}}'

这是如何在Powershell中使用kubectl补丁

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).

当您尝试将JSON放入变量中,然后使用变量调用kubectl补丁时,您会再次遇到转义的麻烦.这就是我最终得到的:

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

这篇关于"kubectl修补程序"在Linux Bash上有效,但在Windows Powershell ISE中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆