package com.educoder.bridge.controller; import com.alibaba.fastjson.JSONObject; import com.educoder.bridge.service.K8sService; import io.fabric8.kubernetes.client.KubernetesClient; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created by liqiankun on 2017/11/16 0016 * Description: */ @Api(value = "资源监控", hidden = true) @RestController @RequestMapping("/monitor") public class MonitorController { Logger logger = LoggerFactory.getLogger(MonitorController.class); @Autowired private K8sService k8sService; /** * 简单地获取集群中的Pod总量及各个节点上的Pod分布 * * @return * @throws Exception */ @RequestMapping(path = "/getPodsInfo") @ApiOperation(value = "获取集群中某个节点的信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public JSONObject podsInfo() throws Exception { int podSum = k8sService.getPodSum(); List nodeIP = k8sService.getNodesIP(); //生成Json格式的返回结果 StringBuffer jsonResult = new StringBuffer("["); for (String ip : nodeIP) { int num = k8sService.getPodNum(ip); jsonResult.append("{\"ip\":" + "\"" + ip + "\"," + "\"num\":" + "\"" + num + "\"},"); } int length = jsonResult.length() - 1; jsonResult.deleteCharAt(length); jsonResult.append("]"); JSONObject response = new JSONObject(); response.put("sum", podSum); response.put("distr", jsonResult); response.put("code", 0); return response; } }