Skip to content

feat: improve logging #1542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.javaoperatorsdk.operator.OperatorException;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.api.config.Cloner;
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
Expand Down Expand Up @@ -104,14 +105,15 @@ private InformerWrapper<T> createEventSource(

@Override
public void stop() {
for (InformerWrapper<T> source : sources.values()) {
log.info("Stopping {}", this);
sources.forEach((ns, source) -> {
try {
log.info("Stopping informer {} -> {}", this, source);
log.debug("Stopping informer for namespace: {} -> {}", ns, source);
source.stop();
} catch (Exception e) {
log.warn("Error stopping informer {} -> {}", this, source, e);
log.warn("Error stopping informer for namespace: {} -> {}", ns, source, e);
}
}
});
}

@Override
Expand Down Expand Up @@ -167,4 +169,14 @@ public List<T> byIndex(String indexName, String indexKey) {
return sources.values().stream().map(s -> s.byIndex(indexName, indexKey))
.flatMap(List::stream).collect(Collectors.toList());
}

@Override
public String toString() {
final var selector = configuration.getLabelSelector();
return "InformerManager ["
+ ReconcilerUtils.getResourceTypeNameWithVersion(configuration.getResourceClass())
+ "] watching: "
+ configuration.getEffectiveNamespaces()
+ (selector != null ? " selector: " + selector : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ public void start() throws OperatorException {
try {
informer.run();
} catch (Exception e) {
final var apiTypeClass = informer.getApiTypeClass();
final var fullResourceName = HasMetadata.getFullResourceName(apiTypeClass);
final var version = HasMetadata.getVersion(apiTypeClass);
log.error("Couldn't start informer for " + fullResourceName + "/" + version + " resources",
e);
ReconcilerUtils.handleKubernetesClientException(e, fullResourceName);
log.error("Couldn't start informer for " + versionedFullResourceName() + " resources", e);
ReconcilerUtils.handleKubernetesClientException(e,
HasMetadata.getFullResourceName(informer.getApiTypeClass()));
throw e;
}
}

private String versionedFullResourceName() {
final var apiTypeClass = informer.getApiTypeClass();
return ReconcilerUtils.getResourceTypeNameWithVersion(apiTypeClass);
}

@Override
public void stop() throws OperatorException {
informer.stop();
Expand Down Expand Up @@ -92,4 +94,9 @@ public void addIndexers(Map<String, Function<T, List<String>>> indexers) {
public List<T> byIndex(String indexName, String indexKey) {
return informer.getIndexer().byIndex(indexName, indexKey);
}

@Override
public String toString() {
return "InformerWrapper [" + versionedFullResourceName() + "] (" + informer + ')';
}
}