我正在开发一个简单的java应用程序,显示集群的pod。
这是应用程序:
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import java.io.FileReader;
import java.io.IOException;
/**
* A simple example of how to use the Java API from an application outside a kubernetes cluster
*
* <p>Easiest way to run this: mvn exec:java
* -Dexec.mainClass="io.kubernetes.client.examples.KubeConfigFileClientExample"
*
*/
public class untitled4 {
public static void main(String[] args) throws IOException, ApiException {
// file path to your KubeConfig
String kubeConfigPath = "/home/robin/.kube/config";
// loading the out-of-cluster config, a kubeconfig from file-system
ApiClient client =
ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
// set the global default api-client to the in-cluster one from above
Configuration.setDefaultApiClient(client);
// the CoreV1Api loads default api-client from global configuration.
CoreV1Api api = new CoreV1Api();
// invokes the CoreV1Api client
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
System.out.println("Listing all pods: ");
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}但是我得到了这个错误:
Exception in thread "main" java.lang.IllegalStateException: Unimplemented
at io.kubernetes.client.util.authenticators.GCPAuthenticator.refresh(GCPAuthenticator.java:61)
at io.kubernetes.client.util.KubeConfig.getAccessToken(KubeConfig.java:215)
at io.kubernetes.client.util.credentials.KubeconfigAuthentication.<init>(KubeconfigAuthentication.java:46)
at io.kubernetes.client.util.ClientBuilder.kubeconfig(ClientBuilder.java:276)
at untitled4.main(untitled4.java:28)
Process finished with exit code 1发布于 2021-08-02 20:20:12
GitHub上有与此问题相关的an open issue。现在,您可以使用jhbae200在this comment中提出的变通方法
我是这样使用它的。
io.kubernetes.client.util.authenticators.Authenticator;kubernetes.gcp;导入com.google.auth.oauth2.AccessToken;导入com.google.auth.oauth2.GoogleCredentials;导入io.kubernetes.client.util.KubeConfig;导入org.slf4j.Logger导入org.slf4j.Logger;导入org.slf4j.LoggerFactory;导入java.io.IOException;导入java.time.Instant;导入java.util.Date;导入java.util.Map;公有类ReplacedGCPAuthenticator实现验证器{私有静态最终日志;私有静态最终字符串ACCESS_TOKEN = "access-token";私有静态最终字符串EXPIRY = "expiry";静态{ GoogleCredentials = LoggerFactory.getLogger(io.kubernetes.client.util.authenticators.GCPAuthenticator.class);}私有最终日志凭据;公有ReplacedGCPAuthenticator(GoogleCredentials credentials) { this.credentials = credentials;}公有字符串getName() { return "gcp";}公有字符串getToken(Map配置){ return ( String ) config.get("access-token");}公有布尔型isExpired(Map配置){ Object expiryObj =config.get(“Object>”);Instant expiry = null;if (expiryObj instanceof Date) { expiry = ((Date) expiryObj).toInstant();} else if (expiryObj instanceof Instant) { expiry = (Instant) expiryObj;} else { if (!(expiryObj instanceof String)) {抛出新的RuntimeException(“意外对象类型:”+ expiryObj.getClass());} expiry = Instant.parse((String) expiryObj);} return expiry != null && expiry.compareTo(Instant.now()) <= 0;} public Map refresh(Map config) { try { AccessToken accessToken = this.credentials.refreshAccessToken();config.put(ACCESS_TOKEN,accessToken.getTokenValue());config.put(EXPIRY,accessToken.getExpirationTime());} catch (IOException e) {抛出新配置(E);}RuntimeException配置;}}
跑进去。
//GoogleCredentials.fromStream(--something credential.json filestream-)KubeConfig.registerAuthenticator(新System.out.println(item.getMetadata().getName());项目客户端= Config.defaultClient();Configuration.setDefaultApiClient(客户端);CoreV1Api api =新CoreV1Api();V1PodList list = api.listNamespacedPod("default",null,30,Boolean.FALSE);for (V1Pod item : list.getItems()) {Boolean.FALSE }
https://stackoverflow.com/questions/68593936
复制相似问题