/**
     * 查询每个node所分配的CPU
     * @return
     * @throws Exception
     */
    @GetMapping(value = "v1/getAllCPUByNodes")
    @ApiOperation(value = "查询每个node所分配的CPU")
//    @RestService(serviceName = "k8s-getAllCPUByNodes",version = "v1",url = "/k8s/v1/getAllCPUByNodes",visualRange = "1")
    @Path(value = "/getAllCPUByNodes")
    @GET
    public Result getAllCPUByNodes() throws Exception {
        HashMap<String, Map<String, String>> map = k8sdescoverService.getAllCPUByNodes();

        try {
            return new Result(true, "success", map);
        } catch (Exception e) {
            return new Result(false, "false");
        }


    }
 /**
     * 查询pod中的cpu memory
     * @return
     * @throws Exception
     */
    @GetMapping(value = "v1/getPodsCpu")
    @ApiOperation(value = "查询pod中的cpu memory")
//    @RestService(serviceName = "k8s-getPodsCpu",version = "v1",url = "/k8s/v1/getPodsCpu",visualRange = "1")
    @Path(value = "/getPodsCpu")
    @GET
    public Result getPodsCpu() throws Exception {
        HashMap<String, PodsCpu> map = k8sdescoverService.getPodsCpu();


        try {
            return new Result(true, "success", map);
        } catch (Exception e) {
            return new Result(false, "false");
        }

    }
    /**
     * 查询volume所占的Disk
     * @return
     * @throws ApiException
     */
    @GetMapping(value = "v1/getPersistentVolume")
    @GET
    @Path("/getPersistentVolume")
    @ApiOperation(value = "查询volume所占的Disk")
//    @RestService(serviceName = "k8s-getPV",version = "v1",url = "/k8s/v1/getPersistentVolume",visualRange = "1")
    public Result getDiskByVolume() throws ApiException{
        HashMap< String ,HashMap<String, Map<String, String>>> map = k8sdescoverService.getDiskByVolume();
        try {
            return new Result(true, "success", map);
        } catch (Exception e) {
            return new Result(false, "false");
        }

    }
public HashMap<String,Map<String, String>> getAllCPUByNodes() throws Exception {


//            String kubeConfigPath = "D:\\wgp\\java\\github\\k8s\\config";
            // 以config作为入参创建的client对象,可以访问到K8S的API Server
            ApiClient client = ClientBuilder
                    .kubeconfig(KubeConfig.loadKubeConfig(new InputStreamReader(K8sDescover.class.getClassLoader().getResourceAsStream("config"))))
                    .build();
            GenericKubernetesApi<NodeMetrics, NodeMetricsList> nodeMetricsListGenericKubernetesApi =  new GenericKubernetesApi<>(
                    NodeMetrics.class,
                    NodeMetricsList.class,
                    "metrics.k8s.io",
                    "v1beta1",
                    "nodes",
                    client);
            NodeMetricsList nodeMetricsList = nodeMetricsListGenericKubernetesApi.list().getObject();
            HashMap<String,Map<String, String>>usages = new HashMap<>();
            for (NodeMetrics item : nodeMetricsList.getItems()) {
                String name = item.getMetadata().getName();
                Map<String, Quantity> usage = item.getUsage();
                Map<String, String> quantity = new HashMap<>();
                for (String s : usage.keySet()) {
                    Quantity quantity1 = usage.get(s);
                    String s1 = quantity1.toSuffixedString();
                    quantity.put(s,s1);
                }
                usages.put(name,quantity);
            }
            return  usages;


        }
 /**
     * 通过container查询cpu
     * @return
     * @throws ApiException
     */
    public HashMap<String,PodsCpu> getPodsCpu() throws ApiException {
        HashMap< String,PodsCpu> map = new HashMap<>();
        V1PodList v1PodList = new CoreV1Api().listPodForAllNamespaces(false, null, null, null, null, null, null, null, null,false);

        for (V1Pod item : v1PodList.getItems()) {
            PodsCpu podsCpu = new PodsCpu();
            String podName = item.getMetadata().getName();
            String nodeName = item.getSpec().getNodeName();
            int containerSize = item.getSpec().getContainers().size();
            HashMap<String, Object> containerMap = new HashMap<>();
            HashMap<String, Map> containerMaps = new HashMap<>();
            HashMap<String, Map> recourseMaps = new HashMap<>();

            for (V1Container container : item.getSpec().getContainers()) {
                String containerName = container.getName();
                V1ResourceRequirements resources = container.getResources();
                Map<String, Quantity> limits = resources.getLimits();
                Map<String, Quantity> requests = resources.getRequests();
                if (limits!=null){
                    Quantity limitCpu = limits.get("cpu");
                    Quantity limitMemory = limits.get("memory");
                    Quantity requestCpu = requests.get("cpu");
                    Quantity requestMemory = requests.get("memory");
                }

                podsCpu.setLimits(limits);
                podsCpu.setRequests(requests);
                recourseMaps.put(containerName,limits);
                recourseMaps.put(containerName,requests);
                podsCpu.setContainerName(containerName);
                containerMap.put("recourse",recourseMaps);
                containerMap.put("containerName", containerName);

                containerMaps.put(containerName,containerMap);
//                podsCpu.setContainer(containerMaps);
            }
            podsCpu.setNodeName(nodeName);

            map.put(podName, podsCpu);
        }
        return map;
    }
 public HashMap< String ,HashMap<String, Map<String, String>>> getDiskByVolume() throws ApiException {
            VolomeDisk volomeDisk = new VolomeDisk();
            V1PersistentVolumeList v1PersistentVolumeList = new CoreV1Api().listPersistentVolume(null, false, null, null, null, null, null, null, null, false);
            HashMap< String ,HashMap<String, Map<String, String>>> map = new HashMap<>();
            for (V1PersistentVolume item : v1PersistentVolumeList.getItems()) {
                Map<String, Quantity> capacity = item.getSpec().getCapacity();
                Map<String, String> quantity = new HashMap<>();
                for (String s : capacity.keySet()) {
                    Quantity quantity1 = capacity.get(s);
                    String s1 = quantity1.toSuffixedString();
                    quantity.put(s,s1);
                }
                volomeDisk.setStorage(quantity);
                String name = item.getMetadata().getName();
                volomeDisk.setName(name);
                String namespace = item.getSpec().getClaimRef().getNamespace();
                volomeDisk.setNamespace(namespace);
                HashMap<String, Map<String, String>> map1= new HashMap<>();
                map1.put(namespace,quantity);
                map.put(name,map1);
            }
            return map;

        }

在这里插入图片描述
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐