如何使用Kubernetes Python客户端创建秘密?

时间:2017-10-16 04:50:22

标签: python json kubernetes openapi kubernetes-python-client

我一直在尝试使用python客户端为Kubernetes集群创建秘密。我不断收到错误消息

Traceback (most recent call last):
File "create_secrets.py", line 19, in <module>
api_response = v1.create_namespaced_secret(namespace, body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 7271, in create_namespaced_secret
(data) = self.create_namespaced_secret_with_http_info(namespace, body, **kwargs)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 7361, in create_namespaced_secret_with_http_info
collection_formats=collection_formats)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 335, in call_api
_preload_content, _request_timeout)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 148, in __call_api
_request_timeout=_request_timeout)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 393, in request
body=body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/rest.py", line 287, in POST
body=body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/rest.py", line 240, in request
raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Mon, 16 Oct 2017 04:17:35 GMT', 'Content-Length': '234'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"none in version \"v1\" cannot be handled as a Secret: no kind \"none\" is registered for version \"v1\"","reason":"BadRequest","code":400}

这是我试图创建秘密的代码。

from __future__ import print_function
import time
import kubernetes.client
from pprint import pprint
from kubernetes import client, config

config.load_kube_config()
v1 = client.CoreV1Api()
namespace = 'kube-system'
metadata = {'name': 'pk-test-tls', 'namespace': 'kube-system'}
data=  {'tls.crt': '###BASE64 encoded crt###', 'tls.key': '###BASE64 encoded Key###'}
api_version = 'v1'
kind = 'none'
body = kubernetes.client.V1Secret(api_version, data , kind, metadata, 
type='kubernetes.io/tls')

api_response = v1.create_namespaced_secret(namespace, body)
pprint(api_response)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

您写的几乎所有内容都没有问题,但要注意从kube-apiserver收到的消息:

  

HTTP响应正文:{&#34; kind&#34;:&#34;状态&#34;,&#34; apiVersion&#34;:&#34; v1&#34;,&#34;元数据&# 34;:{},&#34;状态&#34;:&#34;失败&#34;,&#34;消息&#34;:&#34;无版本\&#34; v1 \&#34 ;不能作为一个秘密处理:没有种类\&#34;没有\&#34;已注册版本\&#34; v1 \&#34;&#34;,&#34;原因&#34;:&#34; BadRequest&#34;,&#34;代码&#34;:400} < / p>

特别是没有&#34;没有&#34; 。这只是错字吗?你有什么想法吗?

这里有种类列表https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#types-kinds

如果你改变了对#34; Secret&#34;那么一切都会好起来的。