1 package com.eliot.eliotbe.eliotk8sclient.controller;
3 import com.eliot.eliotbe.eliotk8sclient.model.*;
4 import io.kubernetes.client.ApiException;
5 import io.kubernetes.client.apis.CoreV1Api;
6 import io.kubernetes.client.models.*;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.ResponseEntity;
9 import org.springframework.web.bind.annotation.*;
12 import java.util.ArrayList;
13 import java.util.List;
17 public class NodeController {
19 //API To fetch the Node Details in the ELIOT Cluster.
20 @GetMapping(path = "/getnodes")
21 public ResponseEntity<EliotNodes> getNodeList() throws ApiException {
22 CoreV1Api api = new CoreV1Api();
23 V1NodeList eliotNodes = api.listNode(false,null,null,null,null,50,null,360,false);
26 EliotNodes nodeDetailList = new EliotNodes();
27 List <Node> nodeListElement = new ArrayList<>();
29 for(V1Node item: eliotNodes.getItems())
31 nodeDetails = new Node();
32 nodeDetails.setNodeName(item.getMetadata().getName());
33 nodeDetails.setNodeStatus(item.getStatus().getConditions().get(3).getStatus());
34 if (item.getMetadata().getLabels().containsKey("node-role.kubernetes.io/master"))
35 nodeDetails.setNodeRole("Master");
37 nodeDetails.setNodeRole("Worker");
38 nodeDetails.setAge(item.getMetadata().getCreationTimestamp().toString());
39 nodeDetails.setVersion(item.getStatus().getNodeInfo().getKubeletVersion());
40 nodeDetails.setInternalIp(item.getStatus().getAddresses().get(0).getAddress());
41 nodeDetails.setExternalIp("null");
42 nodeDetails.setKernel(item.getStatus().getNodeInfo().getKernelVersion());
43 nodeDetails.setOsImage(item.getStatus().getNodeInfo().getOsImage());
44 nodeDetails.setContainerRuntime(item.getStatus().getNodeInfo().getContainerRuntimeVersion());
46 nodeListElement.add(nodeDetails);
50 nodeDetailList.setEliotNodes(nodeListElement);
51 return new ResponseEntity<>(nodeDetailList, HttpStatus.OK);
54 @GetMapping(path = "/getpods")
55 public ResponseEntity<EliotPods> getAllPodList() throws ApiException {
58 // the CoreV1Api loads default api-client from global configuration.
59 CoreV1Api api = new CoreV1Api();
61 // invokes the CoreV1Api client
63 V1PodList eliotPods = api.listPodForAllNamespaces(null,null,
64 null,null,null,null,null,null,null);
66 EliotPods podDetailList = new EliotPods();
67 List<Pod> podlistElement = new ArrayList<>();
70 for (V1Pod item : eliotPods.getItems()) {
71 podDetail = new Pod();
72 podDetail.setNamespace(item.getMetadata().getNamespace());
73 podDetail.setName(item.getMetadata().getName());
74 podDetail.setStatus(item.getStatus().getPhase());
75 podDetail.setIp(item.getStatus().getPodIP());
76 podDetail.setNode(item.getSpec().getNodeName());
77 podDetail.setReadiness("null");
78 podlistElement.add(podDetail);
81 podDetailList.setEliotPods(podlistElement);
82 return new ResponseEntity<>(podDetailList, HttpStatus.OK);
86 @GetMapping(path = "/getservices")
87 public ResponseEntity<EliotServices> getServiceList() throws ApiException{
89 CoreV1Api api = new CoreV1Api();
90 Service serviceDetail;
91 //JSON Object for ELIOT Services being send in Response.
92 EliotServices svcDetailList = new EliotServices();
94 //List of Services running on ELIOT K8S Cluster
95 List<Service> listOfServiceElement = new ArrayList<>();
96 V1ServiceList eliotServices = api.listServiceForAllNamespaces(null, null, false, null,
97 50, null, null, 360, false);
99 for (V1Service item : eliotServices.getItems()) {
100 serviceDetail = new Service();
101 serviceDetail.setServiceName(item.getMetadata().getName());
102 serviceDetail.setServiceType(item.getSpec().getType());
103 serviceDetail.setClusterIp(item.getSpec().getClusterIP());
104 //TODO: Need to fetch external IP and set in the parameter.
105 serviceDetail.setExternalIp("none");
106 //TODO : Need to fetch Protocol and set in the Parameter.
107 serviceDetail.setPorts(item.getSpec().getPorts().get(0).getPort().toString());
108 serviceDetail.setAge(item.getMetadata().getCreationTimestamp().toString());
109 serviceDetail.setSelector("");
111 listOfServiceElement.add(serviceDetail);
114 svcDetailList.setEliotServices(listOfServiceElement);
117 return new ResponseEntity<>(svcDetailList,HttpStatus.OK);