fabric8连接K8S源码阅读记录

/**
* 建立连接使用的业务代码
*/
public static KubernetesClient GetKubernetesClientByToken(String OauthToken, String masterUrl) {
   
        Config config = 
        ((ConfigBuilder)((ConfigBuilder)((ConfigBuilder)(new ConfigBuilder())
        .withTrustCerts(true))
        .withMasterUrl(masterUrl))
        .withOauthToken(OauthToken))
        .build();
        return new DefaultKubernetesClient(config);
    }

源码分析见注释部分

//默认的K8S连接类
public class DefaultKubernetesClient extends BaseClient implements NamespacedKubernetesClient {
   
    public DefaultKubernetesClient() {
   
    }

    public DefaultKubernetesClient(String masterUrl) {
   
        super(masterUrl);
    }

	//用的这种
	//Config主要就是存储一些配置信息
	//比如用户名 用户密码 注册中心url pod的sa的token
	//这里起到关键作用的是url 和 token
	//有这两个就能建立连接
    public DefaultKubernetesClient(Config config) {
   
        super(config);
    }
//上面的super调用的是这个BaseClient
	public BaseClient(Config config) throws KubernetesClientException {
   
        this(HttpClientUtils.createHttpClient(config), config);
    }
//createHttpClient是这样一个方法:
public static OkHttpClient createHttpClient(Config config) {
   
        return createHttpClient(config, (b) -> {
   
        });
 }


private static OkHttpClient createHttpClient(Config config, Consumer<okhttp3.OkHttpClient.Builder> additionalConfig) {
   
        try {
   
        //可以看到是基于okhttp3做的开发
        //okhttp3通过builder来配置实例
            okhttp3.OkHttpClient.Builder httpClientBuilder = new okhttp3.OkHttpClient.Builder();
            httpClientBuilder.followRedirects(true);
            httpClientBuilder.followSslRedirects(true);
            //连接的config将TrustCerts设置为了true
            //config.isTrustCerts为true 信任认证 
            if (config.isTrustCerts() || config.isDisableHostnameVerification()) {
   
            //这里hostnameVerifier方法实际上是这样的:
            //public OkHttpClient.Builder hostnameVerifier(HostnameVerifier hostnameVerifier)
            //HostnameVerifier是一个只有一个方法的接口
            //验证主机名是否与服务器的身份验证方案匹配
            //如果主机名可接受,则为真
            //这里使用lambda表达式完成了一个匿名内部类的创建
                httpClientBuilder.hostnameVerifier((s, sslSession) -> {
   
                    return true;
                });
            }
			
			//TrustManager是JSSE信任管理器的基接口
			/**
			//SSLUtils的trustManagers方法:
			//大概就是按照标准去做一些校验
			public static TrustManager[] trustManagers(Config config) throws CertificateException, NoSuchAlgorithmException, 					KeyStoreException, IOException {
        	return trustManagers(config.getCaCertData(), config.getCaCertFile(), config.isTrustCerts(), config.getTrustStoreFile(), config.getTrustStorePassphrase()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值