|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.workflow; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.time.LocalDateTime; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 9 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; |
| 10 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 11 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 12 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; |
| 13 | + |
| 14 | +/** |
| 15 | + * A generic CRD checking activation condition. Makes sure that the CRD is not checked unnecessarily |
| 16 | + * even used in multiple condition. By default, it checks CRD at most 10 times with a delay at least |
| 17 | + * 10 seconds. To fully customize CRD check trigger behavior you can extend this class and override |
| 18 | + * the {@link CRDPresentActivationCondition#shouldCheckStateNow(CRDCheckState)} method. |
| 19 | + **/ |
| 20 | +public class CRDPresentActivationCondition<R extends HasMetadata, P extends HasMetadata> |
| 21 | + implements Condition<R, P> { |
| 22 | + |
| 23 | + public static final int DEFAULT_CRD_CHECK_LIMIT = 10; |
| 24 | + public static final Duration DEFAULT_CRD_CHECK_INTERVAL = Duration.ofSeconds(10); |
| 25 | + |
| 26 | + private static final Map<String, CRDCheckState> crdPresenceCache = new ConcurrentHashMap<>(); |
| 27 | + |
| 28 | + private final CRDPresentChecker crdPresentChecker; |
| 29 | + private final int checkLimit; |
| 30 | + private final Duration crdCheckInterval; |
| 31 | + |
| 32 | + public CRDPresentActivationCondition() { |
| 33 | + this(DEFAULT_CRD_CHECK_LIMIT, DEFAULT_CRD_CHECK_INTERVAL); |
| 34 | + } |
| 35 | + |
| 36 | + public CRDPresentActivationCondition(int checkLimit, Duration crdCheckInterval) { |
| 37 | + this(new CRDPresentChecker(), checkLimit, crdCheckInterval); |
| 38 | + } |
| 39 | + |
| 40 | + // for testing purposes only |
| 41 | + CRDPresentActivationCondition(CRDPresentChecker crdPresentChecker, int checkLimit, |
| 42 | + Duration crdCheckInterval) { |
| 43 | + this.crdPresentChecker = crdPresentChecker; |
| 44 | + this.checkLimit = checkLimit; |
| 45 | + this.crdCheckInterval = crdCheckInterval; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isMet(DependentResource<R, P> dependentResource, |
| 50 | + P primary, Context<P> context) { |
| 51 | + |
| 52 | + var resourceClass = dependentResource.resourceType(); |
| 53 | + final var crdName = HasMetadata.getFullResourceName(resourceClass); |
| 54 | + |
| 55 | + var crdCheckState = crdPresenceCache.computeIfAbsent(crdName, |
| 56 | + g -> new CRDCheckState()); |
| 57 | + |
| 58 | + synchronized (crdCheckState) { |
| 59 | + if (shouldCheckStateNow(crdCheckState)) { |
| 60 | + boolean isPresent = crdPresentChecker |
| 61 | + .checkIfCRDPresent(crdName, context.getClient()); |
| 62 | + crdCheckState.checkedNow(isPresent); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (crdCheckState.isCrdPresent() == null) { |
| 67 | + throw new IllegalStateException("State should be already checked at this point."); |
| 68 | + } |
| 69 | + return crdCheckState.isCrdPresent(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Override this method to fine tune when the crd state should be refreshed; |
| 74 | + */ |
| 75 | + protected boolean shouldCheckStateNow(CRDCheckState crdCheckState) { |
| 76 | + if (crdCheckState.isCrdPresent() == null) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + // assumption is that if CRD is present, it is not deleted anymore |
| 80 | + if (crdCheckState.isCrdPresent()) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + if (crdCheckState.getCheckCount() >= checkLimit) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (crdCheckState.getLastChecked() == null) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + return LocalDateTime.now().isAfter(crdCheckState.getLastChecked().plus(crdCheckInterval)); |
| 90 | + } |
| 91 | + |
| 92 | + public static class CRDCheckState { |
| 93 | + private Boolean crdPresent; |
| 94 | + private LocalDateTime lastChecked; |
| 95 | + private int checkCount = 0; |
| 96 | + |
| 97 | + public void checkedNow(boolean crdPresent) { |
| 98 | + this.crdPresent = crdPresent; |
| 99 | + lastChecked = LocalDateTime.now(); |
| 100 | + checkCount++; |
| 101 | + } |
| 102 | + |
| 103 | + public Boolean isCrdPresent() { |
| 104 | + return crdPresent; |
| 105 | + } |
| 106 | + |
| 107 | + public LocalDateTime getLastChecked() { |
| 108 | + return lastChecked; |
| 109 | + } |
| 110 | + |
| 111 | + public int getCheckCount() { |
| 112 | + return checkCount; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static class CRDPresentChecker { |
| 117 | + boolean checkIfCRDPresent(String crdName, KubernetesClient client) { |
| 118 | + return client.resources(CustomResourceDefinition.class) |
| 119 | + .withName(crdName).get() != null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** For testing purposes only */ |
| 124 | + public static void clearState() { |
| 125 | + crdPresenceCache.clear(); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments