如何使用Fabric8 Java客户端获取kubernetes服务帐户访问令牌?
我已经在本地计算机上配置了minikube,并将在外部使用kubernetes.我已经在kubernetes中创建了一个服务帐户,使用它的秘密,我可以使用以下命令获取访问令牌.
I have configured minikube in my local machine and going to use kubernetes externally. I have created a Service Account in kubernetes and using it's secret I can get the access token using below command.
kubectl get secret <service-account-secret> -o yaml -n mynamespace
我的问题是如何在运行时使用fabric8 Java客户端执行此操作?我想要的是通过提供服务帐户的机密作为参数来获取访问令牌.
My question is how can I do this using fabric8 java client in runtime ? What I want is to obtain the access token by giving the secret of the Service account as a parameter.
我正在以下面的方式启动配置.
I am initiating the config as bellow.
Config config = new ConfigBuilder().withMasterUrl(masterURL)
.withClientCertFile(certFile).withOauthToken(serviceAccountAccessToken).build();
我可以知道如何使用fabric8 Java客户端获取如上所述的serviceAccountAccessToken吗?
Can I know how to get the serviceAccountAccessToken as described above using fabric8 java client ?
客户端已经为您完成了此任务.
The client already does that for you.
如果您仅创建一个空的Config对象:
If you just create an empty Config object:
Config config = new ConfigBuilder().build();
或创建客户端,例如:
KubernetesClient client = new DefaultKubernetesClient();
在容器中,它将自动为您读取令牌.
from within a pod, it will automatically read the token for you.
如果您需要将其传递给其他地方,则可以:
If you need to pass it elsewhere, you can just:
String token = config.getOauthToken();
或
String token = client.getConfiguration().getOauthToken();