-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: runtime info for health probes (#1594)
- Loading branch information
Showing
25 changed files
with
486 additions
and
39 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
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
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
81 changes: 81 additions & 0 deletions
81
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/RuntimeInfo.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,81 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.util.*; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.javaoperatorsdk.operator.health.EventSourceHealthIndicator; | ||
import io.javaoperatorsdk.operator.health.InformerWrappingEventSourceHealthIndicator; | ||
|
||
/** | ||
* RuntimeInfo in general is available when operator is fully started. You can use "isStarted" to | ||
* check that. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public class RuntimeInfo { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(RuntimeInfo.class); | ||
|
||
private final Set<RegisteredController> registeredControllers; | ||
private final Operator operator; | ||
|
||
public RuntimeInfo(Operator operator) { | ||
this.registeredControllers = operator.getRegisteredControllers(); | ||
this.operator = operator; | ||
} | ||
|
||
public boolean isStarted() { | ||
return operator.isStarted(); | ||
} | ||
|
||
public Set<RegisteredController> getRegisteredControllers() { | ||
checkIfStarted(); | ||
return registeredControllers; | ||
} | ||
|
||
private void checkIfStarted() { | ||
if (!isStarted()) { | ||
log.warn( | ||
"Operator not started yet while accessing runtime info, this might lead to an unreliable behavior"); | ||
} | ||
} | ||
|
||
public boolean allEventSourcesAreHealthy() { | ||
checkIfStarted(); | ||
return registeredControllers.stream() | ||
.filter(rc -> !rc.getControllerHealthInfo().unhealthyEventSources().isEmpty()) | ||
.findFirst().isEmpty(); | ||
} | ||
|
||
/** | ||
* @return Aggregated Map with controller related event sources. | ||
*/ | ||
|
||
public Map<String, Map<String, EventSourceHealthIndicator>> unhealthyEventSources() { | ||
checkIfStarted(); | ||
Map<String, Map<String, EventSourceHealthIndicator>> res = new HashMap<>(); | ||
for (var rc : registeredControllers) { | ||
res.put(rc.getConfiguration().getName(), | ||
rc.getControllerHealthInfo().unhealthyEventSources()); | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* @return Aggregated Map with controller related event sources that wraps an informer. Thus, | ||
* either a | ||
* {@link io.javaoperatorsdk.operator.processing.event.source.controller.ControllerResourceEventSource} | ||
* or an | ||
* {@link io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource}. | ||
*/ | ||
public Map<String, Map<String, InformerWrappingEventSourceHealthIndicator>> unhealthyInformerWrappingEventSourceHealthIndicator() { | ||
checkIfStarted(); | ||
Map<String, Map<String, InformerWrappingEventSourceHealthIndicator>> res = new HashMap<>(); | ||
for (var rc : registeredControllers) { | ||
res.put(rc.getConfiguration().getName(), rc.getControllerHealthInfo() | ||
.unhealthyInformerEventSourceHealthIndicators()); | ||
} | ||
return res; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...framework-core/src/main/java/io/javaoperatorsdk/operator/health/ControllerHealthInfo.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,50 @@ | ||
package io.javaoperatorsdk.operator.health; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import io.javaoperatorsdk.operator.processing.event.EventSourceManager; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public class ControllerHealthInfo { | ||
|
||
private EventSourceManager<?> eventSourceManager; | ||
|
||
public ControllerHealthInfo(EventSourceManager eventSourceManager) { | ||
this.eventSourceManager = eventSourceManager; | ||
} | ||
|
||
public Map<String, EventSourceHealthIndicator> eventSourceHealthIndicators() { | ||
return eventSourceManager.allEventSources().entrySet().stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
public Map<String, EventSourceHealthIndicator> unhealthyEventSources() { | ||
return eventSourceManager.allEventSources().entrySet().stream() | ||
.filter(e -> e.getValue().getStatus() == Status.UNHEALTHY) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
public Map<String, InformerWrappingEventSourceHealthIndicator> informerEventSourceHealthIndicators() { | ||
return eventSourceManager.allEventSources().entrySet().stream() | ||
.filter(e -> e.getValue() instanceof InformerWrappingEventSourceHealthIndicator) | ||
.collect(Collectors.toMap(Map.Entry::getKey, | ||
e -> (InformerWrappingEventSourceHealthIndicator) e.getValue())); | ||
|
||
} | ||
|
||
/** | ||
* @return Map with event sources that wraps an informer. Thus, either a | ||
* {@link io.javaoperatorsdk.operator.processing.event.source.controller.ControllerResourceEventSource} | ||
* or an | ||
* {@link io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource}. | ||
*/ | ||
public Map<String, InformerWrappingEventSourceHealthIndicator> unhealthyInformerEventSourceHealthIndicators() { | ||
return eventSourceManager.allEventSources().entrySet().stream() | ||
.filter(e -> e.getValue().getStatus() == Status.UNHEALTHY) | ||
.filter(e -> e.getValue() instanceof InformerWrappingEventSourceHealthIndicator) | ||
.collect(Collectors.toMap(Map.Entry::getKey, | ||
e -> (InformerWrappingEventSourceHealthIndicator) e.getValue())); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...ork-core/src/main/java/io/javaoperatorsdk/operator/health/EventSourceHealthIndicator.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,6 @@ | ||
package io.javaoperatorsdk.operator.health; | ||
|
||
public interface EventSourceHealthIndicator { | ||
|
||
Status getStatus(); | ||
} |
17 changes: 17 additions & 0 deletions
17
...mework-core/src/main/java/io/javaoperatorsdk/operator/health/InformerHealthIndicator.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,17 @@ | ||
package io.javaoperatorsdk.operator.health; | ||
|
||
public interface InformerHealthIndicator extends EventSourceHealthIndicator { | ||
|
||
boolean hasSynced(); | ||
|
||
boolean isWatching(); | ||
|
||
boolean isRunning(); | ||
|
||
@Override | ||
default Status getStatus() { | ||
return isRunning() && hasSynced() && isWatching() ? Status.HEALTHY : Status.UNHEALTHY; | ||
} | ||
|
||
String getTargetNamespace(); | ||
} |
22 changes: 22 additions & 0 deletions
22
...n/java/io/javaoperatorsdk/operator/health/InformerWrappingEventSourceHealthIndicator.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,22 @@ | ||
package io.javaoperatorsdk.operator.health; | ||
|
||
import java.util.Map; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration; | ||
|
||
public interface InformerWrappingEventSourceHealthIndicator<R extends HasMetadata> | ||
extends EventSourceHealthIndicator { | ||
|
||
Map<String, InformerHealthIndicator> informerHealthIndicators(); | ||
|
||
@Override | ||
default Status getStatus() { | ||
var nonUp = informerHealthIndicators().values().stream() | ||
.filter(i -> i.getStatus() != Status.HEALTHY).findAny(); | ||
|
||
return nonUp.isPresent() ? Status.UNHEALTHY : Status.HEALTHY; | ||
} | ||
|
||
ResourceConfiguration<R> getInformerConfiguration(); | ||
} |
11 changes: 11 additions & 0 deletions
11
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/health/Status.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,11 @@ | ||
package io.javaoperatorsdk.operator.health; | ||
|
||
public enum Status { | ||
|
||
HEALTHY, UNHEALTHY, | ||
/** | ||
* For event sources where it cannot be determined if it is healthy ot not. | ||
*/ | ||
UNKNOWN | ||
|
||
} |
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
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
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
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
Oops, something went wrong.