Skip to content

Commit 1b21fe6

Browse files
committed
feat: make it possible to retrieve actual dependent resource type
1 parent ef8d3a0 commit 1b21fe6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ public Map<String, DependentResource> getDependentResources() {
9494
*/
9595
@SuppressWarnings({"unchecked"})
9696
public <T> DependentResource<T, ?> getDependentResource(String name, Class<T> resourceClass) {
97-
var dependentResource = dependentResources.get(name);
98-
if (dependentResource == null) {
99-
throw new OperatorException("No dependent resource found with name: " + name);
100-
}
97+
DependentResource dependentResource = getDependentResource(name);
10198
final var actual = dependentResource.resourceType();
10299
if (!actual.equals(resourceClass)) {
103100
throw new OperatorException(
@@ -106,4 +103,24 @@ public Map<String, DependentResource> getDependentResources() {
106103
}
107104
return dependentResource;
108105
}
106+
107+
private DependentResource getDependentResource(String name) {
108+
var dependentResource = dependentResources.get(name);
109+
if (dependentResource == null) {
110+
throw new OperatorException("No dependent resource found with name: " + name);
111+
}
112+
return dependentResource;
113+
}
114+
115+
@SuppressWarnings("unchecked")
116+
public <T extends DependentResource> T getAdaptedDependentResource(String name,
117+
Class<T> dependentResourceClass) {
118+
DependentResource dependentResource = getDependentResource(name);
119+
if (dependentResourceClass.isInstance(dependentResource)) {
120+
return (T) dependentResource;
121+
} else {
122+
throw new IllegalArgumentException("Dependent resource associated with name: " + name
123+
+ " is not adaptable to type: " + dependentResourceClass.getCanonicalName());
124+
}
125+
}
109126
}

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
1212
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
1313
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
14+
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
1415
import io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource;
1516
import io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource;
1617

@@ -35,7 +36,12 @@ public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context<MySQLSch
3536
// we only need to update the status if we just built the schema, i.e. when it's present in the
3637
// context
3738
Secret secret = context.getSecondaryResource(Secret.class).orElseThrow();
38-
return context.getSecondaryResource(MySQLSchema.class).map(s -> {
39+
40+
SchemaDependentResource schemaDependentResource =
41+
context.managedDependentResourceContext().getAdaptedDependentResource(
42+
DependentResource.defaultNameFor(SchemaDependentResource.class),
43+
SchemaDependentResource.class);
44+
return schemaDependentResource.fetchResource(schema).map(s -> {
3945
updateStatusPojo(schema, secret.getMetadata().getName(),
4046
secret.getData().get(MYSQL_SECRET_USERNAME));
4147
log.info("Schema {} created - updating CR status", schema.getMetadata().getName());

0 commit comments

Comments
 (0)