|
32 | 32 | import io.fabric8.kubernetes.client.dsl.MixedOperation; |
33 | 33 | import io.fabric8.kubernetes.client.informers.ResourceEventHandler; |
34 | 34 | import io.javaoperatorsdk.operator.OperatorException; |
| 35 | +import io.javaoperatorsdk.operator.ReconcilerUtilsInternal; |
35 | 36 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; |
36 | 37 | import io.javaoperatorsdk.operator.api.config.Informable; |
37 | 38 | import io.javaoperatorsdk.operator.api.config.NamespaceChangeable; |
@@ -186,14 +187,21 @@ public void handleRecentResourceCreate(ResourceID resourceID, R resource) { |
186 | 187 |
|
187 | 188 | @Override |
188 | 189 | public Optional<R> get(ResourceID resourceID) { |
189 | | - if (comparableResourceVersions) { |
190 | | - Optional<R> resource = temporaryResourceCache.getResourceFromCache(resourceID); |
191 | | - if (resource.isPresent()) { |
192 | | - log.debug("Latest resource found in temporary cache for Resource ID: {}", resourceID); |
193 | | - return resource; |
194 | | - } |
195 | | - } |
| 190 | + // The order of these two lookups matters. If we queried the informer cache first, |
| 191 | + // a race condition could occur: we might not find the resource there yet, then |
| 192 | + // process an informer event that evicts the temporary resource cache entry. At that |
| 193 | + // point the resource would already be present in the informer cache, but we would |
| 194 | + // have missed it in both caches during this call. |
| 195 | + Optional<R> resource = temporaryResourceCache.getResourceFromCache(resourceID); |
196 | 196 | var res = cache.get(resourceID); |
| 197 | + if (comparableResourceVersions |
| 198 | + && resource.isPresent() |
| 199 | + && res.filter( |
| 200 | + r -> ReconcilerUtilsInternal.compareResourceVersions(r, resource.orElseThrow()) > 0) |
| 201 | + .isEmpty()) { |
| 202 | + log.debug("Latest resource found in temporary cache for Resource ID: {}", resourceID); |
| 203 | + return resource; |
| 204 | + } |
197 | 205 | log.debug( |
198 | 206 | "Resource not found, or older, in temporary cache. Found in informer cache {}, for" |
199 | 207 | + " Resource ID: {}", |
|
0 commit comments