如何修复"没有这样的文件或目录:"/home/jenkins/.kube/config 在 python openshift rest 客户端中使用 load_incluster_config



我编写了一个脚本来检查OpenShift集群中的一些机密。我使用了 python rest-client 库进行 Openshift,脚本在集群中执行。但我总是得到IOError:[errno 2]没有这样的文件或目录:'/home/jenkins/.kube/config'

我知道我在 pod 中没有 kube 配置,因此我尝试使用 kubernetes.config.load_incluster_config() 方法来启用集群内配置。

from kubernetes import client, config
from openshift.dynamic import DynamicClient
config.load_incluster_config()
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)

我假设不再需要在 load_incluster_config 调用中提供 kube 配置。是否有人使用服务帐户解决了其余客户端和集群执行中的 openshift 的问题?

我感谢任何帮助,谢谢。

我的意思是,您可能已经检查过了,但确定您在正确的目录中?因为从错误的目录运行文件可能会导致"没有这样的文件或目录"的错误。

我用以下内容解决了它:

if os.getenv('KUBERNETES_SERVICE_HOST'):
    config.load_incluster_config()
else:
    config.load_kube_config()
dyn_client = DynamicClient(ApiClient())

ApiClient 正在使用默认配置。

最新更新