Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract unrecoverable events listener from KubernetesInternalRuntime
- Loading branch information
1 parent
d9c210b
commit 21798bf
Showing
7 changed files
with
390 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...g/eclipse/che/workspace/infrastructure/kubernetes/util/UnrecoverablePodEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.workspace.infrastructure.kubernetes.util; | ||
|
||
import com.google.common.base.Strings; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent; | ||
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEventHandler; | ||
|
||
/** | ||
* Listens Pod events and propagates unrecoverable events via the specified handler. | ||
* | ||
* @author Sergii Leshchenko | ||
* @author Ilya Buziuk | ||
*/ | ||
public class UnrecoverablePodEventListener implements PodEventHandler { | ||
|
||
private final Set<String> pods; | ||
private final Consumer<PodEvent> unrecoverableEventHandler; | ||
private final Set<String> unrecoverableEvents; | ||
|
||
public UnrecoverablePodEventListener( | ||
Set<String> unrecoverableEvents, | ||
Set<String> pods, | ||
Consumer<PodEvent> unrecoverableEventHandler) { | ||
this.unrecoverableEvents = unrecoverableEvents; | ||
this.pods = pods; | ||
this.unrecoverableEventHandler = unrecoverableEventHandler; | ||
} | ||
|
||
@Override | ||
public void handle(PodEvent event) { | ||
if (isWorkspaceEvent(event) && isUnrecoverable(event)) { | ||
unrecoverableEventHandler.accept(event); | ||
} | ||
} | ||
|
||
/** Returns true if event belongs to one of the workspace pods, false otherwise */ | ||
private boolean isWorkspaceEvent(PodEvent event) { | ||
String podName = event.getPodName(); | ||
if (Strings.isNullOrEmpty(podName)) { | ||
return false; | ||
} | ||
// Note it is necessary to compare via startsWith rather than equals here, as pods managed by | ||
// deployments have their name set as [deploymentName]-[hash]. `workspacePodName` is used to | ||
// define the deployment name, so pods that are created aren't an exact match. | ||
return pods.stream().anyMatch(podName::startsWith); | ||
} | ||
|
||
/** | ||
* Returns true if event reason or message matches one of the comma separated values defined in | ||
* 'che.infra.kubernetes.workspace_unrecoverable_events',false otherwise | ||
* | ||
* @param event event to check | ||
*/ | ||
private boolean isUnrecoverable(PodEvent event) { | ||
boolean isUnrecoverable = false; | ||
String reason = event.getReason(); | ||
String message = event.getMessage(); | ||
// Consider unrecoverable if event reason 'equals' one of the property values e.g. "Failed | ||
// Mount" | ||
if (unrecoverableEvents.contains(reason)) { | ||
isUnrecoverable = true; | ||
} else { | ||
for (String e : unrecoverableEvents) { | ||
// Consider unrecoverable if event message 'startsWith' one of the property values e.g. | ||
// "Failed to pull image" | ||
if (message != null && message.startsWith(e)) { | ||
isUnrecoverable = true; | ||
} | ||
} | ||
} | ||
return isUnrecoverable; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...se/che/workspace/infrastructure/kubernetes/util/UnrecoverablePodEventListenerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.workspace.infrastructure.kubernetes.util; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent; | ||
|
||
/** | ||
* Helps to create {@link UnrecoverablePodEventListener} instaces. | ||
* | ||
* @author Sergii Leshchenko | ||
*/ | ||
@Singleton | ||
public class UnrecoverablePodEventListenerFactory { | ||
|
||
private final Set<String> unrecoverableEvents; | ||
|
||
@Inject | ||
public UnrecoverablePodEventListenerFactory( | ||
@Named("che.infra.kubernetes.workspace_unrecoverable_events") String[] unrecoverableEvents) { | ||
this.unrecoverableEvents = ImmutableSet.copyOf(unrecoverableEvents); | ||
} | ||
|
||
/** | ||
* Creates unrecoverable events listener. | ||
* | ||
* @param pods pods which unrecoverable events should be propagated | ||
* @param unrecoverableEventHandler handler which is invoked when unrecoverable event occurs | ||
* @return created unrecoverable events listener | ||
* @throws IllegalStateException is unrecoverable events are not configured. | ||
* @see #isConfigured() | ||
*/ | ||
public UnrecoverablePodEventListener create( | ||
Set<String> pods, Consumer<PodEvent> unrecoverableEventHandler) { | ||
if (!isConfigured()) { | ||
throw new IllegalStateException("Unrecoverable events are not configured"); | ||
} | ||
|
||
return new UnrecoverablePodEventListener(unrecoverableEvents, pods, unrecoverableEventHandler); | ||
} | ||
|
||
/** | ||
* Returns true if unrecoverable events are configured and it's possible to create unrecoverable | ||
* events listener, false otherwise | ||
*/ | ||
public boolean isConfigured() { | ||
return !unrecoverableEvents.isEmpty(); | ||
} | ||
} |
Oops, something went wrong.