-
Notifications
You must be signed in to change notification settings - Fork 221
feat: dependent resource context + my sql e2e test improvements #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler.dependent; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.javaoperatorsdk.operator.OperatorException; | ||
|
||
public class ManagedDependentResourceContext { | ||
|
||
private List<DependentResource> dependentResources; | ||
|
||
public ManagedDependentResourceContext(List<DependentResource> dependentResources) { | ||
this.dependentResources = dependentResources; | ||
} | ||
|
||
public List<DependentResource> getDependentResources() { | ||
return Collections.unmodifiableList(dependentResources); | ||
} | ||
|
||
public <T extends DependentResource> T getDependentResource(Class<T> resourceClass) { | ||
var resourceList = | ||
dependentResources.stream() | ||
.filter(dr -> dr.getClass().equals(resourceClass)) | ||
.collect(Collectors.toList()); | ||
if (resourceList.isEmpty()) { | ||
throw new OperatorException( | ||
"No dependent resource found for class: " + resourceClass.getName()); | ||
} | ||
if (resourceList.size() > 1) { | ||
throw new OperatorException( | ||
"More than one dependent resource found for class: " + resourceClass.getName()); | ||
} | ||
return (T) resourceList.get(0); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,22 @@ | |
|
||
import java.util.Optional; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ContextInitializer; | ||
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.ErrorStatusHandler; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.RetryInfo; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
import io.javaoperatorsdk.operator.sample.schema.Schema; | ||
import io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource; | ||
import io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource; | ||
|
||
import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_FINALIZER; | ||
import static io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource.MYSQL_SECRET_USERNAME; | ||
import static java.lang.String.format; | ||
|
||
// todo handle this, should work with finalizer | ||
|
@@ -26,42 +27,23 @@ | |
@Dependent(type = SchemaDependentResource.class) | ||
}) | ||
public class MySQLSchemaReconciler | ||
implements Reconciler<MySQLSchema>, ErrorStatusHandler<MySQLSchema>, | ||
ContextInitializer<MySQLSchema> { | ||
implements Reconciler<MySQLSchema>, ErrorStatusHandler<MySQLSchema> { | ||
|
||
static final String SECRET_FORMAT = "%s-secret"; | ||
static final String USERNAME_FORMAT = "%s-user"; | ||
|
||
static final String MYSQL_SECRET_NAME = "mysql.secret.name"; | ||
static final String MYSQL_SECRET_USERNAME = "mysql.secret.user.name"; | ||
static final String MYSQL_SECRET_PASSWORD = "mysql.secret.user.password"; | ||
static final String BUILT_SCHEMA = "built schema"; | ||
static final Logger log = LoggerFactory.getLogger(MySQLSchemaReconciler.class); | ||
|
||
public MySQLSchemaReconciler() {} | ||
|
||
@Override | ||
public void initContext(MySQLSchema primary, Context context) { | ||
final var name = primary.getMetadata().getName(); | ||
final var password = RandomStringUtils | ||
.randomAlphanumeric(16); // NOSONAR: we don't need cryptographically-strong randomness here | ||
|
||
final var secretName = String.format(SECRET_FORMAT, name); | ||
final var userName = String.format(USERNAME_FORMAT, name); | ||
|
||
// put information in context for other dependents and reconciler to use | ||
context.put(MYSQL_SECRET_PASSWORD, password); | ||
context.put(MYSQL_SECRET_NAME, secretName); | ||
context.put(MYSQL_SECRET_USERNAME, userName); | ||
} | ||
|
||
@Override | ||
public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context context) { | ||
// we only need to update the status if we just built the schema, i.e. when it's present in the | ||
// context | ||
return context.get(BUILT_SCHEMA, Schema.class).map(s -> { | ||
updateStatusPojo(schema, context.getMandatory(MYSQL_SECRET_NAME, String.class), | ||
context.getMandatory(MYSQL_SECRET_USERNAME, String.class)); | ||
Secret secret = context.getSecondaryResource(Secret.class).orElseThrow(); | ||
SchemaDependentResource schemaDependentResource = context.managedDependentResourceContext() | ||
.getDependentResource(SchemaDependentResource.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be better to get the dependent via its class in a way similar to what we do for secondary resources, i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is how it's done, just hidden behind dedicated context. Or?
I intentionally did not cover this, since on denepnds_on we plan to have names of the resources, so I would add that as part of it?! |
||
return schemaDependentResource.getResource(schema).map(s -> { | ||
updateStatusPojo(schema, secret.getMetadata().getName(), | ||
secret.getData().get(MYSQL_SECRET_USERNAME)); | ||
log.info("Schema {} created - updating CR status", schema.getMetadata().getName()); | ||
return UpdateControl.updateStatus(schema); | ||
}).orElse(UpdateControl.noUpdate()); | ||
|
Uh oh!
There was an error while loading. Please reload this page.