Skip to content

Commit 5d8c567

Browse files
authored
fix: avoid computing potentially unused values (#1759)
Fixes #1758
1 parent 57a41b1 commit 5d8c567

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ public class MDCUtils {
1414
private static final String RESOURCE_VERSION = "resource.resourceVersion";
1515
private static final String GENERATION = "resource.generation";
1616
private static final String UID = "resource.uid";
17+
private static final String NO_NAMESPACE = "no namespace";
1718

1819
public static void addResourceIDInfo(ResourceID resourceID) {
1920
MDC.put(NAME, resourceID.getName());
20-
MDC.put(NAMESPACE, resourceID.getNamespace().orElse("no namespace"));
21+
MDC.put(NAMESPACE, resourceID.getNamespace().orElse(NO_NAMESPACE));
2122
}
2223

2324
public static void removeResourceIDInfo() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected ReconcileResult<R> reconcile(P primary, R actualResource, Context<P> c
6666
if (updatable) {
6767
final Matcher.Result<R> match = match(actualResource, primary, context);
6868
if (!match.matched()) {
69-
final var desired = match.computedDesired().orElse(desired(primary, context));
69+
final var desired = match.computedDesired().orElseGet(() -> desired(primary, context));
7070
throwIfNull(desired, primary, "Desired");
7171
logForOperation("Updating", primary, desired);
7272
var updatedResource = handleUpdate(actualResource, desired, primary, context);
@@ -125,7 +125,7 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
125125
* @param primary the {@link ResourceID} of the primary resource associated with the newly created
126126
* resource
127127
* @param created the newly created resource
128-
* @param context
128+
* @param context the context in which this operation is called
129129
*/
130130
protected abstract void onCreated(P primary, R created, Context<P> context);
131131

@@ -136,7 +136,7 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
136136
* resource
137137
* @param updated the updated resource
138138
* @param actual the resource as it was before the update
139-
* @param context
139+
* @param context the context in which this operation is called
140140
*/
141141
protected abstract void onUpdated(P primary, R updated, R actual, Context<P> context);
142142

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private EventProcessor(
8989
.map(c -> Map.of(
9090
Constants.RESOURCE_GVK_KEY, c.getAssociatedGroupVersionKind(),
9191
Constants.CONTROLLER_NAME, controllerConfiguration.getName()))
92-
.orElse(new HashMap<>());
92+
.orElseGet(HashMap::new);
9393
}
9494

9595
@Override

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/Mappers.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@ public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromAnnotation
2020
return fromMetadata(nameKey, null, false);
2121
}
2222

23+
@SuppressWarnings("unused")
2324
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromAnnotation(
2425
String nameKey, String namespaceKey) {
2526
return fromMetadata(nameKey, namespaceKey, false);
2627
}
2728

28-
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(
29-
String nameKey) {
29+
@SuppressWarnings("unused")
30+
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(String nameKey) {
3031
return fromMetadata(nameKey, null, true);
3132
}
3233

3334
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromDefaultAnnotations() {
3435
return fromMetadata(DEFAULT_ANNOTATION_FOR_NAME, DEFAULT_ANNOTATION_FOR_NAMESPACE, false);
3536
}
3637

38+
@SuppressWarnings("unused")
3739
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(
3840
String nameKey, String namespaceKey) {
3941
return fromMetadata(nameKey, namespaceKey, true);
4042
}
4143

4244
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReference() {
4345
return resource -> ResourceID.fromFirstOwnerReference(resource).map(Set::of)
44-
.orElse(Collections.emptySet());
46+
.orElseGet(Collections::emptySet);
4547
}
4648

4749
private static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromMetadata(

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/externalstate/ExternalStateReconciler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
1111
import io.fabric8.kubernetes.client.KubernetesClient;
1212
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
13-
import io.javaoperatorsdk.operator.api.reconciler.*;
13+
import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
14+
import io.javaoperatorsdk.operator.api.reconciler.Context;
15+
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
16+
import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
17+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
18+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
19+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
20+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
1421
import io.javaoperatorsdk.operator.junit.KubernetesClientAware;
1522
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1623
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
@@ -89,9 +96,7 @@ private void createExternalResource(ExternalStateCustomResource resource) {
8996
public DeleteControl cleanup(ExternalStateCustomResource resource,
9097
Context<ExternalStateCustomResource> context) {
9198
var externalResource = context.getSecondaryResource(ExternalResource.class);
92-
externalResource.ifPresent(er -> {
93-
externalService.delete(er.getId());
94-
});
99+
externalResource.ifPresent(er -> externalService.delete(er.getId()));
95100
client.configMaps().inNamespace(resource.getMetadata().getNamespace())
96101
.withName(resource.getMetadata().getName()).delete();
97102
return DeleteControl.defaultDelete();
@@ -116,7 +121,7 @@ public Map<String, EventSource> prepareEventSources(
116121
}
117122
var id = configMap.getData().get(ID_KEY);
118123
var externalResource = externalService.read(id);
119-
return externalResource.map(Set::of).orElse(Collections.emptySet());
124+
return externalResource.map(Set::of).orElseGet(Collections::emptySet);
120125
}, context.getPrimaryCache(), 300L, ExternalResource.class);
121126

122127
return EventSourceInitializer.nameEventSources(configMapEventSource,

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/externalstate/ExternalWithStateDependentResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public Set<ExternalResource> fetchResources(
4040
return configMapOptional.map(configMap -> {
4141
var id = configMap.getData().get(ID_KEY);
4242
var externalResource = externalService.read(id);
43-
return externalResource.map(Set::of).orElse(Collections.emptySet());
44-
}).orElse(Collections.emptySet());
43+
return externalResource.map(Set::of).orElseGet(Collections::emptySet);
44+
}).orElseGet(Collections::emptySet);
4545
}
4646

4747
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static String decode(String value) {
108108
public Set<Schema> fetchResources(MySQLSchema primaryResource) {
109109
try (Connection connection = getConnection()) {
110110
return SchemaService.getSchema(connection, primaryResource.getMetadata().getName())
111-
.map(Set::of).orElse(Collections.emptySet());
111+
.map(Set::of).orElseGet(Collections::emptySet);
112112
} catch (SQLException e) {
113113
throw new RuntimeException("Error while trying read Schema", e);
114114
}
@@ -123,7 +123,7 @@ public ResourcePollerConfig configFrom(SchemaConfig configAnnotation,
123123
Class<SchemaDependentResource> originatingClass) {
124124
if (configAnnotation != null) {
125125
return new ResourcePollerConfig(configAnnotation.pollPeriod(),
126-
new MySQLDbConfig(configAnnotation.host(), "" + configAnnotation.port(),
126+
new MySQLDbConfig(configAnnotation.host(), String.valueOf(configAnnotation.port()),
127127
configAnnotation.user(), configAnnotation.password()));
128128
}
129129
return new ResourcePollerConfig(SchemaConfig.DEFAULT_POLL_PERIOD,

0 commit comments

Comments
 (0)