发生了什么?
我正在编写一个使用client-go rest客户端来修补CRD的方法:
err := c.restClient.
Patch(types.MergePatchType).
Resource(MySuperAwesomeResource).
SubResource("status").
Name(name).
Body(superAwesomeResourceObject).
Do(ctx).
Into(result)
然后惊讶地收到一个错误:
the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml
你期望会发生什么?
由于我通过 Patch(types.MergePatchType)
设置了Content-Type,因此补丁不应该因为Content-Type不正确而失败。
我们如何尽可能精确地重现它?
使用client-go rest客户端,尝试通过将runtime.Object传递给 Body()
方法来修补资源。
我们需要了解其他信息吗?
以下是我所认为的情况:Patch()
方法设置了Content-Type头。
但是,将k8s运行时对象传递给 Body()
会用 ClientContentConfig
中设置的任何Content-Type覆盖该头(如果且仅当传递的对象是k8s运行时对象)。这默认为 application/json
,我认为对于不是 Patch()
的HTTP方法是正确的。
如果将 Body()
对那个头的设置限制在 PATCH
动词上:
if r.verb != "PATCH" {
r.SetHeader("Content-Type", r.c.content.ContentType)
}
那么事情就会正常工作。
还可以在 Body()
之后调用 SetHeader
:
err := c.restClient.
Patch(types.MergePatchType).
Resource(MySuperAwesomeResource).
SubResource("status").
Name(name).
Body(superAwesomeResourceObject).
SetHeader("Content-Type", string(types.MergePatchType)).
Do(ctx).
Into(result)
然后事情就会正常工作。
我不知道我在这里做错了什么,但这感觉更像是bug而不是操作员错误?
Kubernetes版本
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.3", GitCommit:"25b4e43193bcda6c7328a6d147b1fb73a33f1598", GitTreeState:"clean", BuildDate:"2023-06-14T09:53:42Z", GoVersion:"go1.20.5", Compiler:"gc", Platform:"linux/amd64"}
Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.3", GitCommit:"25b4e43193bcda6c7328a6d147b1fb73a33f1598", GitTreeState:"clean", BuildDate:"2023-06-14T09:53:42Z", GoVersion:"go1.20.5", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v5.0.1
Server Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.3", GitCommit:"25b4e43193bcda6c7328a6d147b1fb73a33f1598", GitTreeState:"clean", BuildDate:"2023-06-15T00:36:28Z", GoVersion:"go1.20.5", Compiler:"gc", Platform:"linux/amd64"}```
</details>
### Cloud provider
<details>
self
</details>
### OS version
<details>
```console
# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here
# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here
3条答案
按热度按时间i34xakig1#
/sig api-machinery
kxe2p93d2#
我同意这至少是令人惊讶的。我在#124059(评论)中运行了相同的操作,并应用了相同的解决方法。如果Body只在尚未设置content-type头时才设置它,那么是否存在任何合法的使用场景会被破坏?假设有人调用Patch()或SetHeader("Content-Type", "foo"),那么这是有意为之的。
wlsrxk513#
/triage accepted