Skip to content

Commit 2a7119e

Browse files
author
Jan Kleinert
committedFeb 13, 2019
Initial commit
0 parents  commit 2a7119e

File tree

8 files changed

+386
-0
lines changed

8 files changed

+386
-0
lines changed
 

‎.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
.classpath
3+
.project
4+
.settings/
5+
.vscode/settings\.json
6+
.vscode/

‎pom.xml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.openshift</groupId>
7+
<artifactId>k8svisualizer</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>k8svisualizer</name>
12+
<description>k8svisualizer</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.9.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-web</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.fabric8</groupId>
47+
<artifactId>spring-cloud-starter-kubernetes</artifactId>
48+
<version>0.0.16</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.fabric8</groupId>
52+
<artifactId>openshift-client</artifactId>
53+
<version>1.4.10</version>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.openshift.k8svisualizer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class K8sVisualizerBackend {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(K8sVisualizerBackend.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.openshift.k8svisualizer.controllers;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.bind.annotation.CrossOrigin;
10+
import com.openshift.k8svisualizer.helpers.PlatformObjectHelper;
11+
//import com.openshift.k8svisualizer.models.Game;
12+
//import com.openshift.k8svisualizer.models.Score;
13+
import com.openshift.k8svisualizer.models.PlatformObject;
14+
15+
16+
// oc policy add-role-to-user view system:serviceaccount:wildwest:default where wildwest
17+
// is the project name
18+
19+
// To enable destructrive mode, a different permission needs to be added
20+
// oc policy add-role-to-user edit system:serviceaccount:wildwest:default
21+
22+
23+
@CrossOrigin(origins = "*")
24+
@RestController
25+
public class APIController {
26+
@Autowired
27+
// private GameController gameController;
28+
29+
/* @RequestMapping("/score")
30+
public Score getScore(@RequestParam(value = "gameID") String gameID) {
31+
return this.gameController.getGame(gameID).getScore();
32+
}
33+
34+
@RequestMapping("/createGame")
35+
public Game getScore() {
36+
return gameController.createGame();
37+
}*/
38+
39+
@RequestMapping("/egg")
40+
public String easterEgg() {
41+
return "Every app needs an easter egg!!";
42+
}
43+
44+
@RequestMapping("/objects")
45+
public List<PlatformObject> getPlatformObjects() {
46+
PlatformObjectHelper helper = new PlatformObjectHelper();
47+
return helper.getPlatformObjects();
48+
}
49+
50+
@RequestMapping("/getRandomObject")
51+
public PlatformObject getRandomPlatformObject() {
52+
PlatformObjectHelper helper = new PlatformObjectHelper();
53+
return helper.getRandomPlatformObject();
54+
}
55+
56+
@RequestMapping("/getPods")
57+
public List<PlatformObject> getPods() {
58+
PlatformObjectHelper helper = new PlatformObjectHelper();
59+
return helper.getPods();
60+
}
61+
62+
63+
64+
@RequestMapping("/getServices")
65+
public List<PlatformObject> getServices() {
66+
PlatformObjectHelper helper = new PlatformObjectHelper();
67+
return helper.getServices();
68+
}
69+
70+
@CrossOrigin
71+
@RequestMapping("/deleteObject")
72+
public void deletePlatformObject(@RequestParam(value = "gameID") String gameID,
73+
@RequestParam(value = "objectID") String objectID,
74+
@RequestParam(value = "objectType") String objectType,
75+
@RequestParam(value = "objectName") String objectName) {
76+
77+
PlatformObjectHelper helper = new PlatformObjectHelper();
78+
helper.deletePlatformObject(gameID, objectID, objectType, objectName);
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.openshift.k8svisualizer.controllers;
2+
3+
import java.util.ArrayList;
4+
import java.util.Hashtable;
5+
import java.util.List;
6+
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import io.fabric8.kubernetes.api.model.Pod;
11+
import io.fabric8.kubernetes.api.model.PodList;
12+
import io.fabric8.openshift.client.DefaultOpenShiftClient;
13+
import io.fabric8.openshift.client.OpenShiftClient;
14+
15+
@RestController
16+
public class TestAPI {
17+
18+
private OpenShiftClient client;
19+
20+
// oc policy add-role-to-user view system:serviceaccount:wildwest:default where wildwest
21+
// is the project name
22+
23+
// To enable destructive mode, a different permission needs to be added
24+
// oc policy add-role-to-user edit system:serviceaccount:wildwest:default
25+
26+
@RequestMapping("/kube")
27+
public Hashtable getPlatformObjects() {
28+
client = new DefaultOpenShiftClient();
29+
List<Pod> pods = null;
30+
PodList theList = client.pods().list();
31+
ArrayList arrayList = new ArrayList();
32+
Hashtable hashtable = new Hashtable<>();
33+
pods = theList.getItems();
34+
35+
for (Pod currPod : pods) {
36+
hashtable.put(currPod.getMetadata().getUid(), currPod.getMetadata().getName());
37+
}
38+
39+
return hashtable;
40+
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.openshift.k8svisualizer.helpers;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import com.openshift.k8svisualizer.models.PlatformObject;
7+
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
8+
import io.fabric8.kubernetes.api.model.Pod;
9+
import io.fabric8.kubernetes.api.model.Service;
10+
import io.fabric8.openshift.api.model.Build;
11+
import io.fabric8.openshift.api.model.BuildConfig;
12+
import io.fabric8.openshift.api.model.DeploymentConfig;
13+
import io.fabric8.openshift.api.model.Route;
14+
import io.fabric8.openshift.client.DefaultOpenShiftClient;
15+
import io.fabric8.openshift.client.OpenShiftClient;
16+
17+
public class PlatformObjectHelper {
18+
19+
private OpenShiftClient client;
20+
21+
public PlatformObjectHelper() {
22+
client = new DefaultOpenShiftClient();
23+
}
24+
25+
public List<PlatformObject> getPlatformObjects() {
26+
27+
ArrayList<PlatformObject> platformObjects = new ArrayList<>();
28+
platformObjects.addAll(this.getPods());
29+
platformObjects.addAll(this.getBuilds());
30+
platformObjects.addAll(this.getDeploymentConfigs());
31+
platformObjects.addAll(this.getBuildConfigs());
32+
platformObjects.addAll(this.getPVs());
33+
platformObjects.addAll(this.getServices());
34+
platformObjects.addAll(this.getRoutes());
35+
36+
return platformObjects;
37+
38+
}
39+
40+
public PlatformObject getRandomPlatformObject() {
41+
List<PlatformObject> theObjects = this.getPlatformObjects();
42+
43+
44+
return theObjects.get(new Random().nextInt(theObjects.size()));
45+
}
46+
47+
public List<PlatformObject> getPods() {
48+
String shortName;
49+
String[] result;
50+
ArrayList<PlatformObject> thePods = new ArrayList<>();
51+
List<Pod> pods = client.pods().withLabel("run", "hello-k8s").list().getItems();
52+
for (Pod currPod : pods) {
53+
result = currPod.getMetadata().getName().split("-");
54+
shortName = "hello-k8s..." + result[result.length - 1];
55+
thePods.add(new PlatformObject(currPod.getMetadata().getUid(), shortName, "POD", currPod.getStatus().getPhase()));
56+
}
57+
return thePods;
58+
}
59+
60+
private List<PlatformObject> getBuilds() {
61+
ArrayList<PlatformObject> theBuilds = new ArrayList<>();
62+
63+
List<Build> builds = client.builds().list().getItems();
64+
for (Build currBuild : builds) {
65+
theBuilds.add(
66+
new PlatformObject(currBuild.getMetadata().getUid(), currBuild.getMetadata().getName(), "BUILD", currBuild.getStatus().getPhase()));
67+
}
68+
69+
return theBuilds;
70+
}
71+
72+
private List<PlatformObject> getDeploymentConfigs() {
73+
ArrayList<PlatformObject> theDeployments = new ArrayList<>();
74+
List<DeploymentConfig> deploymentConfigs = client.deploymentConfigs().list().getItems();
75+
for (DeploymentConfig currConfig : deploymentConfigs) {
76+
theDeployments.add(new PlatformObject(currConfig.getMetadata().getUid(), currConfig.getMetadata().getName(),
77+
"DEPLOYMENT_CONFIG", currConfig.getStatus().getDetails().toString()));
78+
}
79+
80+
return theDeployments;
81+
}
82+
83+
private List<PlatformObject> getBuildConfigs() {
84+
ArrayList<PlatformObject> theList = new ArrayList<>();
85+
List<BuildConfig> theItems = client.buildConfigs().list().getItems();
86+
for (BuildConfig currConfig : theItems) {
87+
theList.add(new PlatformObject(currConfig.getMetadata().getUid(), currConfig.getMetadata().getName(),
88+
"BUILD_CONFIG", currConfig.getStatus().toString()));
89+
}
90+
return theList;
91+
}
92+
93+
private List<PlatformObject> getPVs() {
94+
ArrayList<PlatformObject> theList = new ArrayList<>();
95+
List<PersistentVolumeClaim> theItems = client.persistentVolumeClaims().list().getItems();
96+
for (PersistentVolumeClaim currConfig : theItems) {
97+
theList.add(
98+
new PlatformObject(currConfig.getMetadata().getUid(), currConfig.getMetadata().getName(), "PVC", currConfig.getStatus().toString()));
99+
}
100+
return theList;
101+
}
102+
103+
public List<PlatformObject> getServices() {
104+
ArrayList<PlatformObject> theList = new ArrayList<>();
105+
List<Service> theItems = client.services().withLabel("run", "hello-k8s").list().getItems();
106+
for (Service currConfig : theItems) {
107+
theList.add(new PlatformObject(currConfig.getMetadata().getUid(), currConfig.getMetadata().getName(),
108+
"SERVICE", currConfig.getStatus().toString()));
109+
}
110+
return theList;
111+
}
112+
113+
private List<PlatformObject> getRoutes() {
114+
ArrayList<PlatformObject> theList = new ArrayList<>();
115+
List<Route> theItems = client.routes().list().getItems();
116+
for (Route currConfig : theItems) {
117+
theList.add(
118+
new PlatformObject(currConfig.getMetadata().getUid(), currConfig.getMetadata().getName(), "ROUTE", currConfig.getStatus().toString()));
119+
}
120+
return theList;
121+
}
122+
123+
public void deletePlatformObject(String gameID, String objectID, String objectType, String objectName) {
124+
125+
// right now only delete things we can recover from
126+
switch (objectType) {
127+
case "POD":
128+
client.pods().withName(objectName).delete();
129+
break;
130+
case "BUILD":
131+
client.builds().withName(objectName).delete();
132+
break;
133+
}
134+
135+
}
136+
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.openshift.k8svisualizer.models;
2+
3+
public class PlatformObject {
4+
5+
private String objectID;
6+
private String objectName;
7+
private String objectType;
8+
private String objectStatus;
9+
10+
public PlatformObject(String objectID, String objectName, String objectType, String objectStatus) {
11+
this.objectID = objectID;
12+
this.objectName = objectName;
13+
this.objectType = objectType;
14+
this.objectStatus = objectStatus;
15+
}
16+
public String getObjectID() {
17+
return objectID;
18+
}
19+
public void setObjectID(String objectID) {
20+
this.objectID = objectID;
21+
}
22+
public String getObjectType() {
23+
return objectType;
24+
}
25+
public void setObjectType(String objectType) {
26+
this.objectType = objectType;
27+
}
28+
public String getObjectName() {
29+
return objectName;
30+
}
31+
public void setObjectName(String objectName) {
32+
this.objectName = objectName;
33+
}
34+
public String getObjectStatus() {
35+
return objectStatus;
36+
}
37+
public void setObjectStatus(String objectStatus) {
38+
this.objectStatus = objectStatus;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
controller=dummy

0 commit comments

Comments
 (0)
Please sign in to comment.