“kubectl 补丁"适用于 Linux Bash,但不适用于 Windows Powershell ISE [英] 'kubectl patch' works on Linux Bash but not in Windows Powershell ISE

查看:24
本文介绍了“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?

推荐答案

有关详细且非常有用的背景,请参阅 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 patch 时,您会再次遇到转义问题.这就是我最终的结果:

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天全站免登陆