如何获取集群内Go Kubernetes客户端的当前名称空间 [英] How to get current namespace of an in-cluster go Kubernetes client
问题描述
如何使用kubernetes client-go API获取部署/服务的当前名称空间?它似乎不在客户端对象或配置中.
How do I get the current namespace of a deployment/service using the kubernetes client-go API? It doesn't seem to be in the client object or in the config.
推荐答案
使用
ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
当inClusterClientConfig
的Namespace()
方法中存在所需的实现时,
可以工作,但是很难看.但是如何从rest.InClusterConfig()
开始获取该对象?只能从包外部通过NewNonInteractiveDeferredLoadingClientConfig
实例化.
works but is ugly, when the desired implementation is present in the Namespace()
method of inClusterClientConfig
. But how would you get that object starting from rest.InClusterConfig()
? It is only instantiable from outside the package via NewNonInteractiveDeferredLoadingClientConfig
.
我看到了 kubernetes#63707 ,它看起来很相关但被放弃了.
I see kubernetes #63707 which looks related but was abandoned.
这篇关于如何获取集群内Go Kubernetes客户端的当前名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!