|
21 | 21 | import io.javaoperatorsdk.operator.api.config.Cloner;
|
22 | 22 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
|
23 | 23 | import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
|
| 24 | +import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfiguration; |
24 | 25 | import io.javaoperatorsdk.operator.api.monitoring.Metrics.ControllerExecution;
|
25 | 26 | import io.javaoperatorsdk.operator.api.reconciler.Context;
|
26 | 27 | import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
|
@@ -116,35 +117,13 @@ public UpdateControl<R> reconcile(R resource, Context<R> context) {
|
116 | 117 | if (conf.creatable() && dependentResource == null) {
|
117 | 118 | // we need to create the dependent
|
118 | 119 | dependentResource = dependent.buildFor(resource);
|
119 |
| - log.info("Creating '{}' {} dependent in namespace {} for '{}' {}", |
120 |
| - dependentResource.getMetadata().getName(), conf.getResourceTypeName(), |
121 |
| - dependentResource.getMetadata().getNamespace(), resource.getMetadata().getName(), |
122 |
| - configuration.getResourceTypeName()); |
| 120 | + createOrReplaceDependent(resource, conf, dependentResource, "Creating"); |
123 | 121 | } else if (conf.updatable()) {
|
124 | 122 | dependentResource = dependent.update(dependentResource, resource);
|
125 |
| - log.info("Updating '{}' {} dependent in namespace {} for '{}' {}", |
126 |
| - dependentResource.getMetadata().getName(), conf.getResourceTypeName(), |
127 |
| - dependentResource.getMetadata().getNamespace(), resource.getMetadata().getName(), |
128 |
| - configuration.getResourceTypeName()); |
| 123 | + createOrReplaceDependent(resource, conf, dependentResource, "Updating"); |
| 124 | + } else { |
| 125 | + logOperationInfo(resource, conf, dependentResource, "Ignoring"); |
129 | 126 | }
|
130 |
| - |
131 |
| - // create/add the owner reference if needed |
132 |
| - if (conf.owned()) { |
133 |
| - final var metadata = resource.getMetadata(); |
134 |
| - final var updatedMetadata = new ObjectMetaBuilder(dependentResource.getMetadata()) |
135 |
| - .addNewOwnerReference() |
136 |
| - .withUid(metadata.getUid()) |
137 |
| - .withApiVersion(resource.getApiVersion()) |
138 |
| - .withName(metadata.getName()) |
139 |
| - .withKind(resource.getKind()) |
140 |
| - .endOwnerReference().build(); |
141 |
| - dependentResource.setMetadata(updatedMetadata); |
142 |
| - } |
143 |
| - |
144 |
| - // send the changes to the cluster |
145 |
| - // todo: would be nice to be able to update informer directly… |
146 |
| - // todo: add metrics timing for dependent resource |
147 |
| - kubernetesClient.resource(dependentResource).createOrReplace(); |
148 | 127 | });
|
149 | 128 |
|
150 | 129 | return metrics.timeControllerExecution(
|
@@ -178,6 +157,41 @@ public UpdateControl<R> execute() {
|
178 | 157 | });
|
179 | 158 | }
|
180 | 159 |
|
| 160 | + private void createOrReplaceDependent(R resource, DependentResourceConfiguration conf, |
| 161 | + HasMetadata dependentResource, String operationDescription) { |
| 162 | + addOwnerReferenceIfNeeded(resource, conf, dependentResource); |
| 163 | + logOperationInfo(resource, conf, dependentResource, operationDescription); |
| 164 | + // send the changes to the cluster |
| 165 | + // todo: would be nice to be able to update informer directly… |
| 166 | + // todo: add metrics timing for dependent resource |
| 167 | + kubernetesClient.resource(dependentResource).createOrReplace(); |
| 168 | + } |
| 169 | + |
| 170 | + private void logOperationInfo(R resource, DependentResourceConfiguration conf, |
| 171 | + HasMetadata dependentResource, String operationDescription) { |
| 172 | + log.info(operationDescription + " '{}' {} dependent in namespace {} for '{}' {}", |
| 173 | + dependentResource.getMetadata().getName(), conf.getResourceTypeName(), |
| 174 | + dependentResource.getMetadata().getNamespace(), resource.getMetadata().getName(), |
| 175 | + configuration.getResourceTypeName()); |
| 176 | + } |
| 177 | + |
| 178 | + private void addOwnerReferenceIfNeeded(R resource, DependentResourceConfiguration conf, |
| 179 | + HasMetadata dependentResource) { |
| 180 | + // todo: use owner reference support from fabric8 client to avoid adding several times the same |
| 181 | + // reference |
| 182 | + if (conf.owned()) { |
| 183 | + final var metadata = resource.getMetadata(); |
| 184 | + final var updatedMetadata = new ObjectMetaBuilder(dependentResource.getMetadata()) |
| 185 | + .addNewOwnerReference() |
| 186 | + .withUid(metadata.getUid()) |
| 187 | + .withApiVersion(resource.getApiVersion()) |
| 188 | + .withName(metadata.getName()) |
| 189 | + .withKind(resource.getKind()) |
| 190 | + .endOwnerReference().build(); |
| 191 | + dependentResource.setMetadata(updatedMetadata); |
| 192 | + } |
| 193 | + } |
| 194 | + |
181 | 195 | @Override
|
182 | 196 | public void prepareEventSources(EventSourceRegistry<R> eventSourceRegistry, Cloner cloner) {
|
183 | 197 | configuration.getDependentResources().forEach(dependent -> {
|
|
0 commit comments