Skip to content

Secondary resource access with Optional #782

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

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public interface Context {

Optional<RetryInfo> getRetryInfo();

default <T> T getSecondaryResource(Class<T> expectedType) {
default <T> Optional<T> getSecondaryResource(Class<T> expectedType) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I like this change…

return getSecondaryResource(expectedType, null);
}

<T> T getSecondaryResource(Class<T> expectedType, String eventSourceName);
<T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public Optional<RetryInfo> getRetryInfo() {
}

@Override
public <T> T getSecondaryResource(Class<T> expectedType, String eventSourceName) {
public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName) {
final var eventSource =
controller.getEventSourceManager().getResourceEventSourceFor(expectedType, eventSourceName);
return eventSource.map(es -> es.getAssociated(primaryResource)).orElse(null);
return eventSource.map(es -> es.getAssociated(primaryResource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public UpdateControl<InformerEventSourceTestCustomResource> reconcile(

// Reading the config map from the informer not from the API
// name of the config map same as custom resource for sake of simplicity
ConfigMap configMap = context.getSecondaryResource(ConfigMap.class);
ConfigMap configMap = context.getSecondaryResource(ConfigMap.class)
.orElseThrow(() -> new IllegalStateException("Config map should be present."));

String targetStatus = configMap.getData().get(TARGET_CONFIG_MAP_KEY);
LOGGER.debug("Setting target status for CR: {}", targetStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializationContext;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
Expand Down Expand Up @@ -63,19 +68,18 @@ public UpdateControl<Tomcat> reconcile(Tomcat tomcat, Context context) {
createOrUpdateDeployment(tomcat);
createOrUpdateService(tomcat);

Deployment deployment = context.getSecondaryResource(Deployment.class);

if (deployment != null) {
Tomcat updatedTomcat =
updateTomcatStatus(tomcat, deployment);
log.info(
"Updating status of Tomcat {} in namespace {} to {} ready replicas",
tomcat.getMetadata().getName(),
tomcat.getMetadata().getNamespace(),
tomcat.getStatus().getReadyReplicas());
return UpdateControl.updateStatus(updatedTomcat);
}
return UpdateControl.noUpdate();
return context.getSecondaryResource(Deployment.class)
.map(deployment -> {
Tomcat updatedTomcat =
updateTomcatStatus(tomcat, deployment);
log.info(
"Updating status of Tomcat {} in namespace {} to {} ready replicas",
tomcat.getMetadata().getName(),
tomcat.getMetadata().getNamespace(),
tomcat.getStatus().getReadyReplicas());
return UpdateControl.updateStatus(updatedTomcat);
})
.orElse(UpdateControl.noUpdate());
}

private Tomcat updateTomcatStatus(Tomcat tomcat, Deployment deployment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ public UpdateControl<Webapp> reconcile(Webapp webapp, Context context) {
return UpdateControl.noUpdate();
}

Tomcat tomcat = context.getSecondaryResource(Tomcat.class);
if (tomcat == null) {
throw new IllegalStateException("Cannot find Tomcat " + webapp.getSpec().getTomcat()
+ " for Webapp " + webapp.getMetadata().getName() + " in namespace "
+ webapp.getMetadata().getNamespace());
}
Tomcat tomcat = context.getSecondaryResource(Tomcat.class)
.orElseThrow(
() -> new IllegalStateException("Cannot find Tomcat " + webapp.getSpec().getTomcat()
+ " for Webapp " + webapp.getMetadata().getName() + " in namespace "
+ webapp.getMetadata().getNamespace()));

if (tomcat.getStatus() != null
&& Objects.equals(tomcat.getSpec().getReplicas(), tomcat.getStatus().getReadyReplicas())) {
Expand Down