Added Authentication Component
[eliot.git] / blueprints / common / eliot-ui / be / src / eliotk8sclient / src / main / java / com / eliot / eliotbe / eliotk8sclient / controller / NodeController.java
1 package com.eliot.eliotbe.eliotk8sclient.controller;
2
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.*;
10
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 @CrossOrigin
16 @RestController
17 public class NodeController {
18
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);
24         Node nodeDetails;
25
26         EliotNodes nodeDetailList = new EliotNodes();
27         List <Node> nodeListElement = new ArrayList<>();
28
29         for(V1Node item: eliotNodes.getItems())
30         {
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");
36             else
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());
45
46             nodeListElement.add(nodeDetails);
47
48         }
49
50         nodeDetailList.setEliotNodes(nodeListElement);
51         return new ResponseEntity<>(nodeDetailList, HttpStatus.OK);
52     }
53
54     @GetMapping(path = "/getpods")
55     public ResponseEntity<EliotPods> getAllPodList() throws ApiException {
56         Pod podDetail;
57
58         // the CoreV1Api loads default api-client from global configuration.
59         CoreV1Api api = new CoreV1Api();
60
61         // invokes the CoreV1Api client
62
63         V1PodList eliotPods = api.listPodForAllNamespaces(null,null,
64                 null,null,null,null,null,null,null);
65
66         EliotPods podDetailList = new EliotPods();
67         List<Pod> podlistElement = new ArrayList<>();
68
69
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);
79         }
80
81         podDetailList.setEliotPods(podlistElement);
82         return new ResponseEntity<>(podDetailList, HttpStatus.OK);
83     }
84
85
86     @GetMapping(path = "/getservices")
87     public ResponseEntity<EliotServices> getServiceList() throws ApiException{
88
89         CoreV1Api api = new CoreV1Api();
90         Service serviceDetail;
91         //JSON Object for ELIOT Services being send in Response.
92         EliotServices svcDetailList = new EliotServices();
93
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);
98
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("");
110
111                 listOfServiceElement.add(serviceDetail);
112
113             }
114             svcDetailList.setEliotServices(listOfServiceElement);
115
116
117         return new ResponseEntity<>(svcDetailList,HttpStatus.OK);
118
119     }
120 }