使用kubernetes-client访问k8s apiserver,直接贴代码了
package com.pdd.transports.config;
import com.yiran.arch.leo.client.ConfigCache;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.apis.AppsV1Api;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
import io.kubernetes.client.apis.RbacAuthorizationV1Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
* @author jinhaozhi
*/
@Slf4j
@Configuration
public class ApiClientConfig {
// 直接使用eden-api的配置
private static final String CONFIG_KEY = "eden-api.k8s.kube.config";
private static final String CONFIG_DEBUG_KEY = "eden-api.k8s.debug.switch";
@Value("${" + CONFIG_KEY + "}")
private String config;
@Value("${" + CONFIG_DEBUG_KEY + "}")
private String debug;
@Bean
@Primary
public ApiClient initK8sApiClient() {
try {
InputStream configStream = new ByteArrayInputStream(config.getBytes());
final ApiClient apiClient = io.kubernetes.client.util.Config.fromConfig(configStream);
apiClient.setDebugging(Boolean.valueOf(debug));
io.kubernetes.client.Configuration.setDefaultApiClient(apiClient);
ConfigCache.getInstance().addChange((key, value) -> {
if (CONFIG_KEY.equals(key)) {
log.warn("[ LEO ] Change k8s config, this change should have no effect.");
} else if (CONFIG_DEBUG_KEY.equals(key)) {
debug = value;
apiClient.setDebugging(Boolean.valueOf(value));
}
});
log.info("[ K8s ] Client init success, server: {} ------------------------->", apiClient.getBasePath());
return apiClient;
} catch (Exception e) {
log.error("[ K8s ] Client init failed! -------------------------->", e);
return null;
}
}
@Bean
public CoreV1Api coreV1Api(@Autowired ApiClient apiClient) {
return new CoreV1Api(apiClient);
}
@Bean
public AppsV1Api appsV1Api(@Autowired ApiClient apiClient) {
return new AppsV1Api(apiClient);
}
@Bean
public RbacAuthorizationV1Api rbacAuthorizationV1Api(@Autowired ApiClient apiClient) {
return new RbacAuthorizationV1Api(apiClient);
}
@Bean
public ExtensionsV1beta1Api extensionsV1beta1Api(@Autowired ApiClient apiClient) {
return new ExtensionsV1beta1Api(apiClient);
}
}
package com.pdd.transports.k8s;
import com.google.gson.Gson;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.apis.AppsV1Api;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
import io.kubernetes.client.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/*
* @Author jinhaozhi
* @Description //TODO
* @Date 14:46 2019/11/26
* @Param
* @return
**/
@Service
public class ApiServerResourceListBizService {
@Autowired
private CoreV1Api api;
@Autowired
private AppsV1Api appsV1Api;
@Autowired
private ExtensionsV1beta1Api extensionsV1beta1Api;
protected String name() {
return "ApiServerResourceListBizService";
}
public T getApiServerResponse(String namespace, Class t) throws ApiException {
if (t == V1PodList.class) {
V1PodList v1PodList = api.listNamespacedPod(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1PodList;
} else if (t == V1DeploymentList.class) {
V1DeploymentList v1DeploymentList = appsV1Api.listNamespacedDeployment(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1DeploymentList;
} else if (t == V1beta1IngressList.class) {
V1beta1IngressList v1beta1IngressList = extensionsV1beta1Api.listNamespacedIngress(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1beta1IngressList;
} else if (t == V1ServiceList.class) {
V1ServiceList v1ServiceList = api.listNamespacedService(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1ServiceList;
} else if(t == V1ConfigMapList.class) {
V1ConfigMapList v1ConfigMapList = api.listNamespacedConfigMap(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1ConfigMapList;
} else if(t == V1beta1NetworkPolicyList.class) {
V1beta1NetworkPolicyList v1beta1NetworkPolicyList = extensionsV1beta1Api.listNamespacedNetworkPolicy(namespace, true, null, null, null, null, null, null, null, null);
return (T) v1beta1NetworkPolicyList;
} else if(t == V1NodeList.class) {
V1NodeList v1NodeList = api.listNode(null,"true",null,null,null,null,null,null,null);
return (T) v1NodeList;
} else if(t == V1NamespaceList.class) {
V1NamespaceList v1NamespaceList = api.listNamespace(null,"true",null,null,null,null,null,null,null );
return (T) v1NamespaceList;
}
return null;
}
public T getApiServerAllResponse(Class t) throws ApiException {
if (t == V1PodList.class) {
V1PodList v1PodList = api.listPodForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1PodList;
} else if(t == V1ServiceList.class) {
V1ServiceList v1ServiceList = api.listServiceForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1ServiceList;
} else if(t == V1EndpointsList.class) {
V1EndpointsList v1EndpointsList = api.listEndpointsForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1EndpointsList;
} else if(t == V1ConfigMapList.class) {
V1ConfigMapList v1ConfigMapList = api.listConfigMapForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1ConfigMapList;
} else if(t == V1DeploymentList.class) {
V1DeploymentList v1DeploymentList = appsV1Api.listDeploymentForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1DeploymentList;
} else if(t == V1beta1IngressList.class) {
V1beta1IngressList v1beta1IngressList = extensionsV1beta1Api.listIngressForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1beta1IngressList;
} else if(t == V1beta1NetworkPolicyList.class) {
V1beta1NetworkPolicyList v1beta1NetworkPolicyList = extensionsV1beta1Api.listNetworkPolicyForAllNamespaces(null,null,true,null,null,"true",null,null,null);
return (T) v1beta1NetworkPolicyList;
}
return null;
}
public String getApiServerAllNamespaceResponse(String type) {
try {
String result = "";
Gson gson = new Gson();
switch (type) {
case "namespace":
V1NamespaceList v1NamespaceList = api.listNamespace(null,"true",null,null,null,null,null,null,null );
result = gson.toJson(v1NamespaceList);
break;
case "node":
V1NodeList v1NodeList = api.listNode(null,"true",null,null,null,null,null,null,null);
result = gson.toJson(v1NodeList);
break;
case "pod":
V1PodList v1PodList = api.listPodForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1PodList);
break;
case "service":
V1ServiceList v1ServiceList = api.listServiceForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1ServiceList);
break;
case "endPoints":
V1EndpointsList v1EndpointsList = api.listEndpointsForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1EndpointsList);
break;
case "configMap":
V1ConfigMapList v1ConfigMapList = api.listConfigMapForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1ConfigMapList);
break;
case "deployment":
V1DeploymentList v1DeploymentList = appsV1Api.listDeploymentForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1DeploymentList);
break;
case "ingress":
V1beta1IngressList v1beta1IngressList = extensionsV1beta1Api.listIngressForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1beta1IngressList);
break;
case "networkPolicy":
V1beta1NetworkPolicyList v1beta1NetworkPolicyList = extensionsV1beta1Api.listNetworkPolicyForAllNamespaces(null,null,true,null,null,"true",null,null,null);
result = gson.toJson(v1beta1NetworkPolicyList);
break;
default:
System.out.println("输入参数不正确");
}
return result;
} catch (ApiException e) {
e.printStackTrace();
}
return null;
}
}