Move post construct (#1319)

This commit is contained in:
erabii
2023-04-26 00:08:59 +03:00
committed by GitHub
parent f404629a4f
commit 0e7ce7b391
2 changed files with 33 additions and 18 deletions

View File

@@ -16,13 +16,19 @@
package org.springframework.cloud.kubernetes.client.discovery;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import io.kubernetes.client.informer.SharedInformerFactory;
import io.kubernetes.client.informer.cache.Lister;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServiceSpec;
import io.kubernetes.client.util.wait.Wait;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
@@ -101,4 +107,29 @@ final class KubernetesDiscoveryClientUtils {
return serviceMetadata;
}
static void postConstruct(List<SharedInformerFactory> sharedInformerFactories,
KubernetesDiscoveryProperties properties, Supplier<Boolean> informersReadyFunc,
List<Lister<V1Service>> serviceListers) {
sharedInformerFactories.forEach(SharedInformerFactory::startAllRegisteredInformers);
if (!Wait.poll(Duration.ofSeconds(1), Duration.ofSeconds(properties.cacheLoadingTimeoutSeconds()), () -> {
LOG.info(() -> "Waiting for the cache of informers to be fully loaded..");
return informersReadyFunc.get();
})) {
if (properties.waitCacheReady()) {
throw new IllegalStateException(
"Timeout waiting for informers cache to be ready, is the kubernetes service up?");
}
else {
LOG.warn(() -> "Timeout waiting for informers cache to be ready, " +
"ignoring the failure because waitForInformerCacheReady property is false");
}
}
else {
LOG.info(() -> "Cache fully loaded (total " + serviceListers.stream().mapToLong(x -> x.list().size()).sum()
+ " services), discovery client is now available");
}
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.kubernetes.client.discovery;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -27,7 +26,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import io.kubernetes.client.extended.wait.Wait;
import io.kubernetes.client.informer.SharedInformer;
import io.kubernetes.client.informer.SharedInformerFactory;
import io.kubernetes.client.informer.cache.Lister;
@@ -47,6 +45,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.kubernetes.client.discovery.KubernetesDiscoveryClientUtils.matchesServiceLabels;
import static org.springframework.cloud.kubernetes.client.discovery.KubernetesDiscoveryClientUtils.postConstruct;
import static org.springframework.cloud.kubernetes.client.discovery.KubernetesDiscoveryClientUtils.serviceMetadata;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
@@ -238,22 +237,7 @@ public class KubernetesInformerDiscoveryClient implements DiscoveryClient {
@PostConstruct
public void afterPropertiesSet() {
sharedInformerFactories.forEach(SharedInformerFactory::startAllRegisteredInformers);
if (!Wait.poll(Duration.ofSeconds(1), Duration.ofSeconds(properties.cacheLoadingTimeoutSeconds()), () -> {
LOG.info(() -> "Waiting for the cache of informers to be fully loaded..");
return informersReadyFunc.get();
})) {
if (properties.waitCacheReady()) {
throw new IllegalStateException(
"Timeout waiting for informers cache to be ready, is the kubernetes service up?");
}
else {
LOG.warn(
() -> "Timeout waiting for informers cache to be ready, ignoring the failure because waitForInformerCacheReady property is false");
}
}
LOG.info(() -> "Cache fully loaded (total " + serviceListers.stream().mapToLong(x -> x.list().size()).sum()
+ " services), discovery client is now available");
postConstruct(sharedInformerFactories, properties, informersReadyFunc, serviceListers);
}
@Override