|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.workflow; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.concurrent.ConcurrentHashMap; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 10 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; |
| 11 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 12 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; |
| 13 | +import io.javaoperatorsdk.operator.processing.expiration.Expiration; |
| 14 | +import io.javaoperatorsdk.operator.processing.expiration.ExpirationExecution; |
| 15 | +import io.javaoperatorsdk.operator.processing.expiration.RetryExpiration; |
| 16 | +import io.javaoperatorsdk.operator.processing.retry.GenericRetry; |
| 17 | +import io.javaoperatorsdk.operator.processing.retry.Retry; |
| 18 | + |
| 19 | +public class CRDPresentActivationCondition implements Condition<HasMetadata, HasMetadata> { |
| 20 | + |
| 21 | + private static final Logger log = LoggerFactory.getLogger(CRDPresentActivationCondition.class); |
| 22 | + |
| 23 | + public static final int DEFAULT_EXPIRATION_INITIAL_INTERVAL = 1000; |
| 24 | + public static final int DEFAULT_EXPIRATION_INTERVAL_MULTIPLIER = 4; |
| 25 | + public static final int DEFAULT_EXPIRATION_MAX_RETRY_ATTEMPTS = 10; |
| 26 | + |
| 27 | + /** |
| 28 | + * The idea behind default expiration is that on cluster start there might be different phases |
| 29 | + * when CRDs and controllers are added. For a few times it will be checked if the target CRD is |
| 30 | + * not present, after it will just use the cached state. |
| 31 | + */ |
| 32 | + public static Retry DEFAULT_EXPIRATION_RETRY = |
| 33 | + new GenericRetry().setInitialInterval(DEFAULT_EXPIRATION_INITIAL_INTERVAL) |
| 34 | + .setIntervalMultiplier(DEFAULT_EXPIRATION_INTERVAL_MULTIPLIER) |
| 35 | + .setMaxAttempts(DEFAULT_EXPIRATION_MAX_RETRY_ATTEMPTS); |
| 36 | + |
| 37 | + private final Map<String, CRDCheckState> crdPresenceCache = new ConcurrentHashMap<>(); |
| 38 | + |
| 39 | + private final Expiration expiration; |
| 40 | + |
| 41 | + public CRDPresentActivationCondition() { |
| 42 | + this(new RetryExpiration(DEFAULT_EXPIRATION_RETRY)); |
| 43 | + } |
| 44 | + |
| 45 | + public CRDPresentActivationCondition(Expiration expiration) { |
| 46 | + this.expiration = expiration; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean isMet(DependentResource<HasMetadata, HasMetadata> dependentResource, |
| 51 | + HasMetadata primary, Context<HasMetadata> context) { |
| 52 | + |
| 53 | + var resourceClass = dependentResource.resourceType(); |
| 54 | + final var crdName = HasMetadata.getFullResourceName(resourceClass); |
| 55 | + |
| 56 | + var crdCheckState = crdPresenceCache.computeIfAbsent(crdName, |
| 57 | + g -> new CRDCheckState(expiration.initExecution())); |
| 58 | + // in case of parallel execution it is only refreshed once |
| 59 | + synchronized (crdCheckState) { |
| 60 | + if (crdCheckState.isExpired()) { |
| 61 | + log.debug("Refreshing cache for resource: {}", crdName); |
| 62 | + final var found = context.getClient().resources(CustomResourceDefinition.class) |
| 63 | + .withName(crdName).get() != null; |
| 64 | + crdCheckState.refresh(found); |
| 65 | + } |
| 66 | + } |
| 67 | + return crdPresenceCache.get(crdName).isCrdPresent(); |
| 68 | + } |
| 69 | + |
| 70 | + static class CRDCheckState { |
| 71 | + private final ExpirationExecution expirationExecution; |
| 72 | + private boolean crdPresent; |
| 73 | + |
| 74 | + public CRDCheckState(ExpirationExecution expirationExecution) { |
| 75 | + this.expirationExecution = expirationExecution; |
| 76 | + } |
| 77 | + |
| 78 | + void refresh(boolean found) { |
| 79 | + crdPresent = found; |
| 80 | + expirationExecution.refreshed(); |
| 81 | + } |
| 82 | + |
| 83 | + boolean isExpired() { |
| 84 | + return expirationExecution.isExpired(); |
| 85 | + } |
| 86 | + |
| 87 | + public boolean isCrdPresent() { |
| 88 | + return crdPresent; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments