use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent in project devspaces-images by redhat-developer.
the class LogWatcherTest method executorIsCalledJustOnceWhenSameEventArriveAgain.
@Test
public void executorIsCalledJustOnceWhenSameEventArriveAgain() throws InfrastructureException {
// given
LogWatcher logWatcher = new LogWatcher(clientFactory, eventsPublisher, WORKSPACE_ID, NAMESPACE, PODNAMES, executor, TIMEOUTS, LIMIT_BYTES);
logWatcher.addLogHandler(handler);
PodEvent podEvent = new PodEvent(POD, "container123", "Started", "someevenbettermessage", "123456789", "987654321");
// when
logWatcher.handle(podEvent);
logWatcher.handle(podEvent);
// then
verify(executor, times(1)).execute(any());
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent in project devspaces-images by redhat-developer.
the class LogWatcherTest method executorIsNotCalledWhenPodNameDontMatch.
@Test
public void executorIsNotCalledWhenPodNameDontMatch() throws InfrastructureException {
// given
LogWatcher logWatcher = new LogWatcher(clientFactory, eventsPublisher, WORKSPACE_ID, NAMESPACE, PODNAMES, executor, TIMEOUTS, LIMIT_BYTES);
logWatcher.addLogHandler(handler);
PodEvent podEvent = new PodEvent("someOtherPod", "container123", "Started", "someevenbettermessage", "123456789", "987654321");
// when
logWatcher.handle(podEvent);
// then
verify(executor, times(0)).execute(any());
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent in project devspaces-images by redhat-developer.
the class UnrecoverablePodEventListenerTest method testFailedContainersInWorkspacePodAlwaysHandled.
@Test
public void testFailedContainersInWorkspacePodAlwaysHandled() {
// given
PodEvent ev = mockContainerEvent(WORKSPACE_POD_NAME, "Failed", "bah", EVENT_CREATION_TIMESTAMP, getCurrentTimestampWithOneHourShiftAhead());
// when
unrecoverableEventListener.handle(ev);
// then
verify(unrecoverableEventConsumer).accept(any());
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent in project devspaces-images by redhat-developer.
the class KubernetesDeployments method watchEvents.
/**
* Registers a specified handler for handling events about changes in pods containers. Registering
* several handlers doesn't create multiple websocket connections, so it is efficient to call this
* method several times instead of using composite handler to combine other handlers.
*
* @param handler pod container events handler
* @throws InfrastructureException if any error occurs while watcher starting
*/
public void watchEvents(PodEventHandler handler) throws InfrastructureException {
if (containerWatch == null) {
final Watcher<Event> watcher = new Watcher<>() {
@Override
public void eventReceived(Action action, Event event) {
ObjectReference involvedObject = event.getInvolvedObject();
if (POD_OBJECT_KIND.equals(involvedObject.getKind()) || REPLICASET_OBJECT_KIND.equals(involvedObject.getKind()) || DEPLOYMENT_OBJECT_KIND.equals(involvedObject.getKind())) {
String podName = involvedObject.getName();
String lastTimestamp = event.getLastTimestamp();
if (lastTimestamp == null) {
String firstTimestamp = event.getFirstTimestamp();
if (firstTimestamp != null) {
// Done in the same way like it made in
// https://github.com/kubernetes/kubernetes/pull/86557
lastTimestamp = firstTimestamp;
} else {
LOG.debug("lastTimestamp and firstTimestamp are undefined. Event: {}. Fallback to the current time.", event);
lastTimestamp = PodEvents.convertDateToEventTimestamp(new Date());
}
}
PodEvent podEvent = new PodEvent(podName, getContainerName(involvedObject.getFieldPath()), event.getReason(), event.getMessage(), event.getMetadata().getCreationTimestamp(), lastTimestamp);
try {
if (happenedAfterWatcherInitialization(podEvent)) {
containerEventsHandlers.forEach(h -> h.handle(podEvent));
}
} catch (ParseException e) {
LOG.error("Failed to parse last timestamp of the event. Cause: {}. Event: {}", e.getMessage(), podEvent, e);
}
}
}
@Override
public void onClose(WatcherException ignored) {
}
/**
* Returns the container name if the event is related to container. When the event is
* related to container `fieldPath` field contain information in the following format:
* `spec.container{web}`, where `web` is container name
*/
private String getContainerName(String fieldPath) {
String containerName = null;
if (fieldPath != null) {
Matcher containerFieldMatcher = CONTAINER_FIELD_PATH_PATTERN.matcher(fieldPath);
if (containerFieldMatcher.matches()) {
containerName = containerFieldMatcher.group(CONTAINER_NAME_GROUP);
}
}
return containerName;
}
/**
* Returns true if 'lastTimestamp' of the event is *after* the time of the watcher
* initialization
*/
private boolean happenedAfterWatcherInitialization(PodEvent event) throws ParseException {
String eventLastTimestamp = event.getLastTimestamp();
Date eventLastTimestampDate = PodEvents.convertEventTimestampToDate(eventLastTimestamp);
return eventLastTimestampDate.after(watcherInitializationDate);
}
};
try {
watcherInitializationDate = new Date();
containerWatch = clientFactory.create(workspaceId).v1().events().inNamespace(namespace).watch(watcher);
} catch (KubernetesClientException ex) {
throw new KubernetesInfrastructureException(ex);
}
}
containerEventsHandlers.add(handler);
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent in project devspaces-images by redhat-developer.
the class KubernetesInternalRuntimeTest method testDoNotPublishForeignMachineOutput.
@Test
public void testDoNotPublishForeignMachineOutput() throws ParseException {
final MachineLogsPublisher logsPublisher = new MachineLogsPublisher(eventPublisher, machinesCache, IDENTITY);
final PodEvent out1 = mockContainerEvent(WORKSPACE_POD_NAME, "Created", "folder created", EVENT_CREATION_TIMESTAMP, getCurrentTimestampWithOneHourShiftAhead());
logsPublisher.handle(out1);
verify(eventService, never()).publish(any());
}
Aggregations