use of io.fabric8.kubernetes.api.model.PodList in project ephemerals by LivePersonInc.
the class KubernetesDeploymentStatusWaiter method getDeploymentStatus.
@Override
public DeploymentStatus getDeploymentStatus() {
PodList list = client.pods().withLabel(KubernetesDeploymentHandler.DEPLOYMENT_LABEL_KEY, deployment.getId()).list();
Pod pod = list.getItems().get(0);
ContainerStatus containerStatus = pod.getStatus().getContainerStatuses().get(0);
switch(pod.getStatus().getPhase()) {
case "Pending":
return DeploymentStatus.IN_PROGRESS;
case "Running":
if (containerStatus.getReady()) {
return DeploymentStatus.FINISHED;
} else // Out of memory / CPU issues
if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && (containerStatus.getLastState().getTerminated().getExitCode() == 137 || containerStatus.getLastState().getTerminated().getExitCode() == 143)) {
return DeploymentStatus.FAILED;
} else // General error
if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated().getReason().contains("Error") && containerStatus.getState().getTerminated().getReason().contains("Error") && containerStatus.getLastState().getTerminated().getExitCode().equals(containerStatus.getState().getTerminated().getExitCode())) {
return DeploymentStatus.FAILED;
} else // CrashLoopBackOff errors
if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated() != null && containerStatus.getState().getWaiting().getReason().contains("CrashLoopBackOff")) {
return DeploymentStatus.FAILED;
} else // Partial deployment if it was terminated and not restarted
if (containerStatus.getRestartCount() == 0 && containerStatus.getState().getTerminated() != null) {
return DeploymentStatus.PARTIALLY;
} else {
return DeploymentStatus.IN_PROGRESS;
}
case "Failed":
return DeploymentStatus.FAILED;
case "Unknown":
return DeploymentStatus.UNKNOWN;
default:
return DeploymentStatus.UNKNOWN;
}
}
use of io.fabric8.kubernetes.api.model.PodList in project strimzi by strimzi.
the class MockKube method buildStatefulSets.
private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> buildStatefulSets(MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods) {
return new AbstractMockBuilder<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>>(StatefulSet.class, StatefulSetList.class, DoneableStatefulSet.class, castClass(RollableScalableResource.class), ssDb) {
@Override
protected void nameScopedMocks(RollableScalableResource<StatefulSet, DoneableStatefulSet> resource, String resourceName) {
mockGet(resourceName, resource);
// mockCreate("endpoint", endpointDb, resourceName, resource);
mockCascading(resource);
mockPatch(resourceName, resource);
mockDelete(resourceName, resource);
mockIsReady(resourceName, resource);
when(resource.create(any())).thenAnswer(cinvocation -> {
checkNotExists(resourceName);
StatefulSet argument = cinvocation.getArgument(0);
LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
ssDb.put(resourceName, copyResource(argument));
for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
String podName = argument.getMetadata().getName() + "-" + i;
podDb.put(podName, new PodBuilder().withNewMetadata().withNamespace(argument.getMetadata().getNamespace()).withName(podName).endMetadata().build());
}
return argument;
});
EditReplacePatchDeletable<StatefulSet, StatefulSet, DoneableStatefulSet, Boolean> c = mock(EditReplacePatchDeletable.class);
when(resource.cascading(false)).thenReturn(c);
when(c.patch(any())).thenAnswer(patchInvocation -> {
StatefulSet argument = patchInvocation.getArgument(0);
return doPatch(resourceName, argument);
});
when(resource.scale(anyInt(), anyBoolean())).thenAnswer(invocation -> {
checkDoesExist(resourceName);
StatefulSet ss = copyResource(ssDb.get(resourceName));
int newScale = invocation.getArgument(0);
ss.getSpec().setReplicas(newScale);
return doPatch(resourceName, ss);
});
when(resource.scale(anyInt())).thenAnswer(invocation -> {
checkDoesExist(resourceName);
StatefulSet ss = copyResource(ssDb.get(resourceName));
int newScale = invocation.getArgument(0);
ss.getSpec().setReplicas(newScale);
return doPatch(resourceName, ss);
});
when(resource.isReady()).thenAnswer(i -> {
LOGGER.debug("{} {} is ready", resourceType, resourceName);
return true;
});
mockPods.inNamespace(any()).withName(any()).watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
if (action == Action.DELETED) {
String podName = resource.getMetadata().getName();
String podNamespace = resource.getMetadata().getNamespace();
StatefulSet statefulSet = ssDb.get(resourceName);
if (podName.startsWith(resourceName + "-") && Integer.parseInt(podName.substring(podName.lastIndexOf("-") + 1)) < statefulSet.getSpec().getReplicas()) {
mockPods.inNamespace(podNamespace).withName(podName).create(resource);
}
}
}
@Override
public void onClose(KubernetesClientException e) {
}
});
}
private StatefulSet doPatch(String resourceName, StatefulSet argument) {
int oldScale = ssDb.get(resourceName).getSpec().getReplicas();
int newScale = argument.getSpec().getReplicas();
if (newScale > oldScale) {
LOGGER.debug("scaling up {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
Pod examplePod = mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(argument.getMetadata().getName() + "-0").get();
for (int i = oldScale; i < newScale; i++) {
String newPodName = argument.getMetadata().getName() + "-" + i;
mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).create(new PodBuilder(examplePod).editMetadata().withName(newPodName).endMetadata().build());
}
ssDb.put(resourceName, copyResource(argument));
} else if (newScale < oldScale) {
ssDb.put(resourceName, copyResource(argument));
LOGGER.debug("scaling down {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
for (int i = oldScale - 1; i >= newScale; i--) {
String newPodName = argument.getMetadata().getName() + "-" + i;
mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).delete();
}
} else {
ssDb.put(resourceName, copyResource(argument));
}
return argument;
}
}.build();
}
use of io.fabric8.kubernetes.api.model.PodList in project strimzi by strimzi.
the class MockKube method build.
public KubernetesClient build() {
KubernetesClient mockClient = mock(KubernetesClient.class);
MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> mockCms = buildConfigMaps();
MixedOperation<PersistentVolumeClaim, PersistentVolumeClaimList, DoneablePersistentVolumeClaim, Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim>> mockPvcs = buildPvcs();
MixedOperation<Endpoints, EndpointsList, DoneableEndpoints, Resource<Endpoints, DoneableEndpoints>> mockEndpoints = buildEndpoints();
MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> mockSvc = buildServices();
MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods = buildPods();
MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> mockSs = buildStatefulSets(mockPods);
MixedOperation<Deployment, DeploymentList, DoneableDeployment, ScalableResource<Deployment, DoneableDeployment>> mockDep = buildDeployments();
when(mockClient.configMaps()).thenReturn(mockCms);
when(mockClient.services()).thenReturn(mockSvc);
AppsAPIGroupDSL api = mock(AppsAPIGroupDSL.class);
when(api.statefulSets()).thenReturn(mockSs);
when(mockClient.apps()).thenReturn(api);
ExtensionsAPIGroupDSL ext = mock(ExtensionsAPIGroupDSL.class);
when(mockClient.extensions()).thenReturn(ext);
when(ext.deployments()).thenReturn(mockDep);
when(mockClient.pods()).thenReturn(mockPods);
when(mockClient.endpoints()).thenReturn(mockEndpoints);
when(mockClient.persistentVolumeClaims()).thenReturn(mockPvcs);
return mockClient;
}
use of io.fabric8.kubernetes.api.model.PodList in project zeppelin by apache.
the class PodPhaseWatcherTest method testPhase.
@Test
@Ignore("Reamer - ZEPPELIN-5403")
public void testPhase() throws InterruptedException {
KubernetesClient client = server.getClient();
// CREATE
client.pods().inNamespace("ns1").create(new PodBuilder().withNewMetadata().withName("pod1").endMetadata().build());
// READ
PodList podList = client.pods().inNamespace("ns1").list();
assertNotNull(podList);
assertEquals(1, podList.getItems().size());
Pod pod = podList.getItems().get(0);
// WATCH
PodPhaseWatcher podWatcher = new PodPhaseWatcher(phase -> StringUtils.equalsAnyIgnoreCase(phase, "Succeeded", "Failed", "Running"));
Watch watch = client.pods().inNamespace("ns1").withName("pod1").watch(podWatcher);
// Update Pod to "pending" phase
pod.setStatus(new PodStatus(null, null, null, null, null, null, null, "Pending", null, null, null, null, null));
client.pods().inNamespace("ns1").updateStatus(pod);
// Update Pod to "Running" phase
pod.setStatus(new PodStatusBuilder(new PodStatus(null, null, null, null, null, null, null, "Running", null, null, null, null, null)).build());
client.pods().inNamespace("ns1").updateStatus(pod);
assertTrue(podWatcher.getCountDownLatch().await(1, TimeUnit.SECONDS));
watch.close();
}
use of io.fabric8.kubernetes.api.model.PodList in project load-balancer by RestComm.
the class ServerControllerKube method startServer.
@Override
public void startServer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
PodList pods = kube.pods().list();
List<Pod> items = pods.getItems();
for (Pod pod : items) {
if (isValidPod(pod)) {
String currSessionId = getSessionId(pod);
Node currNode = activeNodes.get(currSessionId);
if (currNode == null) {
for (ContainerStatus status : pod.getStatus().getContainerStatuses()) {
if (status.getName().startsWith(nodeName) && status.getReady()) {
Node newNode = getNodeFromPod(pod);
JsonObject jsonObject = parser.parse(gson.toJson(new StartRequestPacket(newNode))).getAsJsonObject();
listener.startRequestReceived(null, jsonObject);
activeNodes.put(newNode.getProperties().get(Protocol.SESSION_ID), newNode);
}
}
} else if (!isGracefulShutdown(pod) || currNode.isGracefulShutdown()) {
JsonObject jsonObject = parser.parse(gson.toJson(new HeartbeatRequestPacket(currNode))).getAsJsonObject();
listener.heartbeatRequestReceived(null, jsonObject);
} else {
if (!currNode.isGracefulShutdown()) {
currNode.setGracefulShutdown(true);
JsonObject jsonObject = parser.parse(gson.toJson(new NodeShutdownRequestPacket(currNode))).getAsJsonObject();
listener.shutdownRequestReceived(null, jsonObject);
}
}
}
}
}
}, 2000, pullPeriod);
}
Aggregations