|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.processing.expectation; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.time.LocalDateTime; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.ConcurrentHashMap; |
| 22 | + |
| 23 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 24 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 25 | +import io.javaoperatorsdk.operator.api.reconciler.Experimental; |
| 26 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 27 | + |
| 28 | +import static io.javaoperatorsdk.operator.api.reconciler.Experimental.API_MIGHT_CHANGE; |
| 29 | + |
| 30 | +@Experimental(API_MIGHT_CHANGE) |
| 31 | +public class ExpectationManager<P extends HasMetadata> { |
| 32 | + |
| 33 | + protected final ConcurrentHashMap<ResourceID, RegisteredExpectation<P>> registeredExpectations = |
| 34 | + new ConcurrentHashMap<>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Checks if the expectation holds, if not sets the expectation with the given timeout. |
| 38 | + * |
| 39 | + * @return false, if the expectation is already fulfilled, therefore, not registered. Returns true |
| 40 | + * if expectation is not met and set with a timeout. |
| 41 | + */ |
| 42 | + public boolean checkAndSetExpectation( |
| 43 | + P primary, Context<P> context, Duration timeout, Expectation<P> expectation) { |
| 44 | + var fulfilled = expectation.isFulfilled(primary, context); |
| 45 | + if (fulfilled) { |
| 46 | + return false; |
| 47 | + } else { |
| 48 | + setExpectation(primary, timeout, expectation); |
| 49 | + return true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets a target expectation with given timeout. |
| 55 | + * |
| 56 | + * @param primary resource |
| 57 | + * @param timeout of expectation |
| 58 | + * @param expectation to set |
| 59 | + */ |
| 60 | + // we might consider in the future to throw an exception if an expectation is already set |
| 61 | + public void setExpectation(P primary, Duration timeout, Expectation<P> expectation) { |
| 62 | + registeredExpectations.put( |
| 63 | + ResourceID.fromResource(primary), |
| 64 | + new RegisteredExpectation<>(LocalDateTime.now(), timeout, expectation)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Checks on expectation with provided name. Return the expectation result. If the result of |
| 69 | + * expectation is fulfilled, the expectation is automatically removed; |
| 70 | + */ |
| 71 | + public ExpectationResult<P> checkExpectation( |
| 72 | + String expectationName, P primary, Context<P> context) { |
| 73 | + var resourceID = ResourceID.fromResource(primary); |
| 74 | + var exp = registeredExpectations.get(ResourceID.fromResource(primary)); |
| 75 | + if (exp != null && expectationName.equals(exp.expectation().name())) { |
| 76 | + return checkExpectation(exp, resourceID, primary, context); |
| 77 | + } else { |
| 78 | + return checkExpectation(null, resourceID, primary, context); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks if actual expectation is fulfilled. Return the expectation result. If the result of |
| 84 | + * expectation is fulfilled, the expectation is automatically removed; |
| 85 | + */ |
| 86 | + public ExpectationResult<P> checkExpectation(P primary, Context<P> context) { |
| 87 | + var resourceID = ResourceID.fromResource(primary); |
| 88 | + var exp = registeredExpectations.get(ResourceID.fromResource(primary)); |
| 89 | + return checkExpectation(exp, resourceID, primary, context); |
| 90 | + } |
| 91 | + |
| 92 | + private ExpectationResult<P> checkExpectation( |
| 93 | + RegisteredExpectation<P> exp, ResourceID resourceID, P primary, Context<P> context) { |
| 94 | + if (exp == null) { |
| 95 | + return new ExpectationResult<>(null, null); |
| 96 | + } |
| 97 | + if (exp.expectation().isFulfilled(primary, context)) { |
| 98 | + registeredExpectations.remove(resourceID); |
| 99 | + return new ExpectationResult<>(exp.expectation(), ExpectationStatus.FULFILLED); |
| 100 | + } else if (exp.isTimedOut()) { |
| 101 | + // we don't remove the expectation so user knows about it's state |
| 102 | + return new ExpectationResult<>(exp.expectation(), ExpectationStatus.TIMED_OUT); |
| 103 | + } else { |
| 104 | + return new ExpectationResult<>(exp.expectation(), ExpectationStatus.NOT_YET_FULFILLED); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * Returns true if there is an expectation for the primary resource, but it is not yet fulfilled |
| 110 | + * neither timed out. |
| 111 | + * The intention behind this is that you can exit reconciliation early with a simple check |
| 112 | + * if true. |
| 113 | + * */ |
| 114 | + public boolean ongoingExpectationPresent(P primary, Context<P> context) { |
| 115 | + var exp = registeredExpectations.get(ResourceID.fromResource(primary)); |
| 116 | + if (exp == null) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + return !exp.isTimedOut() && !exp.expectation().isFulfilled(primary, context); |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isExpectationPresent(P primary) { |
| 123 | + return registeredExpectations.containsKey(ResourceID.fromResource(primary)); |
| 124 | + } |
| 125 | + |
| 126 | + public boolean isExpectationPresent(String name, P primary) { |
| 127 | + var exp = registeredExpectations.get(ResourceID.fromResource(primary)); |
| 128 | + return exp != null && name.equals(exp.expectation().name()); |
| 129 | + } |
| 130 | + |
| 131 | + public Optional<Expectation<P>> getExpectation(P primary) { |
| 132 | + var regExp = registeredExpectations.get(ResourceID.fromResource(primary)); |
| 133 | + return Optional.ofNullable(regExp).map(RegisteredExpectation::expectation); |
| 134 | + } |
| 135 | + |
| 136 | + public Optional<String> getExpectationName(P primary) { |
| 137 | + return getExpectation(primary).map(Expectation::name); |
| 138 | + } |
| 139 | + |
| 140 | + public void removeExpectation(P primary) { |
| 141 | + registeredExpectations.remove(ResourceID.fromResource(primary)); |
| 142 | + } |
| 143 | +} |
0 commit comments