获取kubernetes集群资源对象(控制器,Pod,容器)的详细信息

控制器

List<Deployment> deploymentList = client.apps().deployments().list().getItems();
String namespace = "default";
for(Deployment deployment:deploymentList){
	String name = deployment.getMetadata().getName();
	if(name.equals(controllerName)){
		resourceType = "Deployment";
		namespace = deployment.getMetadata().getNamespace();
		break;
	}
}	
if(resourceType.equals("Deployment")){
	Deployment selectedDeployment =  client.apps().deployments().inNamespace(namespace).withName(controllerName).get();			
	Map<String,String> mapAnnotation = selectedDeployment.getMetadata().getAnnotations();	        
    int annotationNum=0;
    String annotations="";
    for(String key:mapAnnotation.keySet()){
       	String value=mapAnnotation.get(key);
       	//最后一个注解没有分号          
       	if(mapAnnotation.size()>1){
       		if(annotationNum==mapAnnotation.size()-1)
       		{
       			annotations = annotations+key+": "+value;
       			break;
       		}
       		annotations=annotations+key+": "+value+" ; ";
       		annotationNum=annotationNum+1;
       	}else{
       		annotations = annotations+ key+": "+value;
       	}	            	
     }
     Map<String,String> mapLabel = selectedDeployment.getMetadata().getLabels();	       
     int labelNum=0;
     String labels = "";
     for(String key:mapLabel.keySet()){
      	String value=mapLabel.get(key);
      	//最后一个注解没有分号          
      	if(mapLabel.size()>1){
      		if(labelNum==mapLabel.size()-1)
      		{
      			labels = labels+key+": "+value;
      			break;
      		}
      		labels=labels+key+": "+value+" ; ";
      		labelNum=labelNum+1;
      	}else{
      		labels = labels+ key+": "+value;
      	}	            	
     }
     
     int availableReplicas=0;
     int updateReplicas=0;
     int replicas=0;
     if(selectedDeployment.getStatus().getAvailableReplicas()==null){
      	availableReplicas=0;
     }
     else{
      	availableReplicas = selectedDeployment.getStatus().getAvailableReplicas();
     }
     if(selectedDeployment.getStatus().getUpdatedReplicas()==null){
       	updateReplicas=0;
     }
     else{
       	updateReplicas = selectedDeployment.getStatus().getUpdatedReplicas();
     }
     if(selectedDeployment.getStatus().getReplicas()==null){
      	replicas=0;
     }
     else{
      	replicas = selectedDeployment.getStatus().getReplicas();
     }
     //状态
     String status = updateReplicas+"个已更新,共计"+replicas+"个,"+availableReplicas+"个可用,"+(replicas-availableReplicas)+"个不可用";
         
     String createTimestamp = selectedDeployment.getMetadata().getCreationTimestamp(); 
     //策略   
     String strategyType = selectedDeployment.getSpec().getStrategy().getType();
     
     String maxSurge = selectedDeployment.getSpec().getStrategy().getRollingUpdate().getMaxSurge().getStrVal();
     String maxUnavailable = selectedDeployment.getSpec().getStrategy().getRollingUpdate().getMaxUnavailable().getStrVal();
     //滚动更新策略
     String strategy="最大激增数:"+maxSurge+",最大无效数:"+maxUnavailable;
     //历史版本限制值   
     int revisionHostoryLimit = selectedDeployment.getSpec().getRevisionHistoryLimit(); 							            
}

容器组

List<Pod> podList = client.pods().list().getItems();			
String namespace = "default";
for(Pod pod:podList){
	String name = pod.getMetadata().getName();
	if(name.equals(podName)){
		namespace =  pod.getMetadata().getNamespace();
		break;
	}
}	
Pod selectedPod=client.pods().inNamespace(namespace).withName(podName).get();	
			
Map<String,String> podLabelMap =selectedPod.getMetadata().getLabels();
String createTimestamp = selectedPod.getMetadata().getCreationTimestamp();   
String nodeName = selectedPod.getSpec().getNodeName();
String phase = selectedPod.getStatus().getPhase();
String podIP = selectedPod.getStatus().getPodIP();

容器

OneResourceInfoPojo pojoName = new OneResourceInfoPojo();
pojoName.setName("名称");
pojoName.setValue(containerName);

String requestUrl = "";
String containerListExpr = "container_last_seen{image!=\"\",name=\""+containerName+"\"}";	
requestUrl = "http://"+ prometheusIP + ":" + prometheusPort 
		+ "/api/v1/query?query="+containerListExpr; 															
String result = getPrometheusQueryContent(requestUrl);	
if(result.isEmpty() || result.equals(""))
{			
	return;
}			
//对返回的json串进行处理
String jsonData = JSON.parseObject(result).getString("data");
String jsonResult = JSON.parseObject(jsonData).getString("result");
JSONArray resultArray = JSONArray.parseArray(jsonResult);	

JSONObject jo = resultArray.getJSONObject(0);
String jsonMetric = jo.getString("metric"); 	

String node = JSON.parseObject(jsonMetric).getString("instance");
OneResourceInfoPojo pojoNode = new OneResourceInfoPojo();
pojoNode.setName("节点");
pojoNode.setValue(node);

String image = JSON.parseObject(jsonMetric).getString("image");
OneResourceInfoPojo pojoImage = new OneResourceInfoPojo();
pojoImage.setName("镜像");
pojoImage.setValue(image);		
	
String namespace = JSON.parseObject(jsonMetric).getString("namespace");
OneResourceInfoPojo pojoNamespace = new OneResourceInfoPojo();
pojoNamespace.setName("命名空间");
pojoNamespace.setValue(namespace);	

String pod = JSON.parseObject(jsonMetric).getString("pod_name");
OneResourceInfoPojo pojoPod = new OneResourceInfoPojo();
pojoPod.setName("所属容器组");
pojoPod.setValue(pod);	

你可能感兴趣的:(Promethues应用,kubernetes,pod,容器,控制器)