Search in sources :

Example 11 with PodList

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;
    }
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 12 with PodList

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();
}
Also used : DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) Doneable(io.fabric8.kubernetes.api.model.Doneable) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) Resource(io.fabric8.kubernetes.client.dsl.Resource) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PersistentVolumeClaimList(io.fabric8.kubernetes.api.model.PersistentVolumeClaimList) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) ExtensionsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.ExtensionsAPIGroupDSL) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) DoneableDeployment(io.fabric8.kubernetes.api.model.extensions.DoneableDeployment) DoneableEndpoints(io.fabric8.kubernetes.api.model.DoneableEndpoints) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DeploymentList(io.fabric8.kubernetes.api.model.extensions.DeploymentList) EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Service(io.fabric8.kubernetes.api.model.Service) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) Watcher(io.fabric8.kubernetes.client.Watcher) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 13 with PodList

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;
}
Also used : ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) PersistentVolumeClaimList(io.fabric8.kubernetes.api.model.PersistentVolumeClaimList) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DoneableDeployment(io.fabric8.kubernetes.api.model.extensions.DoneableDeployment) DoneableEndpoints(io.fabric8.kubernetes.api.model.DoneableEndpoints) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) DoneableDeployment(io.fabric8.kubernetes.api.model.extensions.DoneableDeployment) DeploymentList(io.fabric8.kubernetes.api.model.extensions.DeploymentList) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ExtensionsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.ExtensionsAPIGroupDSL) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodList(io.fabric8.kubernetes.api.model.PodList) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Resource(io.fabric8.kubernetes.client.dsl.Resource) ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) DoneableEndpoints(io.fabric8.kubernetes.api.model.DoneableEndpoints) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 14 with PodList

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();
}
Also used : PodStatus(io.fabric8.kubernetes.api.model.PodStatus) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Watch(io.fabric8.kubernetes.client.Watch) PodStatusBuilder(io.fabric8.kubernetes.api.model.PodStatusBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with PodList

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);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Pod(io.fabric8.kubernetes.api.model.Pod) StartRequestPacket(org.mobicents.tools.heartbeat.api.StartRequestPacket) Node(org.mobicents.tools.heartbeat.api.Node) HeartbeatRequestPacket(org.mobicents.tools.heartbeat.api.HeartbeatRequestPacket) JsonObject(com.google.gson.JsonObject) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) NodeShutdownRequestPacket(org.mobicents.tools.heartbeat.api.NodeShutdownRequestPacket)

Aggregations

PodList (io.fabric8.kubernetes.api.model.PodList)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Test (org.junit.Test)8 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)7 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)7 Watch (io.fabric8.kubernetes.client.Watch)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Watcher (io.fabric8.kubernetes.client.Watcher)5 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)4 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 Service (io.fabric8.kubernetes.api.model.Service)3 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)3 PodResource (io.fabric8.kubernetes.client.dsl.PodResource)3 Session (io.fabric8.arquillian.kubernetes.Session)2 Controller (io.fabric8.kubernetes.api.Controller)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)2