|
| 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 | +} |
0 commit comments