|
15 | 15 | */ |
16 | 16 | package io.javaoperatorsdk.operator.processing.event.source.informer.pool; |
17 | 17 |
|
18 | | -public class AbstractInformerPool {} |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.concurrent.ExecutionException; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.TimeoutException; |
| 22 | + |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import io.fabric8.kubernetes.api.model.GenericKubernetesResource; |
| 27 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 28 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 29 | +import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; |
| 30 | +import io.fabric8.kubernetes.client.informers.ExceptionHandler; |
| 31 | +import io.fabric8.kubernetes.client.informers.SharedIndexInformer; |
| 32 | +import io.javaoperatorsdk.operator.OperatorException; |
| 33 | +import io.javaoperatorsdk.operator.ReconcilerUtilsInternal; |
| 34 | +import io.javaoperatorsdk.operator.api.config.ConfigurationService; |
| 35 | + |
| 36 | +import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_ALL_NAMESPACES; |
| 37 | + |
| 38 | +public abstract class AbstractInformerPool implements InformerPool { |
| 39 | + |
| 40 | + private static final Logger log = LoggerFactory.getLogger(AbstractInformerPool.class); |
| 41 | + |
| 42 | + protected KubernetesClient client; |
| 43 | + protected ConfigurationService configurationService; |
| 44 | + |
| 45 | + public KubernetesClient getClient() { |
| 46 | + return client; |
| 47 | + } |
| 48 | + |
| 49 | + public void setClient(KubernetesClient client) { |
| 50 | + this.client = client; |
| 51 | + } |
| 52 | + |
| 53 | + public ConfigurationService getConfigurationService() { |
| 54 | + return configurationService; |
| 55 | + } |
| 56 | + |
| 57 | + public void setConfigurationService(ConfigurationService configurationService) { |
| 58 | + this.configurationService = configurationService; |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 62 | + protected SharedIndexInformer createInformer(InformerClassifier<?> classifier) { |
| 63 | + FilterWatchListDeletable filteredClient; |
| 64 | + |
| 65 | + if (WATCH_ALL_NAMESPACES.equals(classifier.namespaceIdentifier())) { |
| 66 | + filteredClient = client.resources(classifier.resourceClass()).inAnyNamespace(); |
| 67 | + |
| 68 | + } else { |
| 69 | + filteredClient = |
| 70 | + client |
| 71 | + .resources(classifier.resourceClass()) |
| 72 | + .inNamespace(classifier.namespaceIdentifier()) |
| 73 | + .withLabelSelector(classifier.labelSelector()) |
| 74 | + .withShardSelector(classifier.shardSelector()); |
| 75 | + } |
| 76 | + |
| 77 | + if (classifier.labelSelector() != null) { |
| 78 | + filteredClient = |
| 79 | + (FilterWatchListDeletable) filteredClient.withLabelSelector(classifier.labelSelector()); |
| 80 | + } |
| 81 | + if (classifier.shardSelector() != null) { |
| 82 | + filteredClient = |
| 83 | + (FilterWatchListDeletable) filteredClient.withShardSelector(classifier.shardSelector()); |
| 84 | + } |
| 85 | + |
| 86 | + if (classifier.fieldSelector() != null && !classifier.fieldSelector().getFields().isEmpty()) { |
| 87 | + for (var f : classifier.fieldSelector().getFields()) { |
| 88 | + if (f.negated()) { |
| 89 | + filteredClient = |
| 90 | + (FilterWatchListDeletable) filteredClient.withoutField(f.path(), f.value()); |
| 91 | + } else { |
| 92 | + filteredClient = (FilterWatchListDeletable) filteredClient.withField(f.path(), f.value()); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (classifier.informerListLimit() != null) { |
| 98 | + filteredClient = |
| 99 | + (FilterWatchListDeletable) filteredClient.withLimit(classifier.informerListLimit()); |
| 100 | + } |
| 101 | + |
| 102 | + var informer = filteredClient.runnableInformer(0); |
| 103 | + |
| 104 | + Optional.ofNullable(classifier.itemStore()).ifPresent(informer::itemStore); |
| 105 | + |
| 106 | + configurationService |
| 107 | + .getInformerStoppedHandler() |
| 108 | + .ifPresent( |
| 109 | + ish -> { |
| 110 | + final var stopped = informer.stopped(); |
| 111 | + if (stopped != null) { |
| 112 | + stopped.handle( |
| 113 | + (res, ex) -> { |
| 114 | + ish.onStop(informer, (Throwable) ex); |
| 115 | + return null; |
| 116 | + }); |
| 117 | + } else { |
| 118 | + final var apiTypeClass = informer.getApiTypeClass(); |
| 119 | + final var fullResourceName = HasMetadata.getFullResourceName(apiTypeClass); |
| 120 | + final var version = HasMetadata.getVersion(apiTypeClass); |
| 121 | + throw new IllegalStateException( |
| 122 | + "Cannot retrieve 'stopped' callback to listen to informer stopping for" |
| 123 | + + " informer for " |
| 124 | + + fullResourceName |
| 125 | + + "/" |
| 126 | + + version); |
| 127 | + } |
| 128 | + }); |
| 129 | + if (!configurationService.stopOnInformerErrorDuringStartup()) { |
| 130 | + informer.exceptionHandler((b, t) -> !ExceptionHandler.isDeserializationException(t)); |
| 131 | + } |
| 132 | + return informer; |
| 133 | + } |
| 134 | + |
| 135 | + protected void start( |
| 136 | + SharedIndexInformer<? extends HasMetadata> informer, |
| 137 | + InformerClassifier<?> informerClassifier) { |
| 138 | + // change thread name for easier debugging |
| 139 | + final var thread = Thread.currentThread(); |
| 140 | + final var name = thread.getName(); |
| 141 | + try { |
| 142 | + thread.setName( |
| 143 | + "InformerInfo[informerInfo" |
| 144 | + + informer.getApiTypeClass().getSimpleName() |
| 145 | + + "]" |
| 146 | + + " " |
| 147 | + + thread.getId()); |
| 148 | + final var resourceName = informer.getApiTypeClass().getSimpleName(); |
| 149 | + log.debug( |
| 150 | + "Starting informer for namespace: {} resource: {}", |
| 151 | + informerClassifier.namespaceIdentifier(), |
| 152 | + resourceName); |
| 153 | + var start = informer.start(); |
| 154 | + // note that in case we don't put here timeout and stopOnInformerErrorDuringStartup is |
| 155 | + // false, and there is a rbac issue the get never returns; therefore operator never really |
| 156 | + // starts |
| 157 | + log.trace( |
| 158 | + "Waiting informer to start namespace: {} resource: {}", |
| 159 | + informerClassifier.namespaceIdentifier(), |
| 160 | + resourceName); |
| 161 | + start |
| 162 | + .toCompletableFuture() |
| 163 | + .get(configurationService.cacheSyncTimeout().toMillis(), TimeUnit.MILLISECONDS); |
| 164 | + log.debug( |
| 165 | + "Started informer for namespace: {} resource: {}", |
| 166 | + informerClassifier.namespaceIdentifier(), |
| 167 | + resourceName); |
| 168 | + } catch (TimeoutException | ExecutionException e) { |
| 169 | + if (configurationService.stopOnInformerErrorDuringStartup()) { |
| 170 | + log.error("Informer startup error. Operator will be stopped. Informer: {}", informer, e); |
| 171 | + throw new OperatorException(e); |
| 172 | + } else { |
| 173 | + log.warn("Informer startup error. Will periodically retry. Informer: {}", informer, e); |
| 174 | + } |
| 175 | + } catch (InterruptedException e) { |
| 176 | + thread.interrupt(); |
| 177 | + throw new IllegalStateException(e); |
| 178 | + } catch (Exception e) { |
| 179 | + ReconcilerUtilsInternal.handleKubernetesClientException( |
| 180 | + e, HasMetadata.getFullResourceName(informer.getApiTypeClass())); |
| 181 | + throw new OperatorException( |
| 182 | + "Couldn't start informer for " + versionedFullResourceName(informer) + " resources", e); |
| 183 | + } finally { |
| 184 | + // restore original name |
| 185 | + thread.setName(name); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private String versionedFullResourceName(SharedIndexInformer<? extends HasMetadata> informer) { |
| 190 | + final var apiTypeClass = informer.getApiTypeClass(); |
| 191 | + if (apiTypeClass.isAssignableFrom(GenericKubernetesResource.class)) { |
| 192 | + return GenericKubernetesResource.class.getSimpleName(); |
| 193 | + } |
| 194 | + return ReconcilerUtilsInternal.getResourceTypeNameWithVersion(apiTypeClass); |
| 195 | + } |
| 196 | +} |
0 commit comments