|
| 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.baseapi.readcacheafterwrite.ownssastatusupdate; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.HashMap; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 23 | + |
| 24 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 25 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 26 | + |
| 27 | +import static io.javaoperatorsdk.operator.baseapi.readcacheafterwrite.ownssastatusupdate.OwnSsaStatusUpdateReconciler.EXTERNAL_LABEL_KEY; |
| 28 | +import static io.javaoperatorsdk.operator.baseapi.readcacheafterwrite.ownssastatusupdate.OwnSsaStatusUpdateReconciler.EXTERNAL_LABEL_VALUE; |
| 29 | +import static io.javaoperatorsdk.operator.baseapi.readcacheafterwrite.ownssastatusupdate.OwnSsaStatusUpdateReconciler.STATUS_VALUE; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.awaitility.Awaitility.await; |
| 32 | + |
| 33 | +/** |
| 34 | + * Verifies that updating the status through {@code |
| 35 | + * resourceOperations().serverSideApplyPrimaryStatus(...)} (instead of returning an {@code |
| 36 | + * UpdateControl}) is read-cache-after-write consistent and does not loop: the own SSA status write |
| 37 | + * is filtered as an own event, so the controller converges. A subsequent external label change must |
| 38 | + * still be picked up by a fresh reconciliation. |
| 39 | + */ |
| 40 | +class OwnSsaStatusUpdateIT { |
| 41 | + |
| 42 | + static final String RESOURCE_NAME = "test-resource"; |
| 43 | + |
| 44 | + OwnSsaStatusUpdateReconciler reconciler = new OwnSsaStatusUpdateReconciler(); |
| 45 | + |
| 46 | + @RegisterExtension |
| 47 | + LocallyRunOperatorExtension extension = |
| 48 | + LocallyRunOperatorExtension.builder().withReconciler(reconciler).build(); |
| 49 | + |
| 50 | + @Test |
| 51 | + void ssaStatusUpdateIsConsistentAndDoesNotLoop() { |
| 52 | + extension.create(testResource()); |
| 53 | + |
| 54 | + // the status is persisted via the own SSA status update |
| 55 | + await() |
| 56 | + .atMost(Duration.ofSeconds(30)) |
| 57 | + .untilAsserted( |
| 58 | + () -> { |
| 59 | + var actual = extension.get(OwnSsaStatusUpdateCustomResource.class, RESOURCE_NAME); |
| 60 | + assertThat(actual.getStatus()).isNotNull(); |
| 61 | + assertThat(actual.getStatus().getValue()).isEqualTo(STATUS_VALUE); |
| 62 | + }); |
| 63 | + |
| 64 | + // the own status write must be filtered: no reconciliation loop |
| 65 | + await() |
| 66 | + .during(Duration.ofSeconds(2)) |
| 67 | + .atMost(Duration.ofSeconds(5)) |
| 68 | + .untilAsserted( |
| 69 | + () -> |
| 70 | + assertThat(reconciler.numberOfExecutions.get()) |
| 71 | + .as("own SSA status update must not trigger a reconciliation loop") |
| 72 | + .isLessThanOrEqualTo(2)); |
| 73 | + |
| 74 | + var executionsBeforeExternalUpdate = reconciler.numberOfExecutions.get(); |
| 75 | + |
| 76 | + // an external party changes a label; this must still trigger a fresh reconciliation |
| 77 | + var current = extension.get(OwnSsaStatusUpdateCustomResource.class, RESOURCE_NAME); |
| 78 | + var labels = new HashMap<String, String>(); |
| 79 | + if (current.getMetadata().getLabels() != null) { |
| 80 | + labels.putAll(current.getMetadata().getLabels()); |
| 81 | + } |
| 82 | + labels.put(EXTERNAL_LABEL_KEY, EXTERNAL_LABEL_VALUE); |
| 83 | + current.getMetadata().setLabels(labels); |
| 84 | + extension.replace(current); |
| 85 | + |
| 86 | + await() |
| 87 | + .atMost(Duration.ofSeconds(30)) |
| 88 | + .untilAsserted( |
| 89 | + () -> { |
| 90 | + assertThat(reconciler.numberOfExecutions.get()) |
| 91 | + .isGreaterThan(executionsBeforeExternalUpdate); |
| 92 | + assertThat(reconciler.externalLabelSeenInLaterReconciliation.get()) |
| 93 | + .as("a later reconciliation must observe the externally-applied label") |
| 94 | + .isTrue(); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + OwnSsaStatusUpdateCustomResource testResource() { |
| 99 | + var r = new OwnSsaStatusUpdateCustomResource(); |
| 100 | + r.setMetadata(new ObjectMetaBuilder().withName(RESOURCE_NAME).build()); |
| 101 | + return r; |
| 102 | + } |
| 103 | +} |
0 commit comments