Skip to content

Commit e2a553a

Browse files
committed
fix: wire Updater/Builder in DependentResource, better owner handling
1 parent 6a031d7 commit e2a553a

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/DependentResource.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,44 @@
77
import io.javaoperatorsdk.operator.processing.event.source.ResourceCache;
88
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;
99

10-
public class DependentResource<R extends HasMetadata, P extends HasMetadata> {
10+
public class DependentResource<R extends HasMetadata, P extends HasMetadata>
11+
implements Builder<R, P>, Updater<R, P> {
1112

1213
private final DefaultDependentResourceConfiguration<R, P> configuration;
13-
private final Builder<R> builder;
14-
private final Updater<R> updater;
14+
private final Builder<R, P> builder;
15+
private final Updater<R, P> updater;
1516
private final Fetcher<R> fetcher;
1617
private ResourceEventSource<R, P> source;
1718

1819
public DependentResource(DefaultDependentResourceConfiguration<R, P> configuration,
19-
Builder<R> builder, Updater<R> updater, Fetcher<R> fetcher) {
20+
Builder<R, P> builder, Updater<R, P> updater, Fetcher<R> fetcher) {
2021
this.configuration = configuration;
2122
this.builder = builder;
2223
this.updater = updater;
2324
this.fetcher = fetcher;
2425
}
2526

26-
public R build() {
27-
if (getConfiguration().creatable()) {
28-
throw new IllegalStateException(
29-
"Should be implemented if DependentResource is configured as creatable");
30-
}
31-
throw new IllegalStateException("Should not be called if DependentResource is not creatable");
27+
@Override
28+
public R buildFor(P primary) {
29+
return builder.buildFor(primary);
3230
}
3331

3432
public ResourceCache<R> getCache() {
3533
return source.getResourceCache();
3634
}
3735

3836
public R fetchFor(HasMetadata owner) {
39-
return getCache().get(ResourceID.fromResource(owner)).orElse(null);
37+
return fetcher != null ? fetcher.fetchFor(owner, getCache())
38+
: getCache().get(ResourceID.fromResource(owner)).orElse(null);
4039
}
4140

4241
public DependentResourceConfiguration<R, P> getConfiguration() {
4342
return configuration;
4443
}
4544

46-
public R update(R fetched) {
47-
if (getConfiguration().updatable()) {
48-
throw new IllegalStateException(
49-
"Should be implemented if DependentResource is configured as updatable");
50-
}
51-
throw new IllegalStateException("Should not be called if DependentResource is not updatable");
45+
@Override
46+
public R update(R fetched, P primary) {
47+
return updater.update(fetched, primary);
5248
}
5349

5450
public void setSource(ResourceEventSource<R, P> source) {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/Controller.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,30 @@ public UpdateControl<R> reconcile(R resource, Context<R> context) {
115115
var dependentResource = dependent.fetchFor(resource);
116116
if (conf.creatable() && dependentResource == null) {
117117
// we need to create the dependent
118-
dependentResource = dependent.build();
119-
120-
if (conf.owned()) {
121-
final var metadata = resource.getMetadata();
122-
final var updatedMetadata = new ObjectMetaBuilder(dependentResource.getMetadata())
123-
.addNewOwnerReference()
124-
.withUid(metadata.getUid())
125-
.withApiVersion(resource.getApiVersion())
126-
.withName(metadata.getName())
127-
.withKind(resource.getKind())
128-
.endOwnerReference().build();
129-
dependentResource.setMetadata(updatedMetadata);
130-
}
131-
118+
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());
132123
} else if (conf.updatable()) {
133-
dependentResource = dependent.update(dependentResource);
124+
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());
129+
}
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);
134142
}
135143

136144
// send the changes to the cluster

0 commit comments

Comments
 (0)