通过令牌与Kubernetes集群进行身份验证

时间:2018-10-04 19:34:02

标签: python kubernetes

设置环境(AWS EKS)的方式是,在我的~/.kube/config中,用户具有执行配置,可以调用aws-iam-authenticator

这样一来,当运行kubectl时,它将请求令牌对Kubernetes集群进行身份验证。

我目前正在编写一个将与Kubernetes API交互的客户端应用程序。这是使用official Python client用Python编写的。

在执行任何示例时,我收到错误消息,不允许system:anonymous执行某些操作(例如,列表窗格)。我认为问题的根源在于,我需要将令牌从aws-iam-authenticator传递到我的客户请求。

不幸的是,我似乎无法弄清楚如何将该令牌与Kubernetes的Python客户端一起传递。我看到了this snippet,但收到一个错误消息,指出api_key属性不是configuration模块的一部分(而且确实不是)。

我应该如何将令牌注入到来自Kubernetes的Python客户端的请求中?

谢谢!

1 个答案:

答案 0 :(得分:0)

我相信您需要通过以下命令配置“ Authorization:Bearer”标头:configuration.api_key_prefix['authorization'] = 'Bearer'。所以基本上:

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint

# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['authorization'] = 'Bearer' ## <== This one

# create an instance of the API class
api_instance = kubernetes.client.ApiregistrationV1Api(kubernetes.client.ApiClient(configuration))
body = kubernetes.client.V1APIService() # V1APIService | 
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)

try: 
    api_response = api_instance.create_api_service(body, pretty=pretty)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ApiregistrationV1Api->create_api_service: %s\n" % e)

基本上是here