您也可以使用defaultClient。
如果应用程序在集群内运行并且具有正确的服务帐户,defaultClient() 方法将创建一个集群内客户端。
您可以在方法here 上从cmets 中查看defaultClient 的规则:
/**
* Easy client creation, follows this plan
*
* <ul>
* <li>If $KUBECONFIG is defined, use that config file.
* <li>If $HOME/.kube/config can be found, use that.
* <li>If the in-cluster service account can be found, assume in cluster config.
* <li>Default to localhost:8080 as a last resort.
* </ul>
*
* @return The best APIClient given the previously described rules
*/
因此,如果应用程序使用 k8s java 客户端,在它自己的集群上运行,它应该能够访问集群上的东西,只要它具有正确的权限。
您需要允许您的客户端应用程序能够访问 CRD,例如 ClusterRole for CRDs of Prometheus Operator 的示例:
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: prometheus-crd-view
labels:
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
rules:
- apiGroups: ["monitoring.coreos.com"]
resources: ["alertmanagers", "prometheuses", "prometheusrules", "servicemonitors"]
verbs: ["get", "list", "watch"]