|
| 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.ownerreferencemultiversion; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 26 | +import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 27 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion; |
| 28 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 29 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.awaitility.Awaitility.await; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration test verifying that {@code Mappers.fromOwnerReferences} correctly maps secondary |
| 36 | + * resources back to primary resources even after a CRD version change. The mapper compares only the |
| 37 | + * group part of the apiVersion (ignoring the version), so owner references created under v1 should |
| 38 | + * still work when the CRD storage version switches to v2. |
| 39 | + */ |
| 40 | +class OwnerRefMultiVersionIT { |
| 41 | + |
| 42 | + private static final Logger log = LoggerFactory.getLogger(OwnerRefMultiVersionIT.class); |
| 43 | + |
| 44 | + private static final String CR_NAME = "test-ownerref-mv"; |
| 45 | + private static final String CRD_NAME = "ownerrefmultiversioncrs.sample.javaoperatorsdk"; |
| 46 | + |
| 47 | + @RegisterExtension |
| 48 | + LocallyRunOperatorExtension operator = |
| 49 | + LocallyRunOperatorExtension.builder() |
| 50 | + .withReconciler(new OwnerRefMultiVersionReconciler()) |
| 51 | + .withBeforeStartHook( |
| 52 | + ext -> { |
| 53 | + // The auto-generated CRD has both v1 (storage) and v2. Remove v2 so the |
| 54 | + // cluster initially only knows about v1. |
| 55 | + var client = ext.getKubernetesClient(); |
| 56 | + var crd = |
| 57 | + client |
| 58 | + .apiextensions() |
| 59 | + .v1() |
| 60 | + .customResourceDefinitions() |
| 61 | + .withName(CRD_NAME) |
| 62 | + .get(); |
| 63 | + if (crd != null) { |
| 64 | + crd.getSpec().getVersions().removeIf(v -> "v2".equals(v.getName())); |
| 65 | + crd.getMetadata().setResourceVersion(null); |
| 66 | + crd.getMetadata().setManagedFields(null); |
| 67 | + client.resource(crd).serverSideApply(); |
| 68 | + log.info("Applied CRD with v1 only"); |
| 69 | + } |
| 70 | + }) |
| 71 | + .build(); |
| 72 | + |
| 73 | + @Test |
| 74 | + void mapperWorksAcrossVersionChange() { |
| 75 | + var reconciler = operator.getReconcilerOfType(OwnerRefMultiVersionReconciler.class); |
| 76 | + |
| 77 | + // 1. Create a v1 custom resource |
| 78 | + var cr = createCR(); |
| 79 | + operator.create(cr); |
| 80 | + |
| 81 | + // 2. Wait for initial reconciliation: ConfigMap is created with owner ref to v1 |
| 82 | + await() |
| 83 | + .atMost(Duration.ofSeconds(30)) |
| 84 | + .pollInterval(Duration.ofMillis(300)) |
| 85 | + .untilAsserted( |
| 86 | + () -> { |
| 87 | + var current = operator.get(OwnerRefMultiVersionCR1.class, CR_NAME); |
| 88 | + assertThat(current.getStatus()).isNotNull(); |
| 89 | + assertThat(current.getStatus().getReconcileCount()).isPositive(); |
| 90 | + |
| 91 | + var cm = operator.get(ConfigMap.class, CR_NAME); |
| 92 | + assertThat(cm).isNotNull(); |
| 93 | + assertThat(cm.getMetadata().getOwnerReferences()).hasSize(1); |
| 94 | + assertThat(cm.getMetadata().getOwnerReferences().get(0).getApiVersion()) |
| 95 | + .isEqualTo("sample.javaoperatorsdk/v1"); |
| 96 | + }); |
| 97 | + |
| 98 | + int countBeforeUpdate = reconciler.getReconcileCount(); |
| 99 | + log.info("Reconcile count before CRD update: {}", countBeforeUpdate); |
| 100 | + |
| 101 | + // 3. Update CRD to add v2 as the new storage version |
| 102 | + updateCrdWithV2AsStorage(operator.getKubernetesClient()); |
| 103 | + |
| 104 | + // 4. Modify the ConfigMap to trigger the informer event source. |
| 105 | + // The mapper should still map the ConfigMap (with v1 owner ref) to the primary CR. |
| 106 | + var cm = operator.get(ConfigMap.class, CR_NAME); |
| 107 | + cm.getData().put("updated", "true"); |
| 108 | + operator.replace(cm); |
| 109 | + |
| 110 | + // 5. Verify reconciliation was triggered again |
| 111 | + await() |
| 112 | + .atMost(Duration.ofSeconds(30)) |
| 113 | + .pollInterval(Duration.ofMillis(300)) |
| 114 | + .untilAsserted( |
| 115 | + () -> { |
| 116 | + assertThat(reconciler.getReconcileCount()).isGreaterThan(countBeforeUpdate); |
| 117 | + }); |
| 118 | + |
| 119 | + log.info("Reconcile count after CRD update: {}", reconciler.getReconcileCount()); |
| 120 | + } |
| 121 | + |
| 122 | + private void updateCrdWithV2AsStorage(KubernetesClient client) { |
| 123 | + var crd = client.apiextensions().v1().customResourceDefinitions().withName(CRD_NAME).get(); |
| 124 | + |
| 125 | + // Set v1 to non-storage |
| 126 | + for (var version : crd.getSpec().getVersions()) { |
| 127 | + if ("v1".equals(version.getName())) { |
| 128 | + version.setStorage(false); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Add v2 as storage version, reusing v1's schema (specs are compatible) |
| 133 | + var v1 = |
| 134 | + crd.getSpec().getVersions().stream() |
| 135 | + .filter(v -> "v1".equals(v.getName())) |
| 136 | + .findFirst() |
| 137 | + .orElseThrow(); |
| 138 | + |
| 139 | + var v2 = new CustomResourceDefinitionVersion(); |
| 140 | + v2.setName("v2"); |
| 141 | + v2.setServed(true); |
| 142 | + v2.setStorage(true); |
| 143 | + v2.setSchema(v1.getSchema()); |
| 144 | + v2.setSubresources(v1.getSubresources()); |
| 145 | + crd.getSpec().getVersions().add(v2); |
| 146 | + |
| 147 | + crd.getMetadata().setResourceVersion(null); |
| 148 | + crd.getMetadata().setManagedFields(null); |
| 149 | + client.resource(crd).serverSideApply(); |
| 150 | + log.info("Updated CRD with v2 as storage version"); |
| 151 | + } |
| 152 | + |
| 153 | + private OwnerRefMultiVersionCR1 createCR() { |
| 154 | + var cr = new OwnerRefMultiVersionCR1(); |
| 155 | + cr.setMetadata(new ObjectMeta()); |
| 156 | + cr.getMetadata().setName(CR_NAME); |
| 157 | + cr.setSpec(new OwnerRefMultiVersionSpec()); |
| 158 | + cr.getSpec().setValue("initial"); |
| 159 | + return cr; |
| 160 | + } |
| 161 | +} |
0 commit comments