首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java中Kubernetes接口:鉴权错误

java中Kubernetes接口:鉴权错误
EN

Stack Overflow用户
提问于 2021-07-30 16:01:12
回答 1查看 463关注 0票数 0

我正在开发一个简单的java应用程序,显示集群的pod。

这是应用程序:

代码语言:javascript
复制
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());
        }
    }
}

但是我得到了这个错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-08-02 20:20:12

GitHub上有与此问题相关的an open issue。现在,您可以使用jhbae200this 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 }

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68593936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档