POST 请求 Kubernetes api 服务器结果 "the namespace of the provided object does not match the namespace sent on the request"

POST request to Kubernetes api server results in "the namespace of the provided object does not match the namespace sent on the request"

我正在编写一个脚本,它将在 Kubernetes 中创建秘密,而不会将 base64 编码的文件以纯文本形式放在除 etcd 之外的任何地方。为此,我直接与 kubernetes 的 api 服务器通信,而不是通过 kubectl。当我发出请求时,它需要一个命名空间,这是有道理的。

但是,我收到的有关命名空间的响应是 "the namespace of the provided object does not match the namespace sent on the request"。我查看了 api 服务器和 kubectl 的源代码,看看 kubectl 是否有一些我没有做的特殊事情会导致这个问题。我收到此错误的原因是什么?我是否缺少 headers 或 cookie 或类似的东西?

这是我的要求(我用JS写的):

var data = JSON.stringify(/* Secret spec */);
var req = http.request({
    hostname: argObj.host ? argObj.host : '127.0.0.1',
    port: argObj.port ? argObj.port : '8080',
    path: '/api/v1/secrets',
    method: argObj.method ? argObj.method : 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
}, (res)=>{/* handle res */});
req.write(data);

我在这个示例中故意遗漏了我的身份验证,我可以确认我已获得授权。

所以解决方案是POST改为“/api/v1/namespaces/{namespace}/secrets”