Skip to content

Commit fb28d5d

Browse files
committed
bulk dependent resource to an interface
1 parent 47e954a commit fb28d5d

File tree

4 files changed

+56
-62
lines changed

4 files changed

+56
-62
lines changed

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

+13-42
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,25 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
2222

2323
protected final boolean creatable = this instanceof Creator;
2424
protected final boolean updatable = this instanceof Updater;
25+
protected final boolean bulk = this instanceof BulkDependentResource;
2526

2627
protected Creator<R, P> creator;
2728
protected Updater<R, P> updater;
29+
protected BulkDependentResource<R, P> bulkDependentResource;
2830

2931
protected List<ResourceDiscriminator<R, P>> resourceDiscriminator = new ArrayList<>(1);
30-
// used just for bulk creation
31-
protected BulkResourceDiscriminatorFactory<R, P> bulkResourceDiscriminatorFactory;
3232

3333
@SuppressWarnings("unchecked")
3434
public AbstractDependentResource() {
3535
creator = creatable ? (Creator<R, P>) this : null;
3636
updater = updatable ? (Updater<R, P>) this : null;
37+
bulkDependentResource = bulk ? (BulkDependentResource<R, P>) this : null;
3738
}
3839

3940
@Override
4041
public ReconcileResult<R> reconcile(P primary, Context<P> context) {
41-
var count = count(primary, context).orElse(1);
42-
if (isBulkResourceCreation(primary, context)) {
42+
var count = bulk ? bulkDependentResource.count(primary, context) : 1;
43+
if (bulk) {
4344
cleanupBulkResourcesIfRequired(count, resourceDiscriminator.size(), primary, context);
4445
adjustDiscriminators(count);
4546
}
@@ -59,23 +60,19 @@ private void cleanupBulkResourcesIfRequired(int targetCount, int actualCount, P
5960
for (int i = targetCount; i < actualCount; i++) {
6061
var resource = getSecondaryResourceIndexAware(primary, i, context);
6162
var index = i;
62-
resource.ifPresent(r -> {
63-
deleteBulkResourceWithIndex(primary, r, index, context);
64-
});
63+
resource.ifPresent(
64+
r -> bulkDependentResource.deleteBulkResourceWithIndex(primary, r, index, context));
6565
}
6666
}
6767

68-
protected void deleteBulkResourceWithIndex(P primary, R resource, int i, Context<P> context) {
69-
throw new IllegalStateException("Implement if handling bulk resources.");
70-
}
71-
7268
private void adjustDiscriminators(int count) {
7369
if (resourceDiscriminator.size() == count) {
7470
return;
7571
}
7672
if (resourceDiscriminator.size() < count) {
7773
for (int i = resourceDiscriminator.size(); i < count; i++) {
78-
resourceDiscriminator.add(bulkResourceDiscriminatorFactory.createResourceDiscriminator(i));
74+
resourceDiscriminator.add(bulkDependentResource.bulkResourceDiscriminatorFactory()
75+
.createResourceDiscriminator(i));
7976
}
8077
}
8178
if (resourceDiscriminator.size() > count) {
@@ -98,7 +95,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
9895
final var actual = maybeActual.get();
9996
if (updatable) {
10097
final Matcher.Result<R> match;
101-
if (isBulkResourceCreation(primary, context)) {
98+
if (bulk) {
10299
match = updater.match(actual, primary, i, context);
103100
} else {
104101
match = updater.match(actual, primary, context);
@@ -124,7 +121,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
124121
}
125122

126123
private R desiredIndexAware(P primary, int i, Context<P> context) {
127-
return isBulkResourceCreation(primary, context) ? desired(primary, i, context)
124+
return bulk ? desired(primary, i, context)
128125
: desired(primary, context);
129126
}
130127

@@ -142,7 +139,7 @@ protected Optional<R> getSecondaryResourceIndexAware(P primary, int index, Conte
142139
throw new IllegalStateException(
143140
"Handling resources in bulk bot no resource discriminators set.");
144141
}
145-
if (!isBulkResourceCreation(primary, context)) {
142+
if (!bulk) {
146143
return getSecondaryResource(primary, context);
147144
}
148145

@@ -207,7 +204,7 @@ protected R desired(P primary, Context<P> context) {
207204
}
208205

209206
protected R desired(P primary, int index, Context<P> context) {
210-
if (!isBulkResourceCreation(primary, context)) {
207+
if (!bulk) {
211208
return desired(primary, context);
212209
} else {
213210
throw new IllegalStateException(
@@ -222,30 +219,4 @@ public AbstractDependentResource<R, P> setResourceDiscriminator(
222219
}
223220
return this;
224221
}
225-
226-
/**
227-
* @param primary resource
228-
* @param context actual context
229-
* @return empty optional if it's not a bulk resource management, number of instances otherwise.
230-
*/
231-
protected Optional<Integer> count(P primary, Context<P> context) {
232-
return Optional.empty();
233-
}
234-
235-
/**
236-
* Override in case the count() is a more heavy
237-
*/
238-
protected boolean isBulkResourceCreation(P primary, Context<P> context) {
239-
return count(primary, context).isPresent();
240-
}
241-
242-
public BulkResourceDiscriminatorFactory<R, P> getBulkResourceDiscriminatorFactory() {
243-
return bulkResourceDiscriminatorFactory;
244-
}
245-
246-
public AbstractDependentResource<R, P> setBulkResourceDiscriminatorFactory(
247-
BulkResourceDiscriminatorFactory<R, P> bulkResourceDiscriminatorFactory) {
248-
this.bulkResourceDiscriminatorFactory = bulkResourceDiscriminatorFactory;
249-
return this;
250-
}
251222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.javaoperatorsdk.operator.processing.dependent;
2+
3+
import io.fabric8.kubernetes.api.model.HasMetadata;
4+
import io.javaoperatorsdk.operator.api.reconciler.Context;
5+
6+
public interface BulkDependentResource<R, P extends HasMetadata> {
7+
8+
int count(P primary, Context<P> context);
9+
10+
default R desired(P primary, int index, Context<P> context) {
11+
throw new IllegalStateException("Implement if the dependent resource is a creator or updater");
12+
}
13+
14+
void deleteBulkResourceWithIndex(P primary, R resource, int i, Context<P> context);
15+
16+
BulkResourceDiscriminatorFactory<R, P> bulkResourceDiscriminatorFactory();
17+
18+
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ public void delete(P primary, Context<P> context) {
146146
resource.ifPresent(r -> client.resource(r).delete());
147147
}
148148

149-
@Override
150-
protected void deleteBulkResourceWithIndex(P primary, R resource, int i, Context<P> context) {
149+
public void deleteBulkResourceWithIndex(P primary, R resource, int i, Context<P> context) {
151150
client.resource(resource).delete();
152151
}
153152

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/standalonebulkdependent/ConfigMapBulkDependentResource.java

+24-18
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,38 @@
1010
import io.fabric8.kubernetes.api.model.ConfigMap;
1111
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
1212
import io.javaoperatorsdk.operator.api.reconciler.Context;
13+
import io.javaoperatorsdk.operator.processing.dependent.BulkDependentResource;
14+
import io.javaoperatorsdk.operator.processing.dependent.BulkResourceDiscriminatorFactory;
1315
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
1416

1517
public class ConfigMapBulkDependentResource
16-
extends CRUDKubernetesDependentResource<ConfigMap, StandaloneBulkDependentTestCustomResource> {
18+
extends CRUDKubernetesDependentResource<ConfigMap, StandaloneBulkDependentTestCustomResource>
19+
implements BulkDependentResource<ConfigMap, StandaloneBulkDependentTestCustomResource> {
1720

1821
private final static Logger log = LoggerFactory.getLogger(ConfigMapBulkDependentResource.class);
1922

2023
public static final String LABEL_KEY = "bulk";
2124
public static final String LABEL_VALUE = "true";
25+
private BulkResourceDiscriminatorFactory<ConfigMap, StandaloneBulkDependentTestCustomResource> factory =
26+
index -> (resource, primary, context) -> {
27+
var resources = context.getSecondaryResources(resource).stream()
28+
.filter(r -> r.getMetadata().getName().endsWith("-" + index))
29+
.collect(Collectors.toList());
30+
if (resources.isEmpty()) {
31+
return Optional.empty();
32+
} else if (resources.size() > 1) {
33+
throw new IllegalStateException("More than one resource found for index:" + index);
34+
} else {
35+
return Optional.of(resources.get(0));
36+
}
37+
};
2238

2339
public ConfigMapBulkDependentResource() {
2440
super(ConfigMap.class);
25-
setBulkResourceDiscriminatorFactory(
26-
index -> (resource, primary, context) -> {
27-
var resources = context.getSecondaryResources(resource).stream()
28-
.filter(r -> r.getMetadata().getName().endsWith("-" + index))
29-
.collect(Collectors.toList());
30-
if (resources.isEmpty()) {
31-
return Optional.empty();
32-
} else if (resources.size() > 1) {
33-
throw new IllegalStateException("More than one resource found for index:" + index);
34-
} else {
35-
return Optional.of(resources.get(0));
36-
}
37-
});
3841
}
3942

4043
@Override
41-
protected ConfigMap desired(StandaloneBulkDependentTestCustomResource primary,
44+
public ConfigMap desired(StandaloneBulkDependentTestCustomResource primary,
4245
int index, Context<StandaloneBulkDependentTestCustomResource> context) {
4346
ConfigMap configMap = new ConfigMap();
4447
configMap.setMetadata(new ObjectMetaBuilder()
@@ -51,10 +54,13 @@ protected ConfigMap desired(StandaloneBulkDependentTestCustomResource primary,
5154
}
5255

5356
@Override
54-
protected Optional<Integer> count(StandaloneBulkDependentTestCustomResource primary,
57+
public int count(StandaloneBulkDependentTestCustomResource primary,
5558
Context<StandaloneBulkDependentTestCustomResource> context) {
56-
return Optional.of(primary.getSpec().getNumberOfResources());
59+
return primary.getSpec().getNumberOfResources();
5760
}
5861

59-
62+
@Override
63+
public BulkResourceDiscriminatorFactory<ConfigMap, StandaloneBulkDependentTestCustomResource> bulkResourceDiscriminatorFactory() {
64+
return factory;
65+
}
6066
}

0 commit comments

Comments
 (0)